MATLAB: Boundary tracing in image

bwboundariesedge detectionimage analysisImage Processing Toolbox

Hello folks,
I am trying to trace the boundaries of a binary edge detected image to the point that I can export the file as polylines into ArcGIS. So far, I have converted the image to a binary image and identified the edges. I've tried to use both bwtraceboundary and bwboundaries to identify the edge outlines and then export this file with polylines, but am not having any luck. The original image is attached as is the code to convert the image to a binary edge-detected image. Any help is much appreciated!
clear
RGB=imread('DJI_0022.jpg'); %inputs image
I=rgb2gray(RGB); %convers to grayscale
figure
imshow(I)
BW1 = edge(I,'Canny',0.6);
imshow(BW1)

Best Answer

  • After Canny, those blobs are single pixel wide blobs, so it makes no sense to call bwboundaries() on them. That would just simply give you the same coordinates you already have, any maybe even doubled up as it goes to one end and back again. I think you want to use regionprops and ask for PixelList.
    rgbImage = imread('DJI_0022.jpg'); %inputs image
    hFig = figure;
    subplot(2, 2, 1);
    imshow(rgbImage)
    axis('on', 'image');
    impixelinfo;
    title('Original RGB Image', 'FontSize', 20);
    grayImage = rgb2gray(rgbImage); %convers to grayscale
    subplot(2, 2, 2);
    imshow(grayImage)
    axis('on', 'image');
    impixelinfo;
    title('Gray Scale Image', 'FontSize', 20);
    edgeImage = edge(grayImage, 'Canny', 0.6);
    subplot(2, 2, 3);
    imshow(edgeImage)
    axis('on', 'image');
    title('Canny Edge Image', 'FontSize', 20);
    hFig.WindowState = 'maximized';
    drawnow;
    % Get coordinates.
    props = regionprops(edgeImage, 'PixelList');
    for k = 1 : length(props)
    thisList = props(k).PixelList;
    fprintf('\nFor blob #%d of %d:\n ', k, length(props))
    for k2 = 1 : size(thisList, 1)
    thisx = thisList(k2, 1);
    thisy = thisList(k2, 2);
    fprintf('(%d, %d), ', thisx, thisy);
    end
    end