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);
0 个评论
采纳的回答
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);
0 个评论
更多回答(2 个)
Walter Roberson
2022-1-2
newI = I;
newI(ymin:ymin+size(GrayRoi,1)-1, xmin:xmin+size(GrayRoi,2)-1) = GrayRoi;
3 个评论
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.
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')
0 个评论
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!