Crop the irrelevant part of the Image

I am new to Matlab. I want to crop the highlighted part of the gray image and remove the rest part. How to do that, kindly explain as well. Thanks.

3 Comments

Somebody help please!
One last help please!
You have not attached the original image. Not the cropped one, and not the one with the red box on it, but the original image. So, did you even try stdfilt()?

Sign in to comment.

 Accepted Answer

If you know the bounding rows and columns, you can do indexing
croppedImage = rgbImage(row1:row2, col1:col2, :);
or use imcrop():
croppedImage = imcrop(rgbImage, [leftCol, topRow, (rightCol-leftCol+1), (bottomRow-topRow+1)]);

6 Comments

Thanks for the prompt reply, ImageAnalyst. I am still perplexed, can you show me an example of the implementation using this image?
Let I be your image of size m,n,3.
r1=round(m/2); Icropped=I(1:r1,:,:);
I gave you the implementation. If you need it in a full demo where I read in and display both images, see attached test.m below the image. It's longer because of that but essentially I'm just using the line:
croppedImage = rgbImage(row1:row2, col1:col2, :);
Zaheer Abbas
Zaheer Abbas on 29 Apr 2017
Edited: Zaheer Abbas on 29 Apr 2017
Thanks. You're great. One last question! In your code, you hard coded the values of rows and columns. Is there any way that rows and columns values can be determined dynamically (not hard coded). This is one sample image, there can be other images with different dimensions.
Yes, but that was not the original question so I didn't answer that. You could use stdfilt() and threshold and find the bounding box. Search for braille here - I know I've done code for it before.
Sir, I tried to find the code, but couldn't find it. If you please help with the code (crop through bounding box). Really sorry, but I am really stuck in this. I have very little time. Thanks in Advance

Sign in to comment.

More Answers (2)

KSSV
KSSV on 28 Apr 2017
Edited: KSSV on 28 Apr 2017
doc imcrop
When propted, draw a rectangle in your interested area.
I = imread('your image') ; % Read image
imshow(I) ;
I1 = imcrop(I) ; % crop the image
imshow(I1)
imwrite(I1,'croppedimage.jpg'); % save the image

5 Comments

I want to crop the highlighted part of the image that is inside the red box. imcrop() needs parameters for cropping the specific area. I guess, I need to know the parameters.
You can input parameters or you can drag a rectangle on your required area. Run the above code, when image opens draw/ drag a rectangle. Your highlighted image will be saved on the name croppedimage.jpg
Zaheer Abbas
Zaheer Abbas on 28 Apr 2017
Edited: Zaheer Abbas on 28 Apr 2017
I don't want to draw a rectangle by myself. It should be auto cropped. So, I need to find those parameters on which basis I can crop the image.
How about picking up half of the matrix after imread?
Thanks a lot sir! :) It helped me..

Sign in to comment.

Zaheer, I'm sure by now you've tried to take my suggestion and use stdfilt() to automatically get the bounding box. You can look at how I did it and compare it to your own.

Community Treasure Hunt

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

Start Hunting!