Overlay ROI on an image
13 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Lluis Roca
am 2 Jan. 2022
Beantwortet: Image Analyst
am 2 Jan. 2022
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 Kommentare
Akzeptierte Antwort
Simon Chan
am 2 Jan. 2022
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 Kommentare
Weitere Antworten (2)
Walter Roberson
am 2 Jan. 2022
newI = I;
newI(ymin:ymin+size(GrayRoi,1)-1, xmin:xmin+size(GrayRoi,2)-1) = GrayRoi;
3 Kommentare
Walter Roberson
am 2 Jan. 2022
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
am 2 Jan. 2022
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 Kommentare
Siehe auch
Kategorien
Mehr zu Image Preview and Device Configuration finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!