Search code examples
matlabrectanglesdrawrectangle

How to draw a rectangle using 4 corners on an image in Matlab R2018b?


In Matlab, there is rectangle('Position',[x,y,w,h]) to draw a rectangle object, that w and h are a width and a height respectively (This link). While I try to draw a rectangle on an image using 4 corners: min_x, max_x, min_y and max_y that are specified in the following image.

Ex:

min_x = 193; max_x = 220; min_y = 168; max_y = 190;

enter image description here

I saw this link and like that, but they couldn't help me. Is there any way to draw a rectangle with 4 corners?


Solution

  • A = imresize( imread('peppers.png'),0.6);%resizing for better visibility
    
    min_x = 193; max_x = 220; min_y = 168; max_y = 190;
    
    x = min_x;
    y = min_y;
    w = max_x-min_x;
    h = max_y-min_y;
    
    imshow(A)
    rectangle('Position',[x,y,w,h],'EdgeColor','r','Linewidth',3);
    % Or insert shape to bitmap:
    % B = insertShape(A,'rectangle',[x,y,w,h]);
    % imshow(B)
    

    enter image description here