重载的"Point_(cv :: Point2f&)"的调用不明确 [英] call of overloaded ‘Point_(cv::Point2f&)’ is ambiguous
问题描述
我正在为OpenCV2& C ++,我被困住了.编译器(Win7上的MinGW,g ++ 4.7.2)说,重载的"Point_(cv :: Point2f&)"的调用是模棱两可的,但我无法找到错误的地方.这是错误:
I am working on some example code for OpenCV2 & C++ and I got stuck. Compiler (MinGW, g++ 4.7.2 on Win7) says that call of overloaded ‘Point_(cv::Point2f&)’ is ambiguous but I can't find exatcly what is wrong. Here is error:
18:09:33 **** Incremental Build of configuration Debug for project Blobs ****
Info: Internal Builder is used for build
g++ "-IC:\\OpenCV246PC\\build\\include" -O0 -g3 -Wall -c -fmessage-length=0 -o blobs.o "..\\blobs.cpp"
..\blobs.cpp: In function ‘int main()’:
..\blobs.cpp:65:21: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
..\blobs.cpp:65:43: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
..\blobs.cpp:99:37: error: call of overloaded ‘Point_(cv::Point2f&)’ is ambiguous
..\blobs.cpp:99:37: note: candidates are:
In file included from ..\blobs.cpp:20:0:
C:\OpenCV246PC\build\include/opencv2/core/core.hpp:740:5: note: cv::Point_<_Tp>::Point_(const CvPoint2D32f&) [with _Tp = int; CvPoint2D32f = CvPoint2D32f]
C:\OpenCV246PC\build\include/opencv2/core/core.hpp:739:5: note: cv::Point_<_Tp>::Point_(const CvPoint&) [with _Tp = int; CvPoint = CvPoint]
C:\OpenCV246PC\build\include/opencv2/core/core.hpp:738:5: note: cv::Point_<_Tp>::Point_(const cv::Point_<_Tp>&) [with _Tp = int; cv::Point_<_Tp> = cv::Point_<int>]
// ERROR IS HERE
// It god mixed up when I pasted, so line number is not the one compiler complains.
cv::circle(result, cv::Point(center), static_cast<int>(radius), cv::Scalar(0), 2);
这是代码:
#include <iostream>
#include <vector>
#include <opencv2/core/core.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/highgui/highgui.hpp>
int main()
{
// Read input binary image
cv::Mat image = cv::imread("binaryGroup.bmp", 0);
if (!image.data)
return 0;
cv::namedWindow("Binary Image");
cv::imshow("Binary Image", image);
// Get the contours of the connected components
std::vector<std::vector<cv::Point> > contours;
cv::findContours(image, contours, // a vector of contours
CV_RETR_EXTERNAL, // retrieve the external contours
CV_CHAIN_APPROX_NONE); // retrieve all pixels of each contours
// Print contours' length
std::cout << "Contours: " << contours.size() << std::endl;
std::vector<std::vector<cv::Point> >::const_iterator itContours = contours.begin();
for (; itContours != contours.end(); ++itContours)
{
std::cout << "Size: " << itContours->size() << std::endl;
}
// draw black contours on white image
cv::Mat result(image.size(), CV_8U, cv::Scalar(255));
cv::drawContours(result, contours, -1, // draw all contours
cv::Scalar(0), // in black
2); // with a thickness of 2
cv::namedWindow("Contours");
cv::imshow("Contours", result);
// Eliminate too short or too long contours
int cmin = 100; // minimum contour length
int cmax = 1000; // maximum contour length
std::vector<std::vector<cv::Point> >::iterator itc = contours.begin();
while (itc != contours.end())
{
if (itc->size() < cmin || itc->size() > cmax)
itc = contours.erase(itc);
else
++itc;
}
// draw contours on the original image
cv::Mat original = cv::imread("group.jpg");
cv::drawContours(original, contours, -1, // draw all contours
cv::Scalar(255, 255, 255), // in white
2); // with a thickness of 2
cv::namedWindow("Contours on Animals");
cv::imshow("Contours on Animals", original);
// Let's now draw black contours on white image
result.setTo(cv::Scalar(255));
cv::drawContours(result, contours, -1, // draw all contours
cv::Scalar(0), // in black
1); // with a thickness of 1
image = cv::imread("binaryGroup.bmp", 0);
// testing the bounding box
cv::Rect r0 = cv::boundingRect(cv::Mat(contours[0]));
cv::rectangle(result, r0, cv::Scalar(0), 2);
// testing the enclosing circle
float radius;
cv::Point2f center;
// http://opencv.willowgarage.com/documentation/cpp/structural_analysis_and_shape_descriptors.html#cv-minenclosingcircle
// void minEnclosingCircle(const Mat& points, Point2f& center, float& radius)
cv::minEnclosingCircle(cv::Mat(contours[1]), center, radius);
// http://opencv.willowgarage.com/documentation/cpp/drawing_functions.html#cv-circle
// void circle(Mat& img, Point center, int radius, const Scalar& color, int thickness=1, int lineType=8, int shift=0)
// ERROR IS HERE
cv::circle(result, cv::Point(center), static_cast<int>(radius), cv::Scalar(0), 2); // <--- ERROR IS HERE
// cv::RotatedRect rrect= cv::fitEllipse(cv::Mat(contours[1]));
// cv::ellipse(result,rrect,cv::Scalar(0),2);
// testing the approximate polygon
std::vector<cv::Point> poly;
cv::approxPolyDP(cv::Mat(contours[2]), poly, 5, true);
std::cout << "Polygon size: " << poly.size() << std::endl;
// Iterate over each segment and draw it
std::vector<cv::Point>::const_iterator itp = poly.begin();
while (itp != (poly.end() - 1))
{
cv::line(result, *itp, *(itp + 1), cv::Scalar(0), 2);
++itp;
}
// last point linked to first point
cv::line(result, *(poly.begin()), *(poly.end() - 1), cv::Scalar(20), 2);
// testing the convex hull
std::vector<cv::Point> hull;
cv::convexHull(cv::Mat(contours[3]), hull);
// Iterate over each segment and draw it
std::vector<cv::Point>::const_iterator it = hull.begin();
while (it != (hull.end() - 1))
{
cv::line(result, *it, *(it + 1), cv::Scalar(0), 2);
++it;
}
// last point linked to first point
cv::line(result, *(hull.begin()), *(hull.end() - 1), cv::Scalar(20), 2);
// testing the moments
// iterate over all contours
itc = contours.begin();
while (itc != contours.end())
{
// compute all moments
cv::Moments mom = cv::moments(cv::Mat(*itc++));
// draw mass center
cv::circle(result,
// position of mass center converted to integer
cv::Point(mom.m10 / mom.m00, mom.m01 / mom.m00), 2, cv::Scalar(0), 2); // draw black dot
}
cv::namedWindow("Some Shape descriptors");
cv::imshow("Some Shape descriptors", result);
// New call to findContours but with CV_RETR_LIST flag
image = cv::imread("binaryGroup.bmp", 0);
// Get the contours of the connected components
cv::findContours(image, contours, // a vector of contours
CV_RETR_LIST, // retrieve the external and internal contours
CV_CHAIN_APPROX_NONE); // retrieve all pixels of each contours
// draw black contours on white image
result.setTo(cv::Scalar(255));
cv::drawContours(result, contours, -1, // draw all contours
cv::Scalar(0), // in black
2); // with a thickness of 2
cv::namedWindow("All Contours");
cv::imshow("All Contours", result);
cv::waitKey();
return 0;
}
我做错了什么?
我希望那些拒绝我投票的人设法解决这个问题. 这不是这里的唯一问题,但是我的投票已经被否决了三遍了吗?
I would like that some of those who gave me down vote try to resolve this. This is not the only question of this type here, yet mine was down voted three times already?
此外,这是加拿大教授撰写的书中的示例( http://www.laganiere.name /opencvCookbook/),谁同时教这两个OpenCV,我是否应该比他更好?我应该为这个问题感到as愧吗?我是C ++和OpenCV的新手吗?我是否应该在其他十个论坛上提问,只有在找不到答案的情况下,才在Stackoverflow上提问吗?
Moreover, this is example from book written by professor from Canada ( http://www.laganiere.name/opencvCookbook/ ), who teaches both OpenCV, am I supposed to be better at this than him? Should I been ashamed that I ask this? I a novice both at C++ and OpenCV? Should I ask on ten other forums and then, only then if I don't find answer ask here on Stackoverflow?
Moreover, I am not lazy, I helped this man by rewriting his code ( OpenCV: record footage in one window and Display the same video in 2nd window but with contours only ). I helped to someone who knows less than me. But, in this case I just don't know how to solve this issue in this code and yet within 30 minutes I got three down votes?
更新:正如OpenCV社区向我指出的那样,实际上应该允许将Point转换为Point2f,反之亦然,但是版本2.4.3(
UPDATE: As pointed to me by OpenCV community, actually it should be allowed to convert Point to Point2f and vice versa, but bug from version 2.4.3 ( http://code.opencv.org/issues/2616#note-1 ) prevents it from doing so. That explain why this code was compiling for original author few years ago, and not for me. Anyway, user @alrikai provided correct answer and workarround for this.
错误已更正为:cv :: circle(结果,cv :: Point2f(center),static_cast(radius),cv :: Scalar(0),2);所以cv :: Point(center)变成了这个cv :: Point2f(center)
Error was corrected as: cv::circle(result, cv::Point2f(center), static_cast(radius), cv::Scalar(0), 2); so cv::Point(center) become this cv::Point2f(center)
推荐答案
如上所述,问题在于您是从cv::Point2f
中创建cv::Point
. cv::Point
被声明为typedef Point2i Point;
,这意味着它等同于cv::Point2i
.因此,从本质上讲,您正在尝试从cv::Point2f
中创建一个cv::Point2i
,这是无效的.
As commented above, the problem was that you're making a cv::Point
from a cv::Point2f
. cv::Point
is declared as typedef Point2i Point;
, meaning that it's equivalent to a cv::Point2i
. So in essence, you're trying to make a cv::Point2i
from a cv::Point2f
, which is invalid.
此外,很高兴听到它为您服务
Also, glad to hear it worked for you
这篇关于重载的"Point_(cv :: Point2f&)"的调用不明确的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!