Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Matlab image segmentation by using line edges

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

like image 811
SimaGuanxing Avatar asked Nov 01 '25 18:11

SimaGuanxing


1 Answers

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

like image 111
Hennadii Madan Avatar answered Nov 04 '25 12:11

Hennadii Madan



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!