接下来学习的是关于颜色特征提取方面的知识。
颜色特征是在图像检索中应用最为广泛的视觉特征,主要原因在于颜色往往和图像中所包含的物体或场景十分相关。此外,与其他的视觉特征相比,颜色特征对图像本身的尺寸、方向、视角的依赖性较小,从而具有较高的鲁棒性。
颜色特征表示方法:
颜色特征提取包括颜色特征区域的提取和颜色特征值的提取。
[L,m]=bwlabel(Ibw,8);%标注二进制图像中连通部分
stats=regionprops(L,'Centroid');%求质心
centroids = cat(1, stats.Centroid);%获取每个连通域的质心坐标值
R=mean(r(:)); %计算RGB三个矩阵的均值
G=mean(g(:));
B=mean(b(:));
H=mean(H(:)); %计算HSI三个矩阵的均值
S=mean(S(:));
I=mean(I(:));
实例一: 在一张溶液图像中标记出溶液部分的中心点,以中心点为原点截取要求大小的正方形区域作为颜色特征区域,并提取出颜色特征区域的颜色特征值。
function varargout = yansetezhengtiqu(varargin)
% YANSETEZHENGTIQU MATLAB code for yansetezhengtiqu.fig
% YANSETEZHENGTIQU, by itself, creates a new YANSETEZHENGTIQU or raises the existing
% singleton*.
%
% H = YANSETEZHENGTIQU returns the handle to a new YANSETEZHENGTIQU or the handle to
% the existing singleton*.
%
% YANSETEZHENGTIQU('CALLBACK',hObject,eventData,handles,...) calls the local
% function named CALLBACK in YANSETEZHENGTIQU.M with the given input arguments.
%
% YANSETEZHENGTIQU('Property','Value',...) creates a new YANSETEZHENGTIQU or raises the
% existing singleton*. Starting from the left, property value pairs are
% applied to the GUI before yansetezhengtiqu_OpeningFcn gets called. An
% unrecognized property name or invalid value makes property application
% stop. All inputs are passed to yansetezhengtiqu_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 yansetezhengtiqu
% Last Modified by GUIDE v2.5 04-Aug-2019 14:42:19
% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', @yansetezhengtiqu_OpeningFcn, ...
'gui_OutputFcn', @yansetezhengtiqu_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 yansetezhengtiqu is made visible.
function yansetezhengtiqu_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 yansetezhengtiqu (see VARARGIN)
% Choose default command line output for yansetezhengtiqu
handles.output = hObject;
% Update handles structure
guidata(hObject, handles);
% UIWAIT makes yansetezhengtiqu wait for user response (see UIRESUME)
% uiwait(handles.figure1);
% --- Outputs from this function are returned to the command line.
function varargout = yansetezhengtiqu_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;
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
function edit3_Callback(hObject, eventdata, handles)
% hObject handle to edit3 (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 edit3 as text
% str2double(get(hObject,'String')) returns contents of edit3 as a double
% --- Executes during object creation, after setting all properties.
function edit3_CreateFcn(hObject, eventdata, handles)
% hObject handle to edit3 (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 edit4_Callback(hObject, eventdata, handles)
% hObject handle to edit4 (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 edit4 as text
% str2double(get(hObject,'String')) returns contents of edit4 as a double
% --- Executes during object creation, after setting all properties.
function edit4_CreateFcn(hObject, eventdata, handles)
% hObject handle to edit4 (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 edit5_Callback(hObject, eventdata, handles)
% hObject handle to edit5 (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 edit5 as text
% str2double(get(hObject,'String')) returns contents of edit5 as a double
% --- Executes during object creation, after setting all properties.
function edit5_CreateFcn(hObject, eventdata, handles)
% hObject handle to edit5 (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 edit6_Callback(hObject, eventdata, handles)
% hObject handle to edit6 (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 edit6 as text
% str2double(get(hObject,'String')) returns contents of edit6 as a double
% --- Executes during object creation, after setting all properties.
function edit6_CreateFcn(hObject, eventdata, handles)
% hObject handle to edit6 (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
% --- Executes on button press in open.
function open_Callback(hObject, eventdata, handles)
% hObject handle to open (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({'*.bmp;*.jpg;*.png;*.jpeg;*.tif;*.gif;*.Image files'},'载入图像');%选择路径打开图像
str=[pathname filename]; %打开图像
im=imread(str); %打开axes1的句柄 进行axes1的操作
axes(handles.axes1); %在axes1中显示图像
imshow(im);
handles.img=im; %把图像发给handles.img
guidata(hObject,handles);%把handles句柄更新
% --- Executes on button press in quyu.
function quyu_Callback(hObject, eventdata, handles)
% hObject handle to quyu (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
%选取S分量
handles.img = im2double(handles.img);
r = handles.img(:, :, 1);
g = handles.img(:, :, 2);
b = handles.img(:, :, 3);
tmp1 = min(min(r, g), b);
tmp2 = r + g + b;
tmp2(tmp2 == 0) = eps;
S = 1 - 3.*tmp1./tmp2;
%阈值分割
level=graythresh(S);%使用最大类间方差法找到图片的一个合适的阈值
BW=im2bw(S,level); %Otsu阈值进行分割
%滤波
x=medfilt2(BW,[5 5],'symmetric');%中值滤波
%中心点坐标
Ibw=bwareaopen(x,100);%删除二值图像x中面积小于100的连通对象
hold on;
[L,m]=bwlabel(Ibw,8);%标注二进制图像中连通部分
stats=regionprops(L,'Centroid');%求质心
centroids = cat(1, stats.Centroid);%获取每个连通域的质心坐标值
a=centroids(:,1); %将质心坐标值x赋给a
b=centroids(:,2); %将质心坐标值y赋给b
c=a(a>600&a<700&b>600&b<700);
d=b(a>600&a<700&b>600&b<700);
for i=1:m
plot(c,d,'R+');
end
hold off;
%截取128×128的图片
[height,width]=size(x);
m=width/10; %剪切图像显示区域的1/x(以图像中心向四周剪切)
n=height/7.5;
rect=[c-m/2 d-n/2 m n];%计算裁剪区域:(以图像中心点为裁剪中心)
f=imcrop(handles.img,rect);% 用imcrop裁剪
axes(handles.axes2);
imshow(f);
set(handles.axes2,'units','pixels');
pos=get(handles.axes2,'pos');
pos(3:4)=[128 128];
set(handles.axes2,'pos',pos);
% --- Executes on button press in zhi.
function zhi_Callback(hObject, eventdata, handles)
% hObject handle to zhi (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
im = im2double(handles.img);%像素值被标准化到0—1
r = im(:, :, 1);
g = im(:, :, 2);
b = im(:, :, 3);
R=mean(r(:)); %计算RGB三个矩阵的均值
G=mean(g(:));
B=mean(b(:));
%I
I = (r + g + b)/3;
%S
tmp1 = min(min(r, g), b);
tmp2 = r + g + b;
tmp2(tmp2 == 0) = eps;
S = 1 - 3.*tmp1./tmp2;
%H
tmp1 = 0.5*((r - g) + (r - b));
tmp2 = sqrt((r - g).^2 + (r - b).*(g - b));
theta = acos(tmp1./(tmp2 + eps));
H = theta;
H(b > g) = 2*pi - H(b > g);
H = H/(2*pi);
H(S == 0) = 0;
H=mean(H(:)); %计算HSI三个矩阵的均值
S=mean(S(:));
I=mean(I(:));
set( handles. edit1 ,'string',num2str( R) );
set( handles. edit2 ,'string',num2str( G) );
set( handles. edit3 ,'string',num2str( B) );
set( handles. edit4 ,'string',num2str( H) );
set( handles. edit5 ,'string',num2str( S) );
set( handles. edit6 ,'string',num2str( I) );
% --- Executes on button press in exit.
function exit_Callback(hObject, eventdata, handles)
% hObject handle to exit (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
clc %清除指令窗
close all %关闭所有句柄可见的窗口
close(gcf) %关闭当前窗口
clear %清除内存变量和函数
第三步:运行。第四步:在完成GUI界面创建后,对所创建界面的效果进行试验。点击“打开图片”按钮在文件夹中选择一张需要处理的溶液图像,点击“颜色特征区域的提取”按钮可以在左边画布得到标记溶液区域中心点的溶液图像,在右边画布中得到截取后的颜色特征区域,点击“颜色特征值的提取”按钮可以在R、G、B、H、S、I对应的可编辑文本框中得到相应的颜色特征值,点击“退出”按钮可以退出界面。