# Detect almost circular shapes in RGB image I want to extract circular trees from this rgb image, I've tried circular hough and segmentation techniques but in vain. Any help?

## 1 Answer

The image itself is somehow blurred a little. Whole edges of trees are not visible. The only clue of tree existence seems like shadows. So I suggest using shadow information and watershed transform and local thresholding. I have done similar exercise in here. The result is not that perfect but it will give some idea. Here are the steps. 1. Find shadows, 2. Expand shadows towards sun direction 3. Create shadow mask 4. Blur the image 5. Watershed transform 6. Threshold each watershed segment individually 7. Fit circle with Hough transform

im=imread('P0Yvl.png');
imOrig=im;

% Grayscale
im=rgb2gray(im);

% Shadows
shadow=im<60;
[xs,ys]=find(shadow);

% Grow shadow towards sun direction

% Create structural element for dilation
str_element_initial=flip(eye(25));
str_element_initial(:,1:12)=0;

shadow2=zeros(size(str_element_initial));
for i=1:7
shadow2 = shadow2 | imresize(imrotate(str_element_initial,40-i*10),size(str_element_initial))>.1;
end

mask_shadow=imdilate(shadow,shadow2);
mask_shadow=mask_shadow-shadow;

% Plot shadow mask and initial shadow locations
figure,imagesc(mask_shadow);axis image;
hold on, plot(ys,xs,'r.') % Blur image
im2=imfilter(im,fspecial('gaussian',[21 21],3));

% Watershed transform
L = watershed(max(im2(:))-im2);
[xw,yw]=find(L==0);

figure,imagesc(imOrig),axis image
hold on, plot(yw,xw,'r.','MarkerSize',1) % Threshold each segment individually
tmp=zeros(size(im));

for i=1:max(L(:))
mask=L==i;
ind=find(mask);
thr =multithresh(im(ind),1);
tmp(ind)=im(ind)>thr*.8;
end

% Remove detections that don't have shadow
trees=tmp & mask_shadow;
trees=bwareaopen(trees,10);

% Find circles with Hough transform
[centers, radii, metric] = imfindcircles(trees,[5 25]);

% Result
figure,imagesc(imOrig),axis image
viscircles(centers, radii,'EnhanceVisibility',0,'LineStyle','-','LineWidth',1); 