How to crop an image using matrices
10 次查看(过去 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 (한국어)