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

1 次查看(过去 30 天)
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.
X = imread("f_000047.tif");
% Adjust data to span data range.
X = imadjust(X);
X = imgaussfilt(X,1.4);
% Threshold image - manual threshold
BW = X > 164;
% Create masked image.
fontSize = 8;
maskedImage = X;
maskedImage(~BW) = 0;
SE = strel("disk",7); % Generating a disk-type structuring element of radius 10 for dilation
maskedImage = imdilate(maskedImage,SE); %Filling up holes or inaccuracies in the intial BGMask
%maskedImage = imrotate(maskedImage,-90);
subplot(3,1,1);
imshow(maskedImage);
title("MaskedImage","FontSize",fontSize);
%----------------------------------------------------------------------------------------------------------
%Finding out higher SD regions across the background
ImgSD = stdfilt(X,ones(13)); %Standard deviation (SD) filtering based on varying SDs across the image
ImgSD(~maskedImage)=0; %Removing the background
subplot(3,2,2);
imshow(ImgSD,[]);
subplot(3,2,3);
histogram(ImgSD,100);
[x,y] = size(ImgSD);
xGrad = zeros(x,y);%Horizontal gradient matrix definition
yGrad = zeros(x,y);%Vertical gradient matrix definition
Tan_ver_add = size(x,y);
Tan_horz_add = size(x,y);
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
h_grad = (circshift(ImgSD,[0,1])-circshift(ImgSD,[0,-1]))/2; %Method 2
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
v_grad = (circshift(ImgSD,[1,0])-circshift(ImgSD,[-1,0]))/2; %Method 2
%-----------------------------------------------------------------------------------------------
%Gradient Modulus
grad_tot_mod =sqrt((h_grad).^2+(v_grad).^2); % finding the magnitude of gradient across the image (Method 2)
GradMod = sqrt((xGrad).^2+(yGrad).^2); %Method 2 for Gradient Modulus
%---------------------------------------------------------------------------------------------
%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 (grad_tot_mod(i,j)>0.1 && grad_tot_mod(i,j)<10) %imposing the gradient condition to plot markers
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
Tan_horz_add= atan2d(h_grad(i+5,j),v_grad(i+5,j));
Tan_horz_sub= atan2d(h_grad(i-5,j),v_grad(i-5,j));
Tan_ver_add= atan2d(h_grad(i,j+5),v_grad(i,j+5));
Tan_ver_sub= atan2d(h_grad(i,j-5),v_grad(i,j-5));
%Changed the angle definition from
%(v_grad(),h_grad()) to (h_grad(),v_grad())
if Tan_horz_add<0
Tan_horz_add= Tan_horz_add+180;
end
if Tan_ver_add < 0
Tan_ver_add = Tan_ver_add+180;
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
if ((abs(Tan_ver_add-Tan_ver_sub)>= 5 && abs(Tan_ver_add-Tan_ver_sub)<15)||(abs(Tan_ver_add-Tan_ver_sub)>=95 && abs(Tan_ver_add-Tan_ver_sub)<120))
if ((abs(Tan_horz_add-Tan_horz_sub)>=5 && abs(Tan_horz_add-Tan_horz_sub)<15))
plot(i,j,"*")
end
end
end
end
end
end
end
hold off
  8 个评论
Image Analyst
Image Analyst 2021-7-31
编辑:Image Analyst 2021-7-31
It works fine with gray scale images. Here's proof:
grayImage = imread('cameraman.tif');
imshow(grayImage, []);
drawnow;
g = gcf;
g.WindowState = 'maximized'
uiwait(msgbox('Draw a freehand curve'));
h = drawfreehand
% Rplace by a curve.
xy = h.Position;
x = xy(:, 1);
y = xy(:, 2);
% Tack first point onto last so the curve will be closed.
x = [x; x(1)];
y = [y; y(1)];
hold on;
delete(h);
plot(x, y, 'y-', 'LineWidth', 3);
Atreya Danturthi
Atreya Danturthi 2021-7-31
"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.

请先登录,再进行评论。

回答(0 个)

产品


版本

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by