Example #1
0
//! find keypoints and compute it's response if _bNonMaxSupression is true
//! return count of detected keypoints
//return the # of keypoints, stored in _uCount;
//store Key point locations into _cvgmKeyPointLocation;
//store the corner strength into _cvgmScore;
int CFast::calcKeyPointsLocation(const cv::gpu::GpuMat& cvgmImg_, const cv::gpu::GpuMat& cvgmMask_)
{
	//using namespace cv::gpu::device::fast;

	CV_Assert(cvgmImg_.type() == CV_8UC1);
	CV_Assert(cvgmMask_.empty() || (cvgmMask_.type() == CV_8UC1 && cvgmMask_.size() == cvgmImg_.size()));

	if (!cv::gpu::TargetArchs::builtWith(cv::gpu::GLOBAL_ATOMICS) || !cv::gpu::DeviceInfo().supports(cv::gpu::GLOBAL_ATOMICS))
		CV_Error(CV_StsNotImplemented, "The device doesn't support global atomics");

	unsigned int uMaxKeypoints = static_cast<unsigned int>(_dKeyPointsRatio * cvgmImg_.size().area());

	ensureSizeIsEnough(1, uMaxKeypoints, CV_16SC2, _cvgmKeyPointLocation);

	if (_bNonMaxSupression)
	{
		ensureSizeIsEnough(cvgmImg_.size(), CV_32SC1, _cvgmScore);
		_cvgmScore.setTo(cv::Scalar::all(0));
	}

	_uCount = btl::device::fast::cudaCalcKeypoints(cvgmImg_, cvgmMask_, uMaxKeypoints, _nThreshold, _cvgmKeyPointLocation.ptr<short2>(), _bNonMaxSupression ? &_cvgmScore : NULL);
	_uCount = std::min(_uCount, uMaxKeypoints);

	return _uCount;
}
void LukasKanadeOpticalFlow::drawMotionField_GPU(cv::gpu::GpuMat &imgU, cv::gpu::GpuMat &imgV, cv::gpu::GpuMat &imgMotion,
					 int xSpace, int ySpace, float minCutoff, float maxCutoff, float multiplier, CvScalar color)
{
	cv::Mat uMat( imgU );
	cv::Mat vMat( imgV );
	cv::Mat drawMat(cv::Size( imgU.size().width, imgU.size().height), CV_8UC3 );
	int x = 0, y = 0;
	float *ptri;
	float deltaX = 0.0, deltaY = 0.0, angle = 0.0, hyp = 0.0;
	cv::Point p0, p1;

	for( y = ySpace; y < uMat.rows; y += ySpace )
	{
		for(x = xSpace; x < uMat.cols; x += xSpace )
		{
			p0.x = x;
			p0.y = y;

			ptri = uMat.ptr<float>(y);
			deltaX = ptri[x];

			ptri = vMat.ptr<float>(y);
			deltaY = ptri[x];

			angle = atan2(deltaY, deltaX);
			hyp = sqrt(deltaX*deltaX + deltaY*deltaY);

			if( hyp > minCutoff && hyp < maxCutoff )
			{
				p1.x = p0.x + cvRound(multiplier*hyp*cos(angle));
				p1.y = p0.y + cvRound(multiplier*hyp*sin(angle));

				cv::line(drawMat,p0,p1,color,1,CV_AA,0);

				/*
				p0.x = p1.x + cvRound(2*cos(angle-M_PI + M_PI/4));
				p0.y = p1.y + cvRound(2*sin(angle-M_PI + M_PI/4));
				cv::line( imgMotion, p0, p1, color,1, CV_AA, 0);

				p0.x = p1.x + cvRound(2*cos(angle-M_PI - M_PI/4));
				p0.y = p1.y + cvRound(2*sin(angle-M_PI - M_PI/4));
				cv::line( imgMotion, p0, p1, color,1, CV_AA, 0);
				*/
			}
		}
	}

	imgMotion.upload( drawMat );
}
void sg::uncropFFTGPU( cv::gpu::GpuMat const & input, cv::gpu::GpuMat & output, std::vector<cv::gpu::GpuMat> & splitBuffer )
{
	cv::Size originalSize = input.size();
	cv::Size oldSize = output.size();

  if( input.channels() > 1 )
  {
		cv::gpu::split( input, splitBuffer );
    splitBuffer[0]( cv::Rect( 0, 0, oldSize.width, oldSize.height ) ).copyTo( output );
  }
	else
  	input( cv::Rect( 0, 0, oldSize.width, oldSize.height ) ).copyTo( output );

	cv::gpu::multiply( output, ( 1.0 / ( originalSize.width * originalSize.height ) ), output );
}
void MotionSubtraction::subtractGlobalMotion(cv::gpu::GpuMat &in__flowVector3DAngle, cv::gpu::GpuMat &in__flowVector3DMagnitude, cv::gpu::GpuMat &in__globalMotionX, cv::gpu::GpuMat &in__globalMotionY, cv::gpu::GpuMat &out__subtractedAngle, cv::gpu::GpuMat &out__subtractedMagnitude) {
    const int64 start = cv::getTickCount();

    //----------------------------------------------------------------------------------------
    // convert from magnitde/angle to x/y
    //----------------------------------------------------------------------------------------
    cv::gpu::GpuMat subtractedX(in__flowVector3DAngle.size(), CV_32FC1, cv::Scalar(0.0f));
    cv::gpu::GpuMat subtractedY(in__flowVector3DAngle.size(), CV_32FC1, cv::Scalar(0.0f));
    cv::gpu::GpuMat flowVector3DX, flowVector3DY;
    cv::gpu::polarToCart(in__flowVector3DMagnitude, in__flowVector3DAngle, flowVector3DX, flowVector3DY, true);



    //---------------------------------------------------------------------------------------------------------------------------------
    // segmentation
    //---------------------------------------------------------------------------------------------------------------------------------
    kernel.subtractMotion(flowVector3DX, flowVector3DY, in__globalMotionX, in__globalMotionY, subtractedX, subtractedY);



    //----------------------------------------------------------------------------------------
    // convert from x/y to magnitde/angle
    //----------------------------------------------------------------------------------------
    cv::gpu::cartToPolar(subtractedX, subtractedY, out__subtractedMagnitude, out__subtractedAngle, true);



    //---------------------------------------------------------------------------------------------------------------------------------
    // display computation time
    //---------------------------------------------------------------------------------------------------------------------------------
    const double timeSec = (cv::getTickCount() - start) / cv::getTickFrequency();
    std::cout << "Motion subtr : \t" << timeSec << " sec" << std::endl;

}
Example #5
0
void cv::gpu::GMG_GPU::operator ()(const cv::gpu::GpuMat& frame, cv::gpu::GpuMat& fgmask, float newLearningRate, cv::gpu::Stream& stream)
{
    using namespace cv::gpu::cudev::bgfg_gmg;

    typedef void (*func_t)(PtrStepSzb frame, PtrStepb fgmask, PtrStepSzi colors, PtrStepf weights, PtrStepi nfeatures,
                           int frameNum, float learningRate, bool updateBackgroundModel, cudaStream_t stream);
    static const func_t funcs[6][4] =
    {
        {update_gpu<uchar>, 0, update_gpu<uchar3>, update_gpu<uchar4>},
        {0,0,0,0},
        {update_gpu<ushort>, 0, update_gpu<ushort3>, update_gpu<ushort4>},
        {0,0,0,0},
        {0,0,0,0},
        {update_gpu<float>, 0, update_gpu<float3>, update_gpu<float4>}
    };

    CV_Assert(frame.depth() == CV_8U || frame.depth() == CV_16U || frame.depth() == CV_32F);
    CV_Assert(frame.channels() == 1 || frame.channels() == 3 || frame.channels() == 4);

    if (newLearningRate != -1.0f)
    {
        CV_Assert(newLearningRate >= 0.0f && newLearningRate <= 1.0f);
        learningRate = newLearningRate;
    }

    if (frame.size() != frameSize_)
        initialize(frame.size(), 0.0f, frame.depth() == CV_8U ? 255.0f : frame.depth() == CV_16U ? std::numeric_limits<ushort>::max() : 1.0f);

    fgmask.create(frameSize_, CV_8UC1);
    fgmask.setTo(cv::Scalar::all(0), stream);

    funcs[frame.depth()][frame.channels() - 1](frame, fgmask, colors_, weights_, nfeatures_, frameNum_, learningRate, updateBackgroundModel, cv::gpu::StreamAccessor::getStream(stream));

    // medianBlur
    if (smoothingRadius > 0)
    {
        boxFilter_->apply(fgmask, buf_, stream);
        int minCount = (smoothingRadius * smoothingRadius + 1) / 2;
        double thresh = 255.0 * minCount / (smoothingRadius * smoothingRadius);
        cv::gpu::threshold(buf_, fgmask, thresh, 255.0, cv::THRESH_BINARY, stream);
    }

    // keep track of how many frames we have processed
    ++frameNum_;
}
Example #6
0
void cv::gpu::MOG_GPU::operator()(const cv::gpu::GpuMat& frame, cv::gpu::GpuMat& fgmask, float learningRate, Stream& stream)
{
    using namespace cv::gpu::cudev::mog;

    CV_Assert(frame.depth() == CV_8U);

    int ch = frame.channels();
    int work_ch = ch;

    if (nframes_ == 0 || learningRate >= 1.0 || frame.size() != frameSize_ || work_ch != mean_.channels())
        initialize(frame.size(), frame.type());

    fgmask.create(frameSize_, CV_8UC1);

    ++nframes_;
    learningRate = learningRate >= 0.0f && nframes_ > 1 ? learningRate : 1.0f / std::min(nframes_, history);
    CV_Assert(learningRate >= 0.0f);

    mog_gpu(frame, ch, fgmask, weight_, sortKey_, mean_, var_, nmixtures_,
            varThreshold, learningRate, backgroundRatio, noiseSigma,
            StreamAccessor::getStream(stream));
}