Ask Your Question

leinaD_natipaC's profile - activity

2019-11-17 18:17:46 -0600 received badge  Student (source)
2019-11-17 18:14:03 -0600 received badge  Popular Question (source)
2014-11-18 09:59:00 -0600 answered a question Preventing information loss on image subtraction

Warning: this answer is a blatant copy of an answer from stackoverflow

This is how a simple convert => substract => convertAndScaleBack application would look like:

input:

enter image description here

and

enter image description here

int main() { cv::Mat input = cv::imread("../inputData/Lenna.png", CV_LOAD_IMAGE_GRAYSCALE); cv::Mat input2 = cv::imread("../inputData/Lenna_edges.png", CV_LOAD_IMAGE_GRAYSCALE);

    cv::Mat input1_16S;
    cv::Mat input2_16S;

    input.convertTo(input1_16S, CV_16SC1);
    input2.convertTo(input2_16S, CV_16SC1);

    // compute difference of 16 bit signed images
    cv::Mat diffImage = input1_16S-input2_16S;

    // now you have a 16S image that has some negative values

    // find minimum and maximum values:
    double min, max;
    cv::minMaxLoc(diffImage, &min, &max);
    std::cout << "min pixel value: " << min<< std::endl;

    cv::Mat backConverted;

    // scale the pixel values so that the smalles value is 0 and the largest one is 255
    diffImage.convertTo(backConverted,CV_8UC1, 255.0/(max-min), -min);

    cv::imshow("backConverted", backConverted);        
    cv::waitKey(0);
}

output:

enter image description here

2014-11-18 09:02:52 -0600 received badge  Supporter (source)
2014-11-18 07:48:43 -0600 commented answer Preventing information loss on image subtraction

That doesn't actually resolve my problem. I guess you are suggesting that I convert my matrix to signed? How do I change from 8UC1 to 16SC!(for example) and back again? Or can I use imwrite() without converting the mat back to CV_8UC1?

2014-11-18 07:45:50 -0600 commented question Preventing information loss on image subtraction

the problem is i won't be able to tell apart -9 from 9 right?

2014-11-14 10:04:37 -0600 asked a question Preventing information loss on image subtraction

I have two images that I am subtracting from one another quite simply:

Mat foo, a, b;
...//imread onto a and b or somesuch
foo = a - b;

Now, as I understand it, any pixel value that goes into the negatives will end up being zero. If that is so, I'd like to know if there is any way to permit it to go under zero so that I may adjust the image later without loss of information.