基于vgg16的图像检索系统(首先要先熟悉VGG16模型)

主要的流程如下:

1. 首先要熟悉vgg16模型;(推荐这位博主的文章介绍,讲的比较清楚!)
2. 其次就是熟悉图像检索的流程;(基于内容的图像检索技术综述-CNN 方法)以上就是我制作这个系统的全部过程;
还有就是可以看一下相关的论文去学习图像检索方面的知识还有就是可以看一下相关的论文去学习图像检索方面的知识。本人是个刚接触matlab的小白,所以两种方式都采用了。

第一步:首先我是看了网上其他博主的方法,制作一个GUI图像检索界面
直接在matlab的命令窗口直接输入guide,然后回车即可弹出GUIDE快速入门的界面
基于vgg16的图像检索系统(首先要先熟悉VGG16模型)_第1张图片
然后根据自己的选择选择guide templates;我这里选择了第一个默认的;确定后就会出现下面这个类似于画板一样的界面;就直接可以在上面操作了;
基于vgg16的图像检索系统(首先要先熟悉VGG16模型)_第2张图片
下面这个是我的GUI界面,接下来的我会继续更新!!!
基于vgg16的图像检索系统(首先要先熟悉VGG16模型)_第3张图片
来了来了:主函数代码如下所示!

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

% Last Modified by GUIDE v2.5 21-Jul-2019 10:31:37

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

% Choose default command line output for main
handles.output = hObject;
global net
% Setup MatConvNet.
run matlab/vl_setupnn ;

% Load a model and upgrade it to MatConvNet current version.
net = load('imagenet-vgg-verydeep-16.mat') ;
net = vl_simplenn_tidy(net) ;
% Update handles structure
guidata(hObject, handles);

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


% --- Outputs from this function are returned to the command line.
function varargout = main_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','blue');
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


% --- 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  features
global net 
[filename,pathname]=...
    uigetfile({'*.jpg';'*.bmp';'*.gif'},'选择图片');
str=[pathname filename];
image=imread(str);
axes(handles.axes1);
imshow(image);
set(gca,'xtick',[],'ytick',[])  
%  归一化
layers = 34; 
im_ = single(image) ; % note: 255 range
im_ = imresize(im_, net.meta.normalization.imageSize(1:2)) ;
im_ = im_ - net.meta.normalization.averageImage ;
%  提取全连接层特征 第34层
% Run the CNN.
res = vl_simplenn(net, im_) ;
% 保存特征
x = res(layers).x;
features = x(:);

% --- 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  B1
global  nnn
global A1
nnn = nnn-1;
if nnn==0;
    nnn = 1;
end
num = nnn*10-9:nnn*10;
load('data.mat');
for i=1:10
    imid = B1(num(i));
    AA=imread(names{imid}); 
    switch i
        case 1
         axes(handles.axes17);
        case 2
         axes(handles.axes18);
        case 3
         axes(handles.axes19);
         case 4
         axes(handles.axes20);
         case 5
         axes(handles.axes21);
         case 6
         axes(handles.axes22);
         case 7
         axes(handles.axes23);
         case 8
         axes(handles.axes24);
         case 9
         axes(handles.axes25);
        otherwise
         axes(handles.axes26);  
    end
    axis off
    imshow(AA);
    set(gca,'xtick',[],'ytick',[])  
  title([num2str(1-A1(i))]);
 
end



% --- 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)
    axes(handles.axes17);
    % cla 清除坐标区
    cla reset 
    axes(handles.axes18);
    cla reset 
    axes(handles.axes19);
    cla reset 
    axes(handles.axes20);
    cla reset 
    axes(handles.axes21);
    cla reset 
    axes(handles.axes22);
    cla reset 
    axes(handles.axes23);
    cla reset 
    axes(handles.axes24);
    cla reset 
    axes(handles.axes25);
    cla reset 
    axes(handles.axes26);  
    cla reset 
