Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

Pixel-wise subtraction of image

So I am trying to multiply an intensity image with 0.5 and subtract it from each color channel using pixel wise operations like ptr and check that the values do not become negative, i.e. the new (R, G, B) values are (max(R − 0.5I, 0), max(G − 0.5I, 0), max(B − 0.5I, 0)). I am an OpenCV novice, here's the code I came up with (obviously doesn't work). Any pointers will be really helpful:

void pixelwiseSubtraction(Mat& bgrImg, Mat& grayImg, Mat& result) {
  cvtColor(bgrImg, grayImg, CV_BGR2GRAY);
  Mat gray_half;
  cvtColor(grayImg * 0.5, gray_half, CV_GRAY2BGR);

  if(true){
    for (int i = 0; i < gray_half.rows; ++i)
    {
        // point to first color in row
        uchar* pixel = gray_half.ptr<uchar>(i);
        uchar* bgr_pixel = bgrImg.ptr<uchar>(i);
        for (int j = 0; j < gray_half.cols; ++j)
        {
            bgr_pixel -= pixel;
        }
    }
  }

  result = bgrImg;
}

I have an error: invalid conversion from ‘int’ to ‘uchar* {aka unsigned char*}’ [-fpermissive] bgr_pixel -= pixel; error. How can I do the operation?

The not pixel-wise version is this:

void subtractIntensityImage(Mat& bgrImg, Mat& grayImg, Mat& result) {
  cvtColor(bgrImg, grayImg, CV_BGR2GRAY);
  Mat gray_half;
  cvtColor(grayImg * 0.5, gray_half, CV_GRAY2BGR);
  // 2. subtract that from original:
  subtract(bgrImg, gray_half, result);
}

Thanks.