/*
 * 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 "detectratio.hpp"

#include <opencv2/opencv.hpp>

using namespace cv;

DetectRatio::DetectRatio(double threshold)
  : mThreshold(threshold)
{
}

void DetectRatio::ratio(InputArray temp, InputArray in, OutputArray out) const
{
  // Calculate ratio
  Mat tmp;
  divide(temp.getMat(), in.getMat(), tmp);
  
  // Return result
  tmp.copyTo(out);
}

void DetectRatio::process(DataUnit& data)
{
  assert(!data.mMat[TEMP].empty());
  assert(!data.mMat[INPU].empty());
  
  // Calculate the ratio
  Mat r;
  ratio(data.mMat[TEMP], data.mMat[INPU], r);

  // Threshold ratio
  data.mMat[OUTP] = abs(r - 1) > mThreshold;
  
  // Keep changes in ROI only
  multiply(data.mMat[OUTP], data.mMat[TROI], data.mMat[OUTP]);
}
