我正在编写用于在 OpenCV 2.3 中检测彩色对象的代码。我在旧 c-Inteface 的弃用 OpenCV 代码中找到了很多示例。
所以我采用了一些示例代码并想将其更改为 OpenCV 2.0+ 语法。这是我使用的代码(无法编译!):
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;
}
问题是编译器提示这段代码:
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 已弃用,需要 cvMoments 作为第一个参数。我的是 cv::Moments(OpenCV 2.0 代码)。
现在我的问题是新的 OpenCV 2.0 语法中没有 cv::GetSpatialMoments 或其他东西。至少我没有找到。有人可以帮我吗?
最佳答案
将问题中的原始发帖人回答转换为实际回答,逐字记录:
好的,我在其他地方找到了答案:
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
这就是诀窍!
关于opencv - OpenCV 2.0+ 中的 cvGetSpatialMoment(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8895749/