Esempio n. 1
0
/**
 * @author      JIA Pei
 * @version     2010-02-22
 * @brief       Calculate Image Patch cv::Rectangle
 * @param       iImg        Input    -- the concerned image
 * @param       pt          Input    -- the point
 * @param       imgSize     Input    -- image size
 * @return      cv::Rect    of size imgSize*imgSize
 */
cv::Rect VO_AFM::VO_CalcImagePatchRect(const cv::Mat& iImg, const cv::Point2f& pt, cv::Size imgSize)
{
    // ensure the small image patch is within the image
    cv::Rect rect;
    if(pt.x - imgSize.width/2 >= 0)
    {
        if(pt.x + imgSize.width/2 < iImg.cols)
            rect.x = cvRound( pt.x - imgSize.width/2);
        else
            rect.x = cvFloor(iImg.cols - imgSize.width);
    }
    else rect.x = 0;

    if(pt.y - imgSize.height/2 >= 0)
    {
        if(pt.y + imgSize.height/2 < iImg.rows)
            rect.y = cvRound(pt.y - imgSize.height/2);
        else
            rect.y = cvFloor(iImg.rows - imgSize.height);
    }
    else rect.y = 0;

    rect.width = imgSize.width;
    rect.height = imgSize.height;

    return rect;
}
Esempio n. 2
0
    static T getValue(const cv::Mat& src, float y, float x, int c, int border_type, cv::Scalar borderVal = cv::Scalar())
    {
        const float xmin = ceilf(x - 2.0f);
        const float xmax = floorf(x + 2.0f);

        const float ymin = ceilf(y - 2.0f);
        const float ymax = floorf(y + 2.0f);

        float sum  = 0.0f;
        float wsum = 0.0f;

        for (float cy = ymin; cy <= ymax; cy += 1.0f)
        {
            for (float cx = xmin; cx <= xmax; cx += 1.0f)
            {
                const float w = bicubicCoeff(x - cx) * bicubicCoeff(y - cy);
                sum += w * readVal<T>(src, cvFloor(cy), cvFloor(cx), c, border_type, borderVal);
                wsum += w;
            }
        }

        float res = (!wsum)? 0 : sum / wsum;

        return cv::saturate_cast<T>(res);
    }
