Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

You need to use a floating point Mat to store float data,

So change your code to

Mat result(img1.size(), CV_32FC3);
for(int i = 0; i < img1.size().height; ++i){
    for(int j = 0; j < img1.size().width; ++j){

        for (int c=0 ; c<img1.channels();c++){
            float target = (float)img1.at<uchar>(i, 3*j+c)/255.0 ;
            float blend =  (float)img2.at<uchar>(i, 3*j+c)/255.0 ;
            if(target > 0.5){
                result.at<float>(i, 3*j+c) = cv::saturate_cast<uchar>((1 - (1-target) * (1-2*(blend-0.5))))/255.;

                }
            else{
                result.at<float>(i, 3*j+c) = cv::saturate_cast<uchar>(target * (2*blend))/255;
                }
        }
    }
}

And for displaying just convert it to 8 bit,

double min;
double max;
cv::minMaxIdx(result, &min, &max);
cv::Mat final;

result.convertTo(final,CV_8UC1, 255 / (max-min), -min);
imshow("final",final);

You need to use a floating point Mat to store float data,data,

So change your code to

Mat result(img1.size(), CV_32FC3);
for(int i = 0; i < img1.size().height; ++i){
    for(int j = 0; j < img1.size().width; ++j){

        for (int c=0 ; c<img1.channels();c++){
            float target = (float)img1.at<uchar>(i, 3*j+c)/255.0 ;
            float blend =  (float)img2.at<uchar>(i, 3*j+c)/255.0 ;
            if(target > 0.5){
                result.at<float>(i, 3*j+c) = cv::saturate_cast<uchar>((1 - (1-target) * (1-2*(blend-0.5))))/255.;

                }
            else{
                result.at<float>(i, 3*j+c) = cv::saturate_cast<uchar>(target * (2*blend))/255;
                }
        }
    }
}

And for displaying just convert it to 8 bit,

double min;
double max;
cv::minMaxIdx(result, &min, &max);
cv::Mat final;

result.convertTo(final,CV_8UC1, 255 / (max-min), -min);
imshow("final",final);