selective search程序理解

1.背景

承接上一篇博文,因爲fast RCNN中需要用到selective search(ss)算法,所以對ss論文及程序進行了理解。

SS論文中提出了使用多種顏色空間,在每個顏色空間提取相同的特徵,根據前人提出的算法先得到初始區域,然後根據相似性準則對所提出的區域進行合併,得到最終的提案。原理理解可參考博文http://blog.csdn.net/mao_kun/article/details/50576003。SS的完整代碼有matlab版本的和python版本的,在網上都可以找到,按照說明簡單進行一下安裝就可以。matlab版本的很多程序是以C++爲底層語言寫的,所以先要安裝C++在matlab中的接口。Python版本的只需要在command中吧SS進行簡單的pip安裝就可以。代碼在作者主頁中找到,也可以在我的下載頻道中找到。以下對matlab代碼進行理解和應用。

2.代碼理解

% This demo shows how to use the software described in our IJCV paper: 
%   Selective Search for Object Recognition,
%   J.R.R. Uijlings, K.E.A. van de Sande, T. Gevers, A.W.M. Smeulders, IJCV 2013
%%
close all;clear;clc;
addpath('Dependencies');

fprintf('Demo of how to run the code for:\n');
fprintf('   J. Uijlings, K. van de Sande, T. Gevers, A. Smeulders\n');
fprintf('   Segmentation as Selective Search for Object Recognition\n');
fprintf('   IJCV 2013\n\n');

% Compile anisotropic gaussian filter
if(~exist('anigauss'))
    fprintf('Compiling the anisotropic gauss filtering of:\n');
    fprintf('   J. Geusebroek, A. Smeulders, and J. van de Weijer\n');
    fprintf('   Fast anisotropic gauss filtering\n');
    fprintf('   IEEE Transactions on Image Processing, 2003\n');
    fprintf('Source code/Project page:\n');
    fprintf('   http://staff.science.uva.nl/~mark/downloads.html#anigauss\n\n');
    mex Dependencies/anigaussm/anigauss_mex.c Dependencies/anigaussm/anigauss.c -output anigauss
end

if(~exist('mexCountWordsIndex'))
    mex Dependencies/mexCountWordsIndex.cpp
end

% Compile the code of Felzenszwalb and Huttenlocher, IJCV 2004.
if(~exist('mexFelzenSegmentIndex'))
    fprintf('Compiling the segmentation algorithm of:\n');
    fprintf('   P. Felzenszwalb and D. Huttenlocher\n');
    fprintf('   Efficient Graph-Based Image Segmentation\n');
    fprintf('   International Journal of Computer Vision, 2004\n');
    fprintf('Source code/Project page:\n');
    fprintf('   http://www.cs.brown.edu/~pff/segment/\n');
    fprintf('Note: A small Matlab wrapper was made.\n');
%     fprintf('   
    mex Dependencies/FelzenSegment/mexFelzenSegmentIndex.cpp -output mexFelzenSegmentIndex;
end

%%
% Parameters. Note that this controls the number of hierarchical
% segmentations which are combined.
colorTypes = {'Hsv', 'Lab', 'RGI', 'H', 'Intensity'};%此處爲quality模式的所有顏色空間
colorType = colorTypes{3}; % Single color space for demo
% colorType = colorTypes(1:2); 在demo中無法實現多個色彩空間
% RGI得到的含有腫塊區域的比較多
% H所得到的偏差比較大


% Here you specify which similarity functions to use in merging
simFunctionHandles = {@SSSimColourTextureSizeFillOrig, @SSSimTextureSizeFill, @SSSimBoxFillOrig, @SSSimSize};%quality模式的相似度準則
simFunctionHandles = simFunctionHandles(1:2); % Two different merging strategies

% Thresholds for the Felzenszwalb and Huttenlocher segmentation algorithm.
% Note that by default, we set minSize = k, and sigma = 0.8.
k = 200; % controls size of segments of initial segmentation. 
minSize = k;
sigma = 0.8;
%%
% As an example, use a single image
% 原始示例
% images = {'000015.jpg'};
%車輛圖片
% im=imread('C:\Users\Administrator\Desktop\SSmatlab\僞彩色增強程序\車輛.jpg');
% 鳥
im=imread('C:\Users\Administrator\Desktop\SSmatlab\僞彩色增強程序\鳥.jpg');
%乳腺圖片
% load('C:\Users\Administrator\Desktop\數據庫\乳腺數據\ICYL.mat');
% im= ICYL(3,:,:);
% im=reshape(im,[1000 600]);
% n=max(im(:));
% map=colormap(jet(n));
% res = grs2rgb1(im,map);
% im=res;
% figure 
% imshow(im, 'DisplayRange',[]);
% title('ICY');

%%

% Perform Selective Search
[boxes blobIndIm blobBoxes hierarchy] = Image2HierarchicalGrouping(im, sigma, k, minSize, colorType, simFunctionHandles);
boxes = BoxRemoveDuplicates(boxes);

% save('C:\Users\Administrator\Desktop\SSmatlab\僞彩色增強程序\車輛.mat','boxes');
% save('C:\Users\Administrator\Desktop\SSmatlab\僞彩色增強程序\breast.mat','boxes');
save('C:\Users\Administrator\Desktop\SSmatlab\僞彩色增強程序\bird.mat','boxes');

% Show boxes
ShowRectsWithinImage(boxes, 5, 5, im);可視化所得提案

% % Show blobs which result from first similarity function
hBlobs = RecreateBlobHierarchyIndIm(blobIndIm, blobBoxes, hierarchy{1});
ShowBlobs(hBlobs, 5, 5, im);%可視化對提案的分割結果

% 在原始圖像上展示所有的框
figure
imshow(im)
hold on
for i=1:size(boxes,1)
    x=boxes(i,1);
    y=boxes(i,2);
    w=boxes(i,4)-boxes(i,2);
    h=boxes(i,3)-boxes(i,1);
    rectangle('Position',[x,y,w,h],'EdgeColor','w','LineWidth',1)
    hold on
end
原始的SS程序跑通以後就可以將demo中的圖片替換爲自己的彩色圖片得到與類獨立的區域提案了。

選擇不同的色彩空間,可以得到不同的提案,在實踐中發現當選擇RGI顏色空間包含腫塊的提案更多。後期會先用RGI得到的提案作爲fast RCNN的輸入,後期再進行不斷地優化。


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章