You are now following this question
- You will see updates in your followed content feed.
- You may receive emails, depending on your communication preferences.
How to crop an image using matrices
5 views (last 30 days)
Show older comments
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
Accepted Answer
Ameer Hamza
on 4 Dec 2020
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 Comments
Arya Gopan
on 4 Dec 2020
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
on 4 Dec 2020
Edited: Ameer Hamza
on 4 Dec 2020
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
on 4 Dec 2020
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
on 4 Dec 2020
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
on 5 Dec 2020
![](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
on 5 Dec 2020
Try
Product = imresize(X, size(A));
Image Analyst
on 5 Dec 2020
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
on 5 Dec 2020
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
on 5 Dec 2020
Which MATLAB release are you using? Try replacing the line with this
Product = imresize(X, [size(A,1) size(A,2)]);
Arya Gopan
on 5 Dec 2020
Im using 2014 version.
Arya Gopan
on 5 Dec 2020
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
on 5 Dec 2020
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
on 5 Dec 2020
Yes i used the data cursor to know the pixel co-ordinates and inlcluded it.
Ameer Hamza
on 5 Dec 2020
Can you attach your image and also your code?
Arya Gopan
on 6 Dec 2020
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
on 6 Dec 2020
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
on 6 Dec 2020
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
on 6 Dec 2020
Yes sir I got it.
Thankyou so much.
Arya Gopan
on 6 Dec 2020
Sir an you help me with finding the inverse fourier transform of the croped image using ifftshift
. I get a blank black ffigure
More Answers (0)
See Also
Categories
Find more on Geometric Transformation and Image Registration in Help Center and File Exchange
Tags
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!An Error Occurred
Unable to complete the action because of changes made to the page. Reload the page to see its updated state.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- 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)
Asia Pacific
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)