2012-01-17 19 views
5

Estoy escribiendo código para detectar Objetos de color en OpenCV 2.3. Encontré muchos ejemplos en código de OpenCV en desuso para el antiguo c-Inteface.cvGetSpatialMoment() en OpenCV 2.0+

Así que adopté algunos de los ejemplos de código y deseo cambiarlos a OpenCV 2.0+ Syntax. Este es el código que utilizo (no compila!):

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; 
} 

El problema es que el compilador se queja de este código:

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 está en desuso y espera cvMoments como primer parámetro. Mine es cv :: Moments (código de OpenCV 2.0).

Ahora mi problema es que no hay cv :: GetSpatialMoments o algo en la nueva sintaxis de OpenCV 2.0. Al menos no encontré ninguno. ¿Puede alguien ayudarme aquí?

+0

+1 Responda a su pregunta hermano .... Puede agregar una respuesta a su pregunta ... Gracias de cualquier manera que estaba buscando .... – Wazzzy

+0

@netsky: ¿puede mover su edición en una respuesta tan que la pregunta desaparece de la lista "sin respuesta"? –

+1

Este ejemplo es muy útil. Rompe todo muy bien, y lo explica todo http://opencv-srf.blogspot.com/2010/09/object-detection-using-color-seperation.html – hcwiley

Respuesta

1

carteles originales respuesta en la pregunta convertida en respuesta real, textualmente:


Ok he encontrado la respuesta en otro lugar:

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 

Eso hace el truco!