Threshold on selected ROI - App designer

1 view (last 30 days)
John Sal
John Sal on 2 Jan 2022
Commented: Image Analyst on 2 Jan 2022
As part of my effort to learn app designer I took the code in this link (https://www.mathworks.com/matlabcentral/answers/1620785-overlay-roi-on-an-image?s_tid=prof_contriblnk) and built a GUI using part of it.
The GUI aim is to upload an image, convert it to grayscale and perform an Otsu's global threshold on the selected ROI (mlapp file attached).
When I run it the Load image button works well but I get the error below when I press the Treshold ROI button:
Warning: Integer operands are required for colon operator when used as index.
Warning: Integer operands are required for colon operator when used as index.
level =
0.3882
Warning: Integer operands are required for colon operator when used as index.
Warning: Integer operands are required for colon operator when used as index.
Unable to perform assignment because the size of the left side is 308-by-376 and the size of the right side is 308-by-376-by-3.
Error in drawrectGUI/loadimageButton_2Pushed (line 37)
app.I(pos(2):pos(2)+pos(4),pos(1):pos(1)+pos(3),:) = repmat(app.BWRoi, [1 1 3]); % Overlay the treshold ROI on the image
Error while evaluating Button PrivateButtonPushedFcn.
Therefore, can someone please explain me the error and how can I solve it.
Thanks

Answers (1)

Image Analyst
Image Analyst on 2 Jan 2022
You need to turn pos into integers instead of fractional values.
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')
  6 Comments
Ali razi
Ali razi on 2 Jan 2022
imoverlay() function require that both A and BW must have the same number of rows and columns. So how can you overlay app.BWRoi on top of the app.I in the same original location?
Image Analyst
Image Analyst on 2 Jan 2022
When @John Sal said "% Overlay the treshold ROI on the image" I assume he'd threshold some gray scale image and then use imoverlay(), like
mask = grayImage > someThreshold;
overlayImage = imoverlay(grayImage, mask);
imshow(overlayImage);
or if he already has a mask in app.BWRoi :
overlayImage = imoverlay(grayImage, app.BWRoi);
imshow(overlayImage);
If he wants tinted instead of solid, opaque overlay he can use labeloverlay().

Sign in to comment.

Products


Release

R2021b

Community Treasure Hunt

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

Start Hunting!