2017-10-15 76 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

相關問題