Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

Here is Overlay blending mode for Photoshop implementation , above formula work as follows for Grayscale image

Mat img1;
Mat img2;
img1 = imread("img1.jpg", CV_LOAD_IMAGE_GRAYSCALE);
img2 = imread("img2.jpg", CV_LOAD_IMAGE_GRAYSCALE);
Mat result(img1.size(), CV_32F);

for(int i = 0; i < img1.size().height; ++i){
    for(int j = 0; j < img1.size().width; ++j){
        float target = float(img1.at<uchar>(i, j)) / 255;
        float blend = float(img2.at<uchar>(i, j)) / 255;
        if(target > 0.5){
            result.at<float>(i, j) = (1 - (1-2*(target-0.5)) * (1-blend));
        }
        else{
            result.at<float>(i, j) = ((2*target) * blend);
        }
    }
}

and for color image you only need to use loop for color channels