Overlay gray ROI on an image

1 回表示 (過去 30 日間)
Ely Raz
Ely Raz 2022 年 1 月 15 日
コメント済み: Walter Roberson 2022 年 1 月 15 日
Based on this question (https://www.mathworks.com/matlabcentral/answers/1620785-overlay-roi-on-an-image) and one of the answers (@Image Analyst), I can not overlay the grayRoi on the rgbImage as the outcome image as some colors where it should be gray. How can I solve it?
Code:
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
GrayRoi = rgb2gray(rgbImage(row1 : row2, col1 : col2, :));
figure
imshow(GrayRoi)
axis('on', 'image')
newI = rgbImage;
newI(row1 : row2, col1 : col2) = GrayRoi;
figure
imshow(newI)

採用された回答

Walter Roberson
Walter Roberson 2022 年 1 月 15 日
newI(row1 : row2, col1 : col2) = repmat(GrayRoi, [1 1 size(newI,3)]);
  2 件のコメント
Ely Raz
Ely Raz 2022 年 1 月 15 日
Thanks for the reply but I get this error:
Unable to perform assignment because the size of the left side is 164-by-140 and the size of the right side is 164-by-140-by-3.
Error in mergeGrayCrop (line 25)
newI(row1 : row2, col1 : col2) = repmat(GrayRoi, [1 1 size(newI,3)]);
Walter Roberson
Walter Roberson 2022 年 1 月 15 日
newI(row1 : row2, col1 : col2, :) = repmat(GrayRoi, [1 1 size(newI,3)]);

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeImages についてさらに検索

製品


リリース

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by