【实验目的】

1.掌握工业零件尺寸和面积测量原理

2.掌握利用Matlab进行编程实现工业零件尺寸和面积测量的方法

3.掌握复杂机器视觉软件系统的设计方法

【实验内容】

1.利用Matlab对标准工业零件图像和自建工业零件图像进行尺寸测量

2.利用Matlab对标准工业零件图像和自建工业零件图像进行面积测量

【实验要求】

1.写出实现工业零件尺寸和面积测量的Matlab源代码

2.对测量结果进行统计和误差分析

实验程序

function varargout = Area(varargin)
% AREA M-file for Area.fig
%      AREA, by itself, creates a new AREA or raises the existing
%      singleton*.
%
%      H = AREA returns the handle to a new AREA or the handle to
%      the existing singleton*.
%
%      AREA('CALLBACK',hObject,eventData,handles,...) calls the local
%      function named CALLBACK in AREA.M with the given input arguments.
%
%      AREA('Property','Value',...) creates a new AREA or raises the
%      existing singleton*.  Starting from the left, property value pairs are
%      applied to the GUI before Area_OpeningFcn gets called.  An
%      unrecognized property name or invalid value makes property application
%      stop.  All inputs are passed to Area_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 Area% Last Modified by GUIDE v2.5 16-May-2011 21:00:46% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name',       mfilename, ...'gui_Singleton',  gui_Singleton, ...'gui_OpeningFcn', @Area_OpeningFcn, ...'gui_OutputFcn',  @Area_OutputFcn, ...'gui_LayoutFcn',  [] , ...'gui_Callback',   []);
if nargin && ischar(varargin{1})gui_State.gui_Callback = str2func(varargin{1});
endif nargout[varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
elsegui_mainfcn(gui_State, varargin{:});
end
% End initialization code - DO NOT EDIT% --- Executes just before Area is made visible.
function Area_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 Area (see VARARGIN)% Choose default command line output for Area
handles.output = hObject;% Update handles structure
guidata(hObject, handles);% UIWAIT makes Area wait for user response (see UIRESUME)
% uiwait(handles.figure1);% --- Outputs from this function are returned to the command line.
function varargout = Area_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 pushbutton1.function pushbutton1_Callback(hObject, eventdata, handles)
% hObject    handle to pushbutton1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA) %设置全局变量,可以在另外的函数中使用它们
global im;
global l1
global l2
%选择图片路径[filename,pathname]=...uigetfile({'*.jpg';'*.bmp';'*.tif';'*.png';'*.JPG'},'图片选择');%合成路径+文件名
str=[pathname,filename];%没有选择任何文件,报错对话框L=length(filename);if L<5errordlg('You do not choose any image','Notice');return;end;
test=filename(1,L-3:L);if test=='.jpg'|test=='.bmp'|test=='.tif'|test=='.png'|test=='.JPG'%读取图片im=imread(str);%关键,指定axes1为当前显示区域axes(handles.axes1);%显示图片imshow(im);[m,n]=size(im);inter_x = m*0.6;inter_y = n*0.071;text(inter_x, inter_y, [sprintf('%1f'),'原图像'],...'Color','g','FontSize',10,'FontWeight','bold');else warndlg('format wrong','warn');%格式错误,警告return;end;
prompt={'输入参照物序号:','输入参照物面积:'};%设置提示字符串name='参数设置';%设置标题numlines=1;%指定输入数据的行数defAns={'1','283.5287'};%设定默认值Resize='on';%设定对话框尺寸可调节a=inputdlg(prompt,name,numlines,defAns,'on');%创建输入对话框l2=str2num(a{1});%获取对话框中的第一个参数l1=str2num(a{2});%获取对话框中的第二个参数% --- Executes on button press in pushbutton2.function pushbutton2_Callback(hObject, eventdata, handles)
% hObject    handle to pushbutton2 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
global l1
global l2
global im
global P
str=get(hObject,'string');
axes(handles.axes2);
hd=rgb2gray(im);%灰度化,被灰度化的图像必须是rgb的
ad=imadjust(hd,stretchlim(hd),[]); %图像增强
x=graythresh(ad);%计算图像的最佳阈值
J=im2bw(hd,x);%图像二值化
P=medfilt2(J,[5,5]);%对二值化图像进行中值滤波[5,5]
[L, num] = bwlabel(P, 8);%8连通域的标记STATS=regionprops(L, 'all');
for i=1:numarea(i)=STATS(i).Area ; %计算各区域的像素点。
end ;
M1=area
for i=1:numM=M1*(l1/area(l2));
end;
imshow(L);%鼠标点击事件
for i=1:num
ginput(1);%鼠标点击图片得到坐标值
[x,y]=ginput(1);
x=floor(x);%将得到的坐标值转换成整数
y=floor(y);
s=L(y,x);if s==l2text(x,y, [sprintf('%1.3f',M(s)),'mm2'],...'Color','r','FontSize',10,'FontWeight','bold');% 参照物的显示面积为红色else  text(x,y, [sprintf('%1.3f_%d',M(s),s)],...'Color','b','FontSize',10,'FontWeight','bold');% 被测工件显示的面积为蓝色
end;
end;
[fn,pn,fid]=uiputfile('.txt','Save selected a txt-file');%创建txt文件。其中fn是lilename ,pn是pathname, fid是
fid=fopen(fn,'w');%打开文件
fprintf(fid,'%gmm2_',M);%向txt文件中写入数据
%fid=fopen('面积保存 …….txt','w');%写入文件路径,可以设置弹出对话框
%[p,q]=size(M);%for i=1:1:p
%    for j=1:1:q
%       if j==p
%         fprintf(fid,'%gmm2_',M(i,j));%     else
%        fprintf(fid,'%gmm2_',M(i,j));
%       end;
%    end;
% end;
fclose(fid);%关闭文件% --- Executes on button press in pushbutton3.function pushbutton3_Callback(hObject, eventdata, handles)
% hObject    handle to pushbutton3 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
global I
I=getimage(gcf); %获取坐标系中的图像文件数据
imwrite(I,'myphoto.jpg') %保存图像为文件
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
new_f_handle=figure('visible','off');
new_axes=copyobj(handles.axes1,new_f_handle); %axes1是GUI界面绘图的坐标系
set(new_axes,'units','default','position','default');
[filename,pathname fileindex]=uiputfile({'*.jpg';'*.bmp';'*.png'},'save picture as');
if ~filenamereturn
elsefile=strcat(pathname,filename);switch fileindex %根据不同的选择保存为不同的类型case 1print(new_f_handle,'-djpeg',file);case 2print(new_f_handle,'-dbmp',file);case 3print(new_f_handle,'-dpng',file);end;
end;
delete(new_f_handle);% --- Executes on button press in pushbutton4.
function pushbutton4_Callback(hObject, eventdata, handles)
% hObject    handle to pushbutton4 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
close(gcf);

