【图像检索】基于matlab GUI综合颜色和形状特征图像检索【含Matlab源码 1370期】

154 阅读13分钟

一、综合颜色和形状特征图像检索简介

0 引言 随着多媒体技术与网络技术的发展, 基于内容的图像检索(Content-Based Image Retrieval, CBIR) 技术作为信息检索技术重要的一部分, 成为国际国内的研究热点,其主要利用图像的视觉特征如颜色、纹理、形状和空间关系等搜索图像库中与待查询图像相似的图像,颜色是图像最底层、最显著的特 征,形状是彩色图像最稳定的特征,其不受亮度等环境变化的影响。 CBIR算法主要考虑的是待查询图像与图像库中图像的相似度, 但是有时候却忽略了图像库中已存在图像之间的相似性, 如果利用聚类算法将图像库中的图像聚类, 使得检索过程在类心或某一类内进行, 就会降低图像检索的空间, 提高检索的速度。聚类算法在图像检索领域中有广泛的应用, K-means算法应用最广,其优点是通用、聚类速度快,保证以二次收敛的速度终止,但缺点是终止于一个局部最小值,对初始类心依赖性强,导致检索结果不稳定。近几年,出现了许多基于K-means算法的图像检索算法 。文献主要提出了平均面积直方图的概念, 然后基于图像密度函数确定初始聚类中心, 按照K-means思想迭代优化, 对特征提取方法的改进提高了检索的准确性, 但是初始类心的确定方式使得算法的时间复杂度较大。文献针对K-means随机选取初始值导致聚类结果不稳定的特点,根据特征距离累加和最大选取初始聚类中心,保证了检索结果的稳定性,提高了检索的准确性,但是检索的效果从绝对意义上来说还有待提高。文献提出了基于区域分割的方法, 利用K-means算法对像素进行聚类进而形成基于局部的特征信息, 然后利用分层聚类算法将相似的物体特征进行聚类,取代了传统的基于图像整体信息的聚类,但是基于像素的处理,以及算法的迭代特性,导致其时间复杂度较大,提取基于局部的特征信息的时候信息丢失量较大,检索精度有待提高。以上算法还有不足,颜色直方图信息丢失量大,特征提取效果较差,而且仅仅通过单一的图像特征难以有效全面地描述图像内容信息和 获得理想的图像检索性能, K-means算法只是考虑了同一类图像的相似性, 并没有考虑不同图像的差异性, 导致其没有更好的检索效果。 鉴于上述情况,本文提出了综合颜色和形状特征的图像检索算法,利用改进的颜色聚合向量提取颜色特征,着重考虑了颜色空间的分布信息,利用Hu不变矩的改进算法提取图像的形状特征,着重考虑了图像中物体的形状信息,该算法基于贡献度聚类且建立综合特征索引库,减少对图像库的访问次数,其迭代特性保证了较低的时间杂性。

