void displayStats() { // Show image size sprintf_s( text, 255, "%dx%d", imgSrc.size().width, imgSrc.size().height ); putTextWithShadow( imgProcessed, text, Point(5, 35) ); // Show FPS sprintf_s( text, 255, "FPS %2.0f", 1000/exec_time); putTextWithShadow( imgProcessed, text, Point(5, 55) ); // When working on files if( PROGRAM_MODE == 1 ) { // Show current file name putTextWithShadow( imgProcessed, getCurentFileName( imgFileList.at(imIt) ).c_str(), Point(5, 75)); sprintf_s( text, 255, "Current Image %d", imIt); putTextWithShadow( imgProcessed, text, Point(5, 115) ); } else if( PROGRAM_MODE == 2 ) { // Show current frame no. sprintf_s( text, 255, "Video pos %d%%", cvRound(videoCapture.get( CV_CAP_PROP_POS_AVI_RATIO)*100)); putTextWithShadow( imgProcessed, text, Point(5, 75)); } }
SColorDetectorResult CColorDetector::detect(IplImage *src){ SColorDetectorResult Result; EResult FieldDetectResult, PuckDetectResult; char string[50]; CvFont base_font, small_font; cvInitFont( &base_font, CV_FONT_VECTOR0, 0.5, 0.5); cvInitFont( &small_font, CV_FONT_VECTOR0, 0.3, 0.3); //---- Field detection FieldDetectResult = Empty; for(int i=0; i < m_params.m_FieldAreas.size(); i++){ SCalculateResult FieldResult = calculate(src, m_params.m_FieldAreas[i]); m_uBluePercentField = FieldResult.uBlueSumm/FieldResult.uSumm; m_uRedPercentField = FieldResult.uRedSumm/FieldResult.uSumm; if((m_uBluePercentField > m_params.m_uBlueTrash) && (m_uRedPercentField <= 0)){ if(FieldDetectResult == Nothing) FieldDetectResult = Nothing; else if(FieldDetectResult == Error) FieldDetectResult = Error; else if(FieldDetectResult == Red) FieldDetectResult = Error; else FieldDetectResult = Blue; } else if((m_uRedPercentField > m_params.m_uRedTrash) && (m_uBluePercentField <= 0)){ if(FieldDetectResult == Nothing) FieldDetectResult = Nothing; else if(FieldDetectResult == Error) FieldDetectResult = Error; else if(FieldDetectResult == Blue) FieldDetectResult = Error; else FieldDetectResult = Red; } else{ if(FieldDetectResult == Error) FieldDetectResult = Error; else FieldDetectResult = Nothing; } if(m_params.m_bWithImages){ if((m_uBluePercentField > m_params.m_uBlueTrash) && (m_uRedPercentField <= 0)){ sprintf(string, "blue"); } else if((m_uRedPercentField > m_params.m_uRedTrash) && (m_uBluePercentField <= 0)){ sprintf(string, "red"); } else sprintf(string, "nothing"); putTextWithShadow(src, string, cvPoint(m_params.m_FieldAreas[i].x+m_params.m_FieldAreas[i].width/2-15, m_params.m_FieldAreas[i].y+m_params.m_FieldAreas[i].height/2), &base_font); } } //---- Puck detection SCalculateResult PuckResult = calculate(src, m_params.m_PuckArea); m_uBluePercentPuck = PuckResult.uBlueSumm/PuckResult.uSumm; m_uRedPercentPuck = PuckResult.uRedSumm/PuckResult.uSumm; if((m_uBluePercentPuck > m_params.m_uBlueTrash) && (m_uRedPercentPuck <= 0)){ if(FieldDetectResult == Blue) PuckDetectResult = Nothing; else PuckDetectResult = Blue; } else if((m_uRedPercentPuck > m_params.m_uRedTrash) && (m_uBluePercentPuck <= 0)){ if(FieldDetectResult == Red) PuckDetectResult = Nothing; else PuckDetectResult = Red; } else{ PuckDetectResult = Nothing; } Result.FieldResult = FieldDetectResult; Result.PuckResult = PuckDetectResult; if(m_params.m_bWithImages){ if(Result.PuckResult == Blue){ sprintf(string, "blue"); } else if(Result.PuckResult == Red){ sprintf(string, "red"); } else sprintf(string, "nothing"); CvRect puckArea = m_params.m_PuckArea; putTextWithShadow(src, string, cvPoint(puckArea.x+puckArea.width/2-15, puckArea.y+puckArea.height/2), &base_font); // putTextWithShadow(src, string, cvPoint(10, src->height-55), &base_font); std::sprintf(string, "%d x %d", src->width, src->height); putTextWithShadow(src, string, cvPoint(10, src->height-35), &small_font); sprintf(string, "blue: %d red %d ", (int)m_uBluePercentPuck, (int)m_uRedPercentPuck); putTextWithShadow(src, string, cvPoint(10, src->height-15), &base_font); cvRectangle( src, cvPoint(puckArea.x,puckArea.y), cvPoint(puckArea.x+puckArea.width,puckArea.y+puckArea.height), cvScalar(0xff,0xff,0xff)); cvPutText( src, "Puck", cvPoint(puckArea.x+3,puckArea.y + 10), &small_font, cvScalar(0xff,0xff,0xff)); for(int i=0; i < m_params.m_FieldAreas.size(); i++){ CvRect fieldArea = m_params.m_FieldAreas[i]; cvRectangle( src, cvPoint(fieldArea.x,fieldArea.y), cvPoint(fieldArea.x+fieldArea.width,fieldArea.y+fieldArea.height), cvScalar(0xff,0xff,0xff)); cvPutText( src, "Field", cvPoint(fieldArea.x+3,fieldArea.y + 10), &small_font, cvScalar(0xff,0xff,0xff)); } } return Result; }
int main(int argc, char** argv ) { IplImage *img, *filterMask = NULL; CvAdaptiveSkinDetector filter(1, CvAdaptiveSkinDetector::MORPHING_METHOD_ERODE_DILATE); ASDFrameSequencer *sequencer; CvFont base_font; char caption[2048], s[256], windowName[256]; long int clockTotal = 0, numFrames = 0; std::clock_t clock; if (argc < 4) { help(argv); sequencer = new ASDFrameSequencerWebCam(); (dynamic_cast<ASDFrameSequencerWebCam*>(sequencer))->open(-1); if (! sequencer->isOpen()) { std::cout << std::endl << "Error: Cannot initialize the default Webcam" << std::endl << std::endl; } } else { sequencer = new ASDFrameSequencerImageFile(); (dynamic_cast<ASDFrameSequencerImageFile*>(sequencer))->open(argv[1], std::atoi(argv[2]), std::atoi(argv[3]) ); // A sequence of images captured from video source, is stored here } std::sprintf(windowName, "%s", "Adaptive Skin Detection Algorithm for Video Sequences"); cvNamedWindow(windowName, CV_WINDOW_AUTOSIZE); cvInitFont( &base_font, CV_FONT_VECTOR0, 0.5, 0.5); // Usage: // c:\>CvASDSample "C:\VideoSequences\sample1\right_view\temp_%05d.jpg" 0 1000 std::cout << "Press ESC to stop." << std::endl << std::endl; while ((img = sequencer->getNextImage()) != 0) { numFrames++; if (filterMask == NULL) { filterMask = cvCreateImage( cvSize(img->width, img->height), IPL_DEPTH_8U, 1); } clock = std::clock(); filter.process(img, filterMask); // DETECT SKIN clockTotal += (std::clock() - clock); displayBuffer(img, filterMask, 0, 255, 0); sequencer->getFrameCaption(caption); std::sprintf(s, "%s - %d x %d", caption, img->width, img->height); putTextWithShadow(img, s, cvPoint(10, img->height-35), &base_font); std::sprintf(s, "Average processing time per frame: %5.2fms", (double(clockTotal*1000/CLOCKS_PER_SEC))/numFrames); putTextWithShadow(img, s, cvPoint(10, img->height-15), &base_font); cvShowImage (windowName, img); cvReleaseImage(&img); if (cvWaitKey(1) == 27) break; } sequencer->close(); delete sequencer; cvReleaseImage(&filterMask); cvDestroyWindow(windowName); std::cout << "Finished, " << numFrames << " frames processed." << std::endl; return 0; }