Overlay ROI on an image
11 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Lluis Roca
le 2 Jan 2022
Réponse apportée : Image Analyst
le 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 commentaires
Réponse acceptée
Simon Chan
le 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 commentaires
Plus de réponses (2)
Walter Roberson
le 2 Jan 2022
newI = I;
newI(ymin:ymin+size(GrayRoi,1)-1, xmin:xmin+size(GrayRoi,2)-1) = GrayRoi;
3 commentaires
Walter Roberson
le 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
le 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 commentaires
Voir également
Catégories
En savoir plus sur Image Preview and Device Configuration dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!