Overlay ROI on an image

9 visualizaciones (últimos 30 días)
Lluis Roca
Lluis Roca el 2 de En. de 2022
Respondida: Image Analyst el 2 de En. de 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);

Respuesta aceptada

Simon Chan
Simon Chan el 2 de En. de 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);

Más respuestas (2)

Walter Roberson
Walter Roberson el 2 de En. de 2022
newI = I;
newI(ymin:ymin+size(GrayRoi,1)-1, xmin:xmin+size(GrayRoi,2)-1) = GrayRoi;
  3 comentarios
Walter Roberson
Walter Roberson el 2 de En. de 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.
Lluis Roca
Lluis Roca el 2 de En. de 2022
I understand that xmin = 600; ymin = 200; and it does not work. I miss something?

Iniciar sesión para comentar.


Image Analyst
Image Analyst el 2 de En. de 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')

Productos


Versión

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by