选择性搜索-源码解析¶
工程结构¶
- 头文件
include/segmentation.h
:头文件入口include/graphsegment/
:图分割算法头文件include/selectivesearch/
:选择性搜索算法头文件
- 源文件:
src
文件夹 - 测试文件:
samples/selectivesearchsegmentation_custom.cpp
├── CMakeLists.txt
├── include
│ ├── graphsegment
│ │ ├── edge.h
│ │ ├── graphsegmentation.h
│ │ ├── graphsegmentationimpl.h
│ │ ├── point_set_element.h
│ │ └── point_set.h
│ ├── segmentation.h
│ └── selectivesearch
│ ├── neighbor.h
│ ├── rect_comparator.h
│ ├── region.h
│ ├── selectivesearchsegmentation.h
│ ├── selectivesearchsegmentationimpl.h
│ ├── selective_search_segmentation_strategy_color.h
│ ├── selective_search_segmentation_strategy_color_impl.h
│ ├── selective_search_segmentation_strategy_fill.h
│ ├── selective_search_segmentation_strategy_fill_impl.h
│ ├── selective_search_segmentation_strategy.h
│ ├── selective_search_segmentation_strategy_multiple.h
│ ├── selective_search_segmentation_strategy_multiple_impl.h
│ ├── selective_search_segmentation_strategy_size.h
│ ├── selective_search_segmentation_strategy_size_impl.h
│ ├── selective_search_segmentation_strategy_texture.h
│ └── selective_search_segmentation_strategy_texture_impl.h
├── samples
│ └── selectivesearchsegmentation_custom.cpp
└── src
├── graphsegmentation.cpp
├── graphsegmentationimpl.cpp
├── neighbor.cpp
├── point_set.cpp
├── region.cpp
├── selectivesearchsegmentation.cpp
├── selectivesearchsegmentationimpl.cpp
├── selective_search_segmentation_strategy_color.cpp
├── selective_search_segmentation_strategy_color_impl.cpp
├── selective_search_segmentation_strategy_fill.cpp
├── selective_search_segmentation_strategy_fill_impl.cpp
├── selective_search_segmentation_strategy_multiple.cpp
├── selective_search_segmentation_strategy_multiple_impl.cpp
├── selective_search_segmentation_strategy_size.cpp
├── selective_search_segmentation_strategy_size_impl.cpp
├── selective_search_segmentation_strategy_texture.cpp
└── selective_search_segmentation_strategy_texture_impl.cpp
类图¶
分两部分实现,首先是选择性搜索算法的全局类图,其次是选择性搜索算法的相似度计算策略
- 类
SelectiveSearchSegmentation
声明了选择性搜索算法的公共函数 - 类
SelectiveSearchSegmentationImpl
定义了选择性搜索算法的具体实现 - 类
GraphSegmentation
声明了图分割算法的公共函数 - 类
SelectiveSearchSegmentationStrategy
声明了相似度度量策略的公共函数 - 类
Region
实现了并查集,作用于区域合并 - 类
Neighbor
定义了相邻区域对
- 类
SelectiveSearchSegmentationStrategy
声明了相似度度量策略的公共函数 - 类
SelectiveSearchSegmentationStrategyColorImpl
定义了基于颜色特征的相似性度量 - 类
SelectiveSearchSegmentationStrategyTextureImpl
定义了基于纹理特征的相似性度量 - 类
SelectiveSearchSegmentationStrategySizeImpl
定义了基于区域大小的相似性度量 - 类
SelectiveSearchSegmentationStrategySizeImpl
定义了基于区域形状的相似性度量 - 类
SelectiveSearchSegmentationStrategyMultiple
声明了组合多个相似性度量策略的公共函数 - 类
SelectiveSearchSegmentationStrategyMultipleImpl
定义了基于多特征的相似性度量计算
超参数¶
图分割相关:
- k:用于计算阈值函数τ,控制两个分量之间的差异必须大于其内部差异的程度
- \sigma:作用于高斯滤波
算法流程¶
根据测试代码samples/selectivesearchsegmentation.cpp
,总体函数流程图如下所示
- 创建选择性搜索对象,输入原始图像,设置检测策略
- 处理图像,获取候选区域
- 绘制候选区域边框
第一步:预处理¶
分为3
个步骤:
- 创建选择性搜索对象,使用函数
createSelectiveSearchSegmentation
- 输入原始图像,使用函数
setBaseImage
- 设置检测策略
创建选择性搜索对象¶
createSelectiveSearchSegmentation
是一个辅助性函数,用于创建SelectiveSearchSegmentation
对象,其返回一个SelectiveSearchSegmentation
指针
输入原始图像¶
通过调用类SelectiveSearchSegmentation
的公共函数setBaseImage
载入原始图像
设置检测策略¶
类SelectiveSearchSegmentation
提供了3
种检测策略:
- 简单计算策略
switchToSingleStrategy
- 快速计算策略
switchToSelectiveSearchFast
- 高质量计算策略
switchToSelectiveSearchQuality
简单计算策略¶
函数switchToSingleStrategy
仅使用单个分组
- 颜色空间:
HSV
- 图分割超参数:
k=200,sigma=0.8
- 相似性度量:
0.25*color + 0.25*texture + 0.25*size + 0.25*size
快速分组策略¶
函数switchToSelectiveSearchFast
使用多个分组
- 颜色空间:
HSV、Lab
(共2
种) - 图分割超参数:
base_k = 150,inc_k = 150,sigma = 0.8
(在程序中最高k
值设置为base_k + inc_k * 2
,所以k
取值为150/300/450
,共3
种) - 相似性度量:
0.25*color + 0.25*texture + 0.25*size + 0.25*fill
、0.3333*fill + 0.3333*texture + 0.3333*size
(共2
种)
所以快速计算策略使用了2*3*2=12
个分组
高质量计算策略¶
函数switchToSelectiveSearchQuality
使用了更多的分组
- 颜色空间:
HSV、Lab、I、H、bgI
(共5
种) - 图分割超参数:
base_k = 150,inc_k = 150,sigma = 0.8
(在程序中最高k
值设置为base_k + inc_k * 2
,所以k
取值为150/300/450
,共3
种) - 相似性度量:
0.25*color + 0.25*texture + 0.25*size + 0.25*fill、0.3333*fill + 0.3333*texture + 0.3333*size、fill、size
(共4
种)
所以高质量计算策略使用了5*3*4=60
个分组
第二步:区域检测¶
区域检测操作在类SelectiveSearchSegmentationImpl
的函数process
中实现,步骤如下:
- 获取初始分割集
- 计算分层分组区域
- 组合所有区域并过滤重复区域
获取初始分割集¶
基于不同颜色空间下的图像以及基于不同图分割参数的GraphSegmentation
对象,调用函数processImage
获取初始分割区域
计算分层分组区域¶
- 计算初始分割区域个数
- 计算初始分割区域大小
- 计算初始分割区域所属边框坐标
- 计算初始分割集中的相邻区域对
基于不同的分组策略,调用函数hierarchicalGrouping
进行分层分组算法
- 计算初始分割集中相邻区域对的相似度
- 基于相似度进行排序,合并相似度最高的区域对
- 重新计算新区域与相邻区域的相似度
- 重新排序,重复上述步骤,直到合并成一个区域
- 通过随机数和合并
level
重新计算每个区域的秩(rank
)
组合所有区域并过滤重复区域¶
- 组合所有分组区域
- 按秩排序
- 过滤重复区域
第三步:绘制候选区域边框¶
每次绘制10
个区域,按d
键增加10
个,按a
键减去10
个,按q
键退出
相似性度量¶
共实现了4
种相似性度量方法:
- 颜色特征:
SelectiveSearchSegmentationStrategyColorImpl
- 纹理特征:
SelectiveSearchSegmentationStrategyTextureImpl
- 形状特征:
SelectiveSearchSegmentationStrategyFillImpl
- 大小特征:
SelectiveSearchSegmentationStrategySizeImpl
每种相似性度量类都继承自SelectiveSearchSegmentationStrategy
,定义了相同的公共函数入口:
- 预处理:
setImage(cv::InputArray img, cv::InputArray regions, cv::InputArray sizes, int image_id = -1)
- 计算相似度:
get(int r1, int r2)
- 合并区域特征:
merge(int r1, int r2)
void setImage(cv::InputArray img, cv::InputArray regions, cv::InputArray sizes, int image_id = -1)
参数解析:
img
:不同颜色空间下的图像regions
:图分割算法得到的初始分割集sizes
:各个区域大小image_id
:图像编号,是否处于同一颜色空间以及同一图分割对象.用于缓存操作
float get(int r1, int r2)
输入两个区域的编号,返回相似度
void merge(int r1, int r2
合并区域r1
和r2