1 颜色量化 在图像检索领域,颜色空间需要压缩进而通过一种更紧凑的方式描述,即对颜色空间进行量化,在不改变主观感受的情况下减小存储量并能提高处理速度。HSV空间能较好地反映人眼对颜色的感知和鉴别能力, 非常适合基于颜色的图像相似比较。 首先将RGB颜色转换为HSV, 然后进行量化。人眼对颜色的色调(H) 非常敏感, 主要描述图像的颜色特征, 色调独立于观察点, 所以要细量化。亮度(V) 反 映了图像的形状特征, 不依赖于图像的颜色信息, 所以量化不能太粗。根据HSV颜色空间的特性作以下非均匀量化, 共获得2+12*6×2=146种颜色。 在这里插入图片描述 量化之后利用公式Q=2+12H+6S+V对颜色进行编码, 编码范围为0145, V<0.1的颜色为黑色, 编码为0, s<0.2且v>0.9的颜色为白色, 编码为1。利用此方法对颜色量化易于计算、符合人眼视觉、减少颜色冗余、失真小, 如图1所示。 在这里插入图片描述 图1 量化前后对比 2颜色特征 Pass等[13] 提出了颜色聚合向量(Color Coherence Vector, CCV) , 弥补了颜色直方图无法表达颜色分布空间位置[14] 的缺点, 主要是基于像素的空间关系将直方图的每个颜色簇划分为聚合和非聚合两部分,如果一个连通区域的像素个数超出了阈值T,则认为该区域的像素是聚合的,否则将被判为非聚合的。整幅图像的像素个数N和阈值t的关系一般为:T=0.01N[13]。基于对颜色分布的空间信息的重视, 改进的颜色聚合向量(Improved Color Coherence Vector, ICCV) 算法考虑到聚合像素所处的连通区域以及非聚合像素所处的非连通区域,对颜色Qk作4-连通标记处理,根据阈值找到其连通域个数c(Qk)、非联通域个数n(Qk)以及聚合数目ak、非聚合数目βk,则Qk的平均聚合数目为M。(Qk)=ak/(c(Qk)N),平均非聚合数目为Mn(Qk)=Bk/(n(Qk)N),为了使其不受图像尺度变化的影响,对其作了归一化处理。如果c(Qk)=0或n(Qk)=0, 则M。(Qk) =0或Mn(Qk) =0, 图像的ICCV为:(Qk, Mc(Qk) , Mn(Qk) ) , 其中k=1, 2, ..., 146, Qk=0, 1, ..., 145, 所以可以用146维的向量来表示一幅图像的颜色特征。如图2所示,黑色代表量化之后编码值为Qk的像素,设阈值为3,则ak都为12,c(Qk)分别为4、1、2,βk都为4,n(Qk)分别为4、2、3,得到M。(Qk)分别为12/(4N) 、12/N、12/(2N) , Mn(Qk) 分别为4/(4N) 、4/(2N) 、4/(3N) , 所以ICCV分别为(Qk, 12/(4N) , 4/(4N) ) 、(Qk, 12/N, 4/(2N) ) 、(Qk,12/(2N),4/(3N) ) 。如果用颜色直方图表示都为(Qk, 16/N) , 用CCV表示都为(Qk, 12/N, 4/(N) ) 。但是可以看到三幅图像完全不同, 所以ICCV的表示更加准确。 在这里插入图片描述 图2 三种图像样图 3 形状特征 3.1不变矩 矩是对图像的一种统计形式, 可以从全局描述整个对象的整体特性, 矩不变量的表示形式很多, 如Legendre矩、Zernike矩、伪Zernike矩等。在图像处理中,几何不变矩可以作为一个重要的特征来表示物体,可以据此特征来对图像进行分类等操作,本文用改进的Hu不变矩方法[15]提取图像的形状特征,不变矩的主要思想是使用对变换不敏感的基于区域的几个矩作为形状特征,对于一幅图像F(x,y),F(x,y)为点(x,y)的灰度值,它的阶矩定义为: 在这里插入图片描述 如果将图像的灰度值看作是一个二维或三维的密度分布函数, 那么矩方法即可用于图像分析领域并用作图像特征的提取。最常用的, 物体的零阶矩表示了图像的“质量”:M0, 0=∫∫F (x, y) dxdy, 一阶矩 (M0, 1, M1, 0) 用于确定图像质心 (xc, yc) ;若将坐标原点移至xc和yc处, 就得到了对于图像位移不变的p+q阶中心矩up, q: 在这里插入图片描述 其中:xc=m1, 0/m0, 0, yc=m0, 1/m0, 0, F (x, y) 规格化的中心矩可以表示为ηp, q=μp, q/μ0γ, 0, 其中γ= (p+q+2) /2, p+q=2, 3, …, 将归一化的二阶矩和三阶矩组合得到7个经典的对平移、旋转和尺度变化的不变矩, 如式 (3) 所示: 在这里插入图片描述 由于图像大小会影响不变矩的特征值, 因此在提取之前应该将图像库中的图像尺寸规范化为相同尺寸。计算Hu不变矩特征量的过程如图3所示。 在这里插入图片描述 图3 图像Hu不变矩提取流程 为了提高图像检索的准确度, 文献[15]参照不变矩定义了图像的离心率特征量e, 如式 (4) 所示: 在这里插入图片描述 离心率特征e表示了图像的最大轴方向与最小轴方向的比率,同样也满足物体仿射(平移、旋转、尺度)不变性。将不变矩特征量中个中7和离心率e合并,就形成了图像的全局形状特征(中1,中2,中3,中4,P5,中6,中7,e)。

