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; if( CV_IS_SEQ( array )) { contour = (CvSeq*)(array); if( !CV_IS_SEQ_POINT_SET( contour )) CV_Error( CV_StsBadArg, "The passed sequence is not a valid contour" ); } int type, coi = 0; CvMat stub, *mat = (CvMat*)(array); CvContour contourHeader; CvSeqBlock block; if( !contour ) { mat = cvGetMat( mat, &stub, &coi ); type = CV_MAT_TYPE( mat->type ); if( type == CV_32SC2 || type == CV_32FC2 ) { contour = cvPointSeqFromMat( CV_SEQ_KIND_CURVE | CV_SEQ_FLAG_CLOSED, mat, &contourHeader, &block ); } } CV_Assert(contour); icvContourMoments(contour, &mom); return mom; }
static void ocl_cvMoments( const void* array, CvMoments* mom, int binary ) { const int TILE_SIZE = 256; int type, depth, cn, coi = 0; CvMat stub, *mat = (CvMat*)array; CvContour contourHeader; CvSeq* contour = 0; CvSeqBlock block; if( CV_IS_SEQ( array )) { contour = (CvSeq*)array; if( !CV_IS_SEQ_POINT_SET( contour )) CV_Error( CV_StsBadArg, "The passed sequence is not a valid contour" ); } if( !moments ) CV_Error( CV_StsNullPtr, "" ); memset( mom, 0, sizeof(*mom)); if( !contour ) { mat = cvGetMat( mat, &stub, &coi ); type = CV_MAT_TYPE( mat->type ); if( type == CV_32SC2 || type == CV_32FC2 ) { contour = cvPointSeqFromMat( CV_SEQ_KIND_CURVE | CV_SEQ_FLAG_CLOSED, mat, &contourHeader, &block ); } } if( contour ) { icvContourMoments( contour, mom ); return; } 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; cv::ocl::oclMat dst_m(blocky * 10, blockx, CV_64FC1); cl_mem sum = openCLCreateBuffer(src.clCxt,CL_MEM_READ_WRITE,10*sizeof(double)); 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}; std::vector<std::pair<size_t , const void *> > args,args_sum; args.push_back( std::make_pair( sizeof(cl_mem) , (void *)&src.data )); args.push_back( std::make_pair( sizeof(cl_int) , (void *)&src.rows )); args.push_back( std::make_pair( sizeof(cl_int) , (void *)&src.cols )); args.push_back( std::make_pair( sizeof(cl_int) , (void *)&src.step )); args.push_back( std::make_pair( sizeof(cl_int) , (void *)&tileSize.width )); args.push_back( std::make_pair( sizeof(cl_int) , (void *)&tileSize.height )); args.push_back( std::make_pair( sizeof(cl_mem) , (void *)&dst_m.data )); args.push_back( std::make_pair( sizeof(cl_int) , (void *)&dst_m.cols )); args.push_back( std::make_pair( sizeof(cl_int) , (void *)&dst_m.step )); args.push_back( std::make_pair( sizeof(cl_int) , (void *)&blocky )); args.push_back( std::make_pair( sizeof(cl_int) , (void *)&type )); args.push_back( std::make_pair( sizeof(cl_int) , (void *)&depth )); args.push_back( std::make_pair( sizeof(cl_int) , (void *)&cn )); args.push_back( std::make_pair( sizeof(cl_int) , (void *)&coi )); args.push_back( std::make_pair( sizeof(cl_int) , (void *)&binary )); args.push_back( std::make_pair( sizeof(cl_int) , (void *)&TILE_SIZE )); openCLExecuteKernel(dst_m.clCxt, &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( std::make_pair( sizeof(cl_int) , (void *)&src.rows )); args_sum.push_back( std::make_pair( sizeof(cl_int) , (void *)&src.cols )); args_sum.push_back( std::make_pair( sizeof(cl_int) , (void *)&tile_height )); args_sum.push_back( std::make_pair( sizeof(cl_int) , (void *)&tile_width )); args_sum.push_back( std::make_pair( sizeof(cl_int) , (void *)&TILE_SIZE )); args_sum.push_back( std::make_pair( sizeof(cl_mem) , (void *)&sum )); args_sum.push_back( std::make_pair( sizeof(cl_mem) , (void *)&dst_m.data )); args_sum.push_back( std::make_pair( sizeof(cl_int) , (void *)&dst_m.step )); openCLExecuteKernel(dst_m.clCxt, &moments, "dst_sum", globalThreadss, localThreadss, args_sum, -1, -1); double* dstsum = new double[10]; memset(dstsum,0,10*sizeof(double)); openCLReadBuffer(dst_m.clCxt,sum,(void *)dstsum,10*sizeof(double)); mom->m00 = dstsum[0]; mom->m10 = dstsum[1]; mom->m01 = dstsum[2]; mom->m20 = dstsum[3]; mom->m11 = dstsum[4]; mom->m02 = dstsum[5]; mom->m30 = dstsum[6]; mom->m21 = dstsum[7]; mom->m12 = dstsum[8]; mom->m03 = dstsum[9]; delete [] dstsum; icvCompleteMomentState( mom ); }