global  B1
global  nnn
global  A1
load('data.mat');
nnn = nnn+1;
if nnn*10< length(B1)
   num = nnn*10-9:nnn*10;
else
   num = nnn*10-9:length(B1);
   nnn = nnn-1;
end
for i=1:length(num)
     imid = B1(num(i));
    AA=imread(names{imid}); 
    switch i
        case 1
         axes(handles.axes17);
        case 2
         axes(handles.axes18);
        case 3
         axes(handles.axes19);
         case 4
         axes(handles.axes20);
         case 5
         axes(handles.axes21);
         case 6
         axes(handles.axes22);
         case 7
         axes(handles.axes23);
         case 8
         axes(handles.axes24);
         case 9
         axes(handles.axes25);
        otherwise
         axes(handles.axes26);  
    end
    axis off
    cla reset 
    imshow(AA);
    set(gca,'xtick',[],'ytick',[])  
   title([num2str(1-A1(num(i)))]);
end

% --- 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)
% jiansuo
global  features
global  B1
global  nnn
global  A1 
load data.mat
% %  和库里的10000计算欧式距离
for count= 1:size(allfeatures,1)
%     count
    new_dist (count) =distance_func(features',allfeatures(count,:)); 
end
new_dist = new_dist/sum(new_dist);
%  距离排序,并选前10个输出
[A1,B1]=sort(new_dist);

nnn = 1;
for i=1:10
    imid = B1(i);
    AA=imread(names{imid}); 
    switch i
        case 1
         axes(handles.axes17);
        case 2
         axes(handles.axes18);
        case 3
         axes(handles.axes19);
         case 4
         axes(handles.axes20);
         case 5
         axes(handles.axes21);
         case 6
         axes(handles.axes22);
         case 7
         axes(handles.axes23);
         case 8
         axes(handles.axes24);
         case 9
         axes(handles.axes25);
        otherwise
         axes(handles.axes26);  
    end
    axis off
    imshow(AA);
    set(gca,'xtick',[],'ytick',[]);  
    title(num2str(1-A1(i)));
end


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

% --- Executes on button press in radiobutton1.
function radiobutton1_Callback(hObject, eventdata, handles)
% hObject    handle to radiobutton1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
% Hint: get(hObject,'Value') returns toggle state of radiobutton1
set(handles.radiobutton2,'Value',0);
set(handles.radiobutton3,'Value',0);
% --- Executes on button press in radiobutton2.
function radiobutton2_Callback(hObject, eventdata, handles)
% hObject    handle to radiobutton2 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
set(handles.radiobutton1,'Value',0);
set(handles.radiobutton3,'Value',0);
% Hint: get(hObject,'Value') returns toggle state of radiobutton2


% --- Executes on button press in radiobutton3.
function radiobutton3_Callback(hObject, eventdata, handles)
% hObject    handle to radiobutton3 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
set(handles.radiobutton1,'Value',0);
set(handles.radiobutton2,'Value',0);
% Hint: get(hObject,'Value') returns toggle state of radiobutton3



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


% --- Executes on button press in pushbutton6.
function pushbutton6_Callback(hObject, eventdata, handles)
% hObject    handle to pushbutton6 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
h1=findobj(handles.axes27,'visible','off');%获取按键1的可视化句柄
clc;
clear all;
set(h1,'visible','on');%设置句柄为可见
mywaitbar(0,'Please Wait...',handles.axes27,handles.figure1,2);
% TheEndTime = 600; 
% for t = 1:TheEndTime
%        mywaitbar(t/TheEndTime,[num2str(floor(t*100/TheEndTime)),'%'],handles.axes27,handles.figure1,2);
% end
% 提取所有的库图像特征
 % Setup MatConvNet.
run matlab/vl_setupnn ;
% 载入模型
net = load('imagenet-vgg-verydeep-16.mat') ;
net = vl_simplenn_tidy(net) ;
layers = 34; 
allfeatures = [];
count = 1;
names  = {};
t =0;
TheEndTime = 10000;
for i = 1:50
    for j = 1:100
        t = t+1;    
        mywaitbar(t/TheEndTime,[num2str(floor(t*100/TheEndTime)),'%'],handles.axes27,handles.figure1,2);
        disp([num2str(i) '_' num2str(j)]);
        filename = ['./Corel5K/' num2str(i-1) '_' num2str((i-1)*100+j) '.jpg'];
        im=imread(filename);
        %  依次读取并提取全连接层特征
        im_ = single(im) ; % note: 255 range
        im_ = imresize(im_, net.meta.normalization.imageSize(1:2)) ;
        im_ = im_ - net.meta.normalization.averageImage ;
        % Run the CNN.
        res = vl_simplenn(net, im_) ;
        x = res(layers).x;
        features = x(:);
        allfeatures = [allfeatures;features'];
        names{count} = filename;
        count = count +1;
    end
end
% 保存特征和图像名字corel10K
% save('data.mat','allfeatures','names');
save('corel5k.mat','allfeatures','names');

% --- Executes on button press in pushbutton7.
function pushbutton7_Callback(hObject, eventdata, handles)
% hObject    handle to pushbutton7 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
% h1=findobj(handles.axes28,'visible','off');%获取按键1的可视化句柄
% set(h1,'visible','on');%设置句柄为可见
mywaitbar(0,'Please Wait...',handles.axes28,handles.figure1,1);
% TheEndTime = 600; 
% for t = 1:TheEndTime
%        mywaitbar(t/TheEndTime,[num2str(floor(t*100/TheEndTime)),'%'],handles.axes28,handles.figure1,1);
% end
% F=R计算  acc edit5  edit6
% load('data.mat');
load('corel5k.mat');
cnt  = 1;
% 这里进行降维处理
[coeff, score, latent] = pca(allfeatures);
allfeatures = allfeatures*coeff(:, 1:128);
% 10000附图循环和库里所有的比对
TheEndTime = 5000;
for i= 1:size(allfeatures,1)
     mywaitbar(cnt/TheEndTime,[num2str(floor(cnt*100/TheEndTime)),'%'],handles.axes28,handles.figure1,1);
     features = allfeatures(i,:);
     count = 1;
     for j= 1:size(allfeatures,1)
         new_dist (count) =distance_func(features,allfeatures(j,:)); 
         count = count+1;
     end
     % 数组元素之和
     new_dist = new_dist/sum(new_dist);
     [~,B1]=sort(new_dist);
     % 返回的图像个数 12 
     B = B1(1:12);
     % 朝零四舍五入
     shang = fix(cnt/100);
     % 除后的余数,取模操作
     yu = mod(cnt,50);
     if yu == 0
         shang = shang-1;
     end
     % length最大数值维度的长度
     % find查找非零元素的索引和值
     precision(cnt) = length(find(B<=50*(shang+1)&  B>50*shang  ))/length(B);
     recall(cnt) = length(find(B<=50*(shang+1)&  B>50*shang  ))/10;
     cnt =cnt+1;
end

set(handles.edit5,'String',num2str(mean(precision)));
set(handles.edit6,'String',num2str(mean(recall))); 
figure;
bar(precision);title('查准率')
figure;
bar(recall);title('查全率')
save('results.mat','precision','recall');
 
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 during object creation, after setting all properties.
function text6_CreateFcn(hObject, eventdata, handles)
% hObject    handle to text6 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called


% --- Executes on key press with focus on edit6 and none of its controls.
function edit6_KeyPressFcn(hObject, eventdata, handles)
% hObject    handle to edit6 (see GCBO)
% eventdata  structure with the following fields (see MATLAB.UI.CONTROL.UICONTROL)
%	Key: name of the key that was pressed, in lower case
%	Character: character interpretation of the key(s) that was pressed
%	Modifier: name(s) of the modifier key(s) (i.e., control, shift) pressed
% handles    structure with handles and user data (see GUIDATA)


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

% Hint: place code in OpeningFcn to populate axes1

你可能感兴趣的:(图像检索)