//\r
//M*/\r
\r
-#include <time.h>\r
-#include <vector>\r
#include "precomp.hpp"\r
\r
#if !defined(HAVE_CUDA)\r
\r
-namespace cv\r
-{\r
-namespace gpu\r
-{\r
-\r
-void meanShiftSegmentation(const GpuMat&, Mat&, int, int, int, TermCriteria) { throw_nogpu(); }\r
-\r
-} // namespace gpu\r
-} // namespace cv\r
-\r
-#else\r
-\r
-//#define _MSSEGMENTATION_DBG\r
+void cv::gpu::meanShiftSegmentation(const GpuMat&, Mat&, int, int, int, TermCriteria) { throw_nogpu(); }\r
\r
-#ifdef _MSSEGMENTATION_DBG\r
-#include <iostream>\r
-#define LOG(s) std::cout << (s) << std::endl\r
-#define LOG2(s1, s2) std::cout << (s1) << (s2) << std::endl\r
-#define DBG(code) code\r
#else\r
-#define LOG(s1)\r
-#define LOG2(s1, s2)\r
-#define DBG(code)\r
-#endif\r
-\r
-#define PIX(y, x) ((y) * ncols + (x))\r
\r
using namespace std;\r
\r
{\r
public:\r
DjSets(int n);\r
- ~DjSets();\r
- int find(int elem) const;\r
+ int find(int elem);\r
int merge(int set1, int set2);\r
\r
- int* parent;\r
- int* rank;\r
- int* size;\r
+ vector<int> parent;\r
+ vector<int> rank;\r
+ vector<int> size;\r
private:\r
DjSets(const DjSets&) {}\r
DjSets operator =(const DjSets&) {}\r
typedef GraphEdge<T> Edge;\r
\r
Graph(int numv, int nume_max);\r
- ~Graph();\r
\r
void addEdge(int from, int to, const T& val=T());\r
\r
- int* start;\r
- Edge* edges;\r
+ vector<int> start;\r
+ vector<Edge> edges;\r
\r
int numv;\r
int nume_max;\r
SegmLink() {}\r
SegmLink(int from, int to, const SegmLinkVal& val) \r
: from(from), to(to), val(val) {}\r
+ bool operator <(const SegmLink& other) const \r
+ {\r
+ return val < other.val;\r
+ }\r
int from;\r
int to;\r
SegmLinkVal val;\r
};\r
\r
-\r
-struct SegmLinkCmp\r
-{\r
- bool operator ()(const SegmLink& lhs, const SegmLink& rhs) const\r
- {\r
- return lhs.val < rhs.val;\r
- }\r
-};\r
-\r
//\r
// Implementation\r
//\r
\r
-DjSets::DjSets(int n)\r
+DjSets::DjSets(int n) : parent(n), rank(n, 0), size(n, 1)\r
{\r
- parent = new int[n];\r
- rank = new int[n];\r
- size = new int[n]; \r
for (int i = 0; i < n; ++i)\r
- {\r
parent[i] = i;\r
- rank[i] = 0;\r
- size[i] = 1;\r
- }\r
}\r
\r
\r
-DjSets::~DjSets()\r
-{\r
- delete[] parent;\r
- delete[] rank;\r
- delete[] size;\r
-}\r
-\r
-\r
-inline int DjSets::find(int elem) const\r
+inline int DjSets::find(int elem)\r
{\r
int set = elem;\r
while (set != parent[set])\r
\r
\r
template <typename T>\r
-Graph<T>::Graph(int numv, int nume_max)\r
+Graph<T>::Graph(int numv, int nume_max) : start(numv, -1), edges(nume_max)\r
{\r
this->numv = numv;\r
this->nume_max = nume_max;\r
- start = new int[numv];\r
- for (int i = 0; i < numv; ++i)\r
- start[i] = -1;\r
- edges = new Edge[nume_max];\r
nume = 0;\r
}\r
\r
\r
template <typename T>\r
-Graph<T>::~Graph()\r
+inline void Graph<T>::addEdge(int from, int to, const T& val)\r
{\r
- delete[] start;\r
- delete[] edges;\r
+ edges[nume] = Edge(to, start[from], val);\r
+ start[from] = nume;\r
+ nume++;\r
}\r
\r
\r
-template <typename T>\r
-inline void Graph<T>::addEdge(int from, int to, const T& val)\r
+inline int pix(int y, int x, int ncols) \r
{\r
- Edge* edge = edges + nume;\r
- new (edge) SegmLink(to, start[from], val);\r
- start[from] = nume;\r
- nume++;\r
+ return y * ncols + x;\r
}\r
\r
\r
-inline int sqr(int x)\r
+inline int sqr(int x) \r
{\r
return x * x;\r
}\r
\r
-} // anonymous namespace\r
\r
-\r
-namespace cv\r
+inline int dist2(const cv::Vec4b& lhs, const cv::Vec4b& rhs) \r
{\r
-namespace gpu\r
+ return sqr(lhs[0] - rhs[0]) + sqr(lhs[1] - rhs[1]) + sqr(lhs[2] - rhs[2]);\r
+}\r
+\r
+\r
+inline int dist2(const cv::Vec2s& lhs, const cv::Vec2s& rhs) \r
{\r
+ return sqr(lhs[0] - rhs[0]) + sqr(lhs[1] - rhs[1]);\r
+}\r
+\r
+} // anonymous namespace\r
+\r
\r
-void meanShiftSegmentation(const GpuMat& src, Mat& dst, int sp, int sr, int minsize, TermCriteria criteria)\r
+void cv::gpu::meanShiftSegmentation(const GpuMat& src, Mat& dst, int sp, int sr, int minsize, TermCriteria criteria)\r
{\r
CV_Assert(src.type() == CV_8UC4);\r
const int nrows = src.rows;\r
const int hr = sr;\r
const int hsp = sp;\r
\r
- DBG(clock_t start = clock());\r
-\r
// Perform mean shift procedure and obtain region and spatial maps\r
GpuMat h_rmap, h_spmap;\r
meanShiftProc(src, h_rmap, h_spmap, sp, sr, criteria);\r
Mat rmap = h_rmap;\r
Mat spmap = h_spmap;\r
\r
- LOG2("meanshift:", clock() - start);\r
- DBG(start = clock());\r
-\r
Graph<SegmLinkVal> g(nrows * ncols, 4 * (nrows - 1) * (ncols - 1)\r
+ (nrows - 1) + (ncols - 1));\r
\r
- LOG2("ragalloc:", clock() - start);\r
- DBG(start = clock());\r
-\r
// Make region adjacent graph from image\r
- // TODO: SSE?\r
Vec4b r1;\r
Vec4b r2[4];\r
- Point_<short> sp1;\r
- Point_<short> sp2[4];\r
+ Vec2s sp1;\r
+ Vec2s sp2[4];\r
int dr[4];\r
int dsp[4];\r
for (int y = 0; y < nrows - 1; ++y)\r
{\r
Vec4b* ry = rmap.ptr<Vec4b>(y);\r
Vec4b* ryp = rmap.ptr<Vec4b>(y + 1);\r
- Point_<short>* spy = spmap.ptr<Point_<short> >(y);\r
- Point_<short>* spyp = spmap.ptr<Point_<short> >(y + 1);\r
+ Vec2s* spy = spmap.ptr<Vec2s>(y);\r
+ Vec2s* spyp = spmap.ptr<Vec2s>(y + 1);\r
for (int x = 0; x < ncols - 1; ++x)\r
{\r
r1 = ry[x];\r
sp2[2] = spyp[x + 1];\r
sp2[3] = spyp[x];\r
\r
- dr[0] = sqr(r1[0] - r2[0][0]) + sqr(r1[1] - r2[0][1]) + sqr(r1[2] - r2[0][2]);\r
- dr[1] = sqr(r1[0] - r2[1][0]) + sqr(r1[1] - r2[1][1]) + sqr(r1[2] - r2[1][2]);\r
- dr[2] = sqr(r1[0] - r2[2][0]) + sqr(r1[1] - r2[2][1]) + sqr(r1[2] - r2[2][2]);\r
- dsp[0] = sqr(sp1.x - sp2[0].x) + sqr(sp1.y - sp2[0].y);\r
- dsp[1] = sqr(sp1.x - sp2[1].x) + sqr(sp1.y - sp2[1].y);\r
- dsp[2] = sqr(sp1.x - sp2[2].x) + sqr(sp1.y - sp2[2].y); \r
+ dr[0] = dist2(r1, r2[0]);\r
+ dr[1] = dist2(r1, r2[1]);\r
+ dr[2] = dist2(r1, r2[2]);\r
+ dsp[0] = dist2(sp1, sp2[0]);\r
+ dsp[1] = dist2(sp1, sp2[1]);\r
+ dsp[2] = dist2(sp1, sp2[2]);\r
\r
r1 = ry[x + 1];\r
sp1 = spy[x + 1];\r
\r
- dr[3] = sqr(r1[0] - r2[3][0]) + sqr(r1[1] - r2[3][1]) + sqr(r1[2] - r2[3][2]);\r
- dsp[3] = sqr(sp1.x - sp2[3].x) + sqr(sp1.y - sp2[3].y); \r
+ dr[3] = dist2(r1, r2[3]);\r
+ dsp[3] = dist2(sp1, sp2[3]);\r
\r
- g.addEdge(PIX(y, x), PIX(y, x + 1), SegmLinkVal(dr[0], dsp[0]));\r
- g.addEdge(PIX(y, x), PIX(y + 1, x), SegmLinkVal(dr[1], dsp[1]));\r
- g.addEdge(PIX(y, x), PIX(y + 1, x + 1), SegmLinkVal(dr[2], dsp[2]));\r
- g.addEdge(PIX(y, x + 1), PIX(y, x + 1), SegmLinkVal(dr[3], dsp[3]));\r
+ g.addEdge(pix(y, x, ncols), pix(y, x + 1, ncols), SegmLinkVal(dr[0], dsp[0]));\r
+ g.addEdge(pix(y, x, ncols), pix(y + 1, x, ncols), SegmLinkVal(dr[1], dsp[1]));\r
+ g.addEdge(pix(y, x, ncols), pix(y + 1, x + 1, ncols), SegmLinkVal(dr[2], dsp[2]));\r
+ g.addEdge(pix(y, x + 1, ncols), pix(y, x + 1, ncols), SegmLinkVal(dr[3], dsp[3]));\r
}\r
}\r
for (int y = 0; y < nrows - 1; ++y)\r
{\r
r1 = rmap.at<Vec4b>(y, ncols - 1);\r
r2[0] = rmap.at<Vec4b>(y + 1, ncols - 1);\r
- sp1 = spmap.at<Point_<short> >(y, ncols - 1);\r
- sp2[0] = spmap.at<Point_<short> >(y + 1, ncols - 1);\r
- dr[0] = sqr(r1[0] - r2[0][0]) + sqr(r1[1] - r2[0][1]) + sqr(r1[2] - r2[0][2]);\r
- dsp[0] = sqr(sp1.x - sp2[0].x) + sqr(sp1.y - sp2[0].y); \r
- g.addEdge(PIX(y, ncols - 1), PIX(y + 1, ncols - 1), SegmLinkVal(dr[0], dsp[0]));\r
+ sp1 = spmap.at<Vec2s>(y, ncols - 1);\r
+ sp2[0] = spmap.at<Vec2s>(y + 1, ncols - 1);\r
+ dr[0] = dist2(r1, r2[0]);\r
+ dsp[0] = dist2(sp1, sp2[0]);\r
+ g.addEdge(pix(y, ncols - 1, ncols), pix(y + 1, ncols - 1, ncols), SegmLinkVal(dr[0], dsp[0]));\r
}\r
for (int x = 0; x < ncols - 1; ++x)\r
{\r
r1 = rmap.at<Vec4b>(nrows - 1, x);\r
r2[0] = rmap.at<Vec4b>(nrows - 1, x + 1);\r
- sp1 = spmap.at<Point_<short> >(nrows - 1, x);\r
- sp2[0] = spmap.at<Point_<short> >(nrows - 1, x + 1);\r
- dr[0] = sqr(r1[0] - r2[0][0]) + sqr(r1[1] - r2[0][1]) + sqr(r1[2] - r2[0][2]);\r
- dsp[0] = sqr(sp1.x - sp2[0].x) + sqr(sp1.y - sp2[0].y); \r
- g.addEdge(PIX(nrows - 1, x), PIX(nrows - 1, x + 1), SegmLinkVal(dr[0], dsp[0]));\r
+ sp1 = spmap.at<Vec2s>(nrows - 1, x);\r
+ sp2[0] = spmap.at<Vec2s>(nrows - 1, x + 1);\r
+ dr[0] = dist2(r1, r2[0]);\r
+ dsp[0] = dist2(sp1, sp2[0]);\r
+ g.addEdge(pix(nrows - 1, x, ncols), pix(nrows - 1, x + 1, ncols), SegmLinkVal(dr[0], dsp[0]));\r
}\r
\r
- LOG2("raginit:", clock() - start);\r
- DBG(start = clock());\r
-\r
DjSets comps(g.numv);\r
\r
- LOG2("djsetinit:", clock() - start);\r
- DBG(start = clock());\r
-\r
// Find adjacent components\r
for (int v = 0; v < g.numv; ++v)\r
{\r
}\r
}\r
\r
- LOG2("findadjacent:", clock() - start);\r
- DBG(start = clock());\r
-\r
vector<SegmLink> edges;\r
edges.reserve(g.numv);\r
\r
- LOG2("initedges:", clock() - start);\r
- DBG(start = clock());\r
-\r
// Prepare edges connecting differnet components\r
for (int v = 0; v < g.numv; ++v)\r
{\r
}\r
}\r
\r
- LOG2("prepareforsort:", clock() - start);\r
- DBG(start = clock());\r
-\r
// Sort all graph's edges connecting differnet components (in asceding order)\r
- sort(edges.begin(), edges.end(), SegmLinkCmp());\r
-\r
- LOG2("sortedges:", clock() - start);\r
- DBG(start = clock());\r
+ sort(edges.begin(), edges.end());\r
\r
// Exclude small components (starting from the nearest couple)\r
for (size_t i = 0; i < edges.size(); ++i)\r
comps.merge(c1, c2);\r
}\r
\r
- LOG2("excludesmall:", clock() - start);\r
- DBG(start = clock());\r
-\r
// Compute sum of the pixel's colors which are in the same segment\r
Mat h_src = src;\r
vector<Vec4i> sumcols(nrows * ncols, Vec4i(0, 0, 0, 0));\r
Vec4b* h_srcy = h_src.ptr<Vec4b>(y);\r
for (int x = 0; x < ncols; ++x)\r
{\r
- int parent = comps.find(PIX(y, x));\r
+ int parent = comps.find(pix(y, x, ncols));\r
Vec4b col = h_srcy[x];\r
Vec4i& sumcol = sumcols[parent];\r
sumcol[0] += col[0];\r
}\r
}\r
\r
- LOG2("computesum:", clock() - start);\r
- DBG(start = clock());\r
-\r
// Create final image, color of each segment is the average color of its pixels\r
dst.create(src.size(), src.type());\r
\r
Vec4b* dsty = dst.ptr<Vec4b>(y);\r
for (int x = 0; x < ncols; ++x)\r
{\r
- int parent = comps.find(PIX(y, x));\r
+ int parent = comps.find(pix(y, x, ncols));\r
const Vec4i& sumcol = sumcols[parent];\r
Vec4b& dstcol = dsty[x];\r
dstcol[0] = static_cast<uchar>(sumcol[0] / comps.size[parent]);\r
dstcol[2] = static_cast<uchar>(sumcol[2] / comps.size[parent]);\r
}\r
}\r
-\r
- LOG2("createfinal:", clock() - start);\r
}\r
\r
-} // namespace gpu\r
-} // namespace cv\r
-\r
#endif // #if !defined (HAVE_CUDA)
\ No newline at end of file