防止图像减法时的信息丢失

时间:2014-11-14 16:20:18

标签: opencv

我有两张图片,我只是简单地相互减去:

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

现在,据我所知,任何进入底片的像素值(或者超过255的像素值)都会被设置为零。如果是这样,我想知道是否有任何方法允许它在零以下,以便我可以稍后调整图像而不会丢失信息。

我正在使用灰度图像,如果这简化了事情。

1 个答案:

答案 0 :(得分:2)

这是一个简单的转换=> substract => convertAndScaleBack应用程序看起来像:

输入:

enter image description here

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);
}

输出:

enter image description here