Using getrect for Image Processing

조회 수: 34 (최근 30일)
Sordin
Sordin 2017년 12월 21일
댓글: vijay Gudipudi 2018년 9월 25일
I am trying to use the getrect function to replace a selected portion of one image with the corresponding portion in another image (provided the images are of the same size and class). getrect(...) returns the coordinates of the selected rectangle so we know what the starting and stopping rows and columns are. I used two sample images:
X=imread('office_1.jpg');
Y=imread('office_5.jpg');
Here is what I did:
imshow(X)
rect = getrect;
x1=rect(1);
x2=rect(2);
y1=rect(3);
y2=rect(4);
X(x1:x2, y1:y2)=Y(x1:x2, y1:y2);
Z = X;
imshow(Z)
However, after I select an area, the resulting image (Z) is just the first image (X). What is wrong here?
Any suggestions would be greatly appreciated.

채택된 답변

Image Analyst
Image Analyst 2017년 12월 21일
LOTS of errors in that. Mostly due to not computing x1,x2,y1,y2 correctly using the rect variable, which is [x, y, width, height], NOT [x1,x2,y1,y2]. Then you didn't use the third index, needed because they are color images not gray scale images. Finally, you reversed x and y in the indexing. Remember arrays index as array(y, x), NOT array(x,y) because the row/y/vertical value is always the first index, not the colulmn/x/horizontal value.
Fixed code is below:
image1=imread('office_1.jpg');
subplot(2, 1, 1);
imshow(image1);
title('Image1', 'FontSize', 20);
image2=imread('office_5.jpg');
subplot(2, 1, 2);
imshow(image2);
title('Image2', 'FontSize', 20);
% Enlarge figure to full screen.
set(gcf, 'Units', 'Normalized', 'OuterPosition', [0, 0.04, 1, 0.96]);
drawnow;
uiwait(helpdlg('Draw out a rectangle over the bottom Image2.'));
rect = getrect;
x1 =rect(1)
x2 = x1 + rect(3)
y1 =rect(2)
y2 = y1 + rect(4)
Z = image1; % Initialize
Z(y1:y2, x1:x2, :) = image2(y1:y2, x1:x2, :);
imshow(Z)
title('Z', 'FontSize', 20);
  댓글 수: 1
vijay Gudipudi
vijay Gudipudi 2018년 9월 25일
please share output for the above code

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by