예제 #1
0
static void computeOffset(cv::Mat img1, cv::Mat img2, int d, cv::Point& _off)
{
  cv::Point offset;
  cv::Size size1 = img1.size(), size2 = img2.size();
  
  if ((size1.width > 2 && size1.height > 2) &&
      (size2.width > 2 && size2.height > 2) &&
      (d < 16)) {
    cv::Mat tmp1, tmp2;
    cv::resize(img1, tmp1, cv::Size(), 0.5f, 0.5f);
    cv::resize(img2, tmp2, cv::Size(), 0.5f, 0.5f);
    computeOffset(tmp1, tmp2, d+1, offset);
    offset.x *= 2, offset.y *= 2;
  } else {
    offset.x = 0, offset.y = 0;
  }
  
  cv::Point result;
  cv::Mat tb1, tb2, eb1, eb2;
  computeBitmaps(img1, tb1, eb1);
  computeBitmaps(img2, tb2, eb2);
  
  cv::Rect bound1(cv::Point(0,0), size1);
  cv::Rect bound2(cv::Point(0,0), size2);
  
  int min_err = (bound1 & bound2).area();
  
  for (int dx = -1; dx <= 1; ++dx) {
    for (int dy = -1; dy <= 1; ++dy) {
      cv::Point displace = offset + cv::Point(dx,dy);
      cv::Rect frame = bound1 & (bound2 + displace);
      cv::Rect shift = bound2 & (bound1 - displace);
      
      // ROIs
      cv::Mat framed_tb1(tb1, frame);
      cv::Mat framed_eb1(eb1, frame);
      cv::Mat shifted_tb2(tb2, shift);
      cv::Mat shifted_eb2(eb2, shift);
      
      //
      cv::Mat tmp1, tmp2;
      cv::bitwise_xor(framed_tb1, shifted_tb2, tmp1);
      cv::bitwise_and(tmp1, framed_eb1, tmp2);
      cv::bitwise_and(shifted_eb2, tmp2, tmp1);
      
      //
      int err = (int)cv::sum(tmp1)[0];
      if (dx == 0 && dy == 0)
        err -= 1;
      if (err < min_err) {
        result = displace;
        min_err = err;
      }
    }
  }
  
  _off = result;
}
예제 #2
0
파일: HDR.cpp 프로젝트: brunopop/hdr
	void Align::getExpShift(const Image& img1, const Image& img2, int shift_bits, std::vector<int>& shift_ret)
	{
		std::vector<int> cur_shift(2, 0);
		int i, j;
		if (shift_bits > 0)
		{
			Image sml_img1 = img1.shrink2();
			Image sml_img2 = img2.shrink2();
			getExpShift(sml_img1, sml_img2, shift_bits-1, cur_shift);
			cur_shift[0] *= 2;
			cur_shift[1] *= 2;
		}
		else
		{
			cur_shift[0] = cur_shift[1] = 0;
		}
		Bitmap tb1; img1.computeThresholdBitmap(tb1);
		Bitmap eb1; img1.computeExclusionBitmap(eb1);
		Bitmap tb2; img2.computeThresholdBitmap(tb2);
		Bitmap eb2; img2.computeExclusionBitmap(eb2);
		int min_err = img1.rows * img1.cols;
		for (i=-1; i<=1; i++)
		{
			for (j=-1; j<=1; j++)
			{
				if (min_err > 0)
				{
					int xs = cur_shift[0] + i;
					int ys = cur_shift[1] + j;
					Bitmap shifted_tb2(img1.rows, img1.cols);
					Bitmap shifted_eb2(img1.rows, img1.cols);
					Bitmap diff_b(img1.rows, img1.cols);
					bitmapShift(tb2, xs, ys, shifted_tb2);
					bitmapShift(eb2, xs, ys, shifted_eb2);
					bitmapXOR(tb1, shifted_tb2, diff_b);
					bitmapAND(diff_b, eb1, diff_b);
					bitmapAND(diff_b, shifted_eb2, diff_b);
					int err = diff_b.total();
					if (err < min_err)
					{
						shift_ret[0] = xs;
						shift_ret[1] = ys;
						min_err = err;
					}
				}
			} // end for j
		} // end for i
	};