(MATLAB)轴承尺寸内外径的检测方法及代码

标签:
杂谈it图片教育文化 |
http://s1/mw690/002jY1Ccgy6JNi4phkI40&690
如上图所示,需要求圆环内径、外径,(面积法,hough直线检测、hough圆检测)
1,可以通过求圆形的面积,根据面积,来求轴承的直径。
首先要提取内圆与外圆,然后再扫描面积,最后求半径,具体代码如下
clc;
clear all;
close all;
warning off all;
I = imread('f:\10mm\1.bmp');
imshow(I)
c=1.0e+03 *[ 0.7705 1.6865 1.6865 0.7585 0.7705];
r =1.0e+03 *[0.4945 0.4945 1.4545 1.4545 0.4945];
J =
roifill(I,c,r);
figure,imshow(J);
figure,imhist(J);
Q=im2bw(I,150/255);
figure,imshow(Q);
M=im2bw(J,150/255);
figure,imshow(M);
N=~M;
figure,imshow(N);
n=bitand(N,Q);
figure,imshow(n)
arae1=bwarea(n);
arae2=bwarea(N);
arae3=bwarea(N)-bwarea(n);
r1=sqrt(arae1/pi);
r2=sqrt(arae2/pi);
fprintf(1,'r1=%d\nr2=%d\n',r1,r2);
2 hough直线检测
通过检测圆内的最长线段来检测圆的直径和圆心,具体代码如下:
clc;clf;clear all;close all;warning off all;
I = imread('f:\10mm\1.bmp');
figure,imshow(I)
;
c=1.0e+03 *[ 0.7705 1.6865 1.6865 0.7585 0.7705];
r =1.0e+03 *[0.4945 0.4945 1.4545 1.4545 0.4945];
J =
roifill(I,c,r);
figure,imshow(J);
figure,imhist(J);
BW1=im2bw(I,150/255);
figure,imshow(BW1);title('BW1');
BW2=im2bw(J,150/255);
figure,imshow(BW2);
title('BW2');
N=~BW2;
figure,imshow(N);title('N');
n=bitand(N,BW1);
figure,imshow(n);title('n');
%检测外圆直径及坐标
[H, theta,
rho]=hough(N);
peaks=houghpeaks(H,2);
lines=houghlines(N,theta,
rho,peaks);
figure,imshow(N),hold on;
max_len1 = 0;
for
k=1:length(lines)
end
title('外径hough直线检测')
plot(xy_long(:,1),xy_long(:,2),'LineWidth',2,'Color','green');
max_len1
lines(1)
lines(2)
[xy]
a1=(xy(1)+xy(3))/2;
b1=(xy(2)+xy(4))/2;
para1=[a1,b1]
%检测内圆直径及圆心坐标
[h,t,r]=hough(n);
peaks=houghpeaks(h,2);
lines=houghlines(n,t,r,peaks);
figure,imshow(n),hold on
max_len2 = 0;
for k=1:length(lines)
end
title('内径huogh直线检测')
plot(xy_long(:,1),xy_long(:,2),'LineWidth',2,'Color','green');
max_len2
lines(1)
lines(2)
[xy]
a2=(xy(1)+xy(3))/2;
b2=(xy(2)+xy(4))/2;
para2=[a2,b2]
3 hough圆变换,具体代码如下
clc;
clear
all;
close
all;
clf;
I=imread('f:\10mm\1.bmp');
J=medfilt2(I,[3,3]);
%迭代法求阈值
ZMax=max(max(J));
ZMin=min(min(J));
TK=(ZMax+ZMin)/2;
bCal=1;
iSize=size(J);
while(bCal)
end
disp(strcat('迭代后的阈值:',num2str(TK)));
BW=im2bw(I,double(TK)/255);
ED=edge(BW,'canny');
%霍夫变化检测圆
step_r = 1;
step_angle =
0.1;
r_min=250;
r_max=300;
%注意半径范围的选取,直接影响到你想要检测的圆。
% r_min=900;
% r_max=950;
p =
0.7;
[hough_space,hough_circle,para]=hough_circle1(ED,step_r,step_angle,r_min,r_max,p);
subplot(231),imshow(I),title('原图')
subplot(232),imshow(J),title('中值滤波')
subplot(233),imshow(BW),title('二值化图像')
subplot(234),imshow(ED),title('边缘检测')
subplot(235),imshow(hough_circle),title('检测结果')
function[hough_space,hough_circle,para]=hough_circle1(BW,step_r,step_angle,r_min,r_max,p)
%------------------------------算法概述-----------------------------
% 该算法通过a = x-r*cos(angle),b = y-r*sin(angle)将圆图像中的边缘点
% 映射到参数空间(a,b,r)中,由于是数字图像且采取极坐标,angle和r都取
% 一定的范围和步长,这样通过两重循环(angle循环和r循环)即可将原图像
% 空间的点映射到参数空间中,再在参数空间(即一个由许多小立方体组成的
% 大立方体)中寻找圆心,然后求出半径坐标。
%------------------------------输入参数-----------------------------
% BW:二值图像;
% step_r:检测的圆半径步长
% step_angle:角度步长,单位为弧度
% r_min:最小圆半径
% r_max:最大圆半径
% p:以p*hough_space的最大值为阈值,p取0,1之间的数
%------------------------------输出参数-----------------------------
% hough_space:参数空间,h(a,b,r)表示圆心在(a,b)半径为r的圆上的点数
% hough_circl:二值图像,检测到的圆
% para:检测到的圆的圆心、半径
[m,n] = size(BW);
size_r = round((r_max-r_min)/step_r)+1;
size_angle =
round(2*pi/step_angle);
hough_space = zeros(m,n,size_r);%返回一个m乘n乘p...的元素为零的数组。
[rows,cols] =
find(BW);
ecount =
size(rows);
% Hough变换
% 将图像空间(x,y)对应到参数空间(a,b,r)
% a = x-r*cos(angle)
% b = y-r*sin(angle)
for i=1:ecount
end
%检索完毕,寻找最大值,求出圆心坐标与半径,保存
max_para =
max(max(max(hough_space)));
index =
find(hough_space>=max_para*p);
length = size(index);
hough_circle=zeros(m,n);
for i=1:ecount
end
% 打印结果
for k=1:length
end
代码大部分都是自己写的,或许有些不太规范,希望大家交流指正