Ask Your Question

# Trying to translate formula for blending mode

I am using opencv c++ for making the blending mode like in photoshop , i want to make overlay mode in it , i search its alternate in opencv in which i found this blending way , but its not the overlay as i want to use the overlay method in it.

overlay method formula from this documentation

(Target > 0.5) * (1 - (1-2*(Target-0.5)) * (1-Blend)) +
(Target <= 0.5) * ((2*Target) * Blend)


Can any one please explain this formula for implementation in opencv c++ , how i can easy understand it for implementation or is there any already build in function for it or any other easy way out :P

edit retag close merge delete

## Comments

that formula looks very expensive.

first break it down into simple multiply()[since it's per element] and add() calls.

you'd have to do it in float format, since e.g. (2*Target) would overflow the original uchar value.

( 2014-04-22 08:41:54 -0500 )edit

What are actually Target ?

( 2014-04-22 08:49:32 -0500 )edit
1

Target,Blend, 2 images.

try to write it on paper:

A = Target > 0.5

B = 1 - Blend

C = Target - 0.5

...

( 2014-04-22 08:54:07 -0500 )edit

## 1 answer

Sort by ยป oldest newest most voted

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

more

Official site

GitHub

Wiki

Documentation

## Stats

Asked: 2014-04-22 07:53:41 -0500

Seen: 943 times

Last updated: May 01 '14