Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

cvGetSpatialMoment() in OpenCV 2.0+

Tags:

opencv

I'm writing code for detecting colored Objects in OpenCV 2.3. I found a lot of examples in deprecated OpenCV code for the old c-Inteface.

So I adopted some of the examples code and want to change it to OpenCV 2.0+ Syntax. This is the code I use (it doesn't 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 first parameter. Mine is cv::Moments (OpenCV 2.0 code).

Now my problem its that there is no cv::GetSpatialMoments or something in new OpenCV 2.0 Syntax. At least I didn't find one. Can anyone help me out here?

like image 774
netsky Avatar asked Nov 14 '22 11:11

netsky


1 Answers

Original posters answer in the question converted to actual answer, verbatim:


Ok I found the answer somewhere else:

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

That does the trick!

like image 90
hyde Avatar answered Jan 16 '23 06:01

hyde