function varargout = hq(varargin)
% HQ M-file for hq.fig
% HQ, by itself, creates a new HQ or raises the existing
% singleton*.
%
% H = HQ returns the handle to a new HQ or the handle to
% the existing singleton*.
%
% HQ('CALLBACK',hObject,eventData,handles,...) calls the local
% function named CALLBACK in HQ.M with the given input arguments.
%
% HQ('Property','Value',...) creates a new HQ or raises the
% existing singleton*. Starting from the left, property value pairs are
% applied to the GUI before hq_OpeningFcn gets called. An
% unrecognized property name or invalid value makes property application
% stop. All inputs are passed to hq_OpeningFcn via varargin.
%
% *See GUI Options on GUIDE's Tools menu. Choose "GUI allows only one
% instance to run (singleton)".
%
% See also: GUIDE, GUIDATA, GUIHANDLES
% Edit the above text to modify the response to help hq
% Last Modified by GUIDE v2.5 19-Aug-2013 18:34:58
% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', @hq_OpeningFcn, ...
'gui_OutputFcn', @hq_OutputFcn, ...
'gui_LayoutFcn', [] , ...
'gui_Callback', []);
if nargin && ischar(varargin{1})
gui_State.gui_Callback = str2func(varargin{1});
end
if nargout
[varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
else
gui_mainfcn(gui_State, varargin{:});
end
% End initialization code - DO NOT EDIT
% --- Executes just before hq is made visible.
function hq_OpeningFcn(hObject, eventdata, handles, varargin)
% This function has no output args, see OutputFcn.
% hObject handle to figure
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% varargin command line arguments to hq (see VARARGIN)
% Choose default command line output for hq
handles.output = hObject;
handles.HNum=0;
set(handles.holenum,'String',handles.HNum);
handles.info=[];
% Update handles structure
guidata(hObject, handles);
% UIWAIT makes hq wait for user response (see UIRESUME)
% uiwait(handles.figure1);
% --- Outputs from this function are returned to the command line.
function varargout = hq_OutputFcn(hObject, eventdata, handles)
% varargout cell array for returning output args (see VARARGOUT);
% hObject handle to figure
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Get default command line output from handles structure
varargout{1} = handles.output;
% --- Executes on button press in pushbutton_ok.
function pushbutton_ok_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton_ok (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
%% 第一步,图像二值化
%读取图像
Traceinput=get(handles.lujingedit,'String');
I_gray=imread(Traceinput);
yuzhi=get(handles.yuzhiedit,'String');
%I_gray=imread('D:\matlab\hq_6\9.png');
figure;
%将图像缩小至和gui界面的axes一致的大小
set(gca,'unit','pixel');
ap = get(gca,'position');
[ax,ay] = size(I_gray);
set(gca,'position',[ap(1),ap(2),ax,ay]);
axes(handles.axes3);
imshow(I_gray);
%获得全局阈值
level=graythresh(I_gray);
I_bw=im2bw(I_gray,level);
%figure,imshow(I_bw);
%% 第二步,找到物体边界。关键点是:图像“缝隙”和“孔洞”的概念和填补方法
%去掉小目标,像素数小于30的,关键点理解图像连通域的含义。
%这个30根据具体应用中目标的大小调节
bw1=bwareaopen(I_bw,30);
%figure,imshow(bw1);
%二值图像去除小区域后,对大区域中的缝隙填充,同时平滑边界
%膨胀腐蚀所用圆单元,需要注意参数3需要调试,以满足不同场合应用。
se=strel('disk',3);
bw2=imclose(bw1,se);
% figure,imshow(bw2);
%在上一步先腐蚀膨胀之后,对连通域填孔,使其变为实心体。
bw3=imfill(bw2,'holes');
% figure,imshow(bw3);
%显示图像的边界,B是边界像素位置,cell组。L是标记矩阵,标识图像中目标个数
%L_bw对不同目标显示不同颜色,目标边界白色显示
[B,L]=bwboundaries(bw3,'noholes');
L_bw=label2rgb(L,@jet,[.5,.5,.5]);
figure;
set(gca,'unit','pixel');
ap = get(gca,'position');
[ax,ay] = size(L_bw);
set(gca,'position',[ap(1),ap(2),ax,ay]);
axes(handles.axes4);
imshow(L_bw);
hold on
for k=1:length(B)
B_object=B{k};
plot(B_object(:,2),B_object(:,1),'w','Linewidth',2);%白色显示边界
end
%% 第三步,找出圆形目标
%求出标记矩阵L中,各个区域的面积和质心
property=regionprops(L,'Area','Centroid');
%判断图像中每个目标是否近似为圆形
for k2=1:length(B);
B_object2=B{k2};
delta=diff(B_object2).^2;
%周长
perimeter=sum(sqrt(sum(delta,2)));
%面积
area=property(k2).Area;
area_str=sprintf('%2.2f',area);
r=sqrt(area/pi);
r_str=sprintf('%2.2f',r);
%近似为圆形度量值test,越接近1标识越近似圆
test=(4*pi*area)/(perimeter^2);
test_str=sprintf('%2.2f',test);
% if test>0.89 %重要!假若度量值大于0.9,就认为是圆形,加入这条if只显示满足条件的圆形
if test>str2double(yuzhi)
%标记圆心
centroid=property(k2).Centroid;
plot(centroid(1),centroid(2),'*','Color','k');
handles.HNum=handles.HNum+1;
if handles.HNum==1
handles.info=[handles.HNum,r,centroid(1),centroid(2),test];
else
handles.info=[handles.info;handles.HNum,r,centroid(1),centroid(2),test];
end
%数值显示
text(B_object2(1,2)-85,B_object2(1,1)-3,strcat('面积:',area_str));
text(B_object2(1,2)-85,B_object2(1,1)-43,strcat('半径:',r_str));
end
end
set(handles.holenum,'String',handles.HNum);
set(handles.infotable,'Data',handles.info);
lujing1=strcat(Traceinput,'_');
lujing2=strcat(lujing1,yuzhi);
lujing=strcat(lujing2,'.xls');
xlswrite(lujing,{'标号','半径','X轴','Y轴','量度'});
xlswrite(lujing,handles.info,1,'A2');
%title('大于度量值0.9可以认为是圆形,该度量值可以自由设定');
function L_Callback(hObject, eventdata, handles)
% hObject handle to L (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hints: get(hObject,'String') returns contents of L as text
% str2double(get(hObject,'String')) returns contents of L as a double
% --- Executes during object creation, after setting all properties.
function L_CreateFcn(hObject, eventdata, handles)
% hObject handle to L (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called
% Hint: edit controls usually have a white background on Windows.
% See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end
function traceedit_Callback(hObject, eventdata, handles)
% hObject handle to traceedit (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hints: get(hObject,'String') returns contents of traceedit as text
% str2double(get(hObject,'String')) returns contents of traceedit as a double
% --- Executes during object creation, after setting all properties.
function traceedit_CreateFcn(hObject, eventdata, handles)
% hObject handle to traceedit (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called
% Hint: edit controls usually have a white background on Windows.
% See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end
function yuzhiedit_Callback(hObject, eventdata, handles)
% hObject handle to yuzhiedit (see GCBO)
matlab圆孔检测
需积分: 41 108 浏览量
2013-09-06
20:07:09
上传
评论 4
收藏 1.14MB ZIP 举报
xiaoxiaofendouwoniu
- 粉丝: 1
- 资源: 9
最新资源
- 2%EF%BC%9A%E9%99%95%E8%A5%BF%E
- yyspdz62_944.apk
- SAP公司间采购EDI配置-如何触发自动MIRO.docx
- python197基于图像识别的仪表实时监控系统.rar
- python196基于循环神经网络的情感分类系统.rar
- I2C驱动SHT30温湿度传感器和LCD12864使用例程(RSCG12864B)
- python193中学地理-中国的江河湖泊教学网(django).rar
- python191基于时间序列分析的大气污染预测软件(django).rar
- python190基于人脸识别智能化小区门禁管理系统.rar
- python189某医院体检挂号系统.rar
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