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

 Réponse acceptée

Ameer Hamza
Ameer Hamza le 4 Déc 2020

0 votes

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 commentaires

Arya Gopan
Arya Gopan le 4 Déc 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
Ameer Hamza le 4 Déc 2020
Modifié(e) : Ameer Hamza le 4 Déc 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
Arya Gopan le 4 Déc 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?
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')
Arya Gopan
Arya Gopan le 5 Déc 2020
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'
Arya Gopan
Arya Gopan le 5 Déc 2020
Try
Product = imresize(X, size(A));
Arya Gopan
Arya Gopan le 5 Déc 2020
still
Image Analyst
Image Analyst le 5 Déc 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
Arya Gopan le 5 Déc 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
Which MATLAB release are you using? Try replacing the line with this
Product = imresize(X, [size(A,1) size(A,2)]);
Arya Gopan
Arya Gopan le 5 Déc 2020
Im using 2014 version.
Arya Gopan
Arya Gopan le 5 Déc 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.
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
Arya Gopan le 5 Déc 2020
Yes i used the data cursor to know the pixel co-ordinates and inlcluded it.
Ameer Hamza
Ameer Hamza le 5 Déc 2020
Can you attach your image and also your code?
Arya Gopan
Arya Gopan le 6 Déc 2020
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;
Arya Gopan
Arya Gopan le 6 Déc 2020
Yes sir I got it.
Thankyou so much.
Arya Gopan
Arya Gopan le 6 Déc 2020
Sir an you help me with finding the inverse fourier transform of the croped image using ifftshift
. I get a blank black ffigure

Connectez-vous pour commenter.

Plus de réponses (0)

Community Treasure Hunt

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

Start Hunting!

Translated by