Search code examples
matlabcontourdotted-line

Convert contour to broken line in image Matlab


I have the following problem in Matlab: I have a 2D closed contour (a set of 2D coordinates of points) representing an object like in this image: image representing 2D contour

I want to convert it into a broken line contour like: dot-line-space-dot-line-space, etc.

Is there a way to solve this problem in Matlab? Thank you very very much


Solution

  • You can first fill in the object using imfill, then trace it using bwboundaries, and plot the result using plot with a given line style.

    % Load the image in and convert to binary
    img = imread('https://i.sstatic.net/G4NLh.png');
    img = img(:,:,1) > 170;
    
    % Fill in the middle hole and compute the boundary
    boundary = bwboundaries(imfill(img, 'holes'));
    
    % Plot the boundary on a black background
    plot(boundary{1}(:,2), boundary{1}(:,1), ...
                'LineStyle', '-.', ...
                'Marker', 'none')
    
    axis image
    axis ij
    

    enter image description here

    Update

    Oh...you already have the x/y points. Oh well! Just use the LineStyle property of plot to accomplish what you want.