mirror of
https://github.com/opencv/opencv.git
synced 2025-01-18 14:13:15 +08:00
Rewrote moments of opencl version.
This commit is contained in:
parent
957c85e9c4
commit
b6b190df5c
@ -1518,7 +1518,12 @@ namespace cv
|
||||
float pos, oclMat &newFrame, oclMat &buf);
|
||||
|
||||
//! computes moments of the rasterized shape or a vector of points
|
||||
CV_EXPORTS Moments ocl_moments(InputArray _array, bool binaryImage);
|
||||
//! _array should be a vector a points standing for the contour
|
||||
CV_EXPORTS Moments ocl_moments(InputArray contour);
|
||||
//! src should be a general image uploaded to the GPU.
|
||||
//! the supported oclMat type are CV_8UC1, CV_16UC1, CV_16SC1, CV_32FC1 and CV_64FC1
|
||||
//! to use type of CV_64FC1, the GPU should support CV_64FC1
|
||||
CV_EXPORTS Moments ocl_moments(oclMat& src, bool binary);
|
||||
|
||||
class CV_EXPORTS StereoBM_OCL
|
||||
{
|
||||
|
@ -26,7 +26,7 @@
|
||||
//
|
||||
// * 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.
|
||||
// and/or other oclMaterials 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.
|
||||
@ -49,41 +49,42 @@
|
||||
using namespace perf;
|
||||
using std::tr1::tuple;
|
||||
using std::tr1::get;
|
||||
using namespace cv;
|
||||
using namespace cv::ocl;
|
||||
using namespace cvtest;
|
||||
using namespace testing;
|
||||
using namespace std;
|
||||
|
||||
|
||||
///////////// Moments ////////////////////////
|
||||
//*! performance of image
|
||||
typedef tuple<Size, MatType, bool> MomentsParamType;
|
||||
typedef TestBaseWithParam<MomentsParamType> MomentsFixture;
|
||||
|
||||
typedef Size_MatType MomentsFixture;
|
||||
|
||||
PERF_TEST_P(MomentsFixture, DISABLED_Moments,
|
||||
PERF_TEST_P(MomentsFixture, Moments,
|
||||
::testing::Combine(OCL_TYPICAL_MAT_SIZES,
|
||||
OCL_PERF_ENUM(CV_8UC1, CV_16SC1, CV_32FC1, CV_64FC1))) // TODO does not work properly (see below)
|
||||
OCL_PERF_ENUM(CV_8UC1, CV_16SC1, CV_16UC1, CV_32FC1, CV_64FC1), ::testing::Values(false, true)))
|
||||
{
|
||||
const Size_MatType_t params = GetParam();
|
||||
const MomentsParamType params = GetParam();
|
||||
const Size srcSize = get<0>(params);
|
||||
const int type = get<1>(params);
|
||||
const bool binaryImage = get<2>(params);
|
||||
|
||||
Mat src(srcSize, type), dst(7, 1, CV_64F);
|
||||
const bool binaryImage = false;
|
||||
randu(src, 0, 255);
|
||||
|
||||
oclMat src_d(src);
|
||||
cv::Moments mom;
|
||||
|
||||
declare.in(src, WARMUP_RNG).out(dst);
|
||||
|
||||
if (RUN_OCL_IMPL)
|
||||
{
|
||||
ocl::oclMat oclSrc(src);
|
||||
|
||||
OCL_TEST_CYCLE() mom = cv::ocl::ocl_moments(oclSrc, binaryImage); // TODO Use oclSrc
|
||||
cv::HuMoments(mom, dst);
|
||||
|
||||
SANITY_CHECK(dst);
|
||||
OCL_TEST_CYCLE() mom = cv::ocl::ocl_moments(src_d, binaryImage);
|
||||
}
|
||||
else if (RUN_PLAIN_IMPL)
|
||||
{
|
||||
TEST_CYCLE() mom = cv::moments(src, binaryImage);
|
||||
cv::HuMoments(mom, dst);
|
||||
|
||||
SANITY_CHECK(dst);
|
||||
}
|
||||
else
|
||||
OCL_PERF_ELSE
|
||||
cv::HuMoments(mom, dst);
|
||||
SANITY_CHECK(dst, 1e-3);
|
||||
}
|
||||
|
@ -26,7 +26,7 @@
|
||||
//
|
||||
// * 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.
|
||||
// and/or other oclMaterials 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.
|
||||
@ -46,6 +46,9 @@
|
||||
#include "precomp.hpp"
|
||||
#include "opencl_kernels.hpp"
|
||||
|
||||
#if defined _MSC_VER
|
||||
#define snprintf sprintf_s
|
||||
#endif
|
||||
namespace cv
|
||||
{
|
||||
namespace ocl
|
||||
@ -141,7 +144,10 @@ static void icvContourMoments( CvSeq* contour, CvMoments* mom )
|
||||
cl_int dst_step = (cl_int)dst_a.step;
|
||||
args.push_back( make_pair( sizeof(cl_int) , (void *)&dst_step ));
|
||||
|
||||
openCLExecuteKernel(dst_a.clCxt, &moments, "icvContourMoments", globalThreads, localThreads, args, -1, -1);
|
||||
char builOption[128];
|
||||
snprintf(builOption, 128, "-D CV_8UC1");
|
||||
|
||||
openCLExecuteKernel(dst_a.clCxt, &moments, "icvContourMoments", globalThreads, localThreads, args, -1, -1, builOption);
|
||||
|
||||
cv::Mat dst(dst_a);
|
||||
a00 = a10 = a01 = a20 = a11 = a02 = a30 = a21 = a12 = a03 = 0.0;
|
||||
@ -214,29 +220,153 @@ static void icvContourMoments( CvSeq* contour, CvMoments* mom )
|
||||
}
|
||||
}
|
||||
|
||||
static void ocl_cvMoments( const void* array, CvMoments* mom, int binary )
|
||||
Moments ocl_moments(oclMat& src, bool binary) //for image
|
||||
{
|
||||
CV_Assert(src.oclchannels() == 1);
|
||||
if(src.type() == CV_64FC1 && Context::getContext()->supportsFeature(FEATURE_CL_DOUBLE))
|
||||
{
|
||||
CV_Error(CV_StsUnsupportedFormat, "Moments - double is not supported by your GPU!");
|
||||
}
|
||||
|
||||
if(binary)
|
||||
{
|
||||
oclMat mask;
|
||||
if(src.type() != CV_8UC1)
|
||||
{
|
||||
src.convertTo(mask, CV_8UC1);
|
||||
}
|
||||
oclMat src8u(src.size(), CV_8UC1);
|
||||
src8u.setTo(Scalar(255), mask);
|
||||
src = src8u;
|
||||
}
|
||||
const int TILE_SIZE = 256;
|
||||
int type, depth, cn, coi = 0;
|
||||
CvMat stub, *mat = (CvMat*)array;
|
||||
CvContour contourHeader;
|
||||
|
||||
CvMoments mom;
|
||||
memset(&mom, 0, sizeof(mom));
|
||||
|
||||
cv::Size size = src.size();
|
||||
int blockx, blocky;
|
||||
blockx = (size.width + TILE_SIZE - 1)/TILE_SIZE;
|
||||
blocky = (size.height + TILE_SIZE - 1)/TILE_SIZE;
|
||||
|
||||
oclMat dst_m;
|
||||
int tile_height = TILE_SIZE;
|
||||
|
||||
size_t localThreads[3] = {1, tile_height, 1};
|
||||
size_t globalThreads[3] = {blockx, size.height, 1};
|
||||
|
||||
if(Context::getContext()->supportsFeature(FEATURE_CL_DOUBLE))
|
||||
{
|
||||
dst_m.create(blocky * 10, blockx, CV_64FC1);
|
||||
}else
|
||||
{
|
||||
dst_m.create(blocky * 10, blockx, CV_32FC1);
|
||||
}
|
||||
|
||||
int src_step = (int)(src.step/src.elemSize());
|
||||
int dstm_step = (int)(dst_m.step/dst_m.elemSize());
|
||||
|
||||
vector<pair<size_t , const void *> > args,args_sum;
|
||||
args.push_back( make_pair( sizeof(cl_mem) , (void *)&src.data ));
|
||||
args.push_back( make_pair( sizeof(cl_int) , (void *)&src.rows ));
|
||||
args.push_back( make_pair( sizeof(cl_int) , (void *)&src.cols ));
|
||||
args.push_back( make_pair( sizeof(cl_int) , (void *)&src_step ));
|
||||
args.push_back( make_pair( sizeof(cl_mem) , (void *)&dst_m.data ));
|
||||
args.push_back( make_pair( sizeof(cl_int) , (void *)&dst_m.cols ));
|
||||
args.push_back( make_pair( sizeof(cl_int) , (void *)&dstm_step ));
|
||||
|
||||
int binary_;
|
||||
if(binary)
|
||||
binary_ = 1;
|
||||
else
|
||||
binary_ = 0;
|
||||
args.push_back( make_pair( sizeof(cl_int) , (void *)&binary_));
|
||||
|
||||
char builOption[128];
|
||||
if(binary || src.type() == CV_8UC1)
|
||||
{
|
||||
snprintf(builOption, 128, "-D CV_8UC1");
|
||||
}else if(src.type() == CV_16UC1)
|
||||
{
|
||||
snprintf(builOption, 128, "-D CV_16UC1");
|
||||
}else if(src.type() == CV_16SC1)
|
||||
{
|
||||
snprintf(builOption, 128, "-D CV_16SC1");
|
||||
}else if(src.type() == CV_32FC1)
|
||||
{
|
||||
snprintf(builOption, 128, "-D CV_32FC1");
|
||||
}else if(src.type() == CV_64FC1)
|
||||
{
|
||||
snprintf(builOption, 128, "-D CV_64FC1");
|
||||
}else
|
||||
{
|
||||
CV_Error( CV_StsUnsupportedFormat, "" );
|
||||
}
|
||||
|
||||
openCLExecuteKernel(Context::getContext(), &moments, "CvMoments", globalThreads, localThreads, args, -1, -1, builOption);
|
||||
|
||||
Mat tmp(dst_m);
|
||||
tmp.convertTo(tmp, CV_64FC1);
|
||||
|
||||
double tmp_m[10] = {0};
|
||||
|
||||
for(int j = 0; j < tmp.rows; j += 10)
|
||||
{
|
||||
for(int i = 0; i < tmp.cols; i++)
|
||||
{
|
||||
tmp_m[0] += tmp.at<double>(j, i);
|
||||
tmp_m[1] += tmp.at<double>(j + 1, i);
|
||||
tmp_m[2] += tmp.at<double>(j + 2, i);
|
||||
tmp_m[3] += tmp.at<double>(j + 3, i);
|
||||
tmp_m[4] += tmp.at<double>(j + 4, i);
|
||||
tmp_m[5] += tmp.at<double>(j + 5, i);
|
||||
tmp_m[6] += tmp.at<double>(j + 6, i);
|
||||
tmp_m[7] += tmp.at<double>(j + 7, i);
|
||||
tmp_m[8] += tmp.at<double>(j + 8, i);
|
||||
tmp_m[9] += tmp.at<double>(j + 9, i);
|
||||
}
|
||||
}
|
||||
|
||||
mom.m00 = tmp_m[0];
|
||||
mom.m10 = tmp_m[1];
|
||||
mom.m01 = tmp_m[2];
|
||||
mom.m20 = tmp_m[3];
|
||||
mom.m11 = tmp_m[4];
|
||||
mom.m02 = tmp_m[5];
|
||||
mom.m30 = tmp_m[6];
|
||||
mom.m21 = tmp_m[7];
|
||||
mom.m12 = tmp_m[8];
|
||||
mom.m03 = tmp_m[9];
|
||||
icvCompleteMomentState( &mom );
|
||||
return mom;
|
||||
}
|
||||
|
||||
Moments ocl_moments(InputArray _contour) //for contour
|
||||
{
|
||||
CvMoments mom;
|
||||
memset(&mom, 0, sizeof(mom));
|
||||
|
||||
Mat arr = _contour.getMat();
|
||||
CvMat c_array = arr;
|
||||
|
||||
const void* array = &c_array;
|
||||
|
||||
CvSeq* contour = 0;
|
||||
CvSeqBlock block;
|
||||
if( CV_IS_SEQ( array ))
|
||||
{
|
||||
contour = (CvSeq*)array;
|
||||
contour = (CvSeq*)(array);
|
||||
if( !CV_IS_SEQ_POINT_SET( contour ))
|
||||
CV_Error( CV_StsBadArg, "The passed sequence is not a valid contour" );
|
||||
}
|
||||
|
||||
if( !mom )
|
||||
CV_Error( CV_StsNullPtr, "" );
|
||||
int type, coi = 0;
|
||||
|
||||
memset( mom, 0, sizeof(*mom));
|
||||
CvMat stub, *mat = (CvMat*)(array);
|
||||
CvContour contourHeader;
|
||||
CvSeqBlock block;
|
||||
|
||||
if( !contour )
|
||||
{
|
||||
|
||||
mat = cvGetMat( mat, &stub, &coi );
|
||||
type = CV_MAT_TYPE( mat->type );
|
||||
|
||||
@ -247,93 +377,11 @@ static void ocl_cvMoments( const void* array, CvMoments* mom, int binary )
|
||||
mat, &contourHeader, &block );
|
||||
}
|
||||
}
|
||||
if( contour )
|
||||
{
|
||||
icvContourMoments( contour, mom );
|
||||
return;
|
||||
|
||||
CV_Assert(contour);
|
||||
|
||||
icvContourMoments(contour, &mom);
|
||||
return mom;
|
||||
}
|
||||
|
||||
type = CV_MAT_TYPE( mat->type );
|
||||
depth = CV_MAT_DEPTH( type );
|
||||
cn = CV_MAT_CN( type );
|
||||
|
||||
cv::Size size = cvGetMatSize( mat );
|
||||
if( cn > 1 && coi == 0 )
|
||||
CV_Error( CV_StsBadArg, "Invalid image type" );
|
||||
|
||||
if( size.width <= 0 || size.height <= 0 )
|
||||
return;
|
||||
|
||||
cv::Mat src0(mat);
|
||||
cv::ocl::oclMat src(src0);
|
||||
cv::Size tileSize;
|
||||
int blockx,blocky;
|
||||
if(size.width%TILE_SIZE == 0)
|
||||
blockx = size.width/TILE_SIZE;
|
||||
else
|
||||
blockx = size.width/TILE_SIZE + 1;
|
||||
if(size.height%TILE_SIZE == 0)
|
||||
blocky = size.height/TILE_SIZE;
|
||||
else
|
||||
blocky = size.height/TILE_SIZE + 1;
|
||||
oclMat dst_m(blocky * 10, blockx, CV_64FC1);
|
||||
oclMat sum(1, 10, CV_64FC1);
|
||||
int tile_width = std::min(size.width,TILE_SIZE);
|
||||
int tile_height = std::min(size.height,TILE_SIZE);
|
||||
size_t localThreads[3] = { tile_height, 1, 1};
|
||||
size_t globalThreads[3] = { size.height, blockx, 1};
|
||||
vector<pair<size_t , const void *> > args,args_sum;
|
||||
args.push_back( make_pair( sizeof(cl_mem) , (void *)&src.data ));
|
||||
args.push_back( make_pair( sizeof(cl_int) , (void *)&src.rows ));
|
||||
args.push_back( make_pair( sizeof(cl_int) , (void *)&src.cols ));
|
||||
args.push_back( make_pair( sizeof(cl_int) , (void *)&src.step ));
|
||||
args.push_back( make_pair( sizeof(cl_mem) , (void *)&dst_m.data ));
|
||||
args.push_back( make_pair( sizeof(cl_int) , (void *)&dst_m.cols ));
|
||||
args.push_back( make_pair( sizeof(cl_int) , (void *)&dst_m.step ));
|
||||
args.push_back( make_pair( sizeof(cl_int) , (void *)&blocky ));
|
||||
args.push_back( make_pair( sizeof(cl_int) , (void *)&depth ));
|
||||
args.push_back( make_pair( sizeof(cl_int) , (void *)&cn ));
|
||||
args.push_back( make_pair( sizeof(cl_int) , (void *)&coi ));
|
||||
args.push_back( make_pair( sizeof(cl_int) , (void *)&binary ));
|
||||
args.push_back( make_pair( sizeof(cl_int) , (void *)&TILE_SIZE ));
|
||||
openCLExecuteKernel(Context::getContext(), &moments, "CvMoments", globalThreads, localThreads, args, -1, depth);
|
||||
|
||||
size_t localThreadss[3] = { 128, 1, 1};
|
||||
size_t globalThreadss[3] = { 128, 1, 1};
|
||||
args_sum.push_back( make_pair( sizeof(cl_int) , (void *)&src.rows ));
|
||||
args_sum.push_back( make_pair( sizeof(cl_int) , (void *)&src.cols ));
|
||||
args_sum.push_back( make_pair( sizeof(cl_int) , (void *)&tile_height ));
|
||||
args_sum.push_back( make_pair( sizeof(cl_int) , (void *)&tile_width ));
|
||||
args_sum.push_back( make_pair( sizeof(cl_int) , (void *)&TILE_SIZE ));
|
||||
args_sum.push_back( make_pair( sizeof(cl_mem) , (void *)&sum.data ));
|
||||
args_sum.push_back( make_pair( sizeof(cl_mem) , (void *)&dst_m.data ));
|
||||
args_sum.push_back( make_pair( sizeof(cl_int) , (void *)&dst_m.step ));
|
||||
openCLExecuteKernel(Context::getContext(), &moments, "dst_sum", globalThreadss, localThreadss, args_sum, -1, -1);
|
||||
|
||||
Mat dstsum(sum);
|
||||
mom->m00 = dstsum.at<double>(0, 0);
|
||||
mom->m10 = dstsum.at<double>(0, 1);
|
||||
mom->m01 = dstsum.at<double>(0, 2);
|
||||
mom->m20 = dstsum.at<double>(0, 3);
|
||||
mom->m11 = dstsum.at<double>(0, 4);
|
||||
mom->m02 = dstsum.at<double>(0, 5);
|
||||
mom->m30 = dstsum.at<double>(0, 6);
|
||||
mom->m21 = dstsum.at<double>(0, 7);
|
||||
mom->m12 = dstsum.at<double>(0, 8);
|
||||
mom->m03 = dstsum.at<double>(0, 9);
|
||||
|
||||
icvCompleteMomentState( mom );
|
||||
}
|
||||
|
||||
Moments ocl_moments( InputArray _array, bool binaryImage )
|
||||
{
|
||||
CvMoments om;
|
||||
Mat arr = _array.getMat();
|
||||
CvMat c_array = arr;
|
||||
ocl_cvMoments(&c_array, &om, binaryImage);
|
||||
return om;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
File diff suppressed because it is too large
Load Diff
@ -10,18 +10,19 @@ using namespace cvtest;
|
||||
using namespace testing;
|
||||
using namespace std;
|
||||
|
||||
PARAM_TEST_CASE(MomentsTest, MatType, bool)
|
||||
PARAM_TEST_CASE(MomentsTest, MatType, bool, bool)
|
||||
{
|
||||
int type;
|
||||
cv::Mat mat1;
|
||||
cv::Mat mat;
|
||||
bool test_contours;
|
||||
|
||||
bool binaryImage;
|
||||
virtual void SetUp()
|
||||
{
|
||||
type = GET_PARAM(0);
|
||||
test_contours = GET_PARAM(1);
|
||||
cv::Size size(10 * MWIDTH, 10 * MHEIGHT);
|
||||
mat1 = randomMat(size, type, 5, 16, false);
|
||||
mat = randomMat(size, type, 0, 256, false);
|
||||
binaryImage = GET_PARAM(2);
|
||||
}
|
||||
|
||||
void Compare(Moments& cpu, Moments& gpu)
|
||||
@ -29,16 +30,13 @@ PARAM_TEST_CASE(MomentsTest, MatType, bool)
|
||||
Mat gpu_dst, cpu_dst;
|
||||
HuMoments(cpu, cpu_dst);
|
||||
HuMoments(gpu, gpu_dst);
|
||||
EXPECT_MAT_NEAR(gpu_dst,cpu_dst, .5);
|
||||
EXPECT_MAT_NEAR(gpu_dst,cpu_dst, 1e-3);
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
|
||||
OCL_TEST_P(MomentsTest, Mat)
|
||||
{
|
||||
bool binaryImage = 0;
|
||||
|
||||
oclMat src_d(mat);
|
||||
for(int j = 0; j < LOOP_TIMES; j++)
|
||||
{
|
||||
if(test_contours)
|
||||
@ -53,18 +51,16 @@ OCL_TEST_P(MomentsTest, Mat)
|
||||
for( size_t i = 0; i < contours.size(); i++ )
|
||||
{
|
||||
Moments m = moments( contours[i], false );
|
||||
Moments dm = ocl::ocl_moments( contours[i], false );
|
||||
Moments dm = ocl::ocl_moments( contours[i]);
|
||||
Compare(m, dm);
|
||||
}
|
||||
}
|
||||
cv::_InputArray _array(mat1);
|
||||
cv::Moments CvMom = cv::moments(_array, binaryImage);
|
||||
cv::Moments oclMom = cv::ocl::ocl_moments(_array, binaryImage);
|
||||
cv::Moments CvMom = cv::moments(mat, binaryImage);
|
||||
cv::Moments oclMom = cv::ocl::ocl_moments(src_d, binaryImage);
|
||||
|
||||
Compare(CvMom, oclMom);
|
||||
|
||||
}
|
||||
}
|
||||
INSTANTIATE_TEST_CASE_P(OCL_ImgProc, MomentsTest, Combine(
|
||||
Values(CV_8UC1, CV_16UC1, CV_16SC1, CV_64FC1), Values(true,false)));
|
||||
Values(CV_8UC1, CV_16UC1, CV_16SC1, CV_32FC1, CV_64FC1), Values(false, true), Values(false, true)));
|
||||
#endif // HAVE_OPENCL
|
||||
|
Loading…
Reference in New Issue
Block a user