实验结果



本人能力有限,解释尚不清楚明了,如遇任何问题,大家可留言或私信。本程序为实验老师提供源程序,供大家学习使用。

本文希望对大家有帮助,当然上文若有不妥之处,欢迎指正。

分享决定高度,学习拉开差距

机器视觉实验三 工件尺寸和面积测量相关推荐

  1. 机器视觉实验三: 基于分水岭算法的肺部图像分割实验(OpenCV-python代码)

    一.实验目的 用OpenCV编写一个基于分水岭算法的图像分割程序能对肺部医学图像进行分割,辅助医生进行病情诊断,强化和巩固学生对图像分割知识的掌握和灵活应用. 二.实验要求 1.用OpenCV编写一个 ...

  2. 机器视觉 基于XAVIS机器视觉组态软件的尺寸测量

    ZM-VS1200 机器视觉教学实验平台是一种创新实验系统,功能强大,使用方便,可满足 机器视觉各类实验开设需求,其中包含图像获取,图像处理.模式识别.形状匹配,形态学分 析.机器学习等功能.     ...

  3. 机器视觉python-openCV实验—— 实验三 人脸装饰物添加

    一. 实验目的 用Python-OpenCV编写一个程序能检测出给定图像中的人脸,并能给人脸添加一些装饰特效,比如给人脸加上戴眼镜或带口罩或戴帽子等装饰物. 二.实验要求 1. 用OpenCV编写一个 ...

  4. 数字图像处理:实验三 图像增强

    实验三 图像增强 图像增强是数字图像处理过程中常采用的一种方法.为了改善视觉效果或便于人和机器对图像的理解和分析,根据图像的特点或存在的问题采取的改善方法或加强特征的措施称为图像增强.图像增强处理是改 ...

  5. 实验三:敏捷开发与XP实践

    Java实验三报告 一.   实验内容 (一)敏捷开发与XP 内容:1.敏捷开发(Agile Development)是一种以人为核心.迭代.循序渐进的开发方法. 2.极限编程(eXtreme Pro ...

  6. 计算机模拟双缝干涉实验报告,电磁场与微波实验三报告——双缝干涉实验.docx...

    <电磁场与微波实验三报告--双缝干涉实验.docx>由会员分享,提供在线免费全文阅读可下载,此文档格式为docx,更多相关<电磁场与微波实验三报告--双缝干涉实验.docx>文 ...

  7. 实验三 图像复原(源代码一站式复制粘贴)

    实验三 图像复原 一.实验目的 二.实验原理 三.实验内容与要求 四.实验的具体实现 一.实验目的 1.了解图像降质/复原处理的模型. 2.了解估计降质函数的基本原理. 3.掌握降质图像中常见噪声模型 ...

  8. 计算机图形学 | 实验三:绘制一个四边形

    计算机图形学 | 实验三:绘制一个四边形 计算机图形学 | 实验三:绘制一个四边形 初始化 顶点输入 数据处理 VAO.VBO 顶点属性 顶点着色器和片段着色器 渲染 EBO 完整代码 华中科技大学& ...

  9. 数字信号与图像处理实验三:图像处理基础与图像变换

    实验三:图像处理基础与图像变换 通过本实验加深对数字图像的理解,熟悉MATLAB中的有关函数:应用DCT对图像进行变换:熟悉图像常见的统计指标,实现图像几何变换的基本方法. 班级 姓名 实验名 完成时 ...

最新文章

  1. 1264: [AHOI2006]基因匹配Match
  2. MySQL CPU 使用率高的原因和解决方法
  3. Office365 Manager Plus之报表
  4. python写订单管理系统_利用Python快速搭建钉钉和邮件数据推送系统
  5. c++STL(标准模板库)理论基础
  6. Android移植之自定义ProgressBar
  7. Spring Boot整合mybatis报错Invalid bound statement (not found)
  8. C语言用字母ABC输出菱形,C语言输出菱形的方法
  9. matlab哪些教材好,新手入门,恳请推荐一本matlab好教材
  10. python plt绘制柱状图形+柱状图增加数字标注
  11. 突发!人人影视字幕组14人被抓,用户超800万,涉案金额超1600万!网友炸了:免费的片没了...
  12. 身份认证技术的产业发展
  13. k折交叉验证(k-fold Cross-validation)
  14. 【文件格式_XML_HTML_】XML、HTML文件
  15. 111个Python数据分析实战项目,代码已跑通,数据可下载
  16. 错误:Element type “select“ must be declared.
  17. 基于STM32的安卓蓝牙多功能遥控车
  18. plantuml 依赖_PlantUML入门
  19. 深度学习中的两种不确定性:偶然不确定性和认知不确定性(Aleatoric Uncertainty Epistemic Uncertainty)
  20. 什么是sq注入及解决

热门文章

  1. NBA运动员球员数据分析
  2. 移动互联网需求革命:由“生理需求”到“自我实现”
  3. 用思维导图赏析老舍话剧著作《茶馆》
  4. Python中的数值运算与逻辑运算
  5. Failed to introspect Class [com.mchange.v2.c3p0.ComboPooledDataSource] from ClassLoader [ParallelWeb
  6. iOS 跳转淘宝、天猫、京东商品详情页
  7. 深度学习小白入门教程(一):以英雄联盟为例,爬取你感兴趣图片,构建你自己的数据集
  8. 当一个女生说她要减肥的时候
  9. 快速理解Raft之日志复制(肝了两千五百字)
  10. Mac之间的 远程控制