3.2 形状特征归一化 全局形状特征矢量中各个元素的幅度值都是不一样的,由此将会使得图像的相似性匹配的结果出现误差,形状特征的归一化的目的就是对该误差进行修正。文献[16]提到了对7个不变矩的归一化处理,利用其思想,如果用(A1,A2,...,AM)来表示图像库中的M幅图像,就获得了M行8列的图像特征矩阵A=a,j,矩阵的每列相对应的是长度为M的特征序列,求得其标准差和均值分别是oj、uj,在这里,a,j表示的是第i幅图像当中的第j个形状特征元素,利用式(5)对图像的形状特征矢量进行高斯归一化处理。 在这里插入图片描述 4 图像的综合特征 已经得到了图像的颜色特征和形状特征,因为要对图像进行聚类,减少图像库的访问次数,将求得的146维的颜色聚合向量与8维的形状特征混合形成154维的综合特征(m1,m2,m3,...,m154),表示一幅图像的综合特征,为了方便计算以及说明,将这8维的形状特征分别看作第147个编码值到154个编码值的纵坐标分量,如果图像库中有M幅图像,则可用下列矩阵表示整个图像库: 在这里插入图片描述

二、部分源代码

function varargout = cbires(varargin)
% CBIRES MATLAB code for cbires.fig
%      CBIRES, by itself, creates a new CBIRES or raises the existing
%      singleton*.
%
%      H = CBIRES returns the handle to a new CBIRES or the handle to
%      the existing singleton*.
%
%      CBIRES('CALLBACK',hObject,eventData,handles,...) calls the local
%      function named CALLBACK in CBIRES.M with the given input arguments.
%
%      CBIRES('Property','Value',...) creates a new CBIRES or raises the
%      existing singleton*.  Starting from the left, property value pairs are
%      applied to the GUI before cbires_OpeningFcn gets called.  An
%      unrecognized property name or invalid value makes property application
%      stop.  All inputs are passed to cbires_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 cbires

% Last Modified by GUIDE v2.5 23-May-2021 22:01:15

% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name',       mfilename, ...
    'gui_Singleton',  gui_Singleton, ...
    'gui_OpeningFcn', @cbires_OpeningFcn, ...
    'gui_OutputFcn',  @cbires_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 cbires is made visible.
function cbires_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 cbires (see VARARGIN)

% Choose default command line output for cbires
handles.output = hObject;

% Update handles structure
guidata(hObject, handles);

% UIWAIT makes cbires wait for user response (see UIRESUME)
% uiwait(handles.figure1);


