이 질문을 팔로우합니다.
- 팔로우하는 게시물 피드에서 업데이트를 확인할 수 있습니다.
- 정보 수신 기본 설정에 따라 이메일을 받을 수 있습니다.
How to crop an image using matrices
조회 수: 5 (최근 30일)
이전 댓글 표시
I need to crop a certain portion of an image. But the problem is that I want the croped image in the same dimension of the orginal image. I am asked to do this with the help of matrices. How can I do it
채택된 답변
Ameer Hamza
2020년 12월 4일
You can use imcrop(): https://www.mathworks.com/help/images/ref/imcrop.html and then apply imresize(): https://www.mathworks.com/help/images/ref/imresize.html to the cropped image.
댓글 수: 21
Arya Gopan
2020년 12월 4일
No sir. I tried using imcrop. But the issue is that while doing so i get an image with different dimension.. I need the dimension of the cropped image same as that of the orginal one.
Can you please tell me whether it is possible to do like this.
A=imread('trial.jpg'); // reading the image
X=zeroes(size(A)); // Creating a matrix of the same size of A nd making all of the elements in it zero
X= ones(120:50,450;500);// Making the desired are of the image which has to be croped zero
Product = A*X
This code so that by multiplying X with A we will get the cropped image. But while I did this in Matlab The 3rd line(bold one) is error.
please help
Ameer Hamza
2020년 12월 4일
편집: Ameer Hamza
2020년 12월 4일
X= ones(120:50,450;500) is not correct MATLAB syntax. I guess you want to do this
A=im2double(imread('trial.jpg'));
X=zeros(size(A)); % Creating a matrix of the same size of A nd making all of the elements in it zero
X(50:120,450:500,:) = 1;
and then
Product = A.*X;
But this will give you white area around the cropped region.
Try something like this
A=im2double(imread('trial.jpg'));
rect = [50 450 70 50];
X = imcrop(A, rect);
X = imresize(X, size(A,[1 2]))
Arya Gopan
2020년 12월 4일
Yes sir I will try both and update you.
But what does this white are around the cropped region denote.. I mean the cropped portion would have the same dimension like that of the image right?
Ameer Hamza
2020년 12월 4일
Following your method, It will have same dimension, but looks like this
A=im2double(imread('pears.png'));
X=zeros(size(A)); % Creating a matrix of the same size of A nd making all of the elements in it zero
X(50:120,450:500,:) = 1;
Product = A.*X;
subplot(2,1,1)
imshow(A);
title('original')
subplot(2,1,2)
imshow(Product);
title('Cropped')
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/445268/image.png)
(* I meant to write black background)
Using my method
A=im2double(imread('pears.png'));
rect = [50 450 70 50];
X = imcrop(A, rect);
Product = imresize(X, size(A,[1 2]));
subplot(2,1,1)
imshow(A);
title('original')
subplot(2,1,2)
imshow(Product);
title('Cropped')
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/445273/image.png)
Arya Gopan
2020년 12월 5일
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/446023/image.png)
And when I tried the second one it says there is errorin the "imresize statement'
Image Analyst
2020년 12월 5일
It looks like you redefined size to be a variable in your program. What does this show in the command window.
>> which -all size
Arya Gopan
2020년 12월 5일
A=im2double(imread('trial.jpg'));
rect =[532 454 654 600];
X=imcrop(A,rect);
Product = imresize(X, size(A));
subplot(2,1,1)
imshow(A);
title ('Orginal')
subplot(2,1,2)
imshow(Product);
title('Cropped')
?? This is the program i wrote
Ameer Hamza
2020년 12월 5일
Which MATLAB release are you using? Try replacing the line with this
Product = imresize(X, [size(A,1) size(A,2)]);
Arya Gopan
2020년 12월 5일
Sir actuallyI am a Post Graduate student of Physics and this is for my final year projet.
Here I am given the image of a fringe, whih I have converted to its fourier transform. So the image that I have attatched above is its fourier transform. Now I am asked to crop one of those lobes so that rest of the calculations can be carried out. While doing imcrop funtion its dimension changes. So I was asked to do it using matrix method.
A=im2double(imread('pears.png'));
X=zeros(size(A)); % Creating a matrix of the same size of A nd making all of the elements in it zero
X(50:120,450:500,:) = 1;
Product = A.*X;
subplot(2,1,1)
imshow(A);
title('original')
subplot(2,1,2)
imshow(Product);
title('Cropped')
// I used similar code with diff variable names but I got that black screen as output.
Ameer Hamza
2020년 12월 5일
Have you changed this line?
X(50:120,450:500,:)
You need to specify the pixel coordinates for the left dot in your image.
Arya Gopan
2020년 12월 6일
This is the fourier transform of the image. The left most dot is to be cropped. ill attatch the code below.![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/448368/image.jpeg)
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/448368/image.jpeg)
Arya Gopan
2020년 12월 6일
clear all
I = imread('a35.jpg');
I = rgb2gray(I);
[r , c] = size(I);
for i = 1:r
X(i,:)=fft(I(i,:));
end
for j = 1:c
Y(:,j)=fft(X(:,j));
end
figure(1)
imshow(I)
figure(2)
M=Y;
M=fftshift(M);
Ab=abs(M);
Ab=(Ab-min(min(Ab)))./(max(max(Ab))).*225;
imshow(Ab)
X=zeros(size(Ab));
X(563:475,639:556,:)=1;
Product = Ab.*X;
subplot(2,1,1)
imshow(Ab);
title('Orginal')
subplot(2,1,2)
imshow(Product);
title('Cropped')
////////////////////////////////// This is te code that I have used so far using matrices
Ameer Hamza
2020년 12월 6일
The indexes must be defined in increasing order. Change the line
X(563:475,639:556,:)=1;
to
X(475:563,556:639,:)=1;
Arya Gopan
2020년 12월 6일
Sir an you help me with finding the inverse fourier transform of the croped image using ifftshift
. I get a blank black ffigure
추가 답변 (0개)
참고 항목
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!오류 발생
페이지가 변경되었기 때문에 동작을 완료할 수 없습니다. 업데이트된 상태를 보려면 페이지를 다시 불러오십시오.
웹사이트 선택
번역된 콘텐츠를 보고 지역별 이벤트와 혜택을 살펴보려면 웹사이트를 선택하십시오. 현재 계신 지역에 따라 다음 웹사이트를 권장합니다:
또한 다음 목록에서 웹사이트를 선택하실 수도 있습니다.
사이트 성능 최적화 방법
최고의 사이트 성능을 위해 중국 사이트(중국어 또는 영어)를 선택하십시오. 현재 계신 지역에서는 다른 국가의 MathWorks 사이트 방문이 최적화되지 않았습니다.
미주
- América Latina (Español)
- Canada (English)
- United States (English)
유럽
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
아시아 태평양
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)