/*
 * Copyright 2016 Jan Dorazil <deu439@gmail.com>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */

#include "normalize.hpp"

#include <opencv2/opencv.hpp>

using namespace cv;

void Normalize::normalize(InputOutputArray input, InputArray mask) const
{
    Scalar meanVec, stddevVec;
    double mean, stddev;
    Mat mInput = input.getMat();

    // Calculate mean and stddev
    meanStdDev(mInput, meanVec, stddevVec, mask);

    // Calculate the mean of mean and stddev throughout channels
    mean = meanVec.val[0];
    stddev = stddevVec.val[0];

    // Normalize
    mInput = (mInput - mean)/stddev;

    mInput.copyTo(input);
}

void Normalize::process(DataUnit& data)
{
  assert(!data.mMat[TEMP].empty());
  assert(!data.mMat[TROI].empty());
  assert(!data.mMat[INPU].empty());
  
  normalize(data.mMat[TEMP], data.mMat[TROI]);
  normalize(data.mMat[INPU], data.mMat[TROI]);
}
