CvGetSpatialMoment () in OpenCV 2.0+

I am writing code to detect colored objects in OpenCV 2.3. I found many examples in legacy OpenCV code for the old c-Inteface.

So, I applied some code examples and want to change it to OpenCV 2.0+ Syntax. This is the code I'm using (it does not compile!):

cv::Mat ProcessorWidget::getTresholdImage(Mat &frame)
{
    cv::Mat hsvImage;
    hsvImage.copySize(frame);
    cv::cvtColor(frame, hsvImage, CV_BGR2HSV);
    cv::Mat threshedImage;
    cv::threshold(frame, threshedImage, double(ui->hTSlider_Thresh->value()), double(ui->lTSlider_Max->value()), cv::THRESH_BINARY);
return threshedImage;
}

cv::Mat ProcessorWidget::trackColoredObject(Mat& frame)
{
    // If this is the first frame, we need to initialize it
    if(!imgScribble)
    {
        imgScribble->copySize(frame); //cvCreateImage(cvGetSize(frame), 8, 3);
    }

    cv::Mat yellowThreshedImage = getTresholdImage(frame);
    cv::Moments  *moments = (cv::Moments*)malloc(sizeof(cv::Moments));
    cv::moments(yellowThreshedImage, moments);

   double moment10 = cvGetSpatialMoment(moments, 1, 0);
            double moment01 = cvGetSpatialMoment(moments, 0, 1);
            double area = cvGetCentralMoment(moments, 0, 0);

            // Holding the last and current ball positions
            static int posX = 0;
            static int posY = 0;

            int lastX = posX;
            int lastY = posY;

            posX = moment10/area;
            posY = moment01/area;

            // We want to draw a line only if its a valid position
            if(lastX>0 && lastY>0 && posX>0 && posY>0)
            {
                // Draw a yellow line from the previous point to the current point
                cv::line(imgScribble, cv::Point(posX, posY), cv::Point(lastX, lastY), cv::Scalar(0,255,255), 5);
            }

            cv::add(frame, imgScribble, frame);
    return frame;
}

The problem is that the compiler complains about this code:

double moment01 = cvGetSpatialMoment(moments, 0, 1);

Error: ../QtCV/processorwidget.cpp:122: error: cannot convert 'cv::Moments*' to 'CvMoments*' for argument '1' to 'double cvGetSpatialMoment(CvMoments*, int, int)'

cvGetSpatialMoments is deprecated and expects cvMoments as the first parameter. Mine - cv :: Moments (OpenCV 2.0 code).

Now my problem is that there is no cv :: GetSpatialMoments or something in the new OpenCV 2.0 syntax. At least I did not find it. Can anyone help me here?

+5
source share
1 answer

, , :


, :

cv::Moments ourMoment; //moments variable
ourMoment=moments(image); //calculat all the moment of image
double moment10=moment.m10; //extract spatial moment 10
double moment01=moment.m01; //extract spatial moment 01
double area=moment.m00; //extract central moment 00

!

+1

All Articles