Q:

Draw rectangles on an image in 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!

A:

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.

A:

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 :)

