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.
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 = imread('image_with_lines.tiff');
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
axis on, axis normal, hold on;
% plot peaks
peak_x = theta(P(:,2)); peak_y = rho(P(:,1));
% find lines
lines = houghlines(img,T,R,P,'FillGap',5,'MinLength',12);
% plot lines
figure, imshow(img), hold on;
for j = 1:length(lines)
xy = [lines(j).point1; lines(j).point2];
This will give a line with ~1px thickness. I'm still working on finding the whole line-shaped object.
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 ...