# Crop an image using coordinate

429 views (last 30 days)
hu on 2 Oct 2014
Commented: Image Analyst on 16 May 2022
Hi,
How can I crop an image by having the top left coordinate (X1,Y1) and bottom right (X2,Y2).
I2 = imcrop(im,[x1 y1 x2 y2])
does not seem to work?
Thanks
Image Analyst on 14 Jan 2020
Nisa, start a new question, and give the code that you used to load up your images structure (was it the dir function?). Your images structure is evidently empty, meaning it found not images.
filePattern = fullfile(pwd, '*.png');
images = dir(filePattern);
if isempty(images)
message = sprintf('No PNG images found in folder %s\n', pwd)
uiwait(errordlg(message))
return;
end
imshow(thisImage)
% Crop one to get rect
croppedImage = imcrop(thisImage);

Image Analyst on 2 Oct 2014
You used 151 instead of 115 in your width. Here, try this:
subplot(1,2,1);
imshow(grayImage);
h = impixelinfo();
axis on;
I2 = imcrop(grayImage,[115, 89, 161-115,142-89]);
subplot(1,2,2);
imshow(I2)
axis on;
##### 2 CommentsShowHide 1 older comment
Image Analyst on 16 May 2022
I don't know. It looks like the original poster changed/edited his post. It no longer mentions 151 and he removed the image.

John BG on 4 Mar 2017
Edited: John BG on 4 Mar 2017
Hi Hu p=ginput(2)
p1max=max(p(:,1));p2max=max(p(:,2));p1min=min(p(:,1));p2min=min(p(:,2));
A(:,[1:p1min],:)=[];
A([1:p2min],:,:)=[];
A(:,[uint64(p1max-p1min+1):end],:)=[];
A([uint64(p2max-p2min+1):end],:,:)=[];
imshow(A) EXPLANATION
1.
2. Selecting 2 diagonal points that define the borders you want to remove. I use command ginput but you already have the values
p=ginput(2)
3. Calculating border values from ginput points
p1max=max(p(:,1));p2max=max(p(:,2));p1min=min(p(:,1));p2min=min(p(:,2));
4. Removing on borders, one side of the frame at a time
A(:,[1:p1min],:)=[];
A([1:p2min],:,:)=[];
A(:,[uint64(p1max-p1min+1):end],:)=[];
A([uint64(p2max-p2min+1):end],:,:)=[];
5.
Check
imshow(A)
Hu
thanks in advance for time and attention
John BG

Nalini Vishnoi on 2 Oct 2014
You can try using the following command
I2 = imcrop(im,[x1 y1 x2-x1 y2-y1]);
imcrop uses the following syntax:
I2 = imcrop(I,RECT);
where RECT is a 4-element vector with the form [XMIN YMIN WIDTH HEIGHT];
hu on 2 Oct 2014
Edited: hu on 2 Oct 2014
Hi,
If I wish to crop the image (get the square in the middle) and based on the code sugested, It does not work:
I2 = imcrop(I,[115, 89, 161-151,142-89]);
imshow(I2)
The coordiantes are (115,89,161,142) (Xtop left,Ytop left, Xbottom right, Ybottom right).
Thanks

Amruta Talreja on 4 Mar 2017
I2 = imcrop(B1,[12659 11677 11281 12661]); imshow(I2); I wrote this code and i am getting a blank screen. plz help.
Image Analyst on 13 Jul 2019
If you use imcrop() without any arguments, you'll be in interactive drawing mode.

### Categories

Find more on Lighting, Transparency, and Shading in Help Center and File Exchange

### Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!