Réponse acceptée

Image Analyst
Image Analyst le 25 Déc 2016

1 vote

See Steve's blog for how you can use watershed segmentation to separate the blobs: http://blogs.mathworks.com/steve/2013/11/19/watershed-transform-question-from-tech-support/

4 commentaires

smallmonster
smallmonster le 26 Déc 2016
It's really helpful for me,thank you!
John BG
John BG le 27 Déc 2016
Modifié(e) : John BG le 27 Déc 2016
Hey hellboy, have you realised that following the segmentation in the link supplied by IA the narrow alleys of the resulting image have been deformed?
.
It's the problem shown in the 1st picture of point 3 of my answer. that coloured picture is the first step of applying Steve's segmentation.
My answer is more basic but doesn't deform the contours. Would you consider choosing my answer as the Accepted Answer for your question?
smallmonster
smallmonster le 28 Déc 2016
I'm so sorry i didn't make myself clear. In fact i want to get 3 blocks from the first picture, and Steve's method is enough for me. Thank you for your help.
John BG
John BG le 7 Jan 2017
fine to me, you choose as accepted answer IA's, it's ok.
But it's worth mentioning that with dell2 you get 3 blocks and there is no contour deformation whatsoever.
Yet with IA suggested segmentation when each block too close each other the 'corridors' suffer deformation.
Your original image has a more or less diagonal alley between the first from left segment and the 2nd segment, kind of hand written, yet IA proposed segmentation returns a Z-like straight segments uniformly narrow alley which is clearly different from the original image.

Connectez-vous pour commenter.

Plus de réponses (2)

John BG
John BG le 25 Déc 2016
Modifié(e) : John BG le 27 Déc 2016
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 commentaires

Walter Roberson
Walter Roberson le 27 Déc 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.
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
John BG le 27 Déc 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
Image Analyst le 28 Déc 2016
Modifié(e) : Image Analyst le 28 Déc 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
smallmonster le 28 Déc 2016
Thank you for your advice. I must say i am a beginner at image processing and i won't do it again. Steve Eddins result is not as good as the 2nd picture i posted, but it's enough for me.
Thanks again.

Connectez-vous pour commenter.

Walter Roberson
Walter Roberson le 25 Déc 2016

1 vote

imerode until the number of regions is 3

Community Treasure Hunt

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

Start Hunting!

Translated by