views:

134

answers:

2

I have blended/merged 2 images img1 and img2 with this code which is woking fine.What i want to know is how to obtain the original two images img1 and img2.The code for blending is as under

img1=imread('C:\MATLAB7\Picture5.jpg');
img2=imread('C:\MATLAB7\Picture6.jpg');
for i=1:size(img1,1)
  for j=1:size(img1,2)
    for k=1:size(img1,3)
      output(i,j,k)=(img1(i,j,k)+img2(i,j,k))/2;
    end
   end
end
imshow(output,[0 255]);
+4  A: 

You could recover the 2nd image if you had one original image plus the blended image.

If you have only the blended image there are a near infinite number of img1 and img2 that could have been combined to create the two images so you can't recover them.

For future matlab programming, note that in matlab you don't need the loops that you've written, this is equivialnt to the code you gave:

img1=imread('C:\MATLAB7\Picture5.jpg');
img2=imread('C:\MATLAB7\Picture6.jpg');
output = (img1 + img2) ./ 2;
imshow(output,[0 255]);
Donnie
Consider how you generated it: `new = (im1 + im2) / 2`. Now, solve for the one you don't have using algebra. Let's say you don't have `im2`: `2*new = im1+im2` -> `im2 = (2*new) - im1`. This gives you the algorithm for recovering the unknown one given a known image and the blend.
Donnie
With a VERY big caveat here. If the pair of images are uint8 values, then adding them together, and then dividing by 2 will often generate 8 bit overflows. For example, A = uint8(255); (A+A)/2, ans = 128
woodchips
+1  A: 

If you blend the images like this:

img1=imread('C:\MATLAB7\Picture5.jpg');
img2=imread('C:\MATLAB7\Picture6.jpg');
blendedImg = (img1/2 + img2/2); % divide images before adding to avoid overflow

You can get back img1 from the blended image (with maybe some rounding errors) if you have img2

img1recovered = 2*(blendedImg - img2/2);

figure,subplot(1,2,1)
imshow(img1,[0 255])
subplot(1,2,2)
imshow(img1recovered,[0 255])
Jonas
gavishna
However, the exact image is not being recovered,there are loss in colors..its darker than the original iamge say img2 is being recovered,but the recovery is not 100% perfect.How to overcome this and get image in original?
gavishna
Convert the images to double before doing the blending and unblending. This dramatically reduces rounding errors.Also, if it is darker: Did you multiply the result from the unblending by 2?
Jonas
@gavishna: use `double(img2)` instead
Jonas
Thank u Jonas.it worked,thanx a ton.
gavishna
Glad it works. Note that if the answer is useful for you, you may want to accept it.
Jonas
oh yes,is there an option to notify that i have accepted the answer?im not familiar with this concept actually.
gavishna
There should be a 'hollow' checkmark to the left of the vote count next to the questions. You click on it, and the answer is marked as accepted. This gives the person who answered points. In turn, if you have a high rate of accepting questions, people are more willing to spend time answering.
Jonas