Clear Filters
Clear Filters

How to detect and crop boundry of the given image deleting unwanted space?

1 view (last 30 days)
I want to crop the "Capture.JPG" image from "1.jpg"(left image=>right image)
I used the imcrop but it doesn't gave me the desired image.
My code is given below. Code requires two folders "Testing Images" and "Preprocess Testing Images".
code :-
Imges = dir('Testing Images\*jpg');
InputSet=[];
OutputSet=[];
for i = 1:numel(Imges)
filename = strcat('Testing Images\',Imges(i).name);
I=imread(filename);
Igray = rgb2gray(I);
imwrite(Igray,strcat('Preprocessed Testing Images\GrayScale',int2str(i),'.jpg'));
Ibw = im2bw(Igray,graythresh(Igray));
imwrite(Ibw,strcat('Preprocessed Testing Images\Binary',int2str(i),'.jpg'));
Ibwn=wiener2(Ibw,[5 5]);
imwrite(Ibwn,strcat('Preprocessed Testing Images\AfterNoise',int2str(i),'.jpg'));
Iedge = edge(Ibwn,'canny');
imwrite(Iedge,strcat('Preprocessed Testing Images\AfterEdge()',int2str(i),'.jpg'));
se = strel('arbitrary',2);
Iedge2 = imdilate(Iedge, se);
imwrite(Iedge2,strcat('Preprocessed Testing Images\AfterImdilate()',int2str(i),'.jpg'));
Ifill= imfill(Iedge2,'holes');
imwrite(Ifill,strcat('Preprocessed Testing Images\AfterImfill()',int2str(i),'.jpg'));
IfillInvert=imcomplement(Ifill);
imwrite(IfillInvert,strcat('Preprocessed Testing Images\AfterImInvert()',int2str(i),'.jpg'));
Ifinal1 = bwareaopen(IfillInvert,100);
[Ilabel num] = bwlabel(Ifinal1);
Iprops = regionprops(Ilabel);
Ibox = [Iprops.BoundingBox];
Icrop = imcrop(I,Ibox);
imwrite(Icrop,strcat('Preprocessed Testing Images\AfterCrop1()',int2str(i),'.jpg'));
end
  7 Comments
Indunie Florence
Indunie Florence on 1 Jun 2014
no, It doesn't crop above image correctly. Actually above code is a testing code for cropping. Your code is working to the given image by me. It is a very clear card. According to my target I use images with some noises. After some functions worked, it produces a image without boundary. I want to crop it too. Your code didn't work in this situation.
Can you help me.Image is given below. It is in binary mode.
Image Analyst
Image Analyst on 1 Jun 2014
It's a different card so will require a different algorithm. Since it's not a green box, you need to invert the image, then call bwconvhull(), then bwlabeland regionprops, finally imcrop. Give it a try.

Sign in to comment.

Accepted Answer

Image Analyst
Image Analyst on 31 May 2014
Try the attached test.m file to produce this image.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!