Code covered by the BSD License  

Highlights from
Cropping an Image with draggable rectangle

4.0

4.0 | 1 rating Rate this file 10 Downloads (last 30 days) File Size: 1.62 KB File ID: #17460

Cropping an Image with draggable rectangle

by Shripad Kondra

 

08 Nov 2007 (Updated 24 Apr 2008)

Crops the Image from a draggable rectangle & displays it (after the figure is closed)

| Watch this File

File Information
Description

% Crops the Image from a draggable rectangle
% & returns the Cropped Image and its co-ordinates
%
% I is the Image to be cropped assumed to be in the
% matlab workspace
%
% w : width (default value : cols/2)
% h : height (default value : rows/2)
%
% EXAMPLES
%
% I = imread('circuit.tif');
% [O] = Crop_it(I);
%
% [O I_crop]=Crop_it(I,0);
%
% [O I_crop]=Crop_it(I,1,100,100);

Acknowledgements
This submission has inspired the following:
Cropping an Image with resizable and draggable rectangle
Required Products Image Processing Toolbox
MATLAB release MATLAB 7.2 (R2006a)
Tags for This File  
Everyone's Tags
Tags I've Applied
Add New Tags Please login to tag files.
Comments and Ratings (5)
08 Nov 2007 someone someone

This submission requires the Image Processing Toolbox

14 Dec 2007 Shripad Kondra

replace the line
% h = imrect(gca, [1 1 h w]); with
h = impositionrect(gca, [1 1 h w]);

if you don't have imrect function for older matlab version.

14 Dec 2007 Shripad Kondra

Here is a more smaller code which returns the cropped Image and its coordinates

function [O I_crop] = Crop_it(I,w,h)
% Crops the Image from a fixed draggable rectangle
% & returns the Cropped Image and its co-ordinates
%
% I is the Image to be cropped assumed to be in the
% matlab workspace
%
% h : width (default value : cols/2)
% w : height (default value : rows/2)
%
% EXAMPLES
%
% I = imread('circuit.tif');
% Crop_it(I);
%
% I = imread('circuit.tif');
% [O I_crop]=Crop_it(I,100,100);

% $date 08-Nov-2007

if nargin~=3
    [r c unused] = size(I);
    w=round(r/2);
    h=round(c/2);
end

figure, imshow(I);
% h = imrect(gca, [1 1 h w]);
h = impositionrect(gca, [1 1 h w]); % for older version of IP toolbox
api = iptgetapi(h);
waitfor(h);
I_crop = imcrop(I,floor(api.getPosition())-1);
O = api.getPosition();
% figure, imshow(I_crop); %uncomment to plot

09 Apr 2008 Erion H

Can you add a feature to change the size of the rectange on the figure rather than having it fixed?

22 Jul 2009 hima

heyy i have executed this code bit im geting errors at 'imrect' and so i have tried with 'impositionrect' then also im geting error undefined function.in the same way the function 'iptgetapi'...please help me

Please login to add a comment or rating.
Updates
24 Apr 2008

To enable initializing the rectangle by hand.

Tag Activity for this File
Tag Applied By Date/Time
application Shripad Kondra 22 Oct 2008 09:34:16
crop Shripad Kondra 22 Oct 2008 09:34:16
cropping Shripad Kondra 22 Oct 2008 09:34:16
image processing Shripad Kondra 22 Oct 2008 09:34:16
draggable rectangle Shripad Kondra 22 Oct 2008 09:34:16
image Shripad Kondra 22 Oct 2008 09:34:16
draggable rectangle zee jan 26 Oct 2010 11:11:07

Contact us at files@mathworks.com