Search code examples
matlabplotsubmatrix

plotting a submatrix (ROI) in MATLAB


I am trying to select a particular region using MATLAB. Before extracting the sub matrix I am defining the region using plot command.

figure,imshow(imgc,[0,3000]);
hold on;
plot([x1,x2],[y1,y1],'Color','r','LineWidth',0.5)
hold on;
plot([x1,x1],[y1,y2],'Color','r','LineWidth',0.5)
hold on;
plot([x2,x2],[y1,y2],'Color','r','LineWidth',0.5)
hold on;
plot([x1,x2],[y2,y2],'Color','r','LineWidth',0.5)
hold on;

After plotting,I extract this region.

ROI=img(x1:x2,y1:y2);
img(x1:x2,y1:y2)=0;

However the region plotted and the sub matrix are different as in the figure:

Image

The red box is the plot of region of interest,the region of the 0s is the extracted sub matrix.Ideally the red box should be around the region of 0s. The blue lines are the axes


Solution

  • The problem is that you should swap the x and y coordinates:

    img(y1:y2, x1:x2)=0;
    

    Explanation

    The problem is that the arguments/indexes of a function/matrix are rather supplied in an opposite order:

    matrix(row, column)
    

    versus

    function(x,y)
    

    x (first argument) often represents the horizontal axes and therefore corresponds with the column (second argument) argument and idem for y and row.