moved image labeling to gpu module
This commit is contained in:
@@ -55,6 +55,22 @@
|
||||
|
||||
namespace cv { namespace gpu {
|
||||
|
||||
//!performs labeling via graph cuts of a 2D regular 4-connected graph.
|
||||
CV_EXPORTS void graphcut(GpuMat& terminals, GpuMat& leftTransp, GpuMat& rightTransp, GpuMat& top, GpuMat& bottom, GpuMat& labels,
|
||||
GpuMat& buf, Stream& stream = Stream::Null());
|
||||
|
||||
//!performs labeling via graph cuts of a 2D regular 8-connected graph.
|
||||
CV_EXPORTS void graphcut(GpuMat& terminals, GpuMat& leftTransp, GpuMat& rightTransp, GpuMat& top, GpuMat& topLeft, GpuMat& topRight,
|
||||
GpuMat& bottom, GpuMat& bottomLeft, GpuMat& bottomRight,
|
||||
GpuMat& labels,
|
||||
GpuMat& buf, Stream& stream = Stream::Null());
|
||||
|
||||
//! compute mask for Generalized Flood fill componetns labeling.
|
||||
CV_EXPORTS void connectivityMask(const GpuMat& image, GpuMat& mask, const cv::Scalar& lo, const cv::Scalar& hi, Stream& stream = Stream::Null());
|
||||
|
||||
//! performs connected componnents labeling.
|
||||
CV_EXPORTS void labelComponents(const GpuMat& mask, GpuMat& components, int flags = 0, Stream& stream = Stream::Null());
|
||||
|
||||
//! removes points (CV_32FC2, single row matrix) with zero mask value
|
||||
CV_EXPORTS void compactPoints(GpuMat &points0, GpuMat &points1, const GpuMat &mask);
|
||||
|
||||
|
195
modules/gpu/perf/perf_labeling.cpp
Normal file
195
modules/gpu/perf/perf_labeling.cpp
Normal file
@@ -0,0 +1,195 @@
|
||||
/*M///////////////////////////////////////////////////////////////////////////////////////
|
||||
//
|
||||
// IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
|
||||
//
|
||||
// By downloading, copying, installing or using the software you agree to this license.
|
||||
// If you do not agree to this license, do not download, install,
|
||||
// copy or use the software.
|
||||
//
|
||||
//
|
||||
// License Agreement
|
||||
// For Open Source Computer Vision Library
|
||||
//
|
||||
// Copyright (C) 2000-2008, Intel Corporation, all rights reserved.
|
||||
// Copyright (C) 2009, Willow Garage Inc., all rights reserved.
|
||||
// Third party copyrights are property of their respective owners.
|
||||
//
|
||||
// Redistribution and use in source and binary forms, with or without modification,
|
||||
// are permitted provided that the following conditions are met:
|
||||
//
|
||||
// * Redistribution's of source code must retain the above copyright notice,
|
||||
// this list of conditions and the following disclaimer.
|
||||
//
|
||||
// * Redistribution's in binary form must reproduce the above copyright notice,
|
||||
// this list of conditions and the following disclaimer in the documentation
|
||||
// and/or other materials provided with the distribution.
|
||||
//
|
||||
// * The name of the copyright holders may not be used to endorse or promote products
|
||||
// derived from this software without specific prior written permission.
|
||||
//
|
||||
// This software is provided by the copyright holders and contributors "as is" and
|
||||
// any express or implied warranties, including, but not limited to, the implied
|
||||
// warranties of merchantability and fitness for a particular purpose are disclaimed.
|
||||
// In no event shall the Intel Corporation or contributors be liable for any direct,
|
||||
// indirect, incidental, special, exemplary, or consequential damages
|
||||
// (including, but not limited to, procurement of substitute goods or services;
|
||||
// loss of use, data, or profits; or business interruption) however caused
|
||||
// and on any theory of liability, whether in contract, strict liability,
|
||||
// or tort (including negligence or otherwise) arising in any way out of
|
||||
// the use of this software, even if advised of the possibility of such damage.
|
||||
//
|
||||
//M*/
|
||||
|
||||
#include "perf_precomp.hpp"
|
||||
|
||||
using namespace std;
|
||||
using namespace testing;
|
||||
using namespace perf;
|
||||
|
||||
DEF_PARAM_TEST_1(Image, string);
|
||||
|
||||
struct GreedyLabeling
|
||||
{
|
||||
struct dot
|
||||
{
|
||||
int x;
|
||||
int y;
|
||||
|
||||
static dot make(int i, int j)
|
||||
{
|
||||
dot d; d.x = i; d.y = j;
|
||||
return d;
|
||||
}
|
||||
};
|
||||
|
||||
struct InInterval
|
||||
{
|
||||
InInterval(const int& _lo, const int& _hi) : lo(-_lo), hi(_hi) {}
|
||||
const int lo, hi;
|
||||
|
||||
bool operator() (const unsigned char a, const unsigned char b) const
|
||||
{
|
||||
int d = a - b;
|
||||
return lo <= d && d <= hi;
|
||||
}
|
||||
|
||||
private:
|
||||
InInterval& operator=(const InInterval&);
|
||||
|
||||
|
||||
};
|
||||
|
||||
GreedyLabeling(cv::Mat img)
|
||||
: image(img), _labels(image.size(), CV_32SC1, cv::Scalar::all(-1)) {stack = new dot[image.cols * image.rows];}
|
||||
|
||||
~GreedyLabeling(){delete[] stack;}
|
||||
|
||||
void operator() (cv::Mat labels) const
|
||||
{
|
||||
labels.setTo(cv::Scalar::all(-1));
|
||||
InInterval inInt(0, 2);
|
||||
int cc = -1;
|
||||
|
||||
int* dist_labels = (int*)labels.data;
|
||||
int pitch = static_cast<int>(labels.step1());
|
||||
|
||||
unsigned char* source = (unsigned char*)image.data;
|
||||
int width = image.cols;
|
||||
int height = image.rows;
|
||||
|
||||
for (int j = 0; j < image.rows; ++j)
|
||||
for (int i = 0; i < image.cols; ++i)
|
||||
{
|
||||
if (dist_labels[j * pitch + i] != -1) continue;
|
||||
|
||||
dot* top = stack;
|
||||
dot p = dot::make(i, j);
|
||||
cc++;
|
||||
|
||||
dist_labels[j * pitch + i] = cc;
|
||||
|
||||
while (top >= stack)
|
||||
{
|
||||
int* dl = &dist_labels[p.y * pitch + p.x];
|
||||
unsigned char* sp = &source[p.y * image.step1() + p.x];
|
||||
|
||||
dl[0] = cc;
|
||||
|
||||
//right
|
||||
if( p.x < (width - 1) && dl[ +1] == -1 && inInt(sp[0], sp[+1]))
|
||||
*top++ = dot::make(p.x + 1, p.y);
|
||||
|
||||
//left
|
||||
if( p.x > 0 && dl[-1] == -1 && inInt(sp[0], sp[-1]))
|
||||
*top++ = dot::make(p.x - 1, p.y);
|
||||
|
||||
//bottom
|
||||
if( p.y < (height - 1) && dl[+pitch] == -1 && inInt(sp[0], sp[+image.step1()]))
|
||||
*top++ = dot::make(p.x, p.y + 1);
|
||||
|
||||
//top
|
||||
if( p.y > 0 && dl[-pitch] == -1 && inInt(sp[0], sp[-static_cast<int>(image.step1())]))
|
||||
*top++ = dot::make(p.x, p.y - 1);
|
||||
|
||||
p = *--top;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
cv::Mat image;
|
||||
cv::Mat _labels;
|
||||
dot* stack;
|
||||
};
|
||||
|
||||
PERF_TEST_P(Image, DISABLED_Labeling_ConnectivityMask,
|
||||
Values<string>("gpu/labeling/aloe-disp.png"))
|
||||
{
|
||||
declare.time(1.0);
|
||||
|
||||
const cv::Mat image = readImage(GetParam(), cv::IMREAD_GRAYSCALE);
|
||||
ASSERT_FALSE(image.empty());
|
||||
|
||||
if (PERF_RUN_GPU())
|
||||
{
|
||||
cv::gpu::GpuMat d_image(image);
|
||||
cv::gpu::GpuMat mask;
|
||||
|
||||
TEST_CYCLE() cv::gpu::connectivityMask(d_image, mask, cv::Scalar::all(0), cv::Scalar::all(2));
|
||||
|
||||
GPU_SANITY_CHECK(mask);
|
||||
}
|
||||
else
|
||||
{
|
||||
FAIL_NO_CPU();
|
||||
}
|
||||
}
|
||||
|
||||
PERF_TEST_P(Image, DISABLED_Labeling_ConnectedComponents,
|
||||
Values<string>("gpu/labeling/aloe-disp.png"))
|
||||
{
|
||||
declare.time(1.0);
|
||||
|
||||
const cv::Mat image = readImage(GetParam(), cv::IMREAD_GRAYSCALE);
|
||||
ASSERT_FALSE(image.empty());
|
||||
|
||||
if (PERF_RUN_GPU())
|
||||
{
|
||||
cv::gpu::GpuMat d_mask;
|
||||
cv::gpu::connectivityMask(cv::gpu::GpuMat(image), d_mask, cv::Scalar::all(0), cv::Scalar::all(2));
|
||||
|
||||
cv::gpu::GpuMat components;
|
||||
|
||||
TEST_CYCLE() cv::gpu::labelComponents(d_mask, components);
|
||||
|
||||
GPU_SANITY_CHECK(components);
|
||||
}
|
||||
else
|
||||
{
|
||||
GreedyLabeling host(image);
|
||||
|
||||
TEST_CYCLE() host(host._labels);
|
||||
|
||||
cv::Mat components = host._labels;
|
||||
CPU_SANITY_CHECK(components);
|
||||
}
|
||||
}
|
534
modules/gpu/src/cuda/ccomponetns.cu
Normal file
534
modules/gpu/src/cuda/ccomponetns.cu
Normal file
@@ -0,0 +1,534 @@
|
||||
/*M///////////////////////////////////////////////////////////////////////////////////////
|
||||
//
|
||||
// IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
|
||||
//
|
||||
// By downloading, copying, installing or using the software you agree to this license.
|
||||
// If you do not agree to this license, do not download, install,
|
||||
// copy or use the software.
|
||||
//
|
||||
//
|
||||
// License Agreement
|
||||
// For Open Source Computer Vision Library
|
||||
//
|
||||
// Copyright (C) 2000-2008, Intel Corporation, all rights reserved.
|
||||
// Copyright (C) 2009, Willow Garage Inc., all rights reserved.
|
||||
// Third party copyrights are property of their respective owners.
|
||||
//
|
||||
// Redistribution and use in source and binary forms, with or without modification,
|
||||
// are permitted provided that the following conditions are met:
|
||||
//
|
||||
// * Redistribution's of source code must retain the above copyright notice,
|
||||
// this list of conditions and the following disclaimer.
|
||||
//
|
||||
// * Redistribution's in binary form must reproduce the above copyright notice,
|
||||
// this list of conditions and the following disclaimer in the documentation
|
||||
// and/or other materials provided with the distribution.
|
||||
//
|
||||
// * The name of the copyright holders may not be used to endorse or promote products
|
||||
// derived from this software without specific prior written permission.
|
||||
//
|
||||
// This software is provided by the copyright holders and contributors "as is" and
|
||||
// any express or implied warranties, including, but not limited to, the implied
|
||||
// warranties of merchantability and fitness for a particular purpose are disclaimed.
|
||||
// In no event shall the Intel Corporation or contributors be liable for any direct,
|
||||
// indirect, incidental, special, exemplary, or consequential damages
|
||||
// (including, but not limited to, procurement of substitute goods or services;
|
||||
// loss of use, data, or profits; or business interruption) however caused
|
||||
// and on any theory of liability, whether in contract, strict liability,
|
||||
// or tort (including negligence or otherwise) arising in any way out of
|
||||
// the use of this software, even if advised of the possibility of such damage.
|
||||
//
|
||||
//M*/
|
||||
|
||||
#if !defined CUDA_DISABLER
|
||||
|
||||
#include <opencv2/core/cuda/common.hpp>
|
||||
#include <opencv2/core/cuda/vec_traits.hpp>
|
||||
#include <opencv2/core/cuda/vec_math.hpp>
|
||||
#include <opencv2/core/cuda/emulation.hpp>
|
||||
|
||||
#include <iostream>
|
||||
#include <stdio.h>
|
||||
|
||||
namespace cv { namespace gpu { namespace cudev
|
||||
{
|
||||
namespace ccl
|
||||
{
|
||||
enum
|
||||
{
|
||||
WARP_SIZE = 32,
|
||||
WARP_LOG = 5,
|
||||
|
||||
CTA_SIZE_X = 32,
|
||||
CTA_SIZE_Y = 8,
|
||||
|
||||
STA_SIZE_MERGE_Y = 4,
|
||||
STA_SIZE_MERGE_X = 32,
|
||||
|
||||
TPB_X = 1,
|
||||
TPB_Y = 4,
|
||||
|
||||
TILE_COLS = CTA_SIZE_X * TPB_X,
|
||||
TILE_ROWS = CTA_SIZE_Y * TPB_Y
|
||||
};
|
||||
|
||||
template<typename T> struct IntervalsTraits
|
||||
{
|
||||
typedef T elem_type;
|
||||
};
|
||||
|
||||
template<> struct IntervalsTraits<unsigned char>
|
||||
{
|
||||
typedef int dist_type;
|
||||
enum {ch = 1};
|
||||
};
|
||||
|
||||
template<> struct IntervalsTraits<uchar3>
|
||||
{
|
||||
typedef int3 dist_type;
|
||||
enum {ch = 3};
|
||||
};
|
||||
|
||||
template<> struct IntervalsTraits<uchar4>
|
||||
{
|
||||
typedef int4 dist_type;
|
||||
enum {ch = 4};
|
||||
};
|
||||
|
||||
template<> struct IntervalsTraits<unsigned short>
|
||||
{
|
||||
typedef int dist_type;
|
||||
enum {ch = 1};
|
||||
};
|
||||
|
||||
template<> struct IntervalsTraits<ushort3>
|
||||
{
|
||||
typedef int3 dist_type;
|
||||
enum {ch = 3};
|
||||
};
|
||||
|
||||
template<> struct IntervalsTraits<ushort4>
|
||||
{
|
||||
typedef int4 dist_type;
|
||||
enum {ch = 4};
|
||||
};
|
||||
|
||||
template<> struct IntervalsTraits<float>
|
||||
{
|
||||
typedef float dist_type;
|
||||
enum {ch = 1};
|
||||
};
|
||||
|
||||
template<> struct IntervalsTraits<int>
|
||||
{
|
||||
typedef int dist_type;
|
||||
enum {ch = 1};
|
||||
};
|
||||
|
||||
typedef unsigned char component;
|
||||
enum Edges { UP = 1, DOWN = 2, LEFT = 4, RIGHT = 8, EMPTY = 0xF0 };
|
||||
|
||||
template<typename T, int CH> struct InInterval {};
|
||||
|
||||
template<typename T> struct InInterval<T, 1>
|
||||
{
|
||||
typedef typename VecTraits<T>::elem_type E;
|
||||
__host__ __device__ __forceinline__ InInterval(const float4& _lo, const float4& _hi) : lo((E)(-_lo.x)), hi((E)_hi.x) {};
|
||||
T lo, hi;
|
||||
|
||||
template<typename I> __device__ __forceinline__ bool operator() (const I& a, const I& b) const
|
||||
{
|
||||
I d = a - b;
|
||||
return lo <= d && d <= hi;
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
template<typename T> struct InInterval<T, 3>
|
||||
{
|
||||
typedef typename VecTraits<T>::elem_type E;
|
||||
__host__ __device__ __forceinline__ InInterval(const float4& _lo, const float4& _hi)
|
||||
: lo (VecTraits<T>::make((E)(-_lo.x), (E)(-_lo.y), (E)(-_lo.z))), hi (VecTraits<T>::make((E)_hi.x, (E)_hi.y, (E)_hi.z)){};
|
||||
T lo, hi;
|
||||
|
||||
template<typename I> __device__ __forceinline__ bool operator() (const I& a, const I& b) const
|
||||
{
|
||||
I d = a - b;
|
||||
return lo.x <= d.x && d.x <= hi.x &&
|
||||
lo.y <= d.y && d.y <= hi.y &&
|
||||
lo.z <= d.z && d.z <= hi.z;
|
||||
}
|
||||
};
|
||||
|
||||
template<typename T> struct InInterval<T, 4>
|
||||
{
|
||||
typedef typename VecTraits<T>::elem_type E;
|
||||
__host__ __device__ __forceinline__ InInterval(const float4& _lo, const float4& _hi)
|
||||
: lo (VecTraits<T>::make((E)(-_lo.x), (E)(-_lo.y), (E)(-_lo.z), (E)(-_lo.w))), hi (VecTraits<T>::make((E)_hi.x, (E)_hi.y, (E)_hi.z, (E)_hi.w)){};
|
||||
T lo, hi;
|
||||
|
||||
template<typename I> __device__ __forceinline__ bool operator() (const I& a, const I& b) const
|
||||
{
|
||||
I d = a - b;
|
||||
return lo.x <= d.x && d.x <= hi.x &&
|
||||
lo.y <= d.y && d.y <= hi.y &&
|
||||
lo.z <= d.z && d.z <= hi.z &&
|
||||
lo.w <= d.w && d.w <= hi.w;
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
template<typename T, typename F>
|
||||
__global__ void computeConnectivity(const PtrStepSz<T> image, PtrStepSzb components, F connected)
|
||||
{
|
||||
int x = threadIdx.x + blockIdx.x * blockDim.x;
|
||||
int y = threadIdx.y + blockIdx.y * blockDim.y;
|
||||
|
||||
if (x >= image.cols || y >= image.rows) return;
|
||||
|
||||
T intensity = image(y, x);
|
||||
component c = 0;
|
||||
|
||||
if ( x > 0 && connected(intensity, image(y, x - 1)))
|
||||
c |= LEFT;
|
||||
|
||||
if ( y > 0 && connected(intensity, image(y - 1, x)))
|
||||
c |= UP;
|
||||
|
||||
if ( x + 1 < image.cols && connected(intensity, image(y, x + 1)))
|
||||
c |= RIGHT;
|
||||
|
||||
if ( y + 1 < image.rows && connected(intensity, image(y + 1, x)))
|
||||
c |= DOWN;
|
||||
|
||||
components(y, x) = c;
|
||||
}
|
||||
|
||||
template< typename T>
|
||||
void computeEdges(const PtrStepSzb& image, PtrStepSzb edges, const float4& lo, const float4& hi, cudaStream_t stream)
|
||||
{
|
||||
dim3 block(CTA_SIZE_X, CTA_SIZE_Y);
|
||||
dim3 grid(divUp(image.cols, block.x), divUp(image.rows, block.y));
|
||||
|
||||
typedef InInterval<typename IntervalsTraits<T>::dist_type, IntervalsTraits<T>::ch> Int_t;
|
||||
|
||||
Int_t inInt(lo, hi);
|
||||
computeConnectivity<T, Int_t><<<grid, block, 0, stream>>>(static_cast<const PtrStepSz<T> >(image), edges, inInt);
|
||||
|
||||
cudaSafeCall( cudaGetLastError() );
|
||||
if (stream == 0)
|
||||
cudaSafeCall( cudaDeviceSynchronize() );
|
||||
}
|
||||
|
||||
template void computeEdges<uchar> (const PtrStepSzb& image, PtrStepSzb edges, const float4& lo, const float4& hi, cudaStream_t stream);
|
||||
template void computeEdges<uchar3> (const PtrStepSzb& image, PtrStepSzb edges, const float4& lo, const float4& hi, cudaStream_t stream);
|
||||
template void computeEdges<uchar4> (const PtrStepSzb& image, PtrStepSzb edges, const float4& lo, const float4& hi, cudaStream_t stream);
|
||||
template void computeEdges<ushort> (const PtrStepSzb& image, PtrStepSzb edges, const float4& lo, const float4& hi, cudaStream_t stream);
|
||||
template void computeEdges<ushort3>(const PtrStepSzb& image, PtrStepSzb edges, const float4& lo, const float4& hi, cudaStream_t stream);
|
||||
template void computeEdges<ushort4>(const PtrStepSzb& image, PtrStepSzb edges, const float4& lo, const float4& hi, cudaStream_t stream);
|
||||
template void computeEdges<int> (const PtrStepSzb& image, PtrStepSzb edges, const float4& lo, const float4& hi, cudaStream_t stream);
|
||||
template void computeEdges<float> (const PtrStepSzb& image, PtrStepSzb edges, const float4& lo, const float4& hi, cudaStream_t stream);
|
||||
|
||||
__global__ void lableTiles(const PtrStepSzb edges, PtrStepSzi comps)
|
||||
{
|
||||
int x = threadIdx.x + blockIdx.x * TILE_COLS;
|
||||
int y = threadIdx.y + blockIdx.y * TILE_ROWS;
|
||||
|
||||
if (x >= edges.cols || y >= edges.rows) return;
|
||||
|
||||
//currently x is 1
|
||||
int bounds = ((y + TPB_Y) < edges.rows);
|
||||
|
||||
__shared__ int labelsTile[TILE_ROWS][TILE_COLS];
|
||||
__shared__ int edgesTile[TILE_ROWS][TILE_COLS];
|
||||
|
||||
int new_labels[TPB_Y][TPB_X];
|
||||
int old_labels[TPB_Y][TPB_X];
|
||||
|
||||
#pragma unroll
|
||||
for (int i = 0; i < TPB_Y; ++i)
|
||||
#pragma unroll
|
||||
for (int j = 0; j < TPB_X; ++j)
|
||||
{
|
||||
int yloc = threadIdx.y + CTA_SIZE_Y * i;
|
||||
int xloc = threadIdx.x + CTA_SIZE_X * j;
|
||||
component c = edges(bounds * (y + CTA_SIZE_Y * i), x + CTA_SIZE_X * j);
|
||||
|
||||
if (!xloc) c &= ~LEFT;
|
||||
if (!yloc) c &= ~UP;
|
||||
|
||||
if (xloc == TILE_COLS -1) c &= ~RIGHT;
|
||||
if (yloc == TILE_ROWS -1) c &= ~DOWN;
|
||||
|
||||
new_labels[i][j] = yloc * TILE_COLS + xloc;
|
||||
edgesTile[yloc][xloc] = c;
|
||||
}
|
||||
|
||||
for (int k = 0; ;++k)
|
||||
{
|
||||
//1. backup
|
||||
#pragma unroll
|
||||
for (int i = 0; i < TPB_Y; ++i)
|
||||
#pragma unroll
|
||||
for (int j = 0; j < TPB_X; ++j)
|
||||
{
|
||||
int yloc = threadIdx.y + CTA_SIZE_Y * i;
|
||||
int xloc = threadIdx.x + CTA_SIZE_X * j;
|
||||
|
||||
old_labels[i][j] = new_labels[i][j];
|
||||
labelsTile[yloc][xloc] = new_labels[i][j];
|
||||
}
|
||||
|
||||
__syncthreads();
|
||||
|
||||
//2. compare local arrays
|
||||
#pragma unroll
|
||||
for (int i = 0; i < TPB_Y; ++i)
|
||||
#pragma unroll
|
||||
for (int j = 0; j < TPB_X; ++j)
|
||||
{
|
||||
int yloc = threadIdx.y + CTA_SIZE_Y * i;
|
||||
int xloc = threadIdx.x + CTA_SIZE_X * j;
|
||||
|
||||
component c = edgesTile[yloc][xloc];
|
||||
int label = new_labels[i][j];
|
||||
|
||||
if (c & UP)
|
||||
label = ::min(label, labelsTile[yloc - 1][xloc]);
|
||||
|
||||
if (c & DOWN)
|
||||
label = ::min(label, labelsTile[yloc + 1][xloc]);
|
||||
|
||||
if (c & LEFT)
|
||||
label = ::min(label, labelsTile[yloc][xloc - 1]);
|
||||
|
||||
if (c & RIGHT)
|
||||
label = ::min(label, labelsTile[yloc][xloc + 1]);
|
||||
|
||||
new_labels[i][j] = label;
|
||||
}
|
||||
|
||||
__syncthreads();
|
||||
|
||||
//3. determine: Is any value changed?
|
||||
int changed = 0;
|
||||
#pragma unroll
|
||||
for (int i = 0; i < TPB_Y; ++i)
|
||||
#pragma unroll
|
||||
for (int j = 0; j < TPB_X; ++j)
|
||||
{
|
||||
if (new_labels[i][j] < old_labels[i][j])
|
||||
{
|
||||
changed = 1;
|
||||
Emulation::smem::atomicMin(&labelsTile[0][0] + old_labels[i][j], new_labels[i][j]);
|
||||
}
|
||||
}
|
||||
|
||||
changed = Emulation::syncthreadsOr(changed);
|
||||
|
||||
if (!changed)
|
||||
break;
|
||||
|
||||
//4. Compact paths
|
||||
const int *labels = &labelsTile[0][0];
|
||||
#pragma unroll
|
||||
for (int i = 0; i < TPB_Y; ++i)
|
||||
#pragma unroll
|
||||
for (int j = 0; j < TPB_X; ++j)
|
||||
{
|
||||
int label = new_labels[i][j];
|
||||
|
||||
while( labels[label] < label ) label = labels[label];
|
||||
|
||||
new_labels[i][j] = label;
|
||||
}
|
||||
__syncthreads();
|
||||
}
|
||||
|
||||
#pragma unroll
|
||||
for (int i = 0; i < TPB_Y; ++i)
|
||||
#pragma unroll
|
||||
for (int j = 0; j < TPB_X; ++j)
|
||||
{
|
||||
int label = new_labels[i][j];
|
||||
int yloc = label / TILE_COLS;
|
||||
int xloc = label - yloc * TILE_COLS;
|
||||
|
||||
xloc += blockIdx.x * TILE_COLS;
|
||||
yloc += blockIdx.y * TILE_ROWS;
|
||||
|
||||
label = yloc * edges.cols + xloc;
|
||||
// do it for x too.
|
||||
if (y + CTA_SIZE_Y * i < comps.rows) comps(y + CTA_SIZE_Y * i, x + CTA_SIZE_X * j) = label;
|
||||
}
|
||||
}
|
||||
|
||||
__device__ __forceinline__ int root(const PtrStepSzi& comps, int label)
|
||||
{
|
||||
while(1)
|
||||
{
|
||||
int y = label / comps.cols;
|
||||
int x = label - y * comps.cols;
|
||||
|
||||
int parent = comps(y, x);
|
||||
|
||||
if (label == parent) break;
|
||||
|
||||
label = parent;
|
||||
}
|
||||
return label;
|
||||
}
|
||||
|
||||
__device__ __forceinline__ void isConnected(PtrStepSzi& comps, int l1, int l2, bool& changed)
|
||||
{
|
||||
int r1 = root(comps, l1);
|
||||
int r2 = root(comps, l2);
|
||||
|
||||
if (r1 == r2) return;
|
||||
|
||||
int mi = ::min(r1, r2);
|
||||
int ma = ::max(r1, r2);
|
||||
|
||||
int y = ma / comps.cols;
|
||||
int x = ma - y * comps.cols;
|
||||
|
||||
atomicMin(&comps.ptr(y)[x], mi);
|
||||
changed = true;
|
||||
}
|
||||
|
||||
__global__ void crossMerge(const int tilesNumY, const int tilesNumX, int tileSizeY, int tileSizeX,
|
||||
const PtrStepSzb edges, PtrStepSzi comps, const int yIncomplete, int xIncomplete)
|
||||
{
|
||||
int tid = threadIdx.y * blockDim.x + threadIdx.x;
|
||||
int stride = blockDim.y * blockDim.x;
|
||||
|
||||
int ybegin = blockIdx.y * (tilesNumY * tileSizeY);
|
||||
int yend = ybegin + tilesNumY * tileSizeY;
|
||||
|
||||
if (blockIdx.y == gridDim.y - 1)
|
||||
{
|
||||
yend -= yIncomplete * tileSizeY;
|
||||
yend -= tileSizeY;
|
||||
tileSizeY = (edges.rows % tileSizeY);
|
||||
|
||||
yend += tileSizeY;
|
||||
}
|
||||
|
||||
int xbegin = blockIdx.x * tilesNumX * tileSizeX;
|
||||
int xend = xbegin + tilesNumX * tileSizeX;
|
||||
|
||||
if (blockIdx.x == gridDim.x - 1)
|
||||
{
|
||||
if (xIncomplete) yend = ybegin;
|
||||
xend -= xIncomplete * tileSizeX;
|
||||
xend -= tileSizeX;
|
||||
tileSizeX = (edges.cols % tileSizeX);
|
||||
|
||||
xend += tileSizeX;
|
||||
}
|
||||
|
||||
if (blockIdx.y == (gridDim.y - 1) && yIncomplete)
|
||||
{
|
||||
xend = xbegin;
|
||||
}
|
||||
|
||||
int tasksV = (tilesNumX - 1) * (yend - ybegin);
|
||||
int tasksH = (tilesNumY - 1) * (xend - xbegin);
|
||||
|
||||
int total = tasksH + tasksV;
|
||||
|
||||
bool changed;
|
||||
do
|
||||
{
|
||||
changed = false;
|
||||
for (int taskIdx = tid; taskIdx < total; taskIdx += stride)
|
||||
{
|
||||
if (taskIdx < tasksH)
|
||||
{
|
||||
int indexH = taskIdx;
|
||||
|
||||
int row = indexH / (xend - xbegin);
|
||||
int col = indexH - row * (xend - xbegin);
|
||||
|
||||
int y = ybegin + (row + 1) * tileSizeY;
|
||||
int x = xbegin + col;
|
||||
|
||||
component e = edges( x, y);
|
||||
if (e & UP)
|
||||
{
|
||||
int lc = comps(y,x);
|
||||
int lu = comps(y - 1, x);
|
||||
|
||||
isConnected(comps, lc, lu, changed);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
int indexV = taskIdx - tasksH;
|
||||
|
||||
int col = indexV / (yend - ybegin);
|
||||
int row = indexV - col * (yend - ybegin);
|
||||
|
||||
int x = xbegin + (col + 1) * tileSizeX;
|
||||
int y = ybegin + row;
|
||||
|
||||
component e = edges(x, y);
|
||||
if (e & LEFT)
|
||||
{
|
||||
int lc = comps(y, x);
|
||||
int ll = comps(y, x - 1);
|
||||
|
||||
isConnected(comps, lc, ll, changed);
|
||||
}
|
||||
}
|
||||
}
|
||||
} while (Emulation::syncthreadsOr(changed));
|
||||
}
|
||||
|
||||
__global__ void flatten(const PtrStepSzb edges, PtrStepSzi comps)
|
||||
{
|
||||
int x = threadIdx.x + blockIdx.x * blockDim.x;
|
||||
int y = threadIdx.y + blockIdx.y * blockDim.y;
|
||||
|
||||
if( x < comps.cols && y < comps.rows)
|
||||
comps(y, x) = root(comps, comps(y, x));
|
||||
}
|
||||
|
||||
enum {CC_NO_COMPACT = 0, CC_COMPACT_LABELS = 1};
|
||||
|
||||
void labelComponents(const PtrStepSzb& edges, PtrStepSzi comps, int flags, cudaStream_t stream)
|
||||
{
|
||||
(void) flags;
|
||||
dim3 block(CTA_SIZE_X, CTA_SIZE_Y);
|
||||
dim3 grid(divUp(edges.cols, TILE_COLS), divUp(edges.rows, TILE_ROWS));
|
||||
|
||||
lableTiles<<<grid, block, 0, stream>>>(edges, comps);
|
||||
cudaSafeCall( cudaGetLastError() );
|
||||
|
||||
int tileSizeX = TILE_COLS, tileSizeY = TILE_ROWS;
|
||||
while (grid.x > 1 || grid.y > 1)
|
||||
{
|
||||
dim3 mergeGrid((int)ceilf(grid.x / 2.f), (int)ceilf(grid.y / 2.f));
|
||||
dim3 mergeBlock(STA_SIZE_MERGE_X, STA_SIZE_MERGE_Y);
|
||||
// debug log
|
||||
// std::cout << "merging: " << grid.y << " x " << grid.x << " ---> " << mergeGrid.y << " x " << mergeGrid.x << " for tiles: " << tileSizeY << " x " << tileSizeX << std::endl;
|
||||
crossMerge<<<mergeGrid, mergeBlock, 0, stream>>>(2, 2, tileSizeY, tileSizeX, edges, comps, (int)ceilf(grid.y / 2.f) - grid.y / 2, (int)ceilf(grid.x / 2.f) - grid.x / 2);
|
||||
tileSizeX <<= 1;
|
||||
tileSizeY <<= 1;
|
||||
grid = mergeGrid;
|
||||
|
||||
cudaSafeCall( cudaGetLastError() );
|
||||
}
|
||||
|
||||
grid.x = divUp(edges.cols, block.x);
|
||||
grid.y = divUp(edges.rows, block.y);
|
||||
flatten<<<grid, block, 0, stream>>>(edges, comps);
|
||||
cudaSafeCall( cudaGetLastError() );
|
||||
|
||||
if (stream == 0)
|
||||
cudaSafeCall( cudaDeviceSynchronize() );
|
||||
}
|
||||
}
|
||||
} } }
|
||||
|
||||
#endif /* CUDA_DISABLER */
|
282
modules/gpu/src/graphcuts.cpp
Normal file
282
modules/gpu/src/graphcuts.cpp
Normal file
@@ -0,0 +1,282 @@
|
||||
/*M///////////////////////////////////////////////////////////////////////////////////////
|
||||
//
|
||||
// IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
|
||||
//
|
||||
// By downloading, copying, installing or using the software you agree to this license.
|
||||
// If you do not agree to this license, do not download, install,
|
||||
// copy or use the software.
|
||||
//
|
||||
//
|
||||
// License Agreement
|
||||
// For Open Source Computer Vision Library
|
||||
//
|
||||
// Copyright (C) 2000-2008, Intel Corporation, all rights reserved.
|
||||
// Copyright (C) 2009, Willow Garage Inc., all rights reserved.
|
||||
// Third party copyrights are property of their respective owners.
|
||||
//
|
||||
// Redistribution and use in source and binary forms, with or without modification,
|
||||
// are permitted provided that the following conditions are met:
|
||||
//
|
||||
// * Redistribution's of source code must retain the above copyright notice,
|
||||
// this list of conditions and the following disclaimer.
|
||||
//
|
||||
// * Redistribution's in binary form must reproduce the above copyright notice,
|
||||
// this list of conditions and the following disclaimer in the documentation
|
||||
// and/or other materials provided with the distribution.
|
||||
//
|
||||
// * The name of the copyright holders may not be used to endorse or promote products
|
||||
// derived from this software without specific prior written permission.
|
||||
//
|
||||
// This software is provided by the copyright holders and contributors "as is" and
|
||||
// any express or implied warranties, including, but not limited to, the implied
|
||||
// warranties of merchantability and fitness for a particular purpose are disclaimed.
|
||||
// In no event shall the Intel Corporation or contributors be liable for any direct,
|
||||
// indirect, incidental, special, exemplary, or consequential damages
|
||||
// (including, but not limited to, procurement of substitute goods or services;
|
||||
// loss of use, data, or profits; or business interruption) however caused
|
||||
// and on any theory of liability, whether in contract, strict liability,
|
||||
// or tort (including negligence or otherwise) arising in any way out of
|
||||
// the use of this software, even if advised of the possibility of such damage.
|
||||
//
|
||||
//M*/
|
||||
|
||||
#include "precomp.hpp"
|
||||
|
||||
#if !defined (HAVE_CUDA) || defined (CUDA_DISABLER)
|
||||
|
||||
void cv::gpu::graphcut(GpuMat&, GpuMat&, GpuMat&, GpuMat&, GpuMat&, GpuMat&, GpuMat&, Stream&) { throw_no_cuda(); }
|
||||
void cv::gpu::graphcut(GpuMat&, GpuMat&, GpuMat&, GpuMat&, GpuMat&, GpuMat&, GpuMat&, GpuMat&, GpuMat&, GpuMat&, GpuMat&, Stream&) { throw_no_cuda(); }
|
||||
|
||||
void cv::gpu::connectivityMask(const GpuMat&, GpuMat&, const cv::Scalar&, const cv::Scalar&, Stream&) { throw_no_cuda(); }
|
||||
void cv::gpu::labelComponents(const GpuMat&, GpuMat&, int, Stream&) { throw_no_cuda(); }
|
||||
|
||||
#else /* !defined (HAVE_CUDA) */
|
||||
|
||||
namespace cv { namespace gpu { namespace cudev
|
||||
{
|
||||
namespace ccl
|
||||
{
|
||||
void labelComponents(const PtrStepSzb& edges, PtrStepSzi comps, int flags, cudaStream_t stream);
|
||||
|
||||
template<typename T>
|
||||
void computeEdges(const PtrStepSzb& image, PtrStepSzb edges, const float4& lo, const float4& hi, cudaStream_t stream);
|
||||
}
|
||||
}}}
|
||||
|
||||
static float4 scalarToCudaType(const cv::Scalar& in)
|
||||
{
|
||||
return make_float4((float)in[0], (float)in[1], (float)in[2], (float)in[3]);
|
||||
}
|
||||
|
||||
void cv::gpu::connectivityMask(const GpuMat& image, GpuMat& mask, const cv::Scalar& lo, const cv::Scalar& hi, Stream& s)
|
||||
{
|
||||
CV_Assert(!image.empty());
|
||||
|
||||
int ch = image.channels();
|
||||
CV_Assert(ch <= 4);
|
||||
|
||||
int depth = image.depth();
|
||||
|
||||
typedef void (*func_t)(const PtrStepSzb& image, PtrStepSzb edges, const float4& lo, const float4& hi, cudaStream_t stream);
|
||||
|
||||
static const func_t suppotLookup[8][4] =
|
||||
{ // 1, 2, 3, 4
|
||||
{ cudev::ccl::computeEdges<uchar>, 0, cudev::ccl::computeEdges<uchar3>, cudev::ccl::computeEdges<uchar4> },// CV_8U
|
||||
{ 0, 0, 0, 0 },// CV_16U
|
||||
{ cudev::ccl::computeEdges<ushort>, 0, cudev::ccl::computeEdges<ushort3>, cudev::ccl::computeEdges<ushort4> },// CV_8S
|
||||
{ 0, 0, 0, 0 },// CV_16S
|
||||
{ cudev::ccl::computeEdges<int>, 0, 0, 0 },// CV_32S
|
||||
{ cudev::ccl::computeEdges<float>, 0, 0, 0 },// CV_32F
|
||||
{ 0, 0, 0, 0 },// CV_64F
|
||||
{ 0, 0, 0, 0 } // CV_USRTYPE1
|
||||
};
|
||||
|
||||
func_t f = suppotLookup[depth][ch - 1];
|
||||
CV_Assert(f);
|
||||
|
||||
if (image.size() != mask.size() || mask.type() != CV_8UC1)
|
||||
mask.create(image.size(), CV_8UC1);
|
||||
|
||||
cudaStream_t stream = StreamAccessor::getStream(s);
|
||||
float4 culo = scalarToCudaType(lo), cuhi = scalarToCudaType(hi);
|
||||
f(image, mask, culo, cuhi, stream);
|
||||
}
|
||||
|
||||
void cv::gpu::labelComponents(const GpuMat& mask, GpuMat& components, int flags, Stream& s)
|
||||
{
|
||||
CV_Assert(!mask.empty() && mask.type() == CV_8U);
|
||||
|
||||
if (!deviceSupports(SHARED_ATOMICS))
|
||||
CV_Error(cv::Error::StsNotImplemented, "The device doesn't support shared atomics and communicative synchronization!");
|
||||
|
||||
components.create(mask.size(), CV_32SC1);
|
||||
|
||||
cudaStream_t stream = StreamAccessor::getStream(s);
|
||||
cudev::ccl::labelComponents(mask, components, flags, stream);
|
||||
}
|
||||
|
||||
namespace
|
||||
{
|
||||
typedef NppStatus (*init_func_t)(NppiSize oSize, NppiGraphcutState** ppState, Npp8u* pDeviceMem);
|
||||
|
||||
class NppiGraphcutStateHandler
|
||||
{
|
||||
public:
|
||||
NppiGraphcutStateHandler(NppiSize sznpp, Npp8u* pDeviceMem, const init_func_t func)
|
||||
{
|
||||
nppSafeCall( func(sznpp, &pState, pDeviceMem) );
|
||||
}
|
||||
|
||||
~NppiGraphcutStateHandler()
|
||||
{
|
||||
nppSafeCall( nppiGraphcutFree(pState) );
|
||||
}
|
||||
|
||||
operator NppiGraphcutState*()
|
||||
{
|
||||
return pState;
|
||||
}
|
||||
|
||||
private:
|
||||
NppiGraphcutState* pState;
|
||||
};
|
||||
}
|
||||
|
||||
void cv::gpu::graphcut(GpuMat& terminals, GpuMat& leftTransp, GpuMat& rightTransp, GpuMat& top, GpuMat& bottom, GpuMat& labels, GpuMat& buf, Stream& s)
|
||||
{
|
||||
#if (CUDA_VERSION < 5000)
|
||||
CV_Assert(terminals.type() == CV_32S);
|
||||
#else
|
||||
CV_Assert(terminals.type() == CV_32S || terminals.type() == CV_32F);
|
||||
#endif
|
||||
|
||||
Size src_size = terminals.size();
|
||||
|
||||
CV_Assert(leftTransp.size() == Size(src_size.height, src_size.width));
|
||||
CV_Assert(leftTransp.type() == terminals.type());
|
||||
|
||||
CV_Assert(rightTransp.size() == Size(src_size.height, src_size.width));
|
||||
CV_Assert(rightTransp.type() == terminals.type());
|
||||
|
||||
CV_Assert(top.size() == src_size);
|
||||
CV_Assert(top.type() == terminals.type());
|
||||
|
||||
CV_Assert(bottom.size() == src_size);
|
||||
CV_Assert(bottom.type() == terminals.type());
|
||||
|
||||
labels.create(src_size, CV_8U);
|
||||
|
||||
NppiSize sznpp;
|
||||
sznpp.width = src_size.width;
|
||||
sznpp.height = src_size.height;
|
||||
|
||||
int bufsz;
|
||||
nppSafeCall( nppiGraphcutGetSize(sznpp, &bufsz) );
|
||||
|
||||
ensureSizeIsEnough(1, bufsz, CV_8U, buf);
|
||||
|
||||
cudaStream_t stream = StreamAccessor::getStream(s);
|
||||
|
||||
NppStreamHandler h(stream);
|
||||
|
||||
NppiGraphcutStateHandler state(sznpp, buf.ptr<Npp8u>(), nppiGraphcutInitAlloc);
|
||||
|
||||
#if (CUDA_VERSION < 5000)
|
||||
nppSafeCall( nppiGraphcut_32s8u(terminals.ptr<Npp32s>(), leftTransp.ptr<Npp32s>(), rightTransp.ptr<Npp32s>(), top.ptr<Npp32s>(), bottom.ptr<Npp32s>(),
|
||||
static_cast<int>(terminals.step), static_cast<int>(leftTransp.step), sznpp, labels.ptr<Npp8u>(), static_cast<int>(labels.step), state) );
|
||||
#else
|
||||
if (terminals.type() == CV_32S)
|
||||
{
|
||||
nppSafeCall( nppiGraphcut_32s8u(terminals.ptr<Npp32s>(), leftTransp.ptr<Npp32s>(), rightTransp.ptr<Npp32s>(), top.ptr<Npp32s>(), bottom.ptr<Npp32s>(),
|
||||
static_cast<int>(terminals.step), static_cast<int>(leftTransp.step), sznpp, labels.ptr<Npp8u>(), static_cast<int>(labels.step), state) );
|
||||
}
|
||||
else
|
||||
{
|
||||
nppSafeCall( nppiGraphcut_32f8u(terminals.ptr<Npp32f>(), leftTransp.ptr<Npp32f>(), rightTransp.ptr<Npp32f>(), top.ptr<Npp32f>(), bottom.ptr<Npp32f>(),
|
||||
static_cast<int>(terminals.step), static_cast<int>(leftTransp.step), sznpp, labels.ptr<Npp8u>(), static_cast<int>(labels.step), state) );
|
||||
}
|
||||
#endif
|
||||
|
||||
if (stream == 0)
|
||||
cudaSafeCall( cudaDeviceSynchronize() );
|
||||
}
|
||||
|
||||
void cv::gpu::graphcut(GpuMat& terminals, GpuMat& leftTransp, GpuMat& rightTransp, GpuMat& top, GpuMat& topLeft, GpuMat& topRight,
|
||||
GpuMat& bottom, GpuMat& bottomLeft, GpuMat& bottomRight, GpuMat& labels, GpuMat& buf, Stream& s)
|
||||
{
|
||||
#if (CUDA_VERSION < 5000)
|
||||
CV_Assert(terminals.type() == CV_32S);
|
||||
#else
|
||||
CV_Assert(terminals.type() == CV_32S || terminals.type() == CV_32F);
|
||||
#endif
|
||||
|
||||
Size src_size = terminals.size();
|
||||
|
||||
CV_Assert(leftTransp.size() == Size(src_size.height, src_size.width));
|
||||
CV_Assert(leftTransp.type() == terminals.type());
|
||||
|
||||
CV_Assert(rightTransp.size() == Size(src_size.height, src_size.width));
|
||||
CV_Assert(rightTransp.type() == terminals.type());
|
||||
|
||||
CV_Assert(top.size() == src_size);
|
||||
CV_Assert(top.type() == terminals.type());
|
||||
|
||||
CV_Assert(topLeft.size() == src_size);
|
||||
CV_Assert(topLeft.type() == terminals.type());
|
||||
|
||||
CV_Assert(topRight.size() == src_size);
|
||||
CV_Assert(topRight.type() == terminals.type());
|
||||
|
||||
CV_Assert(bottom.size() == src_size);
|
||||
CV_Assert(bottom.type() == terminals.type());
|
||||
|
||||
CV_Assert(bottomLeft.size() == src_size);
|
||||
CV_Assert(bottomLeft.type() == terminals.type());
|
||||
|
||||
CV_Assert(bottomRight.size() == src_size);
|
||||
CV_Assert(bottomRight.type() == terminals.type());
|
||||
|
||||
labels.create(src_size, CV_8U);
|
||||
|
||||
NppiSize sznpp;
|
||||
sznpp.width = src_size.width;
|
||||
sznpp.height = src_size.height;
|
||||
|
||||
int bufsz;
|
||||
nppSafeCall( nppiGraphcut8GetSize(sznpp, &bufsz) );
|
||||
|
||||
ensureSizeIsEnough(1, bufsz, CV_8U, buf);
|
||||
|
||||
cudaStream_t stream = StreamAccessor::getStream(s);
|
||||
|
||||
NppStreamHandler h(stream);
|
||||
|
||||
NppiGraphcutStateHandler state(sznpp, buf.ptr<Npp8u>(), nppiGraphcut8InitAlloc);
|
||||
|
||||
#if (CUDA_VERSION < 5000)
|
||||
nppSafeCall( nppiGraphcut8_32s8u(terminals.ptr<Npp32s>(), leftTransp.ptr<Npp32s>(), rightTransp.ptr<Npp32s>(),
|
||||
top.ptr<Npp32s>(), topLeft.ptr<Npp32s>(), topRight.ptr<Npp32s>(),
|
||||
bottom.ptr<Npp32s>(), bottomLeft.ptr<Npp32s>(), bottomRight.ptr<Npp32s>(),
|
||||
static_cast<int>(terminals.step), static_cast<int>(leftTransp.step), sznpp, labels.ptr<Npp8u>(), static_cast<int>(labels.step), state) );
|
||||
#else
|
||||
if (terminals.type() == CV_32S)
|
||||
{
|
||||
nppSafeCall( nppiGraphcut8_32s8u(terminals.ptr<Npp32s>(), leftTransp.ptr<Npp32s>(), rightTransp.ptr<Npp32s>(),
|
||||
top.ptr<Npp32s>(), topLeft.ptr<Npp32s>(), topRight.ptr<Npp32s>(),
|
||||
bottom.ptr<Npp32s>(), bottomLeft.ptr<Npp32s>(), bottomRight.ptr<Npp32s>(),
|
||||
static_cast<int>(terminals.step), static_cast<int>(leftTransp.step), sznpp, labels.ptr<Npp8u>(), static_cast<int>(labels.step), state) );
|
||||
}
|
||||
else
|
||||
{
|
||||
nppSafeCall( nppiGraphcut8_32f8u(terminals.ptr<Npp32f>(), leftTransp.ptr<Npp32f>(), rightTransp.ptr<Npp32f>(),
|
||||
top.ptr<Npp32f>(), topLeft.ptr<Npp32f>(), topRight.ptr<Npp32f>(),
|
||||
bottom.ptr<Npp32f>(), bottomLeft.ptr<Npp32f>(), bottomRight.ptr<Npp32f>(),
|
||||
static_cast<int>(terminals.step), static_cast<int>(leftTransp.step), sznpp, labels.ptr<Npp8u>(), static_cast<int>(labels.step), state) );
|
||||
}
|
||||
#endif
|
||||
|
||||
if (stream == 0)
|
||||
cudaSafeCall( cudaDeviceSynchronize() );
|
||||
}
|
||||
|
||||
#endif /* !defined (HAVE_CUDA) */
|
197
modules/gpu/test/test_labeling.cpp
Normal file
197
modules/gpu/test/test_labeling.cpp
Normal file
@@ -0,0 +1,197 @@
|
||||
/*M///////////////////////////////////////////////////////////////////////////////////////
|
||||
//
|
||||
// IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
|
||||
//
|
||||
// By downloading, copying, installing or using the software you agree to this license.
|
||||
// If you do not agree to this license, do not download, install,
|
||||
// copy or use the software.
|
||||
//
|
||||
//
|
||||
// License Agreement
|
||||
// For Open Source Computer Vision Library
|
||||
//
|
||||
// Copyright (C) 2000-2008, Intel Corporation, all rights reserved.
|
||||
// Copyright (C) 2009, Willow Garage Inc., all rights reserved.
|
||||
// Third party copyrights are property of their respective owners.
|
||||
//
|
||||
// Redistribution and use in source and binary forms, with or without modification,
|
||||
// are permitted provided that the following conditions are met:
|
||||
//
|
||||
// * Redistribution's of source code must retain the above copyright notice,
|
||||
// this list of conditions and the following disclaimer.
|
||||
//
|
||||
// * Redistribution's in binary form must reproduce the above copyright notice,
|
||||
// this list of conditions and the following disclaimer in the documentation
|
||||
// and/or other materials provided with the distribution.
|
||||
//
|
||||
// * The name of the copyright holders may not be used to endorse or promote products
|
||||
// derived from this software without specific prior written permission.
|
||||
//
|
||||
// This software is provided by the copyright holders and contributors "as is" and
|
||||
// any express or implied warranties, including, but not limited to, the implied
|
||||
// warranties of merchantability and fitness for a particular purpose are disclaimed.
|
||||
// In no event shall the Intel Corporation or contributors be liable for any direct,
|
||||
// indirect, incidental, special, exemplary, or consequential damages
|
||||
// (including, but not limited to, procurement of substitute goods or services;
|
||||
// loss of use, data, or profits; or business interruption) however caused
|
||||
// and on any theory of liability, whether in contract, strict liability,
|
||||
// or tort (including negligence or otherwise) arising in any way out of
|
||||
// the use of this software, even if advised of the possibility of such damage.
|
||||
//
|
||||
//M*/
|
||||
|
||||
#include "test_precomp.hpp"
|
||||
|
||||
#ifdef HAVE_CUDA
|
||||
|
||||
namespace
|
||||
{
|
||||
struct GreedyLabeling
|
||||
{
|
||||
struct dot
|
||||
{
|
||||
int x;
|
||||
int y;
|
||||
|
||||
static dot make(int i, int j)
|
||||
{
|
||||
dot d; d.x = i; d.y = j;
|
||||
return d;
|
||||
}
|
||||
};
|
||||
|
||||
struct InInterval
|
||||
{
|
||||
InInterval(const int& _lo, const int& _hi) : lo(-_lo), hi(_hi) {};
|
||||
const int lo, hi;
|
||||
|
||||
bool operator() (const unsigned char a, const unsigned char b) const
|
||||
{
|
||||
int d = a - b;
|
||||
return lo <= d && d <= hi;
|
||||
}
|
||||
};
|
||||
|
||||
GreedyLabeling(cv::Mat img)
|
||||
: image(img), _labels(image.size(), CV_32SC1, cv::Scalar::all(-1)) {}
|
||||
|
||||
void operator() (cv::Mat labels) const
|
||||
{
|
||||
InInterval inInt(0, 2);
|
||||
dot* stack = new dot[image.cols * image.rows];
|
||||
|
||||
int cc = -1;
|
||||
|
||||
int* dist_labels = (int*)labels.data;
|
||||
int pitch = (int) labels.step1();
|
||||
|
||||
unsigned char* source = (unsigned char*)image.data;
|
||||
int width = image.cols;
|
||||
int height = image.rows;
|
||||
int step1 = (int)image.step1();
|
||||
|
||||
for (int j = 0; j < image.rows; ++j)
|
||||
for (int i = 0; i < image.cols; ++i)
|
||||
{
|
||||
if (dist_labels[j * pitch + i] != -1) continue;
|
||||
|
||||
dot* top = stack;
|
||||
dot p = dot::make(i, j);
|
||||
cc++;
|
||||
|
||||
dist_labels[j * pitch + i] = cc;
|
||||
|
||||
while (top >= stack)
|
||||
{
|
||||
int* dl = &dist_labels[p.y * pitch + p.x];
|
||||
unsigned char* sp = &source[p.y * step1 + p.x];
|
||||
|
||||
dl[0] = cc;
|
||||
|
||||
//right
|
||||
if( p.x < (width - 1) && dl[ +1] == -1 && inInt(sp[0], sp[+1]))
|
||||
*top++ = dot::make(p.x + 1, p.y);
|
||||
|
||||
//left
|
||||
if( p.x > 0 && dl[-1] == -1 && inInt(sp[0], sp[-1]))
|
||||
*top++ = dot::make(p.x - 1, p.y);
|
||||
|
||||
//bottom
|
||||
if( p.y < (height - 1) && dl[+pitch] == -1 && inInt(sp[0], sp[+step1]))
|
||||
*top++ = dot::make(p.x, p.y + 1);
|
||||
|
||||
//top
|
||||
if( p.y > 0 && dl[-pitch] == -1 && inInt(sp[0], sp[-step1]))
|
||||
*top++ = dot::make(p.x, p.y - 1);
|
||||
|
||||
p = *--top;
|
||||
}
|
||||
}
|
||||
delete[] stack;
|
||||
}
|
||||
|
||||
void checkCorrectness(cv::Mat gpu)
|
||||
{
|
||||
cv::Mat diff = gpu - _labels;
|
||||
|
||||
int outliers = 0;
|
||||
for (int j = 0; j < image.rows; ++j)
|
||||
for (int i = 0; i < image.cols - 1; ++i)
|
||||
{
|
||||
if ( (_labels.at<int>(j,i) == gpu.at<int>(j,i + 1)) && (diff.at<int>(j, i) != diff.at<int>(j,i + 1)))
|
||||
{
|
||||
outliers++;
|
||||
}
|
||||
}
|
||||
ASSERT_TRUE(outliers < gpu.cols + gpu.rows);
|
||||
}
|
||||
|
||||
cv::Mat image;
|
||||
cv::Mat _labels;
|
||||
};
|
||||
}
|
||||
|
||||
struct Labeling : testing::TestWithParam<cv::gpu::DeviceInfo>
|
||||
{
|
||||
cv::gpu::DeviceInfo devInfo;
|
||||
|
||||
virtual void SetUp()
|
||||
{
|
||||
devInfo = GetParam();
|
||||
cv::gpu::setDevice(devInfo.deviceID());
|
||||
}
|
||||
|
||||
cv::Mat loat_image()
|
||||
{
|
||||
return cv::imread(std::string( cvtest::TS::ptr()->get_data_path() ) + "labeling/label.png");
|
||||
}
|
||||
};
|
||||
|
||||
GPU_TEST_P(Labeling, DISABLED_ConnectedComponents)
|
||||
{
|
||||
cv::Mat image;
|
||||
cvtColor(loat_image(), image, cv::COLOR_BGR2GRAY);
|
||||
|
||||
cv::threshold(image, image, 150, 255, cv::THRESH_BINARY);
|
||||
|
||||
ASSERT_TRUE(image.type() == CV_8UC1);
|
||||
|
||||
GreedyLabeling host(image);
|
||||
host(host._labels);
|
||||
|
||||
cv::gpu::GpuMat mask;
|
||||
mask.create(image.rows, image.cols, CV_8UC1);
|
||||
|
||||
cv::gpu::GpuMat components;
|
||||
components.create(image.rows, image.cols, CV_32SC1);
|
||||
|
||||
cv::gpu::connectivityMask(cv::gpu::GpuMat(image), mask, cv::Scalar::all(0), cv::Scalar::all(2));
|
||||
|
||||
cv::gpu::labelComponents(mask, components);
|
||||
|
||||
host.checkCorrectness(cv::Mat(components));
|
||||
}
|
||||
|
||||
INSTANTIATE_TEST_CASE_P(GPU_ConnectedComponents, Labeling, ALL_DEVICES);
|
||||
|
||||
#endif // HAVE_CUDA
|
Reference in New Issue
Block a user