Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

Getting a good idea from @haris Solution , I did the changes and below code works for me

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

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

When we already divide target and blend by 255 we do not need to divide them again in the result and then convert it by

result.convertTo(result,CV_8UC3,255);

and we will get the result

Getting a good idea from @haris Solution , I did the changes and below code works for me

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 - (1-target) * (1-2*(blend-0.5))));

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

When we already divide target and blend by 255 we do not need to divide them again in the result and then convert it by

result.convertTo(result,CV_8UC3,255);

and we will get the result