# Detecting and counting the number of scales (circular or psuedo circular objects in the image) and drawing boundaries around the scales or circular objects.

2 views (last 30 days)
Atreya Danturthi on 30 Jul 2021
Commented: Atreya Danturthi on 31 Jul 2021
I have asked this question previously one the forum and got a few answers for the same, however they do not really answer the core aspects of what I want to solve. So, I am asking a modified version of the same question again.
I have multiple grayscale images, which have circular or psuedo circular objects within them. I need to detect them, draw boundaries around them and then count the number of these objects in each image.
I am attaching one grayscale image for your reference.
Any inputs will be highly appreciated.
This is the code that I had tried out.
Guys, can anyone give any suggestions, I would really appreciate some help as I am on a ticking clock.
I have attached the photos with objects marked in red.
% Adjust data to span data range.
X = imgaussfilt(X,1.4);
% Threshold image - manual threshold
BW = X > 164;
fontSize = 8;
SE = strel("disk",7); % Generating a disk-type structuring element of radius 10 for dilation
subplot(3,1,1);
%----------------------------------------------------------------------------------------------------------
%Finding out higher SD regions across the background
ImgSD = stdfilt(X,ones(13)); %Standard deviation (SD) filtering based on varying SDs across the image
subplot(3,2,2);
imshow(ImgSD,[]);
subplot(3,2,3);
histogram(ImgSD,100);
[x,y] = size(ImgSD);
Tan_ver_sub = size(x,y);
Tan_horz_sub = size(x,y);
for i = 1:x-1
for j = 2:y-1
xGrad(i,j) = (ImgSD(i,j+1)-ImgSD(i,j-1))/2; % finding the horizontal gradient across the image (Method 1)
end
end
for j = 1:y-1
for i = 2:x-1
yGrad(i,j) = (ImgSD(i+1,j)-ImgSD(i-1,j))/2; % finding the vertical gradient across the image (Method 1)
end
end
%-----------------------------------------------------------------------------------------------
%---------------------------------------------------------------------------------------------
%Calculating double derivative in the horizontal and vertical directions as
%a second parameter for reducing the area of interest
double_der_h = zeros(x,y); %Horizontal double derivative matrix definition
double_der_v = zeros(x,y); %Vertical double derivative matrix definition
for i = 1:x-1
for j = 2:y-1
double_der_h(i,j) = (ImgSD(i,j+1)-2*ImgSD(i,j)+ImgSD(i,j-1)); % finding double derivative across x-direction in the image
end
end
for j = 1:y-1
for i = 2:x-1
double_der_v(i,j) = (ImgSD(i+1,j)-2*ImgSD(i,j)+ImgSD(i-1,j)); % finding double derivative across y-direction in the image
end
end
double_der_tot = double_der_h+double_der_v;% Can we do scalar addition of the individual components of the double derivative or do we need to calculate the modulus
%Should I use a Laplacian operator instead?
%doubleDMod = sqrt((double_der_h).^2+(double_der_v).^2);
subplot(3,3,1);
imshow(X);
hold on
for i = 1:x
for j = 1:y
if (double_der_tot(i,j)~=0) % imposing double derivative condition to plot markers %Ask about a different condition >=0
if ImgSD(i,j)>=0
%Changed the angle definition from
end
end
if Tan_horz_sub < 0
Tan_horz_sub = Tan_horz_sub+180;
end
if Tan_ver_sub < 0
Tan_ver_sub = Tan_ver_sub+180;
end
plot(i,j,"*")
end
end
end
end
end
end
end
hold off
Atreya Danturthi on 31 Jul 2021
"Error using images.internal.imageDisplayValidateParams>validateCData (line 118)
Multi-plane image inputs must be RGB images of size MxNx3.
Error in images.internal.imageDisplayValidateParams (line 30)
common_args.CData = validateCData(common_args.CData,image_type);
Error in images.internal.imageDisplayParseInputs (line 79)
common_args = images.internal.imageDisplayValidateParams(common_args);
Error in imshow (line 253)
images.internal.imageDisplayParseInputs({'Parent','Border','Reduce'},preparsed_varargin{:});
Error in Freehandmethod (line 2)
imshow(grayImage, []);
This is the error that showed up when I replaced the cameraman image with one of my images.

R2021a

### Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!