How to merge two image without losing intensity in opencv
Asked Answered
P

2

6

I have two images in opencv: Image A and Image B.

Image A is output frame from camera.
Image B is alpha transparent image obtained by masking one image.
Before masking Image B it is warped with cvWarpPerspective()

  • I tried cvAddWeighted() - It looses intensity when you give alpha and beta value
  • I tried aishack - Even here you looses overall intensity of Output Image
  • I tried silveiraneto.net - Not helpful in my case

Please help me out with something where I don't lose intensity in the output image after blending.

Thanks in advance

Purplish answered 16/11, 2011 at 6:42 Comment(0)
P
2

I finally got the answer.It consist of 5 steps....

Step - 1

cvGetPerspectiveTransform(q,pnt,warp_matrix); 
//where pnt is four point x and y cordinates and warp_matrix is a 3 x 3 matrix

Step - 2

cvWarpPerspective(dst2, neg_img, warp_matrix,CV_INTER_LINEAR)
//dst2 is overlay image ,neg_img is a blank image 

Step - 3

cvSmooth(neg_img,neg_img,CV_MEDIAN); //smoothing the image

Step - 4

cvThreshold(neg_img, cpy_img, 0, 255, CV_THRESH_BINARY_INV);
//cpy_img is a created image from image_n

Step - 5

cvAnd(cpy_img,image_n,cpy_img);// image_n is a input image
cvOr(neg_img,cpy_img,image_n);

Output - image_n (without loosing intensity of input image)

Purplish answered 5/12, 2011 at 8:20 Comment(1)
And you will get overlay without antialias. You can do it in two steps: 1) get warp_matix, 2) warpPerspective(pic, frame, warp_matrix, frame.size(), INTER_LINEAR, BORDER_TRANSPARENT);Gisser
T
2

When you say, that you lose intensity... you leave the question about, how you lose it?

Do you loose intensity in the sense:

That when you add the images you hit a maximum intensity, and the rest is discarded. (Example for a 8 bit pixel addition: Pix1 = 200 i, Pix2 = 150 i. "Pix1 + Pix2 = 350" but max value at 255, so Pix1 + Pix2 = 255)

That the former values of image A is compromised by adding it to Image B, which only covers some parts of the image. (Example for an 8 bit image: Pix1 = 200 i, Pix2 = 150, (Pix1 + Pix2)/2 = 175, but when the value of a pixel of the second image is zero, Pix2 = 0. Then (Pix1 + Pix2)/2 = 100, which is half the value of the original image)

One of these observations should tell you about what you need to do. I don't quite know, in accordance to the functions you mentioned, which approach they use.

Tekla answered 25/11, 2011 at 16:23 Comment(3)
Thanks for the answer +1 for that but what I need here is....Suppose you have spectacles image (png transparent) in photoshop , how do you overlay that image onto someones face (another image) by dragging it up and adjust bring to front and back...Same thing i want to do in opencv...All you are saying is what my question is...I need some way to avoid that....Purplish
If you could work with a four channel img like RGB(A) for red green blue and aplha, instead of just 3 channel like RGB. You might be able to add them, by changing each pixel in the image A (the image at the back) towards your new image (with transparencies), and doing this on a scale from 0 to 1, based on the alpha channel. I'm afraid I still can't give some exact examples of code.Tekla
Additionally. If your transparency is stricly On and Off values, you might just want to check the alpha value for each pixel in the transparent image, and then just write that value to your background image if the alpha value is "true"Tekla
P
2

I finally got the answer.It consist of 5 steps....

Step - 1

cvGetPerspectiveTransform(q,pnt,warp_matrix); 
//where pnt is four point x and y cordinates and warp_matrix is a 3 x 3 matrix

Step - 2

cvWarpPerspective(dst2, neg_img, warp_matrix,CV_INTER_LINEAR)
//dst2 is overlay image ,neg_img is a blank image 

Step - 3

cvSmooth(neg_img,neg_img,CV_MEDIAN); //smoothing the image

Step - 4

cvThreshold(neg_img, cpy_img, 0, 255, CV_THRESH_BINARY_INV);
//cpy_img is a created image from image_n

Step - 5

cvAnd(cpy_img,image_n,cpy_img);// image_n is a input image
cvOr(neg_img,cpy_img,image_n);

Output - image_n (without loosing intensity of input image)

Purplish answered 5/12, 2011 at 8:20 Comment(1)
And you will get overlay without antialias. You can do it in two steps: 1) get warp_matix, 2) warpPerspective(pic, frame, warp_matrix, frame.size(), INTER_LINEAR, BORDER_TRANSPARENT);Gisser

© 2022 - 2024 — McMap. All rights reserved.