Crop an image using coordinate
조회 수: 60 (최근 30일)
이전 댓글 표시
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
댓글 수: 3
nisa sarfraz
2020년 1월 14일
I=imread(images(1).name); %crop one to get rect
this line of code give error of index matrix dimension exeeds. kindly suggest some solution
Image Analyst
2020년 1월 14일
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
thisImage=imread(images(1).name);
imshow(thisImage)
% Crop one to get rect
croppedImage = imcrop(thisImage);
채택된 답변
Image Analyst
2014년 10월 2일
You used 151 instead of 115 in your width. Here, try this:
grayImage = imread('circuit.tif');
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
Aparna Sivakumar
2022년 5월 16일
Hi how could you get the follwing values>? imcrop(grayImage,[115, 89, 161-115,142-89]);
Image Analyst
2022년 5월 16일
I don't know. It looks like the original poster changed/edited his post. It no longer mentions 151 and he removed the image.
추가 답변 (3개)
John BG
2017년 3월 4일
편집: John BG
2017년 3월 4일
Hi Hu
A=imread('im1.jpg');imshow(A)
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.
as example let's start with this image
A=imread('im1.jpg');imshow(A)
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
if you find these lines useful would you please mark my answer as Accepted Answer?
To any other reader, if you find this answer of any help please click on the thumbs-up vote link,
thanks in advance for time and attention
John BG
댓글 수: 0
Nalini Vishnoi
2014년 10월 2일
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];
댓글 수: 3
Geoff Hayes
2014년 10월 2일
hu - please describe what you mean by ...does not work. Perhaps provide an example pair of coordinates with what you expect to be the desired outcome.
Amruta Talreja
2017년 3월 4일
I2 = imcrop(B1,[12659 11677 11281 12661]); imshow(I2); I wrote this code and i am getting a blank screen. plz help.
댓글 수: 4
Warid Islam
2019년 7월 12일
Hi,
Is it possible to crop the image manually without using a rectangle? For example, can I create a boundary manually with a mouse on the Matlab image and display the cropped image separately?
Image Analyst
2019년 7월 13일
If you use imcrop() without any arguments, you'll be in interactive drawing mode.
참고 항목
카테고리
Help Center 및 File Exchange에서 Image Processing and Computer Vision에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!