how to use imcrop function in matlab

The imcrop function is used to extract a part of an image in MATLAB. Following is the syntax of the function:

main.m
B = imcrop(A, rect)
20 chars
2 lines

where A is the input image and rect is a four-element position vector [xmin ymin width height], specifying the size and position of the crop rectangle.

Here is an example code snippet to crop a rectangular region from an input image:

main.m
% Read the input image
I = imread('example_image.jpg');

% Plot the input image
imshow(I);

% Use imrect to draw a rectangle on the image
h = imrect;

% Wait for the user to finish drawing the rectangle
wait(h);

% Get the position of the rectangle
crop_rect = getPosition(h);

% Crop the region using imcrop
J = imcrop(I, crop_rect);

% Display the cropped region
imshow(J);
376 chars
21 lines

This code will open a window displaying the input image, then allow the user to draw a rectangle on the image using the mouse. After the user finishes drawing the rectangle, the code crops the specified region using imcrop and displays the resulting image.

related categories

gistlibby LogSnag