How to crop an image using matrices

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
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

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
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]))
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?
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')
(* 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')
Sir I have to crop the left most dot from the image. When I did the first code I got the croped image so.
And when I tried the second one it says there is errorin the "imresize statement'
Try
Product = imresize(X, size(A));
It looks like you redefined size to be a variable in your program. What does this show in the command window.
>> which -all size
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
Which MATLAB release are you using? Try replacing the line with this
Product = imresize(X, [size(A,1) size(A,2)]);
Im using 2014 version.
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.
Have you changed this line?
X(50:120,450:500,:)
You need to specify the pixel coordinates for the left dot in your image.
Yes i used the data cursor to know the pixel co-ordinates and inlcluded it.
Can you attach your image and also your code?
This is the fourier transform of the image. The left most dot is to be cropped. ill attatch the code below.
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
The indexes must be defined in increasing order. Change the line
X(563:475,639:556,:)=1;
to
X(475:563,556:639,:)=1;
Yes sir I got it.
Thankyou so much.
Sir an you help me with finding the inverse fourier transform of the croped image using ifftshift
. I get a blank black ffigure

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!