Overlay ROI on an image

11 次查看(过去 30 天)
How do I "burn"/overlay the GrayRoi into the original image (I) based on the four-element position vector [xmin ymin width height]?
Code:
close all;
clear all;
clc;
I = imread('sevilla.jpg');
figure('Name','Sevilla');
imshow(I)
h = drawrectangle('Position',[600,200,250,450],'StripeColor','r');
Iroi = imcrop(I,[600,200,250,450]);
GrayRoi = rgb2gray(Iroi);
figure('Name','GrayEnterenceSevilla');
imshow(GrayRoi);

采纳的回答

Simon Chan
Simon Chan 2022-1-2
The four-element position vector is located at roi.Position, which is [600,200,250,450],
Try the following:
pos=roi.Position;
Iroi = I(pos(2):pos(2)+pos(4),pos(1):pos(1)+pos(3),:); % Extract the ROI
GrayRoi = rgb2gray(Iroi); % Convert to gray scale
I(pos(2):pos(2)+pos(4),pos(1):pos(1)+pos(3),:)=repmat(GrayRoi, [1 1 3]); % Put pack to original image
figure('Name','GrayEnterenceSevilla');
imshow(I);

更多回答(2 个)

Walter Roberson
Walter Roberson 2022-1-2
newI = I;
newI(ymin:ymin+size(GrayRoi,1)-1, xmin:xmin+size(GrayRoi,2)-1) = GrayRoi;
  3 个评论
Walter Roberson
Walter Roberson 2022-1-2
Your question was, "How do I "burn"/overlay the GrayRoi into the original image (I) based on the four-element position vector [xmin ymin width height]?" . That implies you already have xmin and ymin.
Lluis Roca
Lluis Roca 2022-1-2
I understand that xmin = 600; ymin = 200; and it does not work. I miss something?

请先登录,再进行评论。


Image Analyst
Image Analyst 2022-1-2
Try this:
close all;
clear all;
clc;
rgbImage = imread('sevilla.jpg');
figure('Name','Sevilla');
imshow(rgbImage)
axis('on', 'image')
uiwait(helpdlg('Draw a rectangle'))
roi = drawrectangle('StripeColor','r')
pos = roi.Position
% OPTIONAL Get rid of graphical overlay and replace with yellow rectangle.
delete(roi)
rectangle('Position', pos, 'EdgeColor', 'y', 'LineWidth', 2);
% Crop image using indexing.
col1 = floor(pos(1)); % Column 1
col2 = ceil(pos(1) + pos(3)); % Column 2
row1 = floor(pos(2)); % Row 1
row2 = ceil(pos(2) + pos(4)); % Row 2
croppedImage = rgbImage(row1 : row2, col1 : col2, :);
figure
imshow(croppedImage)
axis('on', 'image')

产品


版本

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by