Ask Your Question

Revision history [back]

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