My question is similar to OpenCV: Detect blinking lights in a video feed openCV detect blinking lights
我想从任何带有 LED 对象的图像中检测出 LED 状态。 LED 对象可以是任何大小的(但大多是圆 ) 。 重要的是要在该图像中找到所有 LED 的位置, 尽管它可以是 On 或 OutF 。 首先,我想获得只使用 LED 的状态和位置。 现在, LED 的图像源是静态的, 对我的工作来说, 但它必须来自任何带有 LED 光发产品的视频。 因此没有机会用模板图像来减小背景 。
我尝试了使用 OpenCV (新到 OpenCV) 的门槛、 轮廓和圆形方法, 但没有找到可生成的 。 如果有源代码或解决方案, 请分享 。 解决方案不仅可以使用 OpenCV 来给我带来结果, 请分享 。
与另外两个问题的不同之处在于,我想在图像中找到LED的数量,不管是在屏幕上,还是在FTF上,以及所有LED的状态。我知道这非常复杂。首先,我试图在图像中检测发光的LED。我实施了下面分享的代码。我实施了不同的执行程序,但低于代码,我只能通过绘制轮廓来显示发光的LED,但轮廓的数量比发光的LED还多。所以我无法找到发光的LED的总数。请建议我你的投入。
int main(int argc, char* argv[])
{
IplImage* newImg = NULL;
IplImage* grayImg = NULL;
IplImage* contourImg = NULL;
float minAreaOfInterest = 180.0;
float maxAreaOfInterest = 220.0;
//parameters for the contour detection
CvMemStorage * storage = cvCreateMemStorage(0);
CvSeq * contours = 0;
int mode = CV_RETR_EXTERNAL;
mode = CV_RETR_CCOMP; //detect both outside and inside contour
cvNamedWindow("src", 1);
cvNamedWindow("Threshhold",1);
//load original image
newImg = cvLoadImage(argv[1], 1);
IplImage* imgHSV = cvCreateImage(cvGetSize(newImg), 8, 3);
cvCvtColor(newImg, imgHSV, CV_BGR2HSV);
cvNamedWindow("HSV",1);
cvShowImage( "HSV", imgHSV );
IplImage* imgThreshed = cvCreateImage(cvGetSize(newImg), 8, 1);
cvInRangeS(newImg, cvScalar(20, 100, 100), cvScalar(30, 255, 255), imgThreshed);
cvShowImage( "src", newImg );
cvShowImage( "Threshhold", imgThreshed );
//make a copy of the original image to draw the detected contour
contourImg = cvCreateImage(cvGetSize(newImg), IPL_DEPTH_8U, 3);
contourImg=cvCloneImage( newImg );
cvNamedWindow("Contour",1);
//find the contour
cvFindContours(imgThreshed, storage, &contours, sizeof(CvContour), mode, CV_CHAIN_APPROX_SIMPLE, cvPoint(0,0));
int i = 0;
for (; contours != 0; contours = contours->h_next)
{
i++;
//ext_color = CV_RGB( rand()&255, rand()&255, rand()&255 ); //randomly coloring different contours
cvDrawContours(contourImg, contours, CV_RGB(0, 255, 0), CV_RGB(255, 0, 0), 2, 2, 8, cvPoint(0,0));
}
printf("Total Contours:%d
", i);
cvShowImage( "Contour", contourImg );
cvWaitKey(0);
cvDestroyWindow( "src" ); cvDestroyWindow( "Threshhold" );
cvDestroyWindow( "HSV" );
cvDestroyWindow( "Contour" );
cvReleaseImage( &newImg ); cvReleaseImage( &imgThreshed );
cvReleaseImage( &imgHSV );
cvReleaseImage( &contourImg );
}