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的输入,后期再进行不断地优化。


发布了29 篇原创文章 · 获赞 4 · 访问量 2万+

猜你喜欢

转载自blog.csdn.net/best_scenery/article/details/75948996