# Separate overlapping line shapes (or circle shapes) in raster data

I have a classifier that creates a (raster) segmentation mask that looks like this (after converting Raster to Polygon):

Now I'm wondering, whether there is a method to extract actual objects from the shape of the pixel masks - like fitting lines (respectively circles) on the orange pixels (respectively green) pixels to separate obviously different objects. A desired output would look somehow like this:

Do you have any ideas on how to approach this, with QGIS, ArcGIS or if need be programming (preferably MATLAB, python or C++)?

• -1 for posting and not checking how post look. Update pictures, I remove down vote – FelixIP Nov 17 '16 at 19:15
• Thank you for the hint! I did check how the post looked and pictures were displayed at the time I posted. Apparently the link expired after some time. I updated now with a permanent solution. – Honeybear Nov 18 '16 at 11:26
• Good, upvoted now – FelixIP Nov 18 '16 at 17:21

After I while I figured out two different approaches: To detect line and circle shapes, the Hough transform can be used. It transforms an image to the Hough space in which high values represent high activations for a certain parametrized shape, e.g. a line with parameters rho and theta (that correspond to line normal and distance from left corner). I used a MATLAB implementation.

For the circle shapes, I rather used a watershed segmentations, as the Hough approach is difficult to optimize. It works with roughly convex shapes.

Since my code for these to approaches grew to a rather large library, I'm just giving the approach here. If you're interested in more, comment or message me for details.

### LINES

For lines, a sequence of a hough transformation (hough()), hough space peak detection (houghpeaks) and line segment detection (houghlines) did the trick. It looks something like this:

% load image and binarize
img = imbinarize(img);

% hough transform
[H, theta, rho] = hough(img);
% houghpeaks parameters
nPeaks = 1; % number of lines to fit
nHood = size(H)/5; % neighborhood around houghpeaks in which further detections are supressed
nHood = max(2*ceil(nHood/2) + 1, 1); % numbers need to be odd

% find peaks
P = houghpeaks(H,nPeaks,'NHoodSize',nHood,'Threshold',0.1*max(H(:)));

% plot hough transform
subplot(1,2,2);
imshow(H,[],'XData',theta,'YData',rho,...
'InitialMagnification','fit');
xlabel('\theta'), ylabel('\rho');
axis on, axis normal, hold on;
% plot peaks
peak_x = theta(P(:,2)); peak_y = rho(P(:,1));
plot(peak_x,peak_y,'s','color','white');

% find lines
lines = houghlines(img,T,R,P,'FillGap',5,'MinLength',12);

% plot lines
subplot(1,2,1);
figure, imshow(img), hold on;
for j = 1:length(lines)
xy = [lines(j).point1; lines(j).point2];
plot(xy(:,1),xy(:,2),'LineWidth',2,'Color','blue');
end


This will give a line with ~1px thickness. I'm still working on finding the whole line-shaped object.

### CIRCLES

For circles, there is the MATLAB function imfindcircles with doc here. I used the following command, but you'll have to play around with the parameters.

[centres, radii, metric] = imfindcircles(img, [10, 200], ...
'ObjectPolarity','bright', ... # bright or dark
'Method','TwoStage', ... # TwoStage or PhaseCode
'Sensitivity', 0.96 ...
);