Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Draw rectangles on an image in Matlab

Tags:

matlab

I am trying to figure out how to draw rectangles on an image in Matlab.

Once the rectangles are drawn on the image I would like to save the changes.

Thanks in advance!

like image 761
DrKnowing Avatar asked Oct 12 '14 06:10

DrKnowing


People also ask

How do I draw a rectangle image in MATLAB?

im=imread('face. jpg'); %Image read rectangle('Position', [10 10 30 30] ,... 'EdgeColor', 'r',... 'LineWidth', 3,... 'LineStyle','-');%rectangle properties imshow( im, rectangle); %draw rectangle on image. Save this answer.

What is bounding box in MATLAB?

[ xlim , ylim ] = boundingbox( polyin ) returns the x and y bounds of the smallest rectangle enclosing a polyshape . xlim and ylim are two-element row vectors whose first elements correspond to the lower x and y bounds, and whose second elements correspond to the upper x and y bounds.

How do you use Drawrectangle in MATLAB?

roi = drawrectangle creates a Rectangle object and enables interactive drawing of the ROI on the current axes. To draw the ROI, position the pointer on the image. The cursor changes to a fleur shape. Click and drag to draw the rectangular ROI.


2 Answers

Use getframe

img = imread('cameraman.tif');
fh = figure;
imshow( img, 'border', 'tight' ); %//show your image
hold on;
rectangle('Position', [50 70 30 60] ); %// draw rectangle on image
frm = getframe( fh ); %// get the image+rectangle
imwrite( frm.cdata, 'savedFileName.png' ); %// save to file

See rectanlge for more options on drawing rectangles. The 'Position' argument for rectangle is in format [from_x from_y width height] and is given in units of pixels.

like image 130
Shai Avatar answered Oct 25 '22 17:10

Shai


Without using getframe:

im=imread('face.jpg'); %Image read
rectangle('Position', [10 10 30 30] ,...
    'EdgeColor', 'r',...
    'LineWidth', 3,...
    'LineStyle','-');%rectangle properties
imshow( im, rectangle); %draw rectangle on image.

For more detail visit this MathWorks thread :)

like image 28
Wahab Raja Avatar answered Oct 25 '22 17:10

Wahab Raja