img = img1*mask + img2*(1-mask) How do that ?
Hello,
I would like merge two color images with an image mask.
img1 and img2 are color image with 3 channels mask is grey image with 1 channel
for merge the two image with the mask I do a loop for each pixel.
float c1,c2;
for(int j = 0; j < img1.rows ; j++ ){
for(int i = 0; i < img1.cols ; i++ ){
c1 = (greyGoodScale.at<uchar>(j, i))/255.0;
c2 = 1-c1;
img.at<Vec3b>(j, i)[0] = c2*img1.at<Vec3b>(j, i)[0] + c1*img2.at<Vec3b>(j, i)[0];
img.at<Vec3b>(j, i)[1] = c2*img1.at<Vec3b>(j, i)[1] + c1*img2.at<Vec3b>(j, i)[1];
img.at<Vec3b>(j, i)[2] = c2*img1.at<Vec3b>(j, i)[2] + c1*img2.at<Vec3b>(j, i)[2];
}
}
OK, it's work but my image is 720x500 and i have 70ms of processing time is TOO LONG, I need to be real time. I can't do process on GPU.
Is a way to reduce processing time ?
thank. christophe openCV 3.x
thank you for your message, but your exemple work it only with binaire mask.
In my case, I work with coefficient value mask.
You can modify your loop body to make it faster:
To make It even faster you can make conversion table from uchar to double for c1. Just preprocess values from 0/255.0 to 255.0/255.0. And you can preprocess 1 - c1 too.
Why your code is slow? There are 2 reasons: 1)You are using .at, which is slower than pointer access. 2)You are using Vec3b for pixel access. Every time you are writing .at<vec3b>, new object of class Vec3b is being created.