Time for action – directing a threatening scene

This time we will aim at a task that is a little more difficult than the previous ones. We will try to blend two images taken at the zoo, so that we create a threatening scene. More specifically, the first picture is portraying a team of penguins, standing peacefully in their dome and the second one is portraying a polar bear, strolling in her yard more than 500 meters away. Now, what if we brought the bear closer? This would look alarming, especially if the blending process is performed correctly. Let's start by loading our pictures, as always:

  1. Our two pictures are called penguins.jpg and bears.jpg, so we will load them and display them next to each other:
    >> peng = imread('penguins.jpg'),
    >> bear = imread('bear.jpg'),
    >> imshowpair(peng,bear)
    Time for action – directing a threatening scene
  2. Now, these photographs have almost nothing in common, so the prospects of mixing them don't look very good. Not all hope is lost though. The first step is to crop both pictures, aiming at having the bear and the penguins roughly at the same height, without overlapping each other. So, we should call imcrop twice and try our best, so that our result looks something like this:
    >> bear = imcrop(bear);
    >> peng = imcrop(peng);
    >> subplot(1,2,1),imshow(peng);title('Penguins after cropping')
    >> subplot(1,2,2),imshow(bear);title('Bear after cropping')
    Time for action – directing a threatening scene
  3. However, we are not ready yet. Even if your cropping skills are perfect, the two images could not be exactly the same size. This means that in order for us to be able to use imfuse properly, we should resize one of them to fit the other. The best selection is to resize the bear image, since it is larger and we will not cause much distortion. In order to resize it to the size of the penguin image, we type in:
    >> bear = imresize(bear, [size(peng,1) size(peng,2)]);
    >> imshowpair(peng,bear,'montage'),
    Time for action – directing a threatening scene
  4. Apart from the fact that our sizes are now OK, we now see that the images could actually blend together well. We just have to find a way to perform the most seamless blending possible. Let's try to crop the area of the bear, so that we make a proper mask (we will also include the rock hiding the front foot, so that it does not seem unnatural):
    >> mask = roipoly(bear);
    Time for action – directing a threatening scene
  5. Our next step is to use the mask to keep only the areas we want from the two images. This means that we will keep the area from the bear picture that has a mask value equal to one and the area from the penguins' picture that has a mask value equal to zero. Since we are using color images, we have to perform the masking in every channel. First, we make two new, three-dimensional masks, that is, by concatenating the mask matrix.
    >> bear_mask = cat(3,mask,mask,mask); % Construct bear mask
    >> peng_mask = 1- cat(3,mask,mask,mask); % Construct penguin mask
  6. Then, we will multiply each mask element-by-element (dot multiplication) with the image we want to mask, after we have converted its type to single (so that the multiplication is feasible).
    >> bear_img = single(bear).*bear_mask; % Masking of bear
    >> peng_img = single(peng).*peng_mask; % Masking of penguins
  7. Finally, we will convert the result back to uint8 and display the results. Let's see how:
    >> bear_img = uint8(bear_img); % Convert result to uint8
    >> peng_img = uint8(peng_img); % Convert result to uint8
    >> figure, imshowpair(peng_img, bear_img,'montage'),
    Time for action – directing a threatening scene
  8. Now, for the last step, we need to perform blending of the two images:
    >> pengbear = imfuse(peng_img,bear_img,'blend'),
    >> figure, imshow(result)
    Time for action – directing a threatening scene
  9. The final touch will be to repair the contrast of the resulting image. Composite images created with imfuse, appear to have half their original contrast. A way to fix this would be to multiply the result with a factor of two. A better way to handle it is to use some contrast stretching method. In order for the result to be more vivid, we can use the imadjust function in each color channel separately. To do this, we type in:
    >> for i = 1:size(result,3)
    result(:,:,i)=imadjust(result(:,:,i));
    end
    >> imshow(result)
    Time for action – directing a threatening scene

And this is the result. Poor penguins! Lucky for them, it's just an illusion.

What just happened?

This example mixed a lot of what you have learned so far in this book. We wanted to create a rather challenging composite image, from two images with very few similarities. The only thing in our favor was the non-overlapping positioning of the penguins and the bear after we cropped the two images. Cropping the images was necessary also for the better alignment of the two target areas (penguins and bear) in the vertical axis. Once we did that, we chose to transfer the bear to the penguin territory, since the bear was larger and could be down-sized to fit in the other image without losing quality. After down-sizing the bear, we manually defined a ROI around it to form a mask. The inverse mask was selected for the penguin picture. Then, we performed masking on the two images, using the respective masks in step 5. The resulting images were blended and consequently, the result was filtered in each of its channels using imadjust, to enhance its intensity.

Tip

Sometimes, too much information is not necessarily a good thing. If you look closely, some areas of the blended image at the edges of the bear's legs give away the fact that our image was processed. A trick that can be used to reduce this effect is applying a median filter to smooth the image. This way, the resulting image would be less crisp, but its imperfections would be camouflaged. To apply the median filter to a color image, you would need to do it in each channel separately.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset