Question

Suppose we take any image from Internet and then copy or move some part from that image to any other area inside that image the image should show from where that the part is copied / moved and then pasted. By using matlab.

a = imread('obama.jpg');
a = rgb2gray(a);
[x1 y1] = size(a);
b = uint8(imcrop(a, [170 110 200 150]));
[x2 y2] = size(b);
c = uint8(zeros(x1,y1));
for i = 1:x2
    for j = 1:y2
            c(i+169,j+109) = b(i,j);
    end
end
[x3 y3] = size(c)
subplot(1,3,1),imshow(a);
subplot(1,3,2),imshow(b);
subplot(1,3,3),imshow(c);
Was it helpful?

Solution

Code

%%// Input data and params
a = imread('Lenna.png');
a = rgb2gray(a);

src_xy = [300,300]; %% Starting X-Y of the source from where the portion would be cut from
src_dims = [100 100]; %% Dimensions of the portion to be cut
tgt_xy = [200,200]; %%  Starting X-Y of the target to where the portion would be put into

%%// Get masks
msrc = false(size(a));
msrc(src_xy(1):src_xy(1)+src_dims(1)-1,src_xy(2):src_xy(2)+src_dims(2)-1)=true;

mtgt = false(size(a));
mtgt(tgt_xy(1):tgt_xy(1)+src_dims(1)-1,tgt_xy(2):tgt_xy(2)+src_dims(2)-1)=true;

%%// If you would like to have a cursor based cut, explore ROIPOLY, GINPUT - e.g. - mask1 = roipoly(a)
mask1 = msrc;
a2 = double(a);

%%// Get crop-mask boundary and dilate it a bit to show it as the "frame" on the original image
a2(imdilate(edge(mask1,'sobel'),strel('disk',2))) = 0;
a2 = uint8(a2);

%%// Display original image with cropped portion being highlighted
figure,imshow(a2);title('Cropped portion highlighted')

figure,imshow(a);title('Original Image')

figure,imshow(mask1);title('Mask that was cropped')

img1 = uint8(bsxfun(@times,double(a),mask1));
figure,imshow(img1);title('Masked portion of image')

%%// Get and display original image with cropped portion being overlayed at the target coordinates
a_final = a;
a_final(mtgt) = a(msrc);
figure,imshow(uint8(a_final));title('Original image with the cut portion being overlayed')

Output

enter image description here

Please note that to use RGB images, you would need to tinker a bit more with the above code.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top