How to solve "Warning: Integer operands are required for colon operator when used as index." ?

6 views (last 30 days)
ori_roi = cell(numfiles, 1);
i = 1;
rect = getrect(f); % select roi with mouse
I have this warning for this line of code:
ori_roi{i} = originalImage( rect(2) : (rect(2)+rect(4)) , rect(1) : (rect(1)+rect(3)) , : ); % store roi in matrix
This helps:
I added the code by Image Analyst and it solves my problem:
% Might be floating point, so round:
rect = int32(rect);
% Make sure it's never zero because an index of 0 is not allowed.
rect(rect == 0) = 1;
Thanks alot.

Answers (1)

Walter Roberson
Walter Roberson on 25 Jul 2019
Roi points returned by the roi input routines are often not integers.
When you display an image using image() or imagesc() or imshow(), the same underlying object is used, image() objects. Each image object has an xdata and ydata property that designates the location to place the image in the axes. But default when the user does not supply coordinates, the coordinates used automatically are integers.
Now what you need to know is that for image objects, the coordinates designate the locations of the centers of pixels. So if the axes coordinates are 1:1 ratio with pixels then the coordinates supplied automatically would correspond to the center of the bottom left pixel being at xy (1,1) and the center of the top right being at xy (columns, rows). This places the left edge at x 1/2, the bottom edge at y 1/2, the right edge at columns+1/2 and the top edge at y rows+1/2
But.. When the user uses the interactive roi tools, the user typically traces along edges rather than along pixels. Thus even for the simplest case of rectangular roi, the boundary are likely to be integer+1/2 instead of integer.
For non-rectangular roi, you should assume that the roi boundary might be at arbitrary coordinates ss it cuts on angles.

Categories

Find more on Image Processing Toolbox in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!