Threshold on selected ROI - App designer

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)

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

Thanks but how can I apply it to the app designer GUI and treshold.
I don't understand the problem. Just put the code into a callback, except get rid of figure and other unneeded things (clear, etc.)
To threshold you just compare a gray scale image to some value, like
binaryImage grayImage < 149;
Or use my interactive utility.
Based on @Image Analyst you can try:
pos(1) = floor(pos(1));
pos(2) = floor(pos(2));
pos(3) = ceil(pos(3));
pos(4) = ceil(pos(4));
But I do not know how to handle your overly issue/line 37 error
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
To overlay, you can use the imoverlay() function.
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?
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

Asked:

on 2 Jan 2022

Commented:

on 2 Jan 2022

Community Treasure Hunt

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

Start Hunting!