4

I want to segment the image to 3 parts like figure 2 shows. The first work I have done is using canny edge detection to extract edges like figure 3 shows using code below.

rgb = imread('Camera_205.png');
I = rgb2gray(rgb);
imshow(I)

figure
BW = edge(I,'canny',0.6);
BW = bwareaopen(BW, 80);
imshow(BW);

My question is how to segment the image to 3 parts by using this edges? I think region grows method not works here since the line is not connected to the end of the image. You can feel free to download the first image and test it. Thank you for your help. Original Color Image Image that needs to be segmented to 3 parts Edge image

Web_Designer
  • 72,308
  • 93
  • 206
  • 262
SimaGuanxing
  • 673
  • 2
  • 10
  • 29

1 Answers1

3

These are edge artifacts. Canny uses gradients internally and it's unclear how to calculate gradient at the image boundaries (also they are blurred inside matlab's edge). Just crop the BW image.

EDIT:

The default value of sigma for gaussian blurring in edge is 1.41 pixels. So If you crop approximately twice that and additional 2 pixels to account for sobel kernel used to calculate gradient (5 pixels in total) from each edge (I mean edge of the image, not the detected edges), you'll get rid of the edge artifacts.

Like this

BW_cropped = BW(5:end-5,5:end-5)

Then add back those 5 pixels to each coordinate if your image processing involves finding positions of something in the image.

EDIT2:

For example to get all pixels in regions of cropped image use bwconncomp on inverted image like this:

CC=bwconncomp(not(BW_cropped),4);

Result:

>> CC

CC = 

    Connectivity: 4
       ImageSize: [471 631]
      NumObjects: 3
    PixelIdxList: {[40405x1 double]  [254682x1 double]  [1430x1 double]}

So you get and output structure , where field PixelIdxList gives you a three element (number of regions) of indeces of all pixels within your regions (indexing into the cropped_BW).

You can then use CC in regionprops function to get some info about your regions (like area or centroid, see help for all the oprtions)

EDIT3:

Example code:

a = imread('XEDCa.png');

I = rgb2gray(a);
BW = edge(I,'canny',0.6);
BW_cropped = BW(5:end-5,5:end-5);
CC=bwconncomp(not(BW_cropped),4)
imagesc(labelmatrix(CC))

Result:

enter image description here

rayryeng
  • 102,964
  • 22
  • 184
  • 193
Hennadii Madan
  • 1,573
  • 1
  • 14
  • 30
  • Thank you for the reply. What do you mean by crop the BW image? How can I crop it based on the edges? Thanks. – SimaGuanxing Mar 11 '16 at 05:26
  • @JoeWang I edited the answer, to answer your comment – Hennadii Madan Mar 11 '16 at 05:33
  • Thank you again. Still not sure why you want me to crop the BW image. Is that because it will give me a close region for doing region growth? My final goal is to segment the image in 3 parts as I labeled according to the where the edge lies in the BW image. – SimaGuanxing Mar 11 '16 at 06:04
  • When add back the boundary regions, I think I still face to the segmentation problem to decide which boundary belongs to which region. Right? – SimaGuanxing Mar 11 '16 at 06:12
  • @JoeWang Check my second edit. To visualize the results use `imagesc(labelmatrix(CC))` – Hennadii Madan Mar 11 '16 at 06:17
  • @JoeWang regarding boundary regions: yes obviously they will be unlabeled. But if you want coarse labeling you can do some nearest neighbor interpolation. If you know in advance that the lines are straight, then you can use hough transform to find two lines in your image and segment based on that. – Hennadii Madan Mar 11 '16 at 06:20
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/105968/discussion-between-joe-wang-and-hennadii-madan). – SimaGuanxing Mar 11 '16 at 06:25