How to Crop the detected image

1 view (last 30 days)
MoonSik
MoonSik on 5 Dec 2014
Commented: Image Analyst on 15 Mar 2018
Is there any way to crop only the box? Or I want to make the image black or white except the box. ex)
Please give me some tip! Thank you!

Answers (1)

Image Analyst
Image Analyst on 5 Dec 2014
It already is black around the image. Do you want it white? If it's a gray scale image you can do it roughly using
grayImage(grayImage==0) = 255; % May not be perfect
though that could turn black pixels inside the box white also. There is a more robust way to get a perfect rectangle using imfill() and regionprops(). Let me know if you need that. Otherwise see the FAQ:
  4 Comments
Sufyan Salim
Sufyan Salim on 15 Mar 2018
Edited: Sufyan Salim on 15 Mar 2018
I am using the same algorithm though i want it to automatically crop the detected box object in the end and save it as a new image.... how do i do it?
Image Analyst
Image Analyst on 15 Mar 2018
You can use your mask to find the corners:
[rows, columns] = find(mask);
croppedImage = grayImage(rows(1):rows(end), columns(1):columns(end), :);
imwrite(croppedImage, filename);

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!