% --- Outputs from this function are returned to the command line.
function varargout = cbires_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 btn_BrowseImage.
function btn_BrowseImage_Callback(hObject, eventdata, handles)
% hObject    handle to btn_BrowseImage (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

[query_fname, query_pathname] = uigetfile('*.jpg; *.png; *.bmp', 'Select query image');

if (query_fname ~= 0)
    query_fullpath = strcat(query_pathname, query_fname);
    [pathstr, name, ext] = fileparts(query_fullpath); % fiparts returns char type
    
    if ( strcmp(lower(ext), '.jpg') == 1 || strcmp(lower(ext), '.png') == 1 ...
            || strcmp(lower(ext), '.bmp') == 1 )
        
        queryImage = imread( fullfile( pathstr, strcat(name, ext) ) );
%         handles.queryImage = queryImage;
%         guidata(hObject, handles);
        
        % extract query image features
        queryImage = imresize(queryImage, [384 256]);
        hsvHist = hsvHistogram(queryImage);
        autoCorrelogram = colorAutoCorrelogram(queryImage);
        color_moments = colorMoments(queryImage);
        % for gabor filters we need gary scale image
        img = double(rgb2gray(queryImage))/255;
        [meanAmplitude, msEnergy] = gaborWavelet(img, 4, 6); % 4 = number of scales, 6 = number of orientations
        wavelet_moments = waveletTransform(queryImage);
        % construct the queryImage feature vector
        queryImageFeature = [hsvHist autoCorrelogram color_moments meanAmplitude msEnergy wavelet_moments str2num(name)];
        
        % update handles
        handles.queryImageFeature = queryImageFeature;
        guidata(hObject, handles);
        helpdlg('Proceed with the query by executing the green button!');
        
        % Clear workspace
        clear('query_fname', 'query_pathname', 'query_fullpath', 'pathstr', ...
            'name', 'ext', 'queryImage', 'hsvHist', 'autoCorrelogram', ...
            'color_moments', 'img', 'meanAmplitude', 'msEnergy', ...
            'wavelet_moments', 'queryImageFeature');
    else
        errordlg('You have not selected the correct file type');
    end
else
    return;
end


% --- Executes on selection change in popupmenu_DistanceFunctions.
function popupmenu_DistanceFunctions_Callback(hObject, eventdata, handles)
% hObject    handle to popupmenu_DistanceFunctions (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: contents = cellstr(get(hObject,'String')) returns popupmenu_DistanceFunctions contents as cell array
%        contents{get(hObject,'Value')} returns selected item from popupmenu_DistanceFunctions

handles.DistanceFunctions = get(handles.popupmenu_DistanceFunctions, 'Value');
guidata(hObject, handles);


% --- Executes during object creation, after setting all properties.
function popupmenu_DistanceFunctions_CreateFcn(hObject, eventdata, handles)
% hObject    handle to popupmenu_DistanceFunctions (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: popupmenu 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 selection change in popupmenu_NumOfReturnedImages.
function popupmenu_NumOfReturnedImages_Callback(hObject, eventdata, handles)
% hObject    handle to popupmenu_NumOfReturnedImages (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: contents = cellstr(get(hObject,'String')) returns popupmenu_NumOfReturnedImages contents as cell array
%        contents{get(hObject,'Value')} returns selected item from popupmenu_NumOfReturnedImages

handles.numOfReturnedImages = get(handles.popupmenu_NumOfReturnedImages, 'Value');
guidata(hObject, handles);


% --- Executes during object creation, after setting all properties.
function popupmenu_NumOfReturnedImages_CreateFcn(hObject, eventdata, handles)
% hObject    handle to popupmenu_NumOfReturnedImages (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: popupmenu 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 btnExecuteQuery.
function btnExecuteQuery_Callback(hObject, eventdata, handles)
% hObject    handle to btnExecuteQuery (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% check for image query
if (~isfield(handles, 'queryImageFeature'))
    errordlg('Please select an image first, then choose your similarity metric and num of returned images!');
    return;
end

% check for dataset existence
if (~isfield(handles, 'imageDataset'))
    errordlg('Please load a dataset first. If you dont have one then you should consider creating one!');
    return;
end

% set variables
if (~isfield(handles, 'DistanceFunctions') && ~isfield(handles, 'numOfReturnedImages'))
    metric = get(handles.popupmenu_DistanceFunctions, 'Value');
    numOfReturnedImgs = get(handles.popupmenu_NumOfReturnedImages, 'Value');
elseif (~isfield(handles, 'DistanceFunctions') || ~isfield(handles, 'numOfReturnedImages'))
    if (~isfield(handles, 'DistanceFunctions'))
        metric = get(handles.popupmenu_DistanceFunctions, 'Value');
        numOfReturnedImgs = handles.numOfReturnedImages;
    else
        metric = handles.DistanceFunctions;
        numOfReturnedImgs = get(handles.popupmenu_NumOfReturnedImages, 'Value');
    end
else
    metric = handles.DistanceFunctions;
    numOfReturnedImgs = handles.numOfReturnedImages;
end

if (metric == 1)
    L1(numOfReturnedImgs, handles.queryImageFeature, handles.imageDataset.dataset);
elseif (metric == 2 || metric == 3 || metric == 4 || metric == 5 || metric == 6  || metric == 7 || metric == 8 || metric == 9 || metric == 10 || metric == 11)
    L2(numOfReturnedImgs, handles.queryImageFeature, handles.imageDataset.dataset, metric);
else
    relativeDeviation(numOfReturnedImgs, handles.queryImageFeature, handles.imageDataset.dataset);
end


% --- Executes on button press in btnExecuteSVM.
function btnExecuteSVM_Callback(hObject, eventdata, handles)
% hObject    handle to btnExecuteSVM (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% check for image query
if (~isfield(handles, 'queryImageFeature'))
    errordlg('Please select an image first!');
    return;
end

% check for dataset existence
if (~isfield(handles, 'imageDataset'))
    errordlg('Please load a dataset first. If you dont have one then you should consider creating one!');
    return;
end

三、运行结果

在这里插入图片描述

四、matlab版本及参考文献

1 matlab版本 2014a

2 参考文献 [1] 蔡利梅.MATLAB图像处理——理论、算法与实例分析[M].清华大学出版社,2020. [2]杨丹,赵海滨,龙哲.MATLAB图像处理实例详解[M].清华大学出版社,2013. [3]周品.MATLAB图像处理与图形用户界面设计[M].清华大学出版社,2013. [4]刘成龙.精通MATLAB图像处理[M].清华大学出版社,2015.