五、图像处理及面积计算
1、实现功能:
——通过Matlab工具箱中函数对相机接收到的图像进行预处理;
——通过Matlab工具箱中函数对预处理图像进行面积计算;
2、图像的处理:
- 打开图像文件
image=imread('27.bmp');%%%读取图像数据
imshow(image);%%%打开图像
- 将图像灰度化(如果拍摄时为灰度图,则可以省略此函数)
grayscale=rgb2gray(image0);
imshow(grayscale);
- 预处理方式一:使用二值化函数
grayscale=im2bw(grayscale,0.98);
imshow(grayscale);
- 预处理方式二:使用图像减法函数
subtract=imsubtract(grayscale0,grayscale);%%%grayscale0为背景图像,grayscale为处理图像
由于在测量面积时未拍摄背景图像,因此在此次实验中未使用减法函数,但是使用此函数比二值化函数处理图像的环境条件更加宽松,可以在户外,室内采集图像。其效果与上图相似。
- 基于canny算子的边缘检测
image4=edge(grayscale,'canny');
imshow(image4);
经过使用基于canny算子的边缘检测函数后可以看出已经将圆环的边缘提取出来了,但是有些地方还未连接起来。
- 对边界进行膨胀腐蚀
I=strel('line',3,90);
H=strel('line',3,0);
t=imdilate(image4,[I,H]);
- 对外边界以内进行填充
I=strel('square',55);
t=imclose(t,I);
filled=imfill(t,'holes');
imshow(filled);
3、面积的计算:
- 定标
相机有一定的视场角,由于视场角的实际面积不知道,因此需要定标来确定视场内的参考面积。
定标公式:
在这里就需要用到之前的图像减法函数imsubtract()
%file 目标图片(.bmp)
%file0 对比背景图片(.bmp)
image=imread(file);
image0=imread(file0);
grayscale=rgb2gray(image);
grayscale0=rgb2gray(image0);
subtract=imsubtract(grayscale0,grayscale);
输入目标图像 file
输入背景图像 file0
相减之后的图片 subtract
已知白色区域的像素数,中心黑色正方形的标准面积为1平方厘米,此张图像的像素数,那么根据上面的定标公式知道全视角内的面积。
[x,y]=size(k);%%%k为最终图像的数据,x,y为水平和数直方向的像素数
a=x*y; %%%此张图像的全部像素数
k1=double(k);
k2=sum(sum(k1));%%%白色区域的像素数
s=AREA*(k2/a);%%%AREA为中心黑色正方形的标准面积为1平方厘米
- 外边界内面积的计算
同理,根据定标公式同样也可以算出园内的面积,因此,这里将不再做赘述。
4、图像处理及面积计算全部代码
close all
clear all
clc
image=imread('27.bmp');
figure(1)
imshow(image);
grayscale=rgb2gray(image);
grayscale=im2bw(grayscale,0.98);
figure(2)
imshow(grayscale);
image4=edge(s,'canny');
figure(6)
imshow(image4);
I=strel('line',3,90);
H=strel('line',3,0);
t=imdilate(grayscale,[I,H]);
figure(7)
imshow(t);
I=strel('square',55);
t=imclose(t,I);
k=imfill(t,'holes');
figure(8)
imshow(k);
[x,y]=size(k);
a=x*y;
k1=double(k);
k2=sum(sum(k1));
s=AREA*(k2./a);
六、定标GUI界面
在上一节基于Matlab在线式三维还原及其非接触测量(二)中已介绍过GUI界面的建立及界面属性的设置,因此,在此节将不再赘述。
1、GUI界面布局设计:
2、GUI界面的初始函数设计:
handles.output = hObject;
cla(handles.axes1,'reset'); %清空之前的图片
cla(handles.axes2,'reset'); %清空之前的图片
cla(handles.axes3,'reset'); %清空之前的图片
cla(handles.edit1,'reset');
cla(handles.edit2,'reset');
set(handles.axes1,'visible','off');% 让坐标轴的标注和刻度去掉(其实是隐藏掉)
set(handles.axes2,'visible','off');% 让坐标轴的标注和刻度去掉(其实是隐藏掉)
set(handles.axes3,'visible','off');% 让坐标轴的标注和刻度去掉(其实是隐藏掉)
guidata(hObject, handles);
uiwait(handles.figure1);
3、GUI界面的回调函数设计:
- ‘ 打开图片 '回调函数
[filename pathname] =uigetfile({'*.jpg';'*.bmp';'*.*'},'打开图片');
str=[pathname filename];
%%打开图像
if isequal(filename,0)||isequal(pathname,0)
warndlg('Please select a picture first!','Warning');
return;
else
handles.im=imread(str);
%%打开axes1的句柄 进行axes1的操作
axes(handles.axes1);
%%在axes1中显示 图像
imshow(handles.im);
end
guidata(hObject, handles);
- ‘ 打开背景图片 '回调函数
[filename pathname] =uigetfile({'*.jpg';'*.bmp';'*.*'},'打开图片');
handles.str=[pathname filename];
%%打开图像
if isequal(filename,0)||isequal(pathname,0)
warndlg('Please select a picture first!','Warning');
return;
else
handles.im1=imread(handles.str);
axes(handles.axes2);
imshow(handles.im1);
end
guidata(hObject, handles);
- ‘ 清除 '回调函数
cla(handles.axes1,'reset'); %清空之前的图片
cla(handles.axes2,'reset'); %清空之前的图片
cla(handles.axes3,'reset'); %清空之前的图片
% cla(handles.edit1,'reset');
% cla(handles.edit2,'reset');
set(handles.edit1,'String','');%
set(handles.edit2,'String','');%
set(handles.axes1,'visible','off');% 让坐标轴的标注和刻度去掉(其实是隐藏掉)
set(handles.axes2,'visible','off');% 让坐标轴的标注和刻度去掉(其实是隐藏掉)
set(handles.axes3,'visible','off');% 让坐标轴的标注和刻度去掉(其实是隐藏掉)
guidata(hObject, handles);
- ‘ 定标 '回调函数
axis off %%关闭坐标轴显示
set(handles.axes1,'visible','off');% 让坐标轴的标注和刻度去掉(其实是隐藏掉)
set(handles.axes2,'visible','off');% 让坐标轴的标注和刻度去掉(其实是隐藏掉)
image=handles.im;
image0=handles.im1;
grayscale=rgb2gray(image);
grayscale0=rgb2gray(image0);
subtract=imsubtract(grayscale0,grayscale);
r=imclearborder(subtract,8);
s=imadjust(r,[0.2,0.6],[0,1]);
image4=edge(s,'canny');
I=strel('line',3,90);
H=strel('line',3,0);
t=imdilate(image4,[I,H]);
filled=imfill(t,'holes');
SeD=strel('diamond',1);
final=imerode(filled,SeD);
final=imerode(final,SeD);
final=medfilt2(final,[3,3]);
L=bwlabel(final,8);
s=regionprops(L,'Area','Centroid');
k=ismember(L,find([s.Area]>=1000));
number=size(s,1);
area=[s.Area];
centroid=cat(1,s.Centroid);
[x,y]=size(k);
a=x*y;
k1=double(k);
k2=sum(sum(k1));
s=1e-4*(a/k2);
set(handles.edit1,'String',k2);
set(handles.edit2,'String',s);
handles.output=s;
axes(handles.axes3);
imshow(k);
guidata(hObject, handles);
uiresume(handles.figure1);
4、GUI界面.m文件所有代码:
function varargout = dingbiao(varargin)
% DINGBIAO MATLAB code for dingbiao.fig
% DINGBIAO, by itself, creates a new DINGBIAO or raises the existing
% singleton*.
%
% H = DINGBIAO returns the handle to a new DINGBIAO or the handle to
% the existing singleton*.
%
% DINGBIAO('CALLBACK',hObject,eventData,handles,...) calls the local
% function named CALLBACK in DINGBIAO.M with the given input arguments.
%
% DINGBIAO('Property','Value',...) creates a new DINGBIAO or raises the
% existing singleton*. Starting from the left, property value pairs are
% applied to the GUI before dingbiao_OpeningFcn gets called. An
% unrecognized property name or invalid value makes property application
% stop. All inputs are passed to dingbiao_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 dingbiao
% Last Modified by GUIDE v2.5 07-Nov-2019 17:32:54
% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', @dingbiao_OpeningFcn, ...
'gui_OutputFcn', @dingbiao_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 dingbiao is made visible.
function dingbiao_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 dingbiao (see VARARGIN)
% Choose default command line output for dingbiao
handles.output = hObject;
cla(handles.axes1,'reset'); %清空之前的图片
cla(handles.axes2,'reset'); %清空之前的图片
cla(handles.axes3,'reset'); %清空之前的图片
cla(handles.edit1,'reset'); %清空之前的图片
cla(handles.edit2,'reset'); %清空之前的图片
set(handles.axes1,'visible','off');% 让坐标轴的标注和刻度去掉(其实是隐藏掉)
set(handles.axes2,'visible','off');% 让坐标轴的标注和刻度去掉(其实是隐藏掉)
set(handles.axes3,'visible','off');% 让坐标轴的标注和刻度去掉(其实是隐藏掉)
% % Update handles structure
guidata(hObject, handles);
uiwait(handles.figure1);
% UIWAIT makes dingbiao wait for user respocnse (see UIRESUME)
% uiwait(handles.figure1);
% --- Outputs from this function are returned to the command line.
function varargout = dingbiao_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)
% varargout{1} = handles.output;
% Get default command line output from handles structure
varargout{1} = handles.output;
% delete(handles.figure1);
% --- 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)
axis off %%关闭坐标轴显示
set(handles.axes1,'visible','off');% 让坐标轴的标注和刻度去掉(其实是隐藏掉)
set(handles.axes2,'visible','off');% 让坐标轴的标注和刻度去掉(其实是隐藏掉)
image=handles.im;
image0=handles.im1;
% [filename pathname] =uigetfile({'*.jpg';'*.bmp';'*.*'},'打开图片');
% str=[pathname filename];
% %%打开图像
% im1=imread(str);
% %%打开axes1的句柄 进行axes1的操作
% axes(handles.axes1);
% %%在axes1中显示 图像
% imshow(im1);
%图像处理部分
grayscale=rgb2gray(image);
grayscale0=rgb2gray(image0);
subtract=imsubtract(grayscale0,grayscale);
r=imclearborder(subtract,8);
s=imadjust(r,[0.2,0.6],[0,1]);
image4=edge(s,'canny');
I=strel('line',3,90);
H=strel('line',3,0);
t=imdilate(image4,[I,H]);
filled=imfill(t,'holes');
SeD=strel('diamond',1);
final=imerode(filled,SeD);
final=imerode(final,SeD);
final=medfilt2(final,[3,3]);
L=bwlabel(final,8);
s=regionprops(L,'Area','Centroid');
k=ismember(L,find([s.Area]>=1000));
number=size(s,1);
area=[s.Area];
centroid=cat(1,s.Centroid);
[x,y]=size(k);
a=x*y;
k1=double(k);
k2=sum(sum(k1));
s=1e-4*(a/k2);
set(handles.edit1,'String',k2);
set(handles.edit2,'String',s);
handles.output=s;
% path1='D:\beisaier\feijiechu';
% name='lena.bmp';
% imwrite(k,[path1 name]);
% str1=[path1 name];
% im1=imread(str1);
axes(handles.axes3);
imshow(k);
guidata(hObject, handles);
uiresume(handles.figure1);
% --- 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)
cla(handles.axes1,'reset'); %清空之前的图片
cla(handles.axes2,'reset'); %清空之前的图片
cla(handles.axes3,'reset'); %清空之前的图片
% cla(handles.edit1,'reset'); %清空之前的图片
% cla(handles.edit2,'reset'); %清空之前的图片
set(handles.edit1,'String','');% 让坐标轴的标注和刻度去掉(其实是隐藏掉)
set(handles.edit2,'String','');% 让坐标轴的标注和刻度去掉(其实是隐藏掉)
set(handles.axes1,'visible','off');% 让坐标轴的标注和刻度去掉(其实是隐藏掉)
set(handles.axes2,'visible','off');% 让坐标轴的标注和刻度去掉(其实是隐藏掉)
set(handles.axes3,'visible','off');% 让坐标轴的标注和刻度去掉(其实是隐藏掉)
guidata(hObject, handles);
% --- 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)
[filename pathname] =uigetfile({'*.jpg';'*.bmp';'*.*'},'打开图片');
str=[pathname filename];
%%打开图像
if isequal(filename,0)||isequal(pathname,0)
warndlg('Please select a picture first!','Warning');
return;
else
handles.im=imread(str);
%%打开axes1的句柄 进行axes1的操作
axes(handles.axes1);
%%在axes1中显示 图像
imshow(handles.im);
end
guidata(hObject, handles);
% --- 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)
[filename pathname] =uigetfile({'*.jpg';'*.bmp';'*.*'},'打开图片');
handles.str=[pathname filename];
%%打开图像
if isequal(filename,0)||isequal(pathname,0)
warndlg('Please select a picture first!','Warning');
return;
else
handles.im1=imread(handles.str);
axes(handles.axes2);
imshow(handles.im1);
end
guidata(hObject, handles);
function edit1_Callback(hObject, eventdata, handles)
% hObject handle to edit1 (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 edit1 as text
% str2double(get(hObject,'String')) returns contents of edit1 as a double
% --- Executes during object creation, after setting all properties.
function edit1_CreateFcn(hObject, eventdata, handles)
% hObject handle to edit1 (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 edit2_Callback(hObject, eventdata, handles)
% hObject handle to edit2 (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 edit2 as text
% str2double(get(hObject,'String')) returns contents of edit2 as a double
% --- Executes during object creation, after setting all properties.
function edit2_CreateFcn(hObject, eventdata, handles)
% hObject handle to edit2 (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