Esempio n. 3
0
void margBlobCorrector::calculateLensUndistBounds() {
	
	lensUndistBounds.pts.clear();
	
	int u, v, i;
	float x0 = (float)inW/2.0f,
		  y0 = float(inH)/2.0f;
	float k1 = rX,  
		  k2 = rY,
	      k3 = 0,
	      p1 = tX,  
	      p2 = tY;
	
    for( v = 0; v < inH; v++)
    {
        float y = (v - cY)*ifY, y2 = y*y;
		
        for( u = 0; u < inW; u++ )
        {
            float x = (u - cX)*ifX,
				  x2 = x*x,
				  r2 = x2 + y2,
			      _2xy = 2*x*y;
            float kr = 1 + ((k3*r2 + k2)*r2 + k1)*r2;
            float _x = fX*(x*kr + p1*_2xy + p2*(r2 + 2*x2)) + x0;
            float _y = fY*(y*kr + p1*(r2 + 2*y2) + p2*_2xy) + y0;
            int   ix = cvFloor(_x),
				  iy = cvFloor(_y);
			
            lensUndistBounds.pts.push_back(ofPoint(ix, iy));
        }
    }
	lensUndistBounds.nPts = lensUndistBounds.pts.size();
}
Esempio n. 4
0
Rect RotatedRect::boundingRect() const
{
    Point2f pt[4];
    points(pt);
    Rect r(cvFloor(std::min(std::min(std::min(pt[0].x, pt[1].x), pt[2].x), pt[3].x)),
           cvFloor(std::min(std::min(std::min(pt[0].y, pt[1].y), pt[2].y), pt[3].y)),
           cvCeil(std::max(std::max(std::max(pt[0].x, pt[1].x), pt[2].x), pt[3].x)),
           cvCeil(std::max(std::max(std::max(pt[0].y, pt[1].y), pt[2].y), pt[3].y)));
    r.width -= r.x - 1;
    r.height -= r.y - 1;
    return r;
}
Esempio n. 5
0
static CvStatus
icvUnDistort_8u_CnR( const uchar* src, int srcstep,
                     uchar* dst, int dststep, CvSize size,
                     const float* intrinsic_matrix,
                     const float* dist_coeffs, int cn )
{
    int u, v, i;
    float u0 = intrinsic_matrix[2], v0 = intrinsic_matrix[5];
    float x0 = (size.width-1)*0.5f, y0 = (size.height-1)*0.5f;
    float fx = intrinsic_matrix[0], fy = intrinsic_matrix[4];
    float ifx = 1.f/fx, ify = 1.f/fy;
    float k1 = dist_coeffs[0], k2 = dist_coeffs[1], k3 = dist_coeffs[4];
    float p1 = dist_coeffs[2], p2 = dist_coeffs[3];

    srcstep /= sizeof(src[0]);
    dststep /= sizeof(dst[0]);

    for( v = 0; v < size.height; v++, dst += dststep )
    {
        float y = (v - v0)*ify, y2 = y*y;

        for( u = 0; u < size.width; u++ )
        {
            float x = (u - u0)*ifx, x2 = x*x, r2 = x2 + y2, _2xy = 2*x*y;
            float kr = 1 + ((k3*r2 + k2)*r2 + k1)*r2;
            float _x = fx*(x*kr + p1*_2xy + p2*(r2 + 2*x2)) + x0;
            float _y = fy*(y*kr + p1*(r2 + 2*y2) + p2*_2xy) + y0;
            int ix = cvFloor(_x), iy = cvFloor(_y);

            if( (unsigned)iy < (unsigned)(size.height - 1) &&
                (unsigned)ix < (unsigned)(size.width - 1) )
            {
                const uchar* ptr = src + iy*srcstep + ix*cn;
                _x -= ix; _y -= iy;
                for( i = 0; i < cn; i++ )
                {
                    float t0 = CV_8TO32F(ptr[i]), t1 = CV_8TO32F(ptr[i+srcstep]);
                    t0 += _x*(CV_8TO32F(ptr[i+cn]) - t0);
                    t1 += _x*(CV_8TO32F(ptr[i + srcstep + cn]) - t1);
                    dst[u*cn + i] = (uchar)cvRound(t0 + _y*(t1 - t0));
                }
            }
            else
            {
                for( i = 0; i < cn; i++ )
                    dst[u*cn + i] = 0;
            }
            
        }
    }

    return CV_OK;
}
Esempio n. 6
0
static void getRectSubPix_8u32f
( const uchar* src, size_t src_step, Size src_size,
  float* dst, size_t dst_step, Size win_size, Point2f center0, int cn )
{
    Point2f center = center0;
    Point ip;

    center.x -= (win_size.width-1)*0.5f;
    center.y -= (win_size.height-1)*0.5f;

    ip.x = cvFloor( center.x );
    ip.y = cvFloor( center.y );

    if( cn == 1 &&
            0 <= ip.x && ip.x + win_size.width < src_size.width &&
            0 <= ip.y && ip.y + win_size.height < src_size.height &&
            win_size.width > 0 && win_size.height > 0 )
    {
        float a = center.x - ip.x;
        float b = center.y - ip.y;
        a = MAX(a,0.0001f);
        float a12 = a*(1.f-b);
        float a22 = a*b;
        float b1 = 1.f - b;
        float b2 = b;
        double s = (1. - a)/a;

        src_step /= sizeof(src[0]);
        dst_step /= sizeof(dst[0]);

        // extracted rectangle is totally inside the image
        src += ip.y * src_step + ip.x;

        for( ; win_size.height--; src += src_step, dst += dst_step )
        {
            float prev = (1 - a)*(b1*src[0] + b2*src[src_step]);
            for( int j = 0; j < win_size.width; j++ )
            {
                float t = a12*src[j+1] + a22*src[j+1+src_step];
                dst[j] = prev + t;
                prev = (float)(t*s);
            }
        }
    }
    else
    {
        getRectSubPix_Cn_<uchar, float, float, nop<float>, nop<float> >
        (src, src_step, src_size, dst, dst_step, win_size, center0, cn );
    }
}
Esempio n. 7
0
 void Trajectory::drawOn (cv::Mat & image, const cv::Point2f & endPoint, const float fscale) const {
     int _len = int(m_pointDescs.size()), _idx = 0;
     cv::Point _prev, _cur;
     for (const auto & val : this->m_pointDescs) {
         _cur = cv::Point2f (val.m_point.x * fscale, val.m_point.y * fscale);
         if (_idx != 0) {
             cv::line (image, _prev, _cur, CV_RGB (0, cvFloor (255.0 * _idx / _len), 0), 2, 8, 0);
         }
         _idx ++;
         _prev = _cur;
     }
     _cur = cv::Point2f (endPoint.x * fscale, endPoint.y * fscale);
     cv::line (image, _prev, _cur, CV_RGB (0, cvFloor (255.0 * _idx / _len), 0), 2, 8, 0);
     cv::circle (image, endPoint, 2, CV_RGB (255, 0, 0), -1, 8, 0);
 }
