我正在开发一个程序,用于识别和检测视频Feed中的多边形形状。我在网上抓了一些程序形式的开放代码并添加了视频捕获。我还必须将一些在线代码转换为其C ++等价物。但是,我在查找轮廓功能时遇到错误,并希望得到任何帮助。我已附上以下代码。
#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 :(得分: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。