Problem with ROI dimension - app designer

2 次查看(过去 30 天)
I am using the attached app to select a ROI. One of the possible bugs is when the ROI width (app.col2) is bigger than the image x dimension (app.Ix1) so I added an if statement to fix it but then I get this error:
Index in position 2 exceeds array bounds. Index must not exceed 512.
Error in appROI/otsuthresholdSliderValueChanged (line 107)
app.Icrop = app.I(app.row1 : app.row2, app.col1 : app.col2, :);
Error while evaluating Slider PrivateValueChangedFcn.
The same issue will occur with the ROI height
Any idea why? and how to soilve it?

回答(1 个)

Walter Roberson
Walter Roberson 2022-3-5
Ir = size(app.I,1); Ic = size(app.C,1);
app.Icrop = app.I(max(1,app.row1) : min(Ir,app.row2), max(1,app.col1) : min(Ic,app.col2), :);
  4 个评论
Jane Bat
Jane Bat 2022-3-7
Thanks for the general answer. Any chance to look on the loop?
Walter Roberson
Walter Roberson 2022-3-7
If you use the min() / max() then you do not need the if statements
When I test your code and draw the ROI over to the right border, then the sum of pos(1) and pos(3) is 0.5 greater than the width of your image.
I do not see relevant material in the documentation at this time, but in the past I have seen that for at least some kinds of ROI (not necessarily this kind) that when you give a point to an ROI, that it considers the point you give to be the center of the area, and that the coordinate it returns is intended to be the edge of the selected area. The edge of a selected area is generally 1/2 a pixel further over, so if the ROI was given coordinate 512 it would consider the edge of the area to be 512.5 . And after you add pos(1) and pos(3) you ceil() -- so the 512.5 gets rounded to 513, which is outside the image.
Look for example at the details of imcrop(): it is common to find that imcrop() created an area that is one pixel larger than you expect because of similar kinds of logic.
The min() / max() code I suggest will take care of keeping the coordinates within the image.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Image Processing Toolbox 的更多信息

产品


版本

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by