中文说明:
读图然后根据图像的对象,进行边缘检测,椭圆拟合。
% % % % % % % % 乒乓球面阵图像处理及边界椭圆拟合 % % % % % % % %
% 编写日期:2020年5月26日
% 编写人:ZlinJ
% 大概思路。读图→形态学(关运算,开运算)→边缘检测→区域标记→边界追踪→自定义椭圆函数拟合。
%%图像边缘检测和拟合轮廓
clc
clear
close all
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%% 开始 %%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
tic;
%读取图
I = imread('C:\Users\Administrator\Desktop\201.jpg');
K1=medfilt2(I,[3,3]);
SE=strel('disk',1); %创建圆盘半径1
K2=imclose(K1,SE);%关运算
figure,imshow(K2);title('关运算');
K3=imopen(K2,SE);%开运算
figure,imshow(K3);title('开运算');
I1= im2bw(K1,70/255); %二值化
I2 = imfill(I1,'holes'); %填补孔洞
[M,N] = size(I2);
figure(1),imshow(I2);title('2值化原图');
hold on
% 选取待拟合坐标
% conicP = ginput(15);
bw1 = edge(I2,'canny',0.5); %边缘检测
figure,imshow(bw1);title('边缘检测');
% imwrite(bw1,'C:\Users\Administrator\Desktop\202.jpg'); %输出图,人工检测拟合结果。
[L,num] = bwlabel(bw1); %L指与bw1同样大小的矩阵,num代表图像中对象的数目 %标签标记连通域
YuanX=zeros(num,2);
% X1=zeros(2,num);
% Y1=zeros(2,num);
for i = 1:num
[row,col] = find(L == i);
% [row,col] = find(X, ...)语句理解
% 返回矩阵X中非零元素的行和列的索引值。
% 这个语法对于处理稀疏矩阵尤其有用。
% 如果X是一个N(N>2)维矩阵,col包括列的线性索引。
% 例如,一个5*7*3的矩阵X,有一个非零元素X(4,2,3),find函数将返回row=4和col=16。
% 也就是说,(第1页有7列)+(第2页有7列)+(第3页有2列)=16。
conicP = zeros(length(row),2);% 创建一个和cow矩阵相同行数2列的0矩阵
conicP(:,1) = col;
conicP(:,2) = row;
figure(1),plot(conicP(:,1)', conicP(:,2)', 'xr'); %绘制采样点
%% 自定义椭圆函数拟合
a0 = [1 1 1 1 1 1];
f = @(a,x)a(1)*x(:,1).^2+a(2)*x(:,2).^2+a(3)*x(:,1).*x(:,2)+a(4)*x(:,1)+a(5)*x(:,2)+a(6);%建立方程
p = nlinfit(conicP , zeros(size(conicP, 1), 1), f,[1 2 3 4 5 6]);
syms x y
conic = p(1)*x^2++p(2)*y^2+p(3)*x*y+p(4)*x+p(5)*y+p(6);
A=p(1);B=p(3);C=p(2);D=p(4);E=p(5);F=p(6);
Xc=(B*E-2*C*D)/(4*A*C-B^2);
Yc=(B*D-2*A*E)/(4*A*C-B^2);
X=[Xc,Yc];
YuanX(i,:)=X;
% X1(1,i)=Xc;
% Y1(1,i)=Yc;
%% 在原图上显示拟合结果
c = ezplot(conic,[0,N],[0,M]);
figure(1),set(c, 'Color', 'Blue','LineWidth',2);
end
toc;
English Description:
Read the image and then carry out edge detection and ellipse fitting according to the object of the image p>
%%%%%%%%%%% image processing of table tennis spherical array and boundary ellipse fitting%%%%%%%%%%%
% date of preparation: May 26, 2020
% Author: zlinj
% general idea. Image reading → morphology (off operation, on operation) → edge detection → region marking → boundary tracking → custom elliptic function fitting p>
%% image edge detection and contour fitting
clc
clear
close all
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% p>
% read graph
i = imread (&\39; c:\users\administrator\desktop\201.jpg&\39;) p>
K1=medfilt2(I,[3,3]); p>
SE=strel('disk',1); % Create disc radius 1
k2=imclose (K1, Se);% Close operation
figure, imshow (K2); Title (&\39; related operation &\39;) p>
K3=imopen(K2,SE);% Open operation
figure, imshow (K3); Title (&\39; open operation &\39;) p>
I1= im2bw(K1,70/255); % Binarization
i2 = imfill (I1, &\39; holes&\39;);% Filling holes
[m, n] = size (i2) p>
figure(1),imshow(I2); Title (&\39; 2-valued original drawing &\39;) p>
hold on
% select the coordinate to be fitted
% conicp = ginput (15) p>
bw1 = edge(I2,'canny',0.5); % Edge detection
figure, imshow (bw1); Title (&\39; edge detection &\39;) p>
% imwrite(bw1,'C:\Users\Administrator\Desktop\202.jpg'); % Output the graph and manually detect the fitting results p>
[L,num] = bwlabel(bw1); % L refers to the matrix of the same size as bw1. Num represents the number of objects in the image% label marks the connected domain
yuanx=zeros (Num, 2) p>
% X1=zeros(2,num); p>
% Y1=zeros(2,num); p>
for i = 1:num
[row,col] = find(L == i); p>
% [row,col] = find(X, ...) Statement understanding
% returns the index values of rows and columns of non-zero elements in matrix X p> The syntax
% is particularly useful for dealing with sparse matrices p>
% if x is an n (n> 2) dimensional matrix, col includes the linear index of the column p>
% for example, if a 5*7*3 matrix X has a non-zero element X (4,2,3), the find function will return row=4 and col=16 p>
% that is, (Page 1 has 7 columns) + (page 2 has 7 columns) + (page 3 has 2 columns) =16 p>
conicP = zeros(length(row),2);% Create a 0 matrix with the same row number and 2 columns as the cow matrix
conicp (:, 1) = col p>
conicP(:,2) = row; p>
figure(1),plot(conicP(:,1)', conicP(:,2)', 39; xr'); % Draw sampling points
%% custom elliptic function fitting
A0 = [1 1 1 1 1] p>
f = @(a,x)a(1)*x(:,1).^ 2+a(2)*x(:,2).^ 2+a(3)*x(:,1).* x(:,2)+a(4)*x(:,1)+a(5)*x(:,2)+a(6);% Establish the equation
P = nlinfit (conicp, zeros (size (conicp, 1), 1), F, [123456] p>
syms x y
conic = p(1)*x^2++p(2)*y^2+p(3)*x*y+p(4)*x+p(5)*y+p(6); p>
A=p(1); B=p(3); C=p(2); D=p(4); E=p(5); F=p(6); p>
Xc=(B*E-2*C*D)/(4*A*C-B^2); p>
Yc=(B*D-2*A*E)/(4*A*C-B^2); p>
X=[Xc,Yc]; p>
YuanX(i,:)=X; p>
% X1(1,i)=Xc; p>
% Y1(1,i)=Yc; p>
%% display the fitting result on the original drawing
C = ezplot (conic, [0, n], [0, m]) p>
figure(1),set(c, 'Color', 'Blue','LineWidth',2); p>
end
toc; p>