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:
