Detecting and Plotting A Circular Region, Finding its Coordinates

6 views (last 30 days)
Hi, In the the following code, Specifically, In the section %Circles, I attempt to detect the vaguely circular regions and successfully draw a circular boundary about these regions as seen in the below images.
How do I isolate the center coordinates for the hinge circle (small red central circle) marked in the second figure
and save it to (x y) variables?
This code gives me 18 values for both the centers (centers.mat) and the dia (dia.mat) values. (This includes tiny holes and ther disturbances at the right bottom corner of the image)
The values of dia for the hinge are always between 38 and 50 pixels (4th reading in dia.mat in this case)
I tried filtering the small red circle out on the basis of the MajorAxisLength and MinorAxisLength being approximately equaly, which did not work.
Image 'I'
Image 'bw'
% 1
clc;
clear all;
tic;
% 2
%Find the total number of frames
v = VideoReader('A=45.mov');
n = v.NumFrames
% 3
i = 1
A = zeros(n,1) ;
for i =50 % i:n or choose a specific frame, say the 50th one
I = read(v,i);
imshow(I)
imwrite( I , 'I.png');
% Convert the RGB image to a Grayscale image
G=im2gray(I);
level = graythresh(G);
% Binarize the grayscale image
BW = imbinarize(G,level);
imwrite(BW, 'BW.png')
% Circles
a = imread('BW.png');
bw = a< 1;
imshow(bw)
imwrite(bw, 'bw.png')
title('Image with Circles')
stats = regionprops('table',bw,'Centroid',...
'MajorAxisLength','MinorAxisLength')
centers = stats.Centroid;
dia = mean([stats.MajorAxisLength stats.MinorAxisLength],2);
radii = dia/2;
hold on
viscircles(centers,radii); % HOW DO I MAKE THESE CIRCLES PERMANENT? HOW DO I FIND THE CENTER OF ONLY THE HINGE CIRCLE?
hold off
% FURTHER PROCESSING TO BE CARRIED OUT AFTER DETECTION OF HINGE CENTER COORDINATES
% % 4
% % Fill holes
% BWfill = imfill(BW,'holes');
% % 5
% % Keep Only rectangular elements
% SE = strel('rectangle',[15 15]);
% BWstruct = imopen(BWfill, SE);
% % imshow(BWstruct);
% imwrite(BWstruct,'BWstruct.png');
% % 6
% % Filter the rectangular bar from the image by Area
% BWarea = imread('BWstruct.png');
% BW2 = bwareafilt(BWarea,[38000 60000]); %Check a few random frames using image region analyzer to see what the range of areas is for the rectangular blob
% % imwrite(BW2, 'BW2.png');
% % 7
% % Find the centroid
% measurements = regionprops(BW2, 'Centroid');
% x1 = measurements.Centroid(1);
% y1 = measurements.Centroid(2);
% % 8
% % Find the difference in x and y coordinates
% X = x1 - x;
% Y = y1 - y;
% % Find the arctan, i.e, arctan(X/Y)
% R = atan(X/Y); %Angle in rad
% D = rad2deg(R); %Angle in deg
% % 9
% % Save Displacement values in an array
% A(i) = D;
end
toc;

Answers (2)

yanqi liu
yanqi liu on 8 Feb 2021
sir, may be use the follow code, such as
clear all; clc; close all;
img = imread('./image.png');
J = rgb2lab(img);
l = mat2gray(J(:,:,1));
th = min([graythresh(l)*1.5 0.5]);
mw = im2bw(l, th);
mw = imclearborder(mw);
mw = imfill(mw, 'holes');
mw = bwareafilt(mw,1);
b = mat2gray(J(:,:,3));
th = min([graythresh(b)*1.5 0.5]);
bw = im2bw(b, th);
bw = logical(bw .* mw);
bw = bwareafilt(bw,1);
[r, c] = find(bw);
rect = [min(c) min(r) max(c)-min(c) max(r)-min(r)];
cen = [rect(1)+rect(3)/2, rect(2)];
figure;
imshow(img);
hold on; rectangle('Position', rect, 'EdgeColor', 'y', 'LineWidth', 2, 'LineStyle', '-');
plot(cen(1), cen(2), 'go', 'MarkerFaceColor', 'g')
  1 Comment
KLETECH MOTORSPORTS
KLETECH MOTORSPORTS on 8 Feb 2021
This code works on that specific image, but I am processing a video file composed of a few hundred frames.
I only need the coordinates of the hinge, which is why I'm trying to obtain the coordinates of the circle that I have detected, as it will be detected in every frame.
It does not work for other frames, for example, for the 360th frame:
I noticed in this frame that the green dot does not lie on the red part either.
Is there any way I can modify my existing code to get the centre coordinates of that circle?

Sign in to comment.


yanqi liu
yanqi liu on 8 Feb 2021
sir, sorry, i do not get all the images, so i use guess to figure it out, like follows
clear all; clc; close all;
img = imread('./image2.png');
J = rgb2lab(img);
l = mat2gray(J(:,:,1));
th = min([graythresh(l)*1.5 0.5]);
mw = im2bw(l, th);
mw = bwareafilt(mw,1);
bl = imbinarize(l, graythresh(l));
ml = ~logical(bl.*mw);
mt = bwareafilt(ml,1);
ml(mt) = 0;
ml = logical(ml);
ml = imfill(ml, 'holes');
ml = imopen(ml, strel('disk', 5));
ml = imclose(ml, strel('disk', 5));
ml = imfill(ml, 'holes');
ml = logical(ml);
ml = imopen(ml, strel('disk', 5));
ml = imclose(ml, strel('disk', 5));
[r, c] = find(ml);
rect = [min(c) min(r) max(c)-min(c) max(r)-min(r)];
cen = [rect(1)+rect(3)/2, rect(2)+rect(4)/2];
figure;
imshow(img);
hold on; rectangle('Position', rect, 'EdgeColor', 'y', 'LineWidth', 2, 'LineStyle', '-');
plot(cen(1), cen(2), 'co', 'MarkerFaceColor', 'c')
  1 Comment
KLETECH MOTORSPORTS
KLETECH MOTORSPORTS on 9 Feb 2021
yanqi liu, I figured it out.
I've modified this code for the specifications of the object in the video, and it works fine.
Thank you for your input.
%Find the total number of frames first
v = VideoReader('A=45.mov'); %Any video can be used as 'A'
n = v.NumFrames
disp(n)
%%
I = read(v,1);
% Convert the RGB image to a Grayscale image
G=im2gray(I);
level = graythresh(G);
% Binarize the grayscale image
BW = imbinarize(G,level);
imwrite(BW, 'BW.png')
%Remove holes smaller than the bearing
original = imread('BW.png');
se = strel('disk',7);
noholes = imopen(original,se);
imwrite(noholes,'noholes.png');
% Find Circles
a = imread('noholes.png');
bw = a< 1;
imshow(bw)
stats = regionprops('table',bw,'Centroid',...
'MajorAxisLength','MinorAxisLength')
centers = stats.Centroid;
dia = mean([stats.MajorAxisLength stats.MinorAxisLength],2);
radii = dia/2;
hold on
imwrite(bw,'bwnew.png');
hold off
[centers, radii, metric] = imfindcircles(bw,[20 1400]); %Filters circles between a and b pixels
viscircles(centers, radii,'EdgeColor','#F7B3DA'); %Draws on edge
centers;
x = centers(1)
y = centers(2)
%%

Sign in to comment.

Products


Release

R2020b

Community Treasure Hunt

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

Start Hunting!