【发布时间】:2015-09-14 20:56:42
【问题描述】:
我有一个代码可以找到视频的计数并跟踪我选择的形状。在这段代码中,我正在搜索查看 3 或 4 个轮廓的三角形和矩形。
我需要 2 个问题的帮助:
1- 使用这种方法,我如何检测圆圈?
2- 我如何搜索颜色(形状已完成,所以在我的“如果”中我也需要验证颜色,但是如何?例如,如果我想找到一个红色三角形)
非常感谢
#include "opencv2/core/core.hpp"
#include "opencv2/highgui/highgui.hpp""
#include "opencv2/imgproc/imgproc.hpp"
#include <opencv\cv.h>
#include <math.h>
using namespace cv;
using namespace std;
int iLastX = -1;
int iLastY = -1;
IplImage* imgTracking = 0;
int lastX1 = -1;
int lastY1 = -1;
int lastX2 = -1;
int lastY2 = -1;
void trackObject(IplImage* imgThresh){
CvSeq* contour; //hold the pointer to a contour
CvSeq* result; //hold sequence of points of a contour
CvMemStorage *storage = cvCreateMemStorage(0); //storage area for all contours
//finding all contours in the image
cvFindContours(imgThresh, storage, &contour, sizeof(CvContour), CV_RETR_LIST, CV_CHAIN_APPROX_SIMPLE, cvPoint(0, 0));
//iterating through each contour
while (contour)
{
//obtain a sequence of points of the countour, pointed by the variable 'countour'
result = cvApproxPoly(contour, sizeof(CvContour), storage, CV_POLY_APPROX_DP, cvContourPerimeter(contour)*0.02, 0);
//if there are 3 vertices in the contour and the area of the triangle is more than 100 pixels
if (result->total == 3 && fabs(cvContourArea(result, CV_WHOLE_SEQ))>100)
{
//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
cvLine(imgTracking, *pt[0], *pt[1], cvScalar(255, 0, 0), 4);
cvLine(imgTracking, *pt[1], *pt[2], cvScalar(255, 0, 0), 4);
cvLine(imgTracking, *pt[2], *pt[0], cvScalar(255, 0, 0), 4);
}
else if (result->total == 4 && fabs(cvContourArea(result, CV_WHOLE_SEQ))>100)
{
//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
cvLine(imgTracking, *pt[0], *pt[1], cvScalar(0, 255, 0), 4);
cvLine(imgTracking, *pt[1], *pt[2], cvScalar(0, 255, 0), 4);
cvLine(imgTracking, *pt[2], *pt[3], cvScalar(0, 255, 0), 4);
cvLine(imgTracking, *pt[3], *pt[0], cvScalar(0, 255, 0), 4);
}
else if (CIRCLE???)
{
}
//obtain the next contour
contour = contour->h_next;
}
cvReleaseMemStorage(&storage);
}
int main(){
//load the video file to the memory
CvCapture *capture = cvCaptureFromAVI("F:/TCC/b2.avi");
if (!capture){
printf("Capture failure\n");
return -1;
}
IplImage* frame = 0;
frame = cvQueryFrame(capture);
if (!frame) return -1;
//create a blank image and assigned to 'imgTracking' which has the same size of original video
imgTracking = cvCreateImage(cvGetSize(frame), IPL_DEPTH_8U, 3);
cvZero(imgTracking); //covert the image, 'imgTracking' to black
cvNamedWindow("Video");
//iterate through each frames of the video
while (true){
cvSet(imgTracking, cvScalar(0, 0, 0));
frame = cvQueryFrame(capture);
if (!frame) break;
frame = cvCloneImage(frame);
//smooth the original image using Gaussian kernel
cvSmooth(frame, frame, CV_GAUSSIAN, 3, 3);
//converting the original image into grayscale
IplImage* imgGrayScale = cvCreateImage(cvGetSize(frame), 8, 1);
cvCvtColor(frame, imgGrayScale, CV_BGR2GRAY);
//thresholding the grayscale image to get better results
cvThreshold(imgGrayScale, imgGrayScale, 100, 255, CV_THRESH_BINARY_INV);
//track the possition of the ball
trackObject(imgGrayScale);
// Add the tracking image and the frame
cvAdd(frame, imgTracking, frame);
cvShowImage("Video", frame);
//Clean up used images
cvReleaseImage(&imgGrayScale);
cvReleaseImage(&frame);
//Wait 10mS
int c = cvWaitKey(10);
//If 'ESC' is pressed, break the loop
if ((char)c == 27) break;
}
cvDestroyAllWindows();
cvReleaseImage(&imgTracking);
cvReleaseCapture(&capture);
return 0;
}
【问题讨论】:
-
只是一条建议:使用 C++ 语法,而不是过时的 C 语法..
标签: c++ opencv colors tracking shapes