How to segment this image correctly?
Show older comments
Accepted Answer
More Answers (2)
I tried watershed and label2rgb but didn't produce the desired result
1.
A=imread('im1.jpg');imshow(A)
BW=watershed(A)
Lrgb=label2rgb(BW);imshow(Lrgb)
2.
the Laplacian seems to get somewhere yet the contour is still a bit noisy
A1=double(A(:,:,1))
B=del2(A1);
figure(2);imshow(B)
3.
So I cleaned it with the following basic lines and the contour shows up
A1(A1<80)=0
A1(A1>120)=255
C=del2(A1);
figure(3);imshow(C)

and it also works for the fragmented image
A=imread('im2.jpg');figure(4);imshow(A)
A2=double(A(:,:,1))
A2(A2<80)=0
A2(A2>120)=255
C2=del2(A2);
figure(3);figure(5);imshow(C2)

if you find these lines useful would you please mark my answer as Accepted Answer?
To any other reader, if you find this answer of any help, please click on the thumbs-up vote link,
thanks in advance for time and attention
John BG
5 Comments
Walter Roberson
on 27 Dec 2016
The poster did not present two different scenarios: the poster presented one scenario with the input and a possible output. The segmentation has to be applied to the top object to create something that looks like the bottom object. Your code does not do that.
The fancy colored contouring you show in your "3" is the result of contouring on the accidental nonbinary values caused by the user having posted a binary image using lossy JPEG. If you look at imhist() of the image you can see that everything other than the minimum and maximum value occur in the noise levels.
Unfortunately a lot of the time it is not possible to accurately recover a binary matrix that has gone through JPEG lossy compression; plain thresholding does not work, no matter which threshold you use. JPEG blurs edges, especially vertical edges. For any low value read in from a JPEG file, it is not possible to tell whether it was a pixel that was originally "off" that was blurred by adjacent "on" pixels, or if it was a pixel that was originally "on" that was blurred by adjacent "off" pixels. Or at least no obvious method; I guess there might be some potential morphological reconstruction.
Image Analyst
on 27 Dec 2016
I would have immediately binarized the image if I was forced to use jpg, though he should have saved it as a png instead.
A=imread('im1.jpg');
if ndims(A) == 3
A = A(:,:,2); % Convert to gray scale if needed.
end
A = A > 128; % Convert from gray scale to logical image.
That should get rid of a lot of the clutter
John BG
on 27 Dec 2016
Image Analyst, I hope you don't mind me asking why is it that the cracks of the lower sample look slightly different when applying your answer. than when applying mine?
del2 works equally well when applied to the halved image than when as supplied in the question.
Image Analyst
on 28 Dec 2016
Edited: Image Analyst
on 28 Dec 2016
No of course I don't mind you asking John. Honestly I didn't try either your method or Steve Eddins method.
But with either method, the bad jpeg artifacts should be repaired so that you're starting with a pure binary image.
I saw a lot of clutter in your first two images that just didn't seem right and I think that if he had posted the pure binary images, you wouldn't have all that clutter and you might get different results. But with a binary image you wouldn't want to or need to do the Laplacian. If you just wanted to find the edges you can use bwperim() or bwboundaries(). Have you tried your method after the jpeg junk has been removed/repaired? Maybe you could attach your entire algorithm in one chunk/file so we can easily copy and paste it.
Again, smallmonster, never use JPEG format to save images that you want to do image analysis on. Use PNG instead.
smallmonster
on 28 Dec 2016
Walter Roberson
on 25 Dec 2016
1 vote
imerode until the number of regions is 3
Categories
Find more on Image Segmentation in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!


