所以:我有这个 OpenCV 程序,它从摄像头捕获视频,并将其显示在两个窗口上。一种没有颜色检测;另一个突出显示某些特定颜色(例如红色)。
我需要的是一种方法来确定图像在特定时间是否包含该特定颜色。现在,第一个窗口只是一个普通的视频输出。第二个窗口全黑,直到看到与我指定的颜色匹配的对象,这使得对象在第二个窗口中显示为白色。
我想知道何时检测到它,然后输出“检测到”或“未检测到”。
我该怎么做呢?我想我会遍历修改后图像的宽度和高度,然后检查,但我不知道该怎么做。任何帮助表示赞赏 - 我几天来一直试图找到这个问题的答案,但没有运气。我检查了 StackOverflow,但它没有为我提供我需要的东西。谢谢!
代码:
#include <opencv/cv.h>
#include <opencv/highgui.h>
//This function threshold the HSV image and create a binary image
IplImage* GetThresholdedImage(IplImage* imgHSV){
IplImage* imgThresh=cvCreateImage(cvGetSize(imgHSV),IPL_DEPTH_8U, 1);
IplImage* imgThresh2=cvCreateImage(cvGetSize(imgHSV),IPL_DEPTH_8U, 1);
IplImage* imgThresh3=cvCreateImage(cvGetSize(imgHSV),IPL_DEPTH_8U, 1);
cvInRangeS(imgHSV, cvScalar(170,160,60), cvScalar(180,256,256), imgThresh2);
cvInRangeS(imgHSV, cvScalar(0,160,60), cvScalar(22,256,256), imgThresh3);
cvOr(imgThresh2, imgThresh3, imgThresh);
cvInRangeS(imgHSV, cvScalar(75,160,60), cvScalar(130,256,256), imgThresh3);
cvOr(imgThresh, imgThresh3, imgThresh);
return imgThresh;
}
int main(){
CvCapture* capture =0;
capture = cvCaptureFromCAM(0);
if(!capture){
printf("Capture failure\n");
return -1;
}
IplImage * frame = 0;
cvNamedWindow("Video");
cvNamedWindow("Ball");
//iterate through each frames of the video
while(true){
frame = cvQueryFrame(capture);
if(!frame) break;
frame=cvCloneImage(frame);
cvSmooth(frame, frame, CV_GAUSSIAN,3,3); //smooth the original image using Gaussian kernel
IplImage* imgHSV = cvCreateImage(cvGetSize(frame), IPL_DEPTH_8U, 3);
cvCvtColor(frame, imgHSV, CV_BGR2HSV); //Change the color format from BGR to HSV
IplImage* imgThresh = GetThresholdedImage(imgHSV);
cvSmooth(imgThresh, imgThresh, CV_GAUSSIAN,3,3); //smooth the binary image using Gaussian kernel
cvShowImage("Ball", imgThresh);
cvShowImage("Video", frame);
int sum = 0;
for (int i = 0; i < imgThresh->width; i++) {
for (int j = 0; j < imgThresh->height; j++) {
// WHAT DO I NEED HERE TO CALCULATE CERTAIN COLOR
}
}
if (sum > 1) { cout >> "Detected"; }
else { cout >> "Not Detected"; }
//Clean up used images
cvReleaseImage(&imgHSV);
cvReleaseImage(&imgThresh);
cvReleaseImage(&frame);
//Wait 50mS
int c = cvWaitKey(10);
//If 'ESC' is pressed, break the loop
if((char)c==27 ) break;
}
cvDestroyAllWindows() ;
cvReleaseCapture(&capture);
return 0;
}