Pixel-wise subtraction of image

asked 2016-10-22 10:37:02 -0600

Jishan gravatar 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.

edit retag flag offensive close merge delete

Comments

1
  • your "for-loop" would be correct, if applied to a single 8bit channel . unfortunately, it isn't so here, where both images are bgr (try to replace "uchar" with "Vec3b")

  • in any case, have a look at http://docs.opencv.org/master/de/d7a/... (1st 3 chapters cover this)

  • then again, - you should never do anything on a per-pixel basis in opencv. ( sure i understand your curiosity here )

berak gravatar imageberak ( 2016-10-22 11:09:38 -0600 )edit

I get the following error error: conversion from ‘cv::Vec<unsigned char, 3>*’ to non-scalar type ‘cv::Vec3b {aka cv::Vec<unsigned char, 3>}’ requested Vec3b pixel = gray_half.ptr<Vec3b>(i); ^ Any suggestions please?

Jishan gravatar imageJishan ( 2016-10-22 11:16:12 -0600 )edit