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

1 次查看(过去 30 天)
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 个评论
Indunie Florence
Indunie Florence 2014-6-1
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 2014-6-1
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.

请先登录,再进行评论。

采纳的回答

Image Analyst
Image Analyst 2014-5-31
Try the attached test.m file to produce this image.

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Image Segmentation and Analysis 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by