Detecting and Plotting A Circular Region, Finding its Coordinates
5 views (last 30 days)
Show older comments
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;
0 Comments
Answers (2)
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')
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')
See Also
Categories
Find more on Image Segmentation and Analysis in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!