[英]No instance of overloaded function findcontours matches the argument list
I am working on a program that will identify and detect polygonal shapes from a video feed. 我正在开发一个程序,该程序可以从视频供稿中识别和检测多边形。 I grabbed some part of the program form open code in C online and added video capture to it.
我使用C语言在线获取了一部分程序形式的开放代码,并在其中添加了视频捕获功能。 I also had to convert some of the online code to its C++ equivalent.
我还必须将一些在线代码转换为等效的C ++。 However, I am getting an error with the find contours function and would appreciate any help.
但是,我在查找轮廓函数时遇到了错误,将不胜感激。 I've attached the code below.
我已经附上了下面的代码。
#include <opencv2\opencv.hpp>
#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/highgui/highgui.hpp"
using namespace std;
using namespace cv;
int main()
{
VideoCapture cap(0); //capture the video from web cam
if ( !cap.isOpened() ) // if not success, exit program
{
cout << "Cannot open the web cam" << endl;
return -1;
}
Mat imgOriginal;
bool bSuccess = cap.read(imgOriginal); // read a new frame from video
if (!bSuccess) //if not success, break loop
{
cout << "Cannot read a frame from video stream" << endl;
// break;
}
//show the original image
namedWindow("Raw", CV_WINDOW_AUTOSIZE);
imshow("Raw",imgOriginal);
//converting the original image into grayscale
Mat imgGrayScale(imgOriginal.size(), CV_8UC1);
cvtColor(imgOriginal, imgGrayScale, CV_RGB2GRAY);
//thresholding the grayscale image to get better results
threshold(imgGrayScale,imgGrayScale,128,255,CV_THRESH_BINARY);
CvSeq* contours=0; //hold the pointer to a contour in the memory block
CvSeq* result=0; //hold sequence of points of a contour
CvMemStorage *storage = cvCreateMemStorage(0); //storage area for all contours
//finding all contours in the image
findContours(imgGrayScale, storage, &contours, sizeof(CvContour),
CV_RETR_LIST, CV_CHAIN_APPROX_SIMPLE, cvPoint(0,0));
//iterating through each contour
while(contours)
{
//obtain a sequence of points of contour, pointed by the variable 'contour'
result = cvApproxPoly(contours, sizeof(CvContour), storage, CV_POLY_APPROX_DP, cvContourPerimeter(contours)*0.02, 0);
//if there are 3 vertices in the contour(It should be a triangle)
if(result->total==3 )
{
//iterating through each point
CvPoint *pt[3];
for(int i=0;i<3;i++){
pt[i] = (CvPoint*)cvGetSeqElem(result, i);
}
//drawing lines around the triangle
cv::line(imgOriginal, *pt[0], *pt[1], cvScalar(255,0,0),4);
cv::line(imgOriginal, *pt[1], *pt[2], cvScalar(255,0,0),4);
cv::line(imgOriginal, *pt[2], *pt[0], cvScalar(255,0,0),4);
}
//if there are 4 vertices in the contour(It should be a quadrilateral)
else if(result->total==4 )
{
//iterating through each point
CvPoint *pt[4];
for(int i=0;i<4;i++){
pt[i] = (CvPoint*)cvGetSeqElem(result, i);
}
//drawing lines around the quadrilateral
cv::line(imgOriginal, *pt[0], *pt[1], cvScalar(0,255,0),4);
cv::line(imgOriginal, *pt[1], *pt[2], cvScalar(0,255,0),4);
cv::line(imgOriginal, *pt[2], *pt[3], cvScalar(0,255,0),4);
cv::line(imgOriginal, *pt[3], *pt[0], cvScalar(0,255,0),4);
}
//if there are 7 vertices in the contour(It should be a heptagon)
else if(result->total ==7 )
{
//iterating through each point
CvPoint *pt[7];
for(int i=0;i<7;i++){
pt[i] = (CvPoint*)cvGetSeqElem(result, i);
}
//drawing lines around the heptagon
cv::line(imgOriginal, *pt[0], *pt[1], cvScalar(0,0,255),4);
cv::line(imgOriginal, *pt[1], *pt[2], cvScalar(0,0,255),4);
cv::line(imgOriginal, *pt[2], *pt[3], cvScalar(0,0,255),4);
cv::line(imgOriginal, *pt[3], *pt[4], cvScalar(0,0,255),4);
cv::line(imgOriginal, *pt[4], *pt[5], cvScalar(0,0,255),4);
cv:line(imgOriginal, *pt[5], *pt[6], cvScalar(0,0,255),4);
cv:line(imgOriginal, *pt[6], *pt[0], cvScalar(0,0,255),4);
}
//obtain the next contour
contours = contours->h_next;
}
//show the image in which identified shapes are marked
namedWindow("Tracked", CV_WINDOW_AUTOSIZE);
imshow("Tracked",imgOriginal);
cvWaitKey(0); //wait for a key press
//cleaning up
cvDestroyAllWindows();
cvReleaseMemStorage(&storage);
cvReleaseImage(&imgOriginal);
cvReleaseImage(&imgGrayScale);
return 0;
}
Apart from findContours
, I've spotted another problem. 除了
findContours
之外,我还发现了另一个问题。 That's here: 在这里:
cvtColor(imgOriginal, imgGrayScale, CV_RGB2GRAY);
In OpenCV by default images are captured and saved in BGR
format not RGB
. 默认情况下,在OpenCV中,图像以
BGR
格式而非RGB
捕获并保存。 So you should use CV_BGR2GRAY
. 因此,您应该使用
CV_BGR2GRAY
。
To find contours in your image, you can use findContours
function in this way: 要在图像中查找轮廓,可以通过以下方式使用
findContours
函数:
vector<vector<Point>> contours;
findContours(image, contours, /* other parameters */);
Your contours are stored in that vector. 您的轮廓存储在该向量中。 BTW, Here's a sample code in C++.
顺便说一句,这是C ++中的示例代码 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.