Esempio n. 8
0
const vector<int> Index::createUMax()
{
    int patchSize = ROTATION_PATCH_SIZE;
    // Make sure we forget about what is too close to the boundary
    //edge_threshold_ = std::max(edge_threshold_, patch_size_/2 + kKernelWidth / 2 + 2);
    
    // pre-compute the end of a row in a circular patch
    int halfPatchSize = patchSize / 2;
    vector<int> u_max(halfPatchSize + 2);
    
    int v, v0, vmax = cvFloor(halfPatchSize * sqrt(2.f) / 2 + 1);
    int vmin = cvCeil(halfPatchSize * sqrt(2.f) / 2);
    for (v = 0; v <= vmax; ++v)
        u_max[v] = cvRound(sqrt((double)halfPatchSize * halfPatchSize - v * v));
    
    // Make sure we are symmetric
    for (v = halfPatchSize, v0 = 0; v >= vmin; --v)
    {
        while (u_max[v0] == u_max[v0 + 1])
            ++v0;
        u_max[v] = v0;
        ++v0;
    }
    return u_max;
}
Esempio n. 9
0
void Zernike::reconstruct(map< pair<int, int>, vector<double> >& rnm, const Mat& moments, const Mat& cnm, const Mat& snm, Mat& dst, int nmax, int N)
{
	Mat pqm = Mat(N / 2 + 1, 4 * N + 1, CV_8UC1, Scalar::all(0));
	double s = 0, th;
	for (int p = 1; p <= N/2; p++)
	{
		for (int q = 1; q <= 8 * p; q++)
		{
			s = 0;
			th = CV_PI*q / (4 * p);
			for (int n = 0; n <= nmax;n++)
			{
				for (int m = 1; m <= n;m++)
				{
					if ((n - m) % 2 == 0)
					{
						s += (cnm.at<double>(n, m)*cos(m*th) + snm.at<double>(n, m)*sin(m*th))*rnm[make_pair(n, m)][p];
					}
				}
			}
			s = s*N / 2;
			pqm.at<uchar>(p, q) = s > 255 ? 255 : cvFloor(s);
		}
	}

	inverse(pqm, dst, N, N);
}
Esempio n. 10
0
static bool ocl_threshold( InputArray _src, OutputArray _dst, double & thresh, double maxval, int thresh_type )
{
    int type = _src.type(), depth = CV_MAT_DEPTH(type), cn = CV_MAT_CN(type),
        kercn = ocl::predictOptimalVectorWidth(_src, _dst), ktype = CV_MAKE_TYPE(depth, kercn);
    bool doubleSupport = ocl::Device::getDefault().doubleFPConfig() > 0;

    if ( !(thresh_type == THRESH_BINARY || thresh_type == THRESH_BINARY_INV || thresh_type == THRESH_TRUNC ||
           thresh_type == THRESH_TOZERO || thresh_type == THRESH_TOZERO_INV) ||
         (!doubleSupport && depth == CV_64F))
        return false;

    const char * const thresholdMap[] = { "THRESH_BINARY", "THRESH_BINARY_INV", "THRESH_TRUNC",
                                          "THRESH_TOZERO", "THRESH_TOZERO_INV" };
    ocl::Kernel k("threshold", ocl::imgproc::threshold_oclsrc,
                  format("-D %s -D T=%s -D T1=%s%s", thresholdMap[thresh_type],
                         ocl::typeToStr(ktype), ocl::typeToStr(depth),
                         doubleSupport ? " -D DOUBLE_SUPPORT" : ""));
    if (k.empty())
        return false;

    UMat src = _src.getUMat();
    _dst.create(src.size(), type);
    UMat dst = _dst.getUMat();

    if (depth <= CV_32S)
        thresh = cvFloor(thresh);

    k.args(ocl::KernelArg::ReadOnlyNoSize(src), ocl::KernelArg::WriteOnly(dst, cn, kercn),
           ocl::KernelArg::Constant(Mat(1, 1, depth, Scalar::all(thresh))),
           ocl::KernelArg::Constant(Mat(1, 1, depth, Scalar::all(maxval))));

    size_t globalsize[2] = { dst.cols * cn / kercn, dst.rows };
    return k.run(2, globalsize, NULL, false);
}
Esempio n. 11
0
static void computeOrientation(const cv::Mat& image,
                               std::vector<cv::KeyPoint>& keypoints,
							   int halfPatchSize)
{
        std::vector<int> umax(halfPatchSize + 2);
        int v, v0, vmax = cvFloor(halfPatchSize * sqrt(2.f) / 2 + 1);  
        int vmin = cvCeil(halfPatchSize * sqrt(2.f) / 2);  
        for (v = 0; v <= vmax; ++v)  
            umax[v] = cvRound(sqrt((double)halfPatchSize * halfPatchSize - v * v));  
                  
        // Make sure we are symmetric  
        for (v = halfPatchSize, v0 = 0; v >= vmin; --v)  
        {  
            while (umax[v0] == umax[v0 + 1])  
               ++v0;  
               umax[v] = v0;  
               ++v0;  
        }  

	// Process each keypoint
    for (std::vector<cv::KeyPoint>::iterator keypoint = keypoints.begin(),
		keypointEnd = keypoints.end(); keypoint != keypointEnd; ++keypoint)
	{
		keypoint->angle = IC_Angle(image, halfPatchSize, keypoint->pt, umax);
	}
}
Esempio n. 12
0
void icvConvertIntToDecimal(const int ndigits, CvMat * src, CvMat * dst)
{
  const int nsamples = src->rows;
  const int nnumbers = src->cols;
  assert(dst->rows==nsamples);
  assert(CV_MAT_TYPE(src->type)==CV_32S);
  assert(CV_MAT_TYPE(dst->type)==CV_32F);
  CvMat * values = cvCreateMat(ndigits,10,CV_32F);
  int stepsize = ndigits*10*sizeof(float);
  for (int ii=0;ii<nsamples;ii++){
#if 0 // debug
    fprintf(stderr,"number: ");
    for (int jj=0;jj<nnumbers;jj++){
      fprintf(stderr,"%d ",CV_MAT_ELEM(*src,int,ii,jj));
    }
#endif
    for (int jj=0;jj<nnumbers;jj++){
      cvZero(values);
      int number = CV_MAT_ELEM(*src,int,ii,jj);
      for (int kk=0;kk<ndigits;kk++){
        int pos = cvFloor((number%int(pow(10.f,kk+1)))/pow(10.f,kk));
        CV_MAT_ELEM(*values,float,kk,pos)=1;
      }
      memcpy(dst->data.ptr+stepsize*(nnumbers*ii+jj),values->data.ptr,stepsize);
    }
#if 0 // debug
    fprintf(stderr,"\noutput:\n");
    cvPrintf(stderr,"%.0f ",dst,cvRect(0,ii,dst->cols,1));
#endif
  }
  cvReleaseMat(&values);
}
void CvMLData::set_train_test_split( const CvTrainTestSplit * spl)
{
    CV_FUNCNAME( "CvMLData::set_division" );
    __BEGIN__;

    int sample_count = 0;

    if ( !values )
        CV_ERROR( CV_StsInternal, "data is empty" );

    sample_count = values->rows;

    float train_sample_portion;

    if (spl->train_sample_part_mode == CV_COUNT)
    {
        train_sample_count = spl->train_sample_part.count;
        if (train_sample_count > sample_count)
            CV_ERROR( CV_StsBadArg, "train samples count is not correct" );
        train_sample_count = train_sample_count<=0 ? sample_count : train_sample_count;
    }
    else // dtype.train_sample_part_mode == CV_PORTION
    {
        train_sample_portion = spl->train_sample_part.portion;
        if ( train_sample_portion > 1)
            CV_ERROR( CV_StsBadArg, "train samples count is not correct" );
        train_sample_portion = train_sample_portion <= FLT_EPSILON ||
            1 - train_sample_portion <= FLT_EPSILON ? 1 : train_sample_portion;
        train_sample_count = std::max(1, cvFloor( train_sample_portion * sample_count ));
    }

    if ( train_sample_count == sample_count )
    {
        free_train_test_idx();
        return;
    }

    if ( train_sample_idx && train_sample_idx->cols != train_sample_count )
        free_train_test_idx();

    if ( !sample_idx)
    {
        int test_sample_count = sample_count- train_sample_count;
        sample_idx = (int*)cvAlloc( sample_count * sizeof(sample_idx[0]) );
        for (int i = 0; i < sample_count; i++ )
            sample_idx[i] = i;
        train_sample_idx = cvCreateMatHeader( 1, train_sample_count, CV_32SC1 );
        *train_sample_idx = cvMat( 1, train_sample_count, CV_32SC1, &sample_idx[0] );

        CV_Assert(test_sample_count > 0);
        test_sample_idx = cvCreateMatHeader( 1, test_sample_count, CV_32SC1 );
        *test_sample_idx = cvMat( 1, test_sample_count, CV_32SC1, &sample_idx[train_sample_count] );
    }

    mix = spl->mix;
    if ( mix )
        mix_train_and_test_idx();

    __END__;
}
Esempio n. 14
0
    TheTest & test_float_math()
    {
        typedef typename V_RegTrait128<LaneType>::int_reg Ri;
        Data<R> data1, data2, data3;
        data1 *= 1.1;
        data2 += 10;
        R a1 = data1, a2 = data2, a3 = data3;

        Data<Ri> resB = v_round(a1),
                 resC = v_trunc(a1),
                 resD = v_floor(a1),
                 resE = v_ceil(a1);

        Data<R> resF = v_magnitude(a1, a2),
                resG = v_sqr_magnitude(a1, a2),
                resH = v_muladd(a1, a2, a3);

        for (int i = 0; i < R::nlanes; ++i)
        {
            EXPECT_EQ(cvRound(data1[i]), resB[i]);
            EXPECT_EQ((typename Ri::lane_type)data1[i], resC[i]);
            EXPECT_EQ(cvFloor(data1[i]), resD[i]);
            EXPECT_EQ(cvCeil(data1[i]), resE[i]);

            EXPECT_COMPARE_EQ(std::sqrt(data1[i]*data1[i] + data2[i]*data2[i]), resF[i]);
            EXPECT_COMPARE_EQ(data1[i]*data1[i] + data2[i]*data2[i], resG[i]);
            EXPECT_COMPARE_EQ(data1[i]*data2[i] + data3[i], resH[i]);
        }

        return *this;
    }
Esempio n. 15
0
main( int argc, char* argv[] ) {

    // Choose a negative floating point number.  Take its absolute value,
    // round it, and then take its ceiling and floor.
    double a = -1.23;
    printf( "CV_IABS(a) = %d\n", CV_IABS(a) );
    printf( "cvRound(a) = %d\n", cvRound(a) );
    printf( "cvCeil(a) = %d\n", cvCeil(a) );
    printf( "cvFloor(a) = %d\n", cvFloor(a) );


    // Generate some random numbers.
    CvRNG rngState = cvRNG(-1);
    for (int i = 0; i < 10; i++) {
        printf( "%u %f\n", cvRandInt( &rngState ),
                           cvRandReal( &rngState ) );
    }

    // Create a floating point CvPoint2D32f and convert it to an integer
    // CvPoint.
    CvPoint2D32f point_float1 = cvPoint2D32f(1.0, 2.0);
    CvPoint point_int1 = cvPointFrom32f( point_float1 );

    // Convert a CvPoint to a CvPoint2D32f.
    CvPoint point_int2 = cvPoint(3, 4);
    CvPoint2D32f point_float2 = cvPointTo32f( point_int2 );

}
Esempio n. 16
0
    static T getValue(const cv::Mat& src, float y, float x, int c, int border_type, cv::Scalar borderVal = cv::Scalar())
    {
        int x1 = cvFloor(x);
        int y1 = cvFloor(y);
        int x2 = x1 + 1;
        int y2 = y1 + 1;

        float res = 0;

        res += readVal<T>(src, y1, x1, c, border_type, borderVal) * ((x2 - x) * (y2 - y));
        res += readVal<T>(src, y1, x2, c, border_type, borderVal) * ((x - x1) * (y2 - y));
        res += readVal<T>(src, y2, x1, c, border_type, borderVal) * ((x2 - x) * (y - y1));
        res += readVal<T>(src, y2, x2, c, border_type, borderVal) * ((x - x1) * (y - y1));

        return cv::saturate_cast<T>(res);
    }
Esempio n. 17
0
static void
icvSumCol_32s8u( const int** src, uchar* dst,
                 int dst_step, int count, void* params )
{
#define BLUR_SHIFT 24
    CvBoxFilter* state = (CvBoxFilter*)params;
    int ksize = state->get_kernel_size().height;
    int i, width = state->get_width();
    int cn = CV_MAT_CN(state->get_src_type());
    double scale = state->get_scale();
    int iscale = cvFloor(scale*(1 << BLUR_SHIFT));
    int* sum = (int*)state->get_sum_buf();
    int* _sum_count = state->get_sum_count_ptr();
    int sum_count = *_sum_count;

    width *= cn;
    src += sum_count;
    count += ksize - 1 - sum_count;

    for( ; count--; src++ )
    {
        const int* sp = src[0];
        if( sum_count+1 < ksize )
        {
            for( i = 0; i <= width - 2; i += 2 )
            {
                int s0 = sum[i] + sp[i], s1 = sum[i+1] + sp[i+1];
                sum[i] = s0; sum[i+1] = s1;
            }

            for( ; i < width; i++ )
                sum[i] += sp[i];

            sum_count++;
        }
        else
        {
            const int* sm = src[-ksize+1];
            for( i = 0; i <= width - 2; i += 2 )
            {
                int s0 = sum[i] + sp[i], s1 = sum[i+1] + sp[i+1];
                int t0 = CV_DESCALE(s0*iscale, BLUR_SHIFT), t1 = CV_DESCALE(s1*iscale, BLUR_SHIFT);
                s0 -= sm[i]; s1 -= sm[i+1];
                sum[i] = s0; sum[i+1] = s1;
                dst[i] = (uchar)t0; dst[i+1] = (uchar)t1;
            }

            for( ; i < width; i++ )
            {
                int s0 = sum[i] + sp[i], t0 = CV_DESCALE(s0*iscale, BLUR_SHIFT);
                sum[i] = s0 - sm[i]; dst[i] = (uchar)t0;
            }
            dst += dst_step;
        }
    }

    *_sum_count = sum_count;
#undef BLUR_SHIFT
}
Esempio n. 18
0
/*************************************************************************
* @函数名称:
*	calMISSIM()
* @输入:
*   const IplImage* image1           - 输入图像1
*   const IplImage* image2           - 输入图像2
*	int n							 - 每个方块的大小
* @返回值:
*   double MISSIM                    - 返回图像的平均改进结构相似度
* @说明:
*   计算图像的平均改进结构相似度
**************************************************************************/
double calMISSIM(const IplImage* image1, const IplImage* image2, int n)
{
	double MISSIM = 0;
	int i, j, k;
	int row1 = image1->height;
	int col1 = image1->width;
	int row2 = image2->height;
	int col2 = image2->width;
	if (row1 != row2 || col1 != col2)
	{
		printf("Size can't match in calMISSIM()!!");
	}

	int nr = cvFloor(row1 / n);
	int nc = cvFloor(col1 / n);
	int N = nr*nc;
	double ISSIM=0;
	double sum = 0;

	CvMat tmp1;
	CvMat tmp2;
	IplImage* temp1 = cvCreateImage(cvSize(n, n), image1->depth, image1->nChannels);
	IplImage* temp2 = cvCreateImage(cvSize(n, n), image1->depth, image1->nChannels);

	for (i = 0, k = 0; i < nr; i++)
	{
		for (j = 0; j < nc; j++, k++)
		{
			cvGetSubRect(image1, &tmp1, cvRect(j*n, i*n, n, n));
			cvGetSubRect(image2, &tmp2, cvRect(j*n, i*n, n, n));

			cvScale(&tmp1, temp1, 1, 0);
			cvScale(&tmp2, temp2, 1, 0);

			ISSIM = calISSIM(temp1, temp2);
			sum += ISSIM;
		}
	}

	MISSIM = sum / N;
	cvReleaseImage(&temp1);
	cvReleaseImage(&temp2);

	return MISSIM;
}
void CMagicCabsineUniversalProperty_feature_texture_spectral_DFT::SplitImage(const IplImage *imgSrc, const int n, vector<IplImage*> &subImgs)
{
	IplImage *img = cvCloneImage(imgSrc);
	int subWidth  = cvFloor(img->width/n);
	int subHeight = cvFloor(img->height/n);
	for(int y=0; y<n; y++)//from top to bottom, left to right
	{
		for(int x=0; x<n; x++)
		{
			cvSetImageROI(img, cvRect(x*subWidth, y*subHeight, subWidth, subHeight));
			IplImage *roiImg = cvCreateImage(cvSize(subWidth,subHeight),img->depth,3);
			cvCopy(img, roiImg, 0);
			subImgs.push_back(roiImg);
			cvResetImageROI(img);
		}
	}
	cvReleaseImage(&img);
}
Esempio n. 20
0
/*
Interpolates an entry into the array of orientation histograms that form
the feature descriptor.

@param hist 2D array of orientation histograms
@param rbin sub-bin row coordinate of entry
@param cbin sub-bin column coordinate of entry
@param obin sub-bin orientation coordinate of entry
@param mag size of entry
@param d width of 2D array of orientation histograms
@param n number of bins per orientation histogram
*/
void interp_hist_entry( double*** hist, double rbin, double cbin,
					   double obin, double mag, int d, int n )
{
	double d_r, d_c, d_o, v_r, v_c, v_o;
	double** row, * h;
	int r0, c0, o0, rb, cb, ob, r, c, o;

	r0 = cvFloor( rbin );
	c0 = cvFloor( cbin );
	o0 = cvFloor( obin );
	d_r = rbin - r0;
	d_c = cbin - c0;
	d_o = obin - o0;

	/*
	The entry is distributed into up to 8 bins.  Each entry into a bin
	is multiplied by a weight of 1 - d for each dimension, where d is the
	distance from the center value of the bin measured in bin units.
	*/
	for( r = 0; r <= 1; r++ )
	{
		rb = r0 + r;
		if( rb >= 0  &&  rb < d )
		{
			v_r = mag * ( ( r == 0 )? 1.0 - d_r : d_r );
			row = hist[rb];
			for( c = 0; c <= 1; c++ )
			{
				cb = c0 + c;
				if( cb >= 0  &&  cb < d )
				{
					v_c = v_r * ( ( c == 0 )? 1.0 - d_c : d_c );
					h = row[cb];
					for( o = 0; o <= 1; o++ )
					{
						ob = ( o0 + o ) % n;
						v_o = v_c * ( ( o == 0 )? 1.0 - d_o : d_o );
						h[ob] += v_o;
					}
				}
			}
		}
	}
}
Esempio n. 21
0
static void
intersect( CvPoint2D32f pt, CvSize win_size, CvSize imgSize,
           CvPoint* min_pt, CvPoint* max_pt )
{
    CvPoint ipt;

    ipt.x = cvFloor( pt.x );
    ipt.y = cvFloor( pt.y );

    ipt.x -= win_size.width;
    ipt.y -= win_size.height;

    win_size.width = win_size.width * 2 + 1;
    win_size.height = win_size.height * 2 + 1;

    min_pt->x = MAX( 0, -ipt.x );
    min_pt->y = MAX( 0, -ipt.y );
    max_pt->x = MIN( win_size.width, imgSize.width - ipt.x );
    max_pt->y = MIN( win_size.height, imgSize.height - ipt.y );
}
Esempio n. 22
0
CvScalar Kinect::hsv2rgb(float hue)
{
	int rgb[3], p, sector;
	static const int sector_data[][3] =
	{ { 0, 2, 1 }, { 1, 2, 0 }, { 1, 0, 2 }, { 2, 0, 1 }, { 0, 1, 2 } };
	hue *= 0.0333333333333f;
	sector = cvFloor(hue);//hue 값을 버림하여 정수형으로 변환
	p = cvRound(255 * (hue - sector));
	/*(홀수&1)=1 (짝수&1)=0 sector가 홀수면 : sector&1 =1 ==>p=255 */
	p^=sector &1 ?255:0;//secotr가 짝수면:(sector&1=0==>p=0
	rgb[sector_data[sector][0]]=255;
	rgb[sector_data[sector][1]]=0;
	rgb[sector_data[sector][2]]=p;
	return cvScalar(rgb[2],rgb[1],rgb[0],0);
}
CvScalar CamShift::_HSV2RGB( float _hue )
{
	int rgb[3], p, sector;
	static const int sector_data[][3] =
	{{0,2,1}, {1,2,0}, {1,0,2}, {2,0,1}, {2,1,0}, {0,1,2}};
	_hue *= 0.033333333333333333333333333333333f;
	sector = cvFloor(_hue);
	p = cvRound(255*(_hue - sector));
	p ^= sector & 1 ? 255 : 0;

	rgb[sector_data[sector][0]] = 255;
	rgb[sector_data[sector][1]] = 0;
	rgb[sector_data[sector][2]] = p;

	return cvScalar(rgb[2], rgb[1], rgb[0], 0);
}
Esempio n. 24
0
CvScalar CamShiftIris::hsvrgb( float hue1 )
{
    int rgb[3], p, sector;
    static const int sector_data[][3]=
        {{0,2,1}, {1,2,0}, {1,0,2}, {2,0,1}, {2,1,0}, {0,1,2}};
    hue1 *= 0.033333333333333333333333333333333f;
    sector = cvFloor(hue1);
    p = cvRound(255*(hue1 - sector));
    p ^= sector & 1 ? 255 : 0;

    rgb[sector_data[sector][0]] = 255;
    rgb[sector_data[sector][1]] = 0;
    rgb[sector_data[sector][2]] = p;

    return cvScalar(rgb[2], rgb[1], rgb[0],0);
}
void ApplyWeakClassifier(WEAKCLASSIFIER &wc, vector<int> &vecResult, CvMat *features_matrix, vector<int> &vecLabel, vector<float> &vecWeight)
{
	int numSamples = features_matrix->rows;
	int numFeatures = features_matrix->cols;

	vector<float> feature_dim, feature_dim_sort;
	for (int s = 0;s < numSamples;s++)
	{
		feature_dim.push_back(features_matrix->data.fl[s*numFeatures+wc.iDim]);
		feature_dim_sort.push_back(features_matrix->data.fl[s*numFeatures+wc.iDim]);
	}
	std::sort(feature_dim_sort.begin(), feature_dim_sort.end());
	float fval_thresh = feature_dim_sort[cvFloor(numSamples*wc.fThr)];

	vecResult.resize(vecLabel.size());

	if (wc.iDirection == 1)
	{
		for (int s = 0;s < numSamples;s++)
		{
			if (feature_dim[s] >= fval_thresh)
				vecResult[s] = 1;
			else
				vecResult[s] = -1;
		}
	}
	else if (wc.iDirection == -1)
	{
		for (int s = 0;s < numSamples;s++)
		{
			if (feature_dim[s] >= fval_thresh)
				vecResult[s] = -1;
			else
				vecResult[s] = 1;
		}
	}

	wc.fError = 0;
	for (int s = 0; s < numSamples; s++)
		wc.fError = wc.fError + abs(vecLabel[s] - vecResult[s]) / 2 * vecWeight[s];
	if (wc.fError == 0)
		wc.fAlpha = 0.5 * log((1-wc.fError)/ 1e-5);
	else
		wc.fAlpha = 0.5 * log((1-wc.fError)/(wc.fError));

	wc.fThreshold = fval_thresh;
}
Esempio n. 26
0
CvScalar th_hsv2bgr_alt(float hue) {
	int rgb[3], p, sector;
	while ((hue >= 180))
		hue = hue - 180;

	while ((hue < 0))
		hue = hue + 180;

	int sectorData[][3] = { { 0, 2, 1 }, { 1, 2, 0 }, { 1, 0, 2 }, { 2, 0, 1 }, { 2, 1, 0 }, { 0, 1, 2 } };
	hue *= 0.033333333333333333333333333333333f;
	sector = cvFloor(hue);
	p = cvRound(255 * (hue - sector));
	p ^= sector & 1 ? 255 : 0;
	rgb[sectorData[sector][0]] = 255;
	rgb[sectorData[sector][1]] = 0;
	rgb[sectorData[sector][2]] = p;
	return cvScalar(rgb[2], rgb[1], rgb[0], 0);
}
CvScalar hsv2rgb(float hue)
{
	int rgb[3], p, sector;
	static const int sector_data[][3] =
	{ { 0, 2, 1 }, { 1, 2, 0 }, { 1, 0, 2 }, { 2, 0, 1 }, { 2, 1, 0 }, { 0, 1, 2 } };
	hue *= 0.033333333333333333333333333333333f;
	sector = cvFloor(hue);  //捨去小數點
	//std::cout<<"sector:" << sector << std::endl;
	p = cvRound(255 * (hue - sector));//取最近整數
	printf("\np before:%d\n", p);
	printf("sector:%d\n", sector);
	p ^= sector ? 255 : 0;    //當sector!=0 則 p^=255  ===> 即p=p^11111111(做XOR運算),相當於減法;    當sector=0則p^=0  ===> 即p與0做XOR運算,相當於不改變值
	printf("p after:%d\n", p);
	rgb[sector_data[sector][0]] = 255;
	rgb[sector_data[sector][1]] = 0;
	rgb[sector_data[sector][2]] = p;
	return cvScalar(rgb[2], rgb[1], rgb[0], 0);
}
Esempio n. 28
0
static bool ocl_threshold( InputArray _src, OutputArray _dst, double & thresh, double maxval, int thresh_type )
{
    int type = _src.type(), depth = CV_MAT_DEPTH(type), cn = CV_MAT_CN(type),
        kercn = ocl::predictOptimalVectorWidth(_src, _dst), ktype = CV_MAKE_TYPE(depth, kercn);
    bool doubleSupport = ocl::Device::getDefault().doubleFPConfig() > 0;

    if ( !(thresh_type == THRESH_BINARY || thresh_type == THRESH_BINARY_INV || thresh_type == THRESH_TRUNC ||
           thresh_type == THRESH_TOZERO || thresh_type == THRESH_TOZERO_INV) ||
         (!doubleSupport && depth == CV_64F))
        return false;

    const char * const thresholdMap[] = { "THRESH_BINARY", "THRESH_BINARY_INV", "THRESH_TRUNC",
                                          "THRESH_TOZERO", "THRESH_TOZERO_INV" };
    ocl::Device dev = ocl::Device::getDefault();
    int stride_size = dev.isIntel() && (dev.type() & ocl::Device::TYPE_GPU) ? 4 : 1;

    ocl::Kernel k("threshold", ocl::imgproc::threshold_oclsrc,
                  format("-D %s -D T=%s -D T1=%s -D STRIDE_SIZE=%d%s", thresholdMap[thresh_type],
                         ocl::typeToStr(ktype), ocl::typeToStr(depth), stride_size,
                         doubleSupport ? " -D DOUBLE_SUPPORT" : ""));
    if (k.empty())
        return false;

    UMat src = _src.getUMat();
    _dst.create(src.size(), type);
    UMat dst = _dst.getUMat();

    if (depth <= CV_32S)
        thresh = cvFloor(thresh);

    const double min_vals[] = { 0, CHAR_MIN, 0, SHRT_MIN, INT_MIN, -FLT_MAX, -DBL_MAX, 0 };
    double min_val = min_vals[depth];

    k.args(ocl::KernelArg::ReadOnlyNoSize(src), ocl::KernelArg::WriteOnly(dst, cn, kercn),
           ocl::KernelArg::Constant(Mat(1, 1, depth, Scalar::all(thresh))),
           ocl::KernelArg::Constant(Mat(1, 1, depth, Scalar::all(maxval))),
           ocl::KernelArg::Constant(Mat(1, 1, depth, Scalar::all(min_val))));

    size_t globalsize[2] = { static_cast<size_t>(dst.cols * cn / kercn), static_cast<size_t>(dst.rows) };
    globalsize[1] = (globalsize[1] + stride_size - 1) / stride_size;
    return k.run(2, globalsize, NULL, false);
}
const CvMat* CvMLData::get_var_idx()
{
     CV_FUNCNAME( "CvMLData::get_var_idx" );
    __BEGIN__;

    int avcount = 0;

    if ( !values )
        CV_ERROR( CV_StsInternal, "data is empty" );

    assert( var_idx_mask );

    avcount = cvFloor( cvNorm( var_idx_mask, 0, CV_L1 ) );
    int* vidx;

    if ( avcount == values->cols )
        return 0;

    if ( !var_idx_out || ( var_idx_out && var_idx_out->cols != avcount ) )
    {
        cvReleaseMat( &var_idx_out );
        var_idx_out = cvCreateMat( 1, avcount, CV_32SC1);
        if ( response_idx >=0 )
            var_idx_mask->data.ptr[response_idx] = 0;
    }

    vidx = var_idx_out->data.i;

    for(int i = 0; i < var_idx_mask->cols; i++)
        if ( var_idx_mask->data.ptr[i] )
        {
            *vidx = i;
            vidx++;
        }

    __END__;

    return var_idx_out;
}
const CvMat* CvMLData::get_var_types()
{
    CV_FUNCNAME( "CvMLData::get_var_types" );
    __BEGIN__;

    uchar *var_types_out_ptr = 0;
    int avcount, vt_size;
    if ( !values )
        CV_ERROR( CV_StsInternal, "data is empty" );

    assert( var_idx_mask );

    avcount = cvFloor( cvNorm( var_idx_mask, 0, CV_L1 ) );
    vt_size = avcount + (response_idx >= 0);

    if ( avcount == values->cols || (avcount == values->cols-1 && response_idx == values->cols-1) )
        return var_types;

    if ( !var_types_out || ( var_types_out && var_types_out->cols != vt_size ) )
    {
        cvReleaseMat( &var_types_out );
        var_types_out = cvCreateMat( 1, vt_size, CV_8UC1 );
    }

    var_types_out_ptr = var_types_out->data.ptr;
    for( int i = 0; i < var_types->cols; i++)
    {
        if (i == response_idx || !var_idx_mask->data.ptr[i]) continue;
        *var_types_out_ptr = var_types->data.ptr[i];
        var_types_out_ptr++;
    }
    if ( response_idx >= 0 )
        *var_types_out_ptr = var_types->data.ptr[response_idx];

    __END__;

    return var_types_out;
}