2017-10-15 108 views
0

我正在研究一个程序,该程序将从视频源识别和检测多边形形状。我在C在线获取了部分程序表单开放代码,并添加了视频捕获。我还必须将一些在线代码转换为C++等价物。但是,我遇到了查找轮廓功能的错误,并希望得到任何帮助。我附上了下面的代码。没有重载函数findcontours的实例匹配参数列表

#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; 
} 
+0

你为什么要把陈旧的C api和C++ api混合在一起? – Miki

回答

0

除了findContours,我发现了另一个问题。这是在这里:

cvtColor(imgOriginal, imgGrayScale, CV_RGB2GRAY); 

在OpenCV的默认图像被捕获并保存在BGR格式不RGB。所以你应该使用CV_BGR2GRAY

要查找您图像轮廓,您可以以这种方式使用findContours功能:

vector<vector<Point>> contours; 
findContours(image, contours, /* other parameters */); 

你的轮廓都存储在矢量。顺便说一句,这是C++中的sample code

相关问题