Thanks all for answer and interest but as I didn't found the answer completely following my question , I update my own solution which I get from here
First, the author of the link you provided assumed that a pixel color as a value between 0 and 1.
Imagine you want to blend 2 images img1 and img2. The formula says that if a pixel in img1 as a value Target > 0.5
then the resulting value is (1 - (1-2*(Target-0.5)) * (1-Blend))
for the blended image where Blend is the value of the pixel of img2.
On the other hand, if Target <= 0.5
the resulting color value will be ((2*Target) * Blend)
.
You need to do this for each pixel.
This link provides an overlay blending function with OpenCV.
Here is an example with a grayscale image. For a RGB image, you need to do this for each channel. Of course img1
and img2
must have the same size. Maybe there is a quicker way to do it with OpenCV.
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);
}
}
}