Skip to content

Commit 5bd4930

Browse files
committed
feat(selectivesearch): 重构OpenCV selectivesearch源码
1 parent ba2b4b4 commit 5bd4930

33 files changed

+1633
-6
lines changed

cplusplus/CMakeLists.txt

Lines changed: 44 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,52 @@ MESSAGE("OpenCV OpenCV_INCLUDE_DIRS: ${OpenCV_INCLUDE_DIRS}")
1414
MESSAGE("OpenCV OpenCV_LIBS: ${OpenCV_LIBS}")
1515

1616
#图分割算法
17-
add_executable(cpluplus samples/graphsegmentation_custom.cpp
18-
include/segmentation.h
17+
#add_executable(cpluplus samples/graphsegmentation_custom.cpp
18+
# include/segmentation.h
19+
# include/graphsegment/graphsegmentation.h include/graphsegment/graphsegmentationimpl.h
20+
# include/graphsegment/edge.h include/graphsegment/point_set_element.h include/graphsegment/point_set.h
21+
# src/graphsegmentation.cpp src/graphsegmentationimpl.cpp src/point_set.cpp
22+
# )
23+
add_executable(cpluplus samples/selectivesearchsegmentation_custom.cpp
24+
include/selectivesearch/selectivesearchsegmentation.h
25+
include/selectivesearch/selectivesearchsegmentationimpl.h
26+
27+
# Helpers
28+
include/selectivesearch/region.h include/selectivesearch/neighbor.h
29+
include/selectivesearch/selective_search_segmentation_strategy.h
30+
include/selectivesearch/rect_comparator.h
31+
32+
# Strategy
33+
include/selectivesearch/selective_search_segmentation_strategy_color.h
34+
include/selectivesearch/selective_search_segmentation_strategy_size.h
35+
include/selectivesearch/selective_search_segmentation_strategy_texture.h
36+
include/selectivesearch/selective_search_segmentation_strategy_fill.h
37+
include/selectivesearch/selective_search_segmentation_strategy_multiple.h
38+
39+
include/selectivesearch/selective_search_segmentation_strategy_color_impl.h
40+
include/selectivesearch/selective_search_segmentation_strategy_size_impl.h
41+
include/selectivesearch/selective_search_segmentation_strategy_texture_impl.h
42+
include/selectivesearch/selective_search_segmentation_strategy_fill_impl.h
43+
include/selectivesearch/selective_search_segmentation_strategy_multiple_impl.h
44+
45+
src/selective_search_segmentation_strategy_color_impl.cpp
46+
src/selective_search_segmentation_strategy_color.cpp
47+
src/selective_search_segmentation_strategy_multiple.cpp
48+
src/selective_search_segmentation_strategy_multiple_impl.cpp
49+
src/selective_search_segmentation_strategy_size.cpp
50+
src/selective_search_segmentation_strategy_size_impl.cpp
51+
src/selective_search_segmentation_strategy_fill.cpp
52+
src/selective_search_segmentation_strategy_fill_impl.cpp
53+
src/selective_search_segmentation_strategy_texture.cpp
54+
src/selective_search_segmentation_strategy_texture_impl.cpp
55+
src/selectivesearchsegmentation.cpp
56+
src/selectivesearchsegmentationimpl.cpp
57+
src/region.cpp
58+
src/neighbor.cpp
59+
60+
# 图分割算法
1961
include/graphsegment/graphsegmentation.h include/graphsegment/graphsegmentationimpl.h
2062
include/graphsegment/edge.h include/graphsegment/point_set_element.h include/graphsegment/point_set.h
2163
src/graphsegmentation.cpp src/graphsegmentationimpl.cpp src/point_set.cpp
2264
)
23-
#add_executable(cpluplus samples/selectivesearchsegmentation_custom.cpp)
2465
target_link_libraries(cpluplus ${OpenCV_LIBS})

cplusplus/include/segmentation.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,6 @@
66
#define CPLUPLUS_SEGMENTATION_H
77

88
#include "graphsegment/graphsegmentation.h"
9+
#include "selectivesearch/selectivesearchsegmentation.h"
910

1011
#endif //CPLUPLUS_SEGMENTATION_H
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
//
2+
// Created by zj on 2020/2/14.
3+
//
4+
5+
#ifndef CPLUPLUS_NEIGHBOR_H
6+
#define CPLUPLUS_NEIGHBOR_H
7+
8+
#include <iostream>
9+
10+
namespace segmentation {
11+
class Neighbour {
12+
public:
13+
int from;
14+
int to;
15+
float similarity;
16+
17+
friend std::ostream &operator<<(std::ostream &os, const Neighbour &n);
18+
19+
bool operator<(const Neighbour &n) const {
20+
return similarity < n.similarity;
21+
}
22+
};
23+
}
24+
25+
#endif //CPLUPLUS_NEIGHBOR_H
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
//
2+
// Created by zj on 2020/2/15.
3+
//
4+
5+
#ifndef CPLUPLUS_RECT_COMPARATOR_H
6+
#define CPLUPLUS_RECT_COMPARATOR_H
7+
8+
#include <opencv2/opencv.hpp>
9+
10+
namespace segmentation {
11+
// Comparator to sort cv::rect (used for a std::map).
12+
struct rectComparator {
13+
bool operator()(const cv::Rect_<int> &a, const cv::Rect_<int> &b) const {
14+
if (a.x < b.x) {
15+
return true;
16+
}
17+
if (a.x > b.x) {
18+
return false;
19+
}
20+
if (a.y < b.y) {
21+
return true;
22+
}
23+
if (a.y > b.y) {
24+
return false;
25+
}
26+
if (a.width < b.width) {
27+
return true;
28+
}
29+
if (a.width > b.width) {
30+
return false;
31+
}
32+
if (a.height < b.height) {
33+
return true;
34+
}
35+
if (a.height > b.height) {
36+
return false;
37+
}
38+
return false;
39+
}
40+
};
41+
42+
}
43+
44+
#endif //CPLUPLUS_RECT_COMPARATOR_H
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
//
2+
// Created by zj on 2020/2/14.
3+
//
4+
5+
#ifndef CPLUPLUS_REGION_H
6+
#define CPLUPLUS_REGION_H
7+
8+
#include <iostream>
9+
#include <opencv2/opencv.hpp>
10+
11+
namespace segmentation {
12+
13+
class Region {
14+
public:
15+
int id;
16+
int level;
17+
int merged_to;
18+
double rank;
19+
cv::Rect bounding_box;
20+
21+
Region() : id(0), level(0), merged_to(0), rank(0) {}
22+
23+
friend std::ostream &operator<<(std::ostream &os, const Region &n);
24+
25+
bool operator<(const Region &n) const {
26+
return rank < n.rank;
27+
}
28+
};
29+
}
30+
31+
#endif //CPLUPLUS_REGION_H
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
//
2+
// Created by zj on 2020/2/14.
3+
//
4+
5+
#ifndef CPLUPLUS_SELECTIVE_SEARCH_SEGMENTATION_STRATEGY_H
6+
#define CPLUPLUS_SELECTIVE_SEARCH_SEGMENTATION_STRATEGY_H
7+
8+
#include <opencv2/opencv.hpp>
9+
10+
namespace segmentation {
11+
class SelectiveSearchSegmentationStrategy {
12+
public:
13+
/** @brief Set a initial image, with a segmentation.
14+
@param img The input image. Any number of channel can be provided
15+
@param regions A segmentation of the image. The parameter must be the same size of img.
16+
@param sizes The sizes of different regions
17+
@param image_id If not set to -1, try to cache pre-computations. If the same set og (img, regions, size) is used, the image_id need to be the same.
18+
*/
19+
virtual void setImage(cv::InputArray img, cv::InputArray regions, cv::InputArray sizes, int image_id = -1) = 0;
20+
21+
/** @brief Return the score between two regions (between 0 and 1)
22+
@param r1 The first region
23+
@param r2 The second region
24+
*/
25+
virtual float get(int r1, int r2) = 0;
26+
27+
/** @brief Inform the strategy that two regions will be merged
28+
@param r1 The first region
29+
@param r2 The second region
30+
*/
31+
virtual void merge(int r1, int r2) = 0;
32+
};
33+
34+
}
35+
36+
#endif //CPLUPLUS_SELECTIVE_SEARCH_SEGMENTATION_STRATEGY_H
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
//
2+
// Created by zj on 2020/2/14.
3+
//
4+
5+
#ifndef CPLUPLUS_SELECTIVE_SEARCH_SEGMENTATION_STRATEGY_COLOR_H
6+
#define CPLUPLUS_SELECTIVE_SEARCH_SEGMENTATION_STRATEGY_COLOR_H
7+
8+
#include "selective_search_segmentation_strategy.h"
9+
#include <iostream>
10+
11+
namespace segmentation {
12+
13+
/** @brief Color-based strategy for the selective search segmentation algorithm
14+
The class is implemented from the algorithm described in @cite uijlings2013selective.
15+
*/
16+
class SelectiveSearchSegmentationStrategyColor : public SelectiveSearchSegmentationStrategy {};
17+
18+
/** @brief Create a new color-based strategy */
19+
std::shared_ptr<SelectiveSearchSegmentationStrategyColor> createSelectiveSearchSegmentationStrategyColor();
20+
21+
}
22+
23+
#endif //CPLUPLUS_SELECTIVE_SEARCH_SEGMENTATION_STRATEGY_COLOR_H
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
//
2+
// Created by zj on 2020/2/14.
3+
//
4+
5+
#ifndef CPLUPLUS_SELECTIVE_SEARCH_SEGMENTATION_STRATEGY_COLOR_IMPL_H
6+
#define CPLUPLUS_SELECTIVE_SEARCH_SEGMENTATION_STRATEGY_COLOR_IMPL_H
7+
8+
#include <opencv2/opencv.hpp>
9+
#include "selective_search_segmentation_strategy_color.h"
10+
11+
namespace segmentation {
12+
class SelectiveSearchSegmentationStrategyColorImpl final : public SelectiveSearchSegmentationStrategyColor {
13+
public:
14+
SelectiveSearchSegmentationStrategyColorImpl() {
15+
name_ = "SelectiveSearchSegmentationStrategyColor";
16+
last_image_id = -1;
17+
}
18+
19+
virtual void
20+
setImage(cv::InputArray img, cv::InputArray regions, cv::InputArray sizes, int image_id = -1) override;
21+
22+
virtual float get(int r1, int r2) override;
23+
24+
virtual void merge(int r1, int r2) override;
25+
26+
private:
27+
std::string name_;
28+
29+
cv::Mat histograms; // [Region X Histogram]
30+
cv::Mat sizes;
31+
int histogram_size{};
32+
33+
int last_image_id; // If the image_id is not equal to -1 and the same as the previous call for setImage, computations are used again
34+
cv::Mat last_histograms;
35+
};
36+
37+
}
38+
39+
#endif //CPLUPLUS_SELECTIVE_SEARCH_SEGMENTATION_STRATEGY_COLOR_IMPL_H
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
//
2+
// Created by zj on 2020/2/14.
3+
//
4+
5+
#ifndef CPLUPLUS_SELECTIVE_SEARCH_SEGMENTATION_STRATEGY_FILL_H
6+
#define CPLUPLUS_SELECTIVE_SEARCH_SEGMENTATION_STRATEGY_FILL_H
7+
8+
#include "selective_search_segmentation_strategy.h"
9+
10+
namespace segmentation {
11+
/** @brief Fill-based strategy for the selective search segmentation algorithm
12+
The class is implemented from the algorithm described in @cite uijlings2013selective.
13+
*/
14+
class SelectiveSearchSegmentationStrategyFill : public SelectiveSearchSegmentationStrategy {
15+
};
16+
17+
/** @brief Create a new fill-based strategy */
18+
std::shared_ptr<SelectiveSearchSegmentationStrategyFill> createSelectiveSearchSegmentationStrategyFill();
19+
20+
}
21+
22+
#endif //CPLUPLUS_SELECTIVE_SEARCH_SEGMENTATION_STRATEGY_FILL_H
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
//
2+
// Created by zj on 2020/2/14.
3+
//
4+
5+
#ifndef CPLUPLUS_SELECTIVE_SEARCH_SEGMENTATION_STRATEGY_FILL_IMPL_H
6+
#define CPLUPLUS_SELECTIVE_SEARCH_SEGMENTATION_STRATEGY_FILL_IMPL_H
7+
8+
#include "selective_search_segmentation_strategy_fill.h"
9+
10+
namespace segmentation {
11+
class SelectiveSearchSegmentationStrategyFillImpl final : public SelectiveSearchSegmentationStrategyFill {
12+
public:
13+
SelectiveSearchSegmentationStrategyFillImpl() {
14+
name_ = "SelectiveSearchSegmentationStrategyFill";
15+
}
16+
17+
virtual void
18+
setImage(cv::InputArray img, cv::InputArray regions, cv::InputArray sizes, int image_id = -1) override;
19+
20+
virtual float get(int r1, int r2) override;
21+
22+
virtual void merge(int r1, int r2) override;
23+
24+
private:
25+
std::string name_;
26+
27+
cv::Mat sizes;
28+
int size_image;
29+
std::vector<cv::Rect> bounding_rects;
30+
};
31+
}
32+
33+
#endif //CPLUPLUS_SELECTIVE_SEARCH_SEGMENTATION_STRATEGY_FILL_IMPL_H

0 commit comments

Comments
 (0)