-1

How to fill black color in a rotated portion of an image with background of original image? Note that, image gets sheared on sides only with very small displacement. The size of original and transformed image is same.

This is what i have tried. Can anyone suggest a better approach? The MATLAB code and and corresponding results:

A=imread('cameraman.tif');
tform=affine2d([1 0 0 ; 0.05 1 0 ; 0 0 1]);
J=imwarp(A, tform, 'OutputView',imref2d(size(A)));
subplot(1,3,1)
imshow(A),title('Original Image');
subplot(1,3,2)
imshow(J),title('Rotated Image');
zeropixels = J == 0;
J(zeropixels) = A(zeropixels);
subplot(1,3,3)
imshow(J),title('Correct Image');        

enter image description here

erbal
  • 421
  • 5
  • 18
  • @lxer: I am trying something [like this] (http://stackoverflow.com/questions/7742601/to-replace-black-portion-of-an-image-with-any-other-image-in-matlab) – erbal Sep 07 '15 at 10:18

1 Answers1

0

Try the following: 1) Rotate an image with all pixels with a constant value(to find the regions in a definitive way). 2) The pixel location with 0 as pixel values now can be used to create a mask. 3) copy the pixels in the original image to the rotated image, using the created mask.

To improve the results(edge consistency), smoothing the image prior replacing the value might help.

  • Thanks. Kindly check my updated question with sample code. – erbal Sep 07 '15 at 14:40
  • 1
    There is one drawback. What if some pixel inside the image has zero value. Then you would be replacing values at locations inside the original image. – aditya bhardwaj Sep 08 '15 at 05:59
  • yes, you are write. But mostly in my project, im using colour images with almost no black portion. – erbal Oct 11 '15 at 03:33