void QOpenCvImageBox::old_detect_and_draw_faces( IplImage* img, CvMemStorage* storage, CvHaarClassifierCascade* cascade ) { // Create two points to represent the face locations CvPoint pt1, pt2; IplImage *gray, *small_img; int i; if( cascade ) { storage = cvCreateMemStorage(0); gray = cvCreateImage( cvSize(img->width,img->height), 8, 1 ); small_img = cvCreateImage( cvSize( cvRound (img->width/od_image_scale), cvRound (img->height/od_image_scale)), 8, 1 ); cvCvtColor( img, gray, CV_RGB2GRAY ); cvResize( gray, small_img, CV_INTER_LINEAR ); cvEqualizeHist( small_img, small_img ); cvClearMemStorage( storage ); double t = (double)cvGetTickCount(); CvSeq* faces = cvHaarDetectObjects( small_img, cascade, storage, 1.1, 2, 0 //|CV_HAAR_FIND_BIGGEST_OBJECT //|CV_HAAR_DO_ROUGH_SEARCH |CV_HAAR_DO_CANNY_PRUNING //|CV_HAAR_SCALE_IMAGE , cvSize(30, 30) ); t = (double)cvGetTickCount() - t; printf( "detection time = %gms\n", t/((double)cvGetTickFrequency()*1000.) ); cvReleaseImage( &gray ); cvReleaseImage( &small_img ); // Loop the number of faces found. printf("Detected %d faces!\n", faces->total); for( i = 0; i < (faces ? faces->total : 0); i++ ) { // Create a new rectangle for drawing the face CvRect* r = (CvRect*)cvGetSeqElem( faces, i ); if(r) { // Find the dimensions of the face,and scale it if necessary pt1.x = r->x*od_image_scale; pt2.x = (r->x+r->width)*od_image_scale; pt1.y = r->y*od_image_scale; pt2.y = (r->y+r->height)*od_image_scale; // Draw the rectangle in the input image cvRectangle( img, pt1, pt2, CV_RGB(255,0,0), 3, 8, 0 ); } } cvClearSeq(faces); cvReleaseMemStorage(&storage); } }
/** * This is an example on how to call the thinning function above. */ int main() { cv::Mat src = cv::imread("../test_image.png"); if (src.empty()) return -1; cv::Mat bw; cv::cvtColor(src, bw, CV_BGR2GRAY); cv::threshold(bw, bw, 10, 255, CV_THRESH_BINARY); const int N = 30; int64 time = 0; for (int i = 0; i < N; ++i) { cv::Mat bb = i < N - 1 ? bw.clone() : bw; int64 t = cvGetTickCount(); thinning(bb, false); time += (cvGetTickCount() - t); } printf("%f", time / cvGetTickFrequency() / N); //thinning(bw); cv::imshow("src", src); cv::imshow("dst", bw); cv::waitKey(0); return 0; }
// Function to detect objects static void detect(image_session_t *mySession) { double t = 0; uint16_t current_category; LinkedCascade *cascade; for(current_category = 0; current_category < num_image_categories; current_category++) { if(CI_DEBUG_LEVEL >= PERF_DEBUG_LEVEL) t = cvGetTickCount(); // Find whether the cascade is loaded, to find the objects. If yes, then: if(mySession->detected[current_category].category->cascade_array) { cascade = getFreeCascade(mySession->detected[current_category].category); // There can be more than one object in an image. So create a growable sequence of detected objects. // Detect the objects and store them in the sequence #ifdef HAVE_OPENCV mySession->detected[current_category].detected = cvHaarDetectObjects( mySession->rightImage, cascade->cascade, mySession->dstorage, 1.1, 1, 0, cvSize(0, 0) ); #endif #ifdef HAVE_OPENCV_22X mySession->detected[current_category].detected = cvHaarDetectObjects( mySession->rightImage, cascade->cascade, mySession->dstorage, 1.1, 1, 0, cvSize(0, 0), cvSize(mySession->rightImage->width, mySession->rightImage->height)); #endif unBusyCascade(mySession->detected[current_category].category, cascade); } if(CI_DEBUG_LEVEL >= PERF_DEBUG_LEVEL) { t = cvGetTickCount() - t; t = t / ((double)cvGetTickFrequency() * 1000.); ci_debug_printf(8, "srv_classify_image: File: %s Object: %s (%d) Detection took: %gms.\n", (rindex(mySession->fname, '/'))+1, mySession->detected[current_category].category->name, mySession->detected[current_category].detected->total, t); } } }
void detect_and_draw( IplImage* img) { static CvScalar colors[] = { {{0,0,255}}, {{0,128,255}}, {{0,255,255}}, {{0,255,0}}, {{255,128,0}}, {{255,255,0}}, {{255,0,0}}, {{255,0,255}} }; double scale = 1.3; IplImage* gray = cvCreateImage( cvSize(img->width,img->height), 8, 1 ); IplImage* small_img = cvCreateImage( cvSize( cvRound (img->width/scale), cvRound (img->height/scale)), 8, 1 ); int i; IplImage* onlyhaart = cvCreateImage(cvGetSize(img), img->depth, img->nChannels); cvCopy(img, onlyhaart); cvCvtColor( img, gray, CV_BGR2GRAY ); cvResize( gray, small_img, CV_INTER_LINEAR ); cvEqualizeHist( small_img, small_img ); cvClearMemStorage( storageHaart ); if(cascade) { double t = (double)cvGetTickCount(); CvSeq* faces = cvHaarDetectObjects(small_img, cascade, storageHaart, 1.1, 2, 0/*CV_HAAR_DO_CANNY_PRUNING*/, cvSize(30, 30) ); t = (double)cvGetTickCount() - t; for(i = 0; i < (faces ? faces->total : 0); i++ ) { CvRect* r = (CvRect*)cvGetSeqElem( faces, i ); CvRect rect = cvRect(r->x, r->y, r->width, r->height); if ((rect.height < (img->height + 1)) & (rect.width < (img->width + 1))) { printf( "detection time = %gms\n", t/((double)cvGetTickFrequency()*100.) ); CvPoint center; int radius; center.x = cvRound((rect.x + rect.width*0.5)*scale); center.y = cvRound((rect.y + rect.height*0.5)*scale); radius = cvRound((rect.width + rect.height)*0.25*scale); cvCircle( img, center, radius, colors[i%8], 3, 8, 0 ); } CvPoint center; int radius; center.x = cvRound((rect.x + rect.width*0.5)*scale); center.y = cvRound((rect.y + rect.height*0.5)*scale); radius = cvRound((rect.width + rect.height)*0.25*scale); cvCircle( onlyhaart, center, radius, colors[i%8], 3, 8, 0 ); } } cvShowImage( "Detecta", img ); cvShowImage( "onlyhaart", onlyhaart); cvReleaseImage( &gray ); cvReleaseImage( &small_img ); }
void CFaceDetect::detectAndDisplay(Mat& img, RECT* detectBox) { double scale=1.2; Mat small_img(cvRound (img.rows/scale), cvRound(img.cols/scale), CV_8UC1); Mat gray(cvRound (img.rows/scale),cvRound(img.cols/scale),8,1); cvtColor(img, gray, CV_BGR2GRAY); resize(gray, small_img, small_img.size(), 0, 0, INTER_LINEAR); equalizeHist(small_img,small_img); //Ö±·½Í¼¾ùºâ //Detect the biggest face double t = (double)cvGetTickCount(); std::vector<cv::Rect> faces; cascade.detectMultiScale(small_img, faces, 1.1, 2, CV_HAAR_FIND_BIGGEST_OBJECT, cvSize(30, 30)); t = (double)cvGetTickCount() - t; printf( "detection time = %gms\n", t/((double)cvGetTickFrequency()*1000.) ); //found objects and draw boxes around them if(faces.size() == 1) { (*detectBox).left = faces[0].x * scale; (*detectBox).right = (faces[0].x + faces[0].width) * scale; (*detectBox).top = faces[0].y * scale + 10; (*detectBox).bottom = (faces[0].y + faces[0].height) * scale + 10; rectangle(img, cvPoint((*detectBox).left,(*detectBox).top), cvPoint((*detectBox).right,(*detectBox).bottom), Scalar( 255, 0, 0 )); } else { printf("Fail to detect faces.\n"); } return; }
/** * @param trackalg- input and output the track algorithm, will record some information for every frame * @param iImg - input input image * @param iShape - input the current tracked shape * @return bool whether the tracked shape is acceptable? */ bool CRecognitionAlgs::EvaluateFaceTrackedByProbabilityImage( CTrackingAlgs* trackalg, const Mat& iImg, const VO_Shape& iShape, Size smallSize, Size bigSize) { double t = (double)cvGetTickCount(); Rect rect = iShape.GetShapeBoundRect(); trackalg->SetConfiguration( CTrackingAlgs::CAMSHIFT, CTrackingAlgs::PROBABILITYIMAGE); trackalg->Tracking( rect, iImg, smallSize, bigSize ); bool res = false; if( !trackalg->IsObjectTracked() ) res = false; else if ( ((double)rect.height/(double)rect.width <= 0.75) || ((double)rect.height/(double)rect.width >= 2.5) ) res = false; else res = true; t = ((double)cvGetTickCount() - t ) / (cvGetTickFrequency()*1000.); cout << "Camshift Tracking time cost: " << t << "millisec" << endl; return res; }
// Perform a detection on the input image, using the given Haar Cascade. // Returns a rectangle for each detected region in the given image. CvSeq* detectInImage(IplImage *imageProcessed, CvHaarClassifierCascade* cascade) { // possibly convert to gray and equalize to improve detection //convert to gray IplImage * greyImg = convertImageToGreyscale(imageProcessed); const CvSize minFeatureSize = cvSize(100/pyrDownScale, 100/pyrDownScale); const int flags = CV_HAAR_DO_ROUGH_SEARCH | CV_HAAR_DO_CANNY_PRUNING; float search_scale_factor = 1.5f ; double t; unsigned int ms = 0; cvClearMemStorage( bodyStorage ); // down-scale the image to get performance boost IplImage * small_img = cvCreateImage( cvSize(greyImg->width/pyrDownScale,greyImg->height/pyrDownScale), IPL_DEPTH_8U, greyImg->nChannels ); cvPyrDown(greyImg, small_img, CV_GAUSSIAN_5x5); // Detect all the faces in the greyscale image. t = (double)cvGetTickCount(); rects = cvHaarDetectObjects( small_img, cascade, bodyStorage, search_scale_factor, 2, flags, minFeatureSize); t = (double)cvGetTickCount() - t; ms = cvRound( t / ((double)cvGetTickFrequency() * 1000.0) ); printf("Detection took %d ms and found %d objects\n", ms, rects->total); cvReleaseImage(&small_img); cvReleaseImage(&greyImg); return rects; }
/** * @author JIA Pei * @version 2010-02-05 * @brief Load Training data for texture model * @param mtd Input -- ltc method * @return void */ void VO_AFM::VO_LoadFeatureTrainingData(unsigned int mtd) { this->m_vLTCs.resize(this->m_iNbOfSamples); this->m_vNormalizedLTCs.resize(this->m_iNbOfSamples); cv::Mat img; cv::Mat_<float> oneLTC, tmpRow; for(unsigned int i = 0; i < this->m_iNbOfSamples; ++i) { unsigned int count = 0; this->m_vLTCs[i].Resize(this->m_iNbOfPoints, this->m_localImageSize.width*this->m_localImageSize.height); img = cv::imread ( this->m_vStringTrainingImageNames[i].c_str (), 0 ); double start = (double)cvGetTickCount(); // Explained by JIA Pei -- wavelet feature extraction for(unsigned int j = 0; j < this->m_iNbOfPoints; ++j) { oneLTC = this->VO_LoadLTC4OneAnnotatedPoint(img, this->m_vShapes[i], j, this->m_localImageSize, mtd); tmpRow = this->m_vLTCs[i].m_MatTexture.row(j); oneLTC.copyTo(tmpRow); } double end = (double)cvGetTickCount(); double elapsed = (end - start) / (cvGetTickFrequency()*1000.0); } }
static void getRightSize(image_session_t *mySession) { double t; int maxDim = mySession->origImage->width > mySession->origImage->height ? mySession->origImage->width : mySession->origImage->height; // Create a new image based on the input image if(CI_DEBUG_LEVEL >= PERF_DEBUG_LEVEL) t = (double)cvGetTickCount(); if(maxDim > IMAGE_SCALE_DIMENSION) mySession->scale = maxDim / (float) IMAGE_SCALE_DIMENSION; if(mySession->scale > IMAGE_MAX_SCALE) mySession->scale = IMAGE_MAX_SCALE; if(mySession->scale < 1.005f) mySession->scale = 1.0f; // We shouldn't waste time with such small rescales mySession->rightImage = cvCreateImage( cvSize(cvRound(mySession->origImage->width / mySession->scale), cvRound(mySession->origImage->height / mySession->scale)), IPL_DEPTH_8U, 1 ); if(mySession->scale > 1.0f) { IplImage *gray = cvCreateImage( cvSize(mySession->origImage->width, mySession->origImage->height), IPL_DEPTH_8U, 1 ); cvCvtColor( mySession->origImage, gray, CV_BGR2GRAY ); cvResize( gray, mySession->rightImage, IMAGE_INTERPOLATION ); cvReleaseImage( &gray ); } else cvCvtColor( mySession->origImage, mySession->rightImage, CV_BGR2GRAY ); if(mySession->scale != 1.0f) ci_debug_printf(9, "srv_classify_image: Image Resized - X: %d, Y: %d (shrunk by a factor of %f)\n", mySession->rightImage->width, mySession->rightImage->height, mySession->scale); if(CI_DEBUG_LEVEL >= PERF_DEBUG_LEVEL) { t = cvGetTickCount() - t; t = t / ((double)cvGetTickFrequency() * 1000.); ci_debug_printf(8, "srv_classify_image: Scaling and Color Prep took: %gms.\n", t); } }
//============================================================================= int main(int argc, const char** argv) { //parse command line arguments char ftFile[256],conFile[256],triFile[256]; bool fcheck = false; double scale = 1; int fpd = -1; bool show = true; if(parse_cmd(argc,argv,ftFile,conFile,triFile,fcheck,scale,fpd)<0)return 0; //set other tracking parameters std::vector<int> wSize1(1); wSize1[0] = 7; std::vector<int> wSize2(3); wSize2[0] = 11; wSize2[1] = 9; wSize2[2] = 7; int nIter = 5; double clamp=3,fTol=0.01; FACETRACKER::Tracker model(ftFile); cv::Mat tri=FACETRACKER::IO::LoadTri(triFile); cv::Mat con=FACETRACKER::IO::LoadCon(conFile); //initialize camera and display window cv::Mat frame,gray,im; double fps=0; char sss[256]; std::string text; CvCapture* camera = cvCreateCameraCapture(CV_CAP_ANY); if(!camera)return -1; int64 t1,t0 = cvGetTickCount(); int fnum=0; cvNamedWindow("Face Tracker",1); std::cout << "Hot keys: " << std::endl << "\t ESC - quit" << std::endl << "\t d - Redetect" << std::endl; //loop until quit (i.e user presses ESC) bool failed = true; while(1){ //grab image, resize and flip IplImage* I = cvQueryFrame(camera); if(!I)continue; frame = I; if(scale == 1)im = frame; else cv::resize(frame,im,cv::Size(scale*frame.cols,scale*frame.rows)); cv::flip(im,im,1); cv::cvtColor(im,gray,CV_BGR2GRAY); //track this image std::vector<int> wSize; if(failed)wSize = wSize2; else wSize = wSize1; if(model.Track(gray,wSize,fpd,nIter,clamp,fTol,fcheck) == 0){ int idx = model._clm.GetViewIdx(); failed = false; Draw(im,model._shape,con,tri,model._clm._visi[idx]); }else{ if(show){cv::Mat R(im,cvRect(0,0,150,50)); R = cv::Scalar(0,0,255);} model.FrameReset(); failed = true; } //draw framerate on display image if(fnum >= 9){ t1 = cvGetTickCount(); fps = 10.0/((double(t1-t0)/cvGetTickFrequency())/1e+6); t0 = t1; fnum = 0; }else fnum += 1; if(show){ sprintf(sss,"%d frames/sec",(int)round(fps)); text = sss; cv::putText(im,text,cv::Point(10,20), CV_FONT_HERSHEY_SIMPLEX,0.5,CV_RGB(255,255,255)); } //show image and check for user input imshow("Face Tracker",im); int c = cvWaitKey(10); if(c == 27)break; else if(char(c) == 'd')model.FrameReset(); }return 0; }
int main(int argc, char *argv[]) { CvCapture *capture = NULL; IplImage *src_frame, *image, *dst_frame; char *infile, *outfile; Matrix matrix; Args args; int64 t0, t1; double tps, deltatime; CvVideoWriter *writer; CvSize size; double fps; int frame_count; int i; infile = argv[1]; outfile = argv[2]; args.c = argc - 3; for (i = 0; i < 3; i++) args.v[i] = argv[i + 3]; capture = cvCaptureFromFile(infile); if (capture == NULL) { printf("Could not load video \"%s\".\n", infile); return EXIT_FAILURE; } src_frame = cvQueryFrame(capture); fps = cvGetCaptureProperty(capture, CV_CAP_PROP_FPS); size = cvSize( (int) cvGetCaptureProperty(capture, CV_CAP_PROP_FRAME_WIDTH), (int) cvGetCaptureProperty(capture, CV_CAP_PROP_FRAME_HEIGHT) ); writer = cvCreateVideoWriter(outfile, CV_FOURCC('M', 'J', 'P', 'G'), fps, size, 1); printf("Saving to \"%s\"...\n", outfile); image = cvCreateImage(size, IPL_DEPTH_8U, 1); dst_frame = cvCreateImage(size, IPL_DEPTH_8U, 3); matrix.width = dst_frame->width; matrix.height = dst_frame->height; frame_count = 0; t0 = cvGetTickCount(); while ((src_frame = cvQueryFrame(capture)) != NULL) { cvCvtColor(src_frame, image, CV_BGR2GRAY); matrix.data = (unsigned char *) image->imageData; proc(&matrix, &args); cvCvtColor(image, dst_frame, CV_GRAY2BGR); cvWriteFrame(writer, dst_frame); frame_count++; } t1 = cvGetTickCount(); tps = cvGetTickFrequency() * 1.0e6; deltatime = (double) (t1 - t0) / tps; printf("%d frames of %dx%d processed in %.3f seconds.\n", frame_count, dst_frame->width, dst_frame->height, deltatime); cvReleaseVideoWriter(&writer); cvReleaseImage(&dst_frame); cvReleaseCapture(&capture); return EXIT_SUCCESS; }
void pixkit::Timer::Start(){ if (is_started){ printf("pixkit::timer '%s' is already started. Nothing done.\n", title.c_str()); start_clock = cvGetTickCount(); return; } is_started = true; n_starts++; start_clock = cvGetTickCount(); }
void update() { if (!cascade) { cvSetNumThreads(cvRound(threads * 100)); f0r_param_string classifier; get_param_value(&classifier, FACEBL0R_PARAM_CLASSIFIER); if (classifier) { cascade = (CvHaarClassifierCascade*) cvLoad(classifier, 0, 0, 0 ); if (!cascade) fprintf(stderr, "ERROR: Could not load classifier cascade %s\n", classifier); storage = cvCreateMemStorage(0); } else { memcpy(out, in, size * 4); return; } } // copy input image to OpenCV if( !image ) image = cvCreateImage(cvSize(width, height), IPL_DEPTH_8U, 4); memcpy(image->imageData, in, size * 4); // only re-detect periodically to control performance and reduce shape jitter int recheckInt = abs(cvRound(recheck * 1000)); if ( recheckInt > 0 && count % recheckInt ) { // skip detect count++; // fprintf(stderr, "draw-only counter %u\n", count); } else { count = 1; // reset the recheck counter if (objects) // reset the list of objects cvClearSeq(objects); double elapsed = (double) cvGetTickCount(); objects = detect(); // use detection time to throttle frequency of re-detect vs. redraw (automatic recheck) elapsed = cvGetTickCount() - elapsed; elapsed = elapsed / ((double) cvGetTickFrequency() * 1000.0); // Automatic recheck uses an undocumented negative parameter value, // which is not compliant, but technically feasible. if (recheck < 0 && cvRound( elapsed / (1000.0 / (recheckInt + 1)) ) <= recheckInt) count += recheckInt - cvRound( elapsed / (1000.0 / (recheckInt + 1))); // fprintf(stderr, "detection time = %gms counter %u\n", elapsed, count); } draw(); // copy filtered OpenCV image to output memcpy(out, image->imageData, size * 4); cvReleaseImage(&image); }
void update(double time, uint32_t* out, const uint32_t* in) { if (cascade.empty()) { cv::setNumThreads(cvRound(threads * 100)); if (classifier.length() > 0) { if (!cascade.load(classifier.c_str())) fprintf(stderr, "ERROR: Could not load classifier cascade %s\n", classifier.c_str()); } else { memcpy(out, in, size * 4); return; } } // sanitize parameters search_scale = CLAMP(search_scale, 0.11, 1.0); neighbors = CLAMP(neighbors, 0.01, 1.0); // copy input image to OpenCV image = cv::Mat(height, width, CV_8UC4, (void*)in); // only re-detect periodically to control performance and reduce shape jitter int recheckInt = abs(cvRound(recheck * 1000)); if ( recheckInt > 0 && count % recheckInt ) { // skip detect count++; // fprintf(stderr, "draw-only counter %u\n", count); } else { count = 1; // reset the recheck counter if (objects.size() > 0) // reset the list of objects objects.clear(); double elapsed = (double) cvGetTickCount(); objects = detect(); // use detection time to throttle frequency of re-detect vs. redraw (automatic recheck) elapsed = cvGetTickCount() - elapsed; elapsed = elapsed / ((double) cvGetTickFrequency() * 1000.0); // Automatic recheck uses an undocumented negative parameter value, // which is not compliant, but technically feasible. if (recheck < 0 && cvRound( elapsed / (1000.0 / (recheckInt + 1)) ) <= recheckInt) count += recheckInt - cvRound( elapsed / (1000.0 / (recheckInt + 1))); // fprintf(stderr, "detection time = %gms counter %u\n", elapsed, count); } draw(); // copy filtered OpenCV image to output memcpy(out, image.data, size * 4); }
int main (int argc, char **argv) { int width=960, height=640; IplImage *img=0; double c, f; f = cvGetTickFrequency()*1000; int cx = width/2; int cy = height/2; double radius = 100; double angle = 0; CvScalar color = cvScalarAll(255); CvFont font; cvInitFont(&font, CV_FONT_HERSHEY_SIMPLEX, 0.5, 1.0, 1.0, 1, CV_AA); cvNamedWindow ("hexagon", CV_WINDOW_AUTOSIZE); while (1) { // (1)allocate and initialize an image img = cvCreateImage(cvSize(width, height), IPL_DEPTH_8U, 3); if(img == 0) return -1; cvZero(img); // (2) draw hexagon c = cvGetTickCount(); myHexagon(img, cx, cy, radius, angle, color); printf("%fms\n", (cvGetTickCount()-c)/f); // (3)show the iamge, and press some key cvPutText(img, "Coordinate Right(D) Left(A) Up(W) Down(X)", cvPoint(10, 20), &font, cvScalarAll(255)); cvPutText(img, "Rotate Right(R) Left(E)", cvPoint(10, 40), &font, cvScalarAll(255)); cvPutText(img, "Radius Big(V) Small(C)", cvPoint(10, 60), &font, cvScalarAll(255)); cvPutText(img, "Quit(Q, esc)", cvPoint(10, 80), &font, cvScalarAll(255)); char s[64]; sprintf(s, "cx:%d cy:%d radius:%f angle:%f", cx, cy, radius, angle); cvPutText(img, s, cvPoint(10, 110), &font, cvScalarAll(255)); cvShowImage ("hexagon", img); char key = cvWaitKey (0); if (key == 27 || key == 'q') break; else if (key == 'r') angle += 5; else if (key == 'e') angle -= 5; else if (key == 'a') cx -= 5; else if (key == 'd') cx += 5; else if (key == 'w') cy -= 5; else if (key == 'x') cy += 5; else if (key == 'v') radius += 5; else if (key == 'c') radius -= 5; } cvDestroyWindow("hexagon"); cvReleaseImage(&img); return 0; }
int main(int argc, char** argv) { int width = kDefaultWidth; int height = kDefaultHeight; if (argc > 2) { int w = atoi(argv[1]); width = w ? w : width; int h = atoi(argv[2]); height = h ? h : height; } // カメラからのビデオキャプチャを初期化する CvCapture* capture = cvCaptureFromCAM(CV_CAP_ANY); if (capture == NULL) { fprintf(stderr, "ERROR: Camera not found\n"); return 1; } // キャプチャサイズを設定する cvSetCaptureProperty(capture, CV_CAP_PROP_FRAME_WIDTH, width); cvSetCaptureProperty(capture, CV_CAP_PROP_FRAME_HEIGHT, height); // ウィンドウを作成する cvNamedWindow(kWindowName, CV_WINDOW_AUTOSIZE); // フォント構造体を初期化する CvFont font; cvInitFont(&font, CV_FONT_HERSHEY_PLAIN, 1.0f, 1.0f, 0.0f, 1, CV_AA); // カメラから画像をキャプチャする while (1) { long startTick = cvGetTickCount(); IplImage* image = cvQueryFrame(capture); // カメラから一つのフレームを取り出して返す long stopTick = cvGetTickCount(); char message[kMessageSize]; snprintf(message, kMessageSize, "%.3f [ms]", (stopTick - startTick) / cvGetTickFrequency() / 1000); cvPutText(image, message, cvPoint(10, 20), &font, CV_RGB(0, 0, 0)); // 画像に文字列を描画する cvShowImage(kWindowName, image); // ウィンドウに画像を表示する int key = cvWaitKey(1); // キーが押されるまで待機する if (key == 'q') { break; } else if (key == 's') { char* filename = "capture.png"; printf("Save a capture image: %s\n", filename); //cvSaveImage(filename, image); // OpenCV 1.0 cvSaveImage(filename, image, 0); // OpenCV 2.0 } } cvReleaseCapture(&capture); cvDestroyWindow(kWindowName); return 0; }
/** * @brief Tracking by ASM/AAM * @param img * @param obj * @param isTracked * @param smallSize * @param bigSize * @return tracking time */ double CTrackingAlgs::ASMAAMTracking(const cv::Mat& img, cv::Rect& obj, bool& isTracked, cv::Size smallSize, cv::Size bigSize) { double res = (double)cvGetTickCount(); res = ((double)cvGetTickCount() - res) / ((double)cvGetTickFrequency()*1000.); return res; }
void detect_and_draw( IplImage* img, int muncul ) { static CvScalar colors[] = { {{0,0,255}}, {{0,128,255}}, {{0,255,255}}, {{0,255,0}}, {{255,128,0}}, {{255,255,0}}, {{255,0,0}}, {{255,0,255}} }; double scale = 1.3; IplImage* gray = cvCreateImage( cvSize(img->width,img->height), 8, 1 ); IplImage* small_img = cvCreateImage( cvSize( cvRound (img->width/scale), cvRound (img->height/scale)), 8, 1 ); int i; cvCvtColor( img, gray, CV_BGR2GRAY ); cvResize( gray, small_img, CV_INTER_LINEAR ); cvEqualizeHist( small_img, small_img ); cvClearMemStorage( storage ); if( cascade ) { double t = (double)cvGetTickCount(); CvSeq* faces = cvHaarDetectObjects( small_img, cascade, storage, 1.1, 2, 0/*CV_HAAR_DO_CANNY_PRUNING*/, cvSize(30, 30) ); t = (double)cvGetTickCount() - t; //printf( "detection time = %gms\n", t/((double)cvGetTickFrequency()*1000.) ); printf( "%s detected area = %d\n", input_name, faces->total); for( i = 0; i < (faces ? faces->total : 0); i++ ) { CvRect* r = (CvRect*)cvGetSeqElem( faces, i ); CvPoint center; int radius; center.x = cvRound((r->x + r->width*0.5)*scale); center.y = cvRound((r->y + r->height*0.5)*scale); radius = cvRound((r->width + r->height)*0.25*scale); cvCircle( img, center, radius, colors[i%8], CV_FILLED, 8, 0 ); } } if(muncul==1) { cvShowImage( "result", img ); } cvReleaseImage( &gray ); cvReleaseImage( &small_img ); }
static void CheckGamePatterns(int sizex, int sizey, int patID) { // Get crosshair coordinate (centre of screen) Point2f crosshair(sizex / 2, sizey / 2); if (patternsCoordinates[patID].size()) { // Get distance from crosshair to pattern int dist = norm(patternsCoordinates[patID][4] - crosshair); //cout << dist << endl; // Check distance and drone altitude if (dist < 200 && ardrone.getAltitude() < 0.7) { // Start current pattern seen timer int now = cvGetTickCount(); int passedSinceSeen = ((now - patternTimer) / (cvGetTickFrequency() * 1000)) / 1000; cout << passedSinceSeen << endl; // Check if pattern is within range for more than 2 seconds if (true) { //if (passedSinceSeen > 1) { switch (patID) { case 1: cout << "!!!! PLATFORM" << endl; if (peoplePicked) { points += 1000; peoplePicked = false; } if (cratePicked) { points += 100; cratePicked = false; } break; case 2: if (gameOn) { cout << "!!!! PEOPLE" << endl; peoplePicked = true; } break; case 3: if (gameOn) { cout << "!!!! CRATE" << endl; cratePicked = true; } break; default: break; } } } else patternTimer = cvGetTickCount(); } }
/** * @author JIA Pei * @version 2010-05-20 * @brief Direct AAM Fitting, for dynamic image sequence * @param iImg Input - image to be fitted * @param ioShape Input and Output - the fitted shape * @param oImg Output - the fitted image * @param epoch Input - the iteration epoch */ float VO_FittingAAMBasic::VO_DirectAAMFitting(const Mat& iImg, VO_Shape& ioShape, Mat& oImg, unsigned int epoch) { double t = (double)cvGetTickCount(); t = ((double)cvGetTickCount() - t )/ (cvGetTickFrequency()*1000.); cout << "Direct fitting time cost: " << t << " millisec" << endl; return t; }
/** * @author JIA Pei * @version 2010-05-20 * @brief Direct AAM Fitting, for static images, so that we record the whole fitting process * @param iImg Input - image to be fitted * @param epoch Input - the iteration epoch * @param oImages Output - the fitted shape */ float VO_FittingAAMBasic::VO_DirectAAMFitting(const Mat& iImg, vector<Mat>& oImages, unsigned int epoch, bool record) { double t = (double)cvGetTickCount(); t = ((double)cvGetTickCount() - t )/ (cvGetTickFrequency()*1000.); cout << "Direct fitting time cost: " << t << " millisec" << endl; return t; }
void detect_and_draw(IplImage* img ) { double scale=1.2; static CvScalar colors[] = { {{0,0,255}},{{0,128,255}},{{0,255,255}},{{0,255,0}}, {{255,128,0}},{{255,255,0}},{{255,0,0}},{{255,0,255}} };//Just some pretty colors to draw with //Image Preparation // IplImage* gray = cvCreateImage(cvSize(img->width,img->height),8,1); IplImage* small_img=cvCreateImage(cvSize(cvRound(img->width/scale),cvRound(img->height/scale)),8,1); cvCvtColor(img,gray, CV_BGR2GRAY); cvResize(gray, small_img, CV_INTER_LINEAR); cvEqualizeHist(small_img,small_img); //Ö±·½Í¼¾ùºâ //Detect objects if any // cvClearMemStorage(storage); double t = (double)cvGetTickCount(); CvSeq* objects = cvHaarDetectObjects(small_img, cascade, storage, 1.1, 2, 0/*CV_HAAR_DO_CANNY_PRUNING*/, cvSize(30,30)); t = (double)cvGetTickCount() - t; printf( "detection time = %gms\n", t/((double)cvGetTickFrequency()*1000.) ); //Loop through found objects and draw boxes around them for(int i=0; i<(objects? objects->total:0); ++i) { CvRect* r=(CvRect*)cvGetSeqElem(objects,i); cvRectangle(img, cvPoint(r->x*scale,r->y*scale), cvPoint((r->x+r->width)*scale,(r->y+r->height)*scale), colors[i%8]); } for( int i = 0; i < (objects? objects->total : 0); i++ ) { CvRect* r = (CvRect*)cvGetSeqElem( objects, i ); CvPoint center; int radius; center.x = cvRound((r->x + r->width*0.5)*scale); center.y = cvRound((r->y + r->height*0.5)*scale); radius = cvRound((r->width + r->height)*0.25*scale); cvCircle( img, center, radius, colors[i%8], 3, 8, 0 ); } cvShowImage( "result", img ); cvReleaseImage(&gray); cvReleaseImage(&small_img); }
void detectFaceInImage(IplImage *orig, IplImage* input, CvHaarClassifierCascade* cascade, FLANDMARK_Model *model, int *bbox, double *landmarks) { // Smallest face size. CvSize minFeatureSize = cvSize(40, 40); int flags = CV_HAAR_DO_CANNY_PRUNING; // How detailed should the search be. float search_scale_factor = 1.1f; CvMemStorage* storage; CvSeq* rects; int nFaces; storage = cvCreateMemStorage(0); cvClearMemStorage(storage); // Detect all the faces in the greyscale image. rects = cvHaarDetectObjects(input, cascade, storage, search_scale_factor, 2, flags, minFeatureSize); nFaces = rects->total; double t = (double)cvGetTickCount(); for (int iface = 0; iface < (rects ? nFaces : 0); ++iface) { CvRect *r = (CvRect*)cvGetSeqElem(rects, iface); bbox[0] = r->x; bbox[1] = r->y; bbox[2] = r->x + r->width; bbox[3] = r->y + r->height; flandmark_detect(input, bbox, model, landmarks); // display landmarks cvRectangle(orig, cvPoint(bbox[0], bbox[1]), cvPoint(bbox[2], bbox[3]), CV_RGB(255,0,0) ); cvRectangle(orig, cvPoint(model->bb[0], model->bb[1]), cvPoint(model->bb[2], model->bb[3]), CV_RGB(0,0,255) ); cvCircle(orig, cvPoint((int)landmarks[0], (int)landmarks[1]), 3, CV_RGB(0, 0,255), CV_FILLED); for (int i = 2; i < 2*model->data.options.M; i += 2) { cvCircle(orig, cvPoint(int(landmarks[i]), int(landmarks[i+1])), 3, CV_RGB(255,0,0), CV_FILLED); } } t = (double)cvGetTickCount() - t; int ms = cvRound( t / ((double)cvGetTickFrequency() * 1000.0) ); if (nFaces > 0) { printf("Faces detected: %d; Detection of facial landmark on all faces took %d ms\n", nFaces, ms); } else { printf("NO Face\n"); } cvReleaseMemStorage(&storage); }
/** * @author JIA Pei * @version 2010-02-02 * @brief Camshift Tracking * @param img Input - image to be searched within * @param objs Input - object to be tracked * @param isTracked output - is this objs tracked? * @param smallSize Input - the smallest possible object size * @param bigSize Input - the biggest possible object size * @return detection time cost */ double CTrackingAlgs::CamshiftTracking( const cv::Mat& img, cv::MatND& hist, cv::Rect& obj, bool& isTracked, cv::Size smallSize, cv::Size bigSize) { double res = (double)cvGetTickCount(); if(obj.x <= 0) obj.x = 0; if(obj.y <= 0) obj.y = 0; if(obj.x + obj.width > img.cols) obj.width = img.cols - obj.x; if(obj.y + obj.height > img.rows) obj.height = img.rows - obj.y; cv::Rect trackwindow = obj; cv::Mat hsv, hue, mask, backproject; cv::cvtColor( img, hsv, CV_BGR2HSV ); int _vmin = CTrackingAlgs::vmin, _vmax = CTrackingAlgs::vmax; cv::inRange( hsv, cv::Scalar(0,CTrackingAlgs::smin,MIN(_vmin,_vmax),0), cv::Scalar(180,256,MAX(_vmin,_vmax),0), mask ); std::vector<cv::Mat> vhsv(3); cv::split( hsv, vhsv ); vhsv[0].copyTo(hue); cv::calcBackProject( &hue, 1, CTrackingAlgs::channels, hist, backproject, CTrackingAlgs::ranges); cv::bitwise_and( backproject, mask, backproject ); cv::RotatedRect trackbox = cv::CamShift( backproject, trackwindow, cv::TermCriteria(cv::TermCriteria::COUNT+cv::TermCriteria::EPS, 10, 1) ); obj = trackwindow; // cv::ellipse(img, trackbox, CV_RGB(255,0,0), 3, CV_AA); cv::Point pt1 = cv::Point( (int)(obj.x), (int)(obj.y) ); cv::Point pt2 = cv::Point( (int)(obj.x + obj.width), (int)(obj.y + obj.height) ); // Judge whether it is losing the object or not... if(obj.width >= bigSize.width || obj.height >= bigSize.height || obj.width <= smallSize.width || obj.height <= smallSize.height || pt1.x < FRAMEEDGE || pt1.y < FRAMEEDGE || (pt2.x > (img.cols - FRAMEEDGE)) || (pt2.y > (img.rows - FRAMEEDGE))) { isTracked = false; obj.x = obj.y = obj.width = obj.height = -1; } else isTracked = true; res = ((double)cvGetTickCount() - res) / ((double)cvGetTickFrequency()*1000.); return res; }
float VO_FittingAAMForwardIA::VO_FAIAAAMFitting(const Mat& iImg, VO_Shape& ioShape, Mat& oImg, unsigned int epoch) { double t = (double)cvGetTickCount(); t = ((double)cvGetTickCount() - t )/ (cvGetTickFrequency()*1000.); cout << "FAIA fitting time cost: " << t << " millisec" << endl; this->m_fFittingTime = t; return t; }
int main( int argc, char** argv){ IplImage *frame; int key; /* supply the AVI file to play */ assert( argc == 2 ); /* load the AVI file */ CvCapture *capture = cvCreateFileCapture(argv[1]) ; //cvCaptureFromAVI( argv[1] ); /* always check */ if( !capture ) { printf("ERROR: capture == NULL \n"); return 1; } /* get fps, needed to set the delay */ int fps = ( int )cvGetCaptureProperty( capture, CV_CAP_PROP_FPS ); int frameH = (int) cvGetCaptureProperty(capture, CV_CAP_PROP_FRAME_HEIGHT); int frameW = (int) cvGetCaptureProperty(capture, CV_CAP_PROP_FRAME_WIDTH); /* display video */ cvNamedWindow( "video", CV_WINDOW_AUTOSIZE ); while( key != 'q' ) { double t1=(double)cvGetTickCount(); /* get a frame */ frame = cvQueryFrame( capture ); double t2=(double)cvGetTickCount(); printf("time: %gms fps: %.2g\n",(t2-t1)/(cvGetTickFrequency()*1000.), 1000./((t2-t1)/(cvGetTickFrequency()*1000.))); /* always check */ if( !frame ) break; /* display frame */ cvShowImage( "video", frame ); /* quit if user press 'q' */ key = cvWaitKey( 1000 / fps ); } /* free memory */ cvReleaseCapture( &capture ); cvDestroyWindow( "video" ); return 0; }
void dostuff(IplImage *frm) { frame=frm; // Linux and Windows OpenCV implementations seems to differ // Flip input rfames upside down for win-version #ifdef WIN32 cvFlip(frame,NULL,0); #endif if(first) { first=false; if(!hb.Init(frm,outfile)) exit(2); if(!mshb.Init(frm,outfile,sourcename,frame_begin)) exit(2); gray = cvCreateImage(cvGetSize(frm), IPL_DEPTH_8U, 1); } // CVUtil::RGB2GRAY(frm,gray); if (frm->nChannels==1) cvCopy(frm,gray); else cvCvtColor(frm,gray,CV_RGB2GRAY); //CVUtil::DrawCircleFeature(frm,cvPoint(10,10),4.0); //cvShowImage(win2, frm ); double t,ft; t = (double)cvGetTickCount();Processing=true; //hb.ProcessFrame(gray); mshb.ProcessFrame(gray); t = (double)cvGetTickCount() - t;Processing=false; ifr++; ft=t/(cvGetTickFrequency()*1000.); avg=((ifr-1)* avg + ft)/ifr; //printf("%4d: ",ifr); //nIPs=hb.NumberOfDetectedIPs();TotalIPs+=nIPs; nIPs=mshb.NumberOfDetectedIPs();TotalIPs+=nIPs; if (ifr%20==0){ printf("Fame: %5d - ",ifr); printf("IPs[this:%2d, total:%4d]",nIPs,TotalIPs); //printf(" - Perf: Time= %.1f - Avg Time=%.1f - Avg FPS=%.1f ", ft,avg, 1000/avg); printf(" - Perf: Avg FPS=%.1f ",1000/avg); printf("\n"); } }
int adaboostDetect::detectAndDraw(IplImage* img, CvRect** regions) { double t = (double) cvGetTickCount(); int fii = 0; IplImage* gray = cvCreateImage(cvSize(img->width, img->height), 8, 1); IplImage* smallImg = cvCreateImage( cvSize( cvRound (img->width/scaleFactor), cvRound (img->height/scaleFactor)), 8, 1 ); cvCvtColor(img, gray, CV_BGR2GRAY); cvResize(gray, smallImg,CV_INTER_LINEAR); cvEqualizeHist(smallImg, smallImg); cvClearMemStorage(storage); int nx1, nx2, ny1, ny2; CvRect* nR; if (!cascade) { return 0; } CvSeq* faces = cvHaarDetectObjects( smallImg, cascade, storage, scaleFactor, minNeighbours, flags, minSize); for (int i=0; i<(faces ? faces->total : 0); i++) { if (i == 0) { nR = (CvRect*) malloc(1 * sizeof(CvRect)); } else { nR = (CvRect*) realloc(nR, (i+1) * sizeof(CvRect)); } CvRect* r = (CvRect*) cvGetSeqElem(faces, i); CvPoint center; center.x = cvRound((r->x + r->width * 0.5) * scaleFactor); center.y = cvRound((r->y + r->height * 0.5) * scaleFactor); nx1 = cvRound(r->x * scaleFactor); ny1 = cvRound(r->y * scaleFactor); nx2 = cvRound((r->x + r->width) * scaleFactor); ny2 = cvRound((r->y + r->height) * scaleFactor); nR[fii] = cvRect(nx1, ny1, nx2-nx1, ny2-ny1); CvScalar color; color = CV_RGB(0, 255, 0); cvRectangle(img, cvPoint(nx1, ny1), cvPoint(nx2, ny2), color); fii++; } *regions = nR; cvShowImage("result", img); cvReleaseImage(&gray); cvReleaseImage(&smallImg); t = (double) cvGetTickCount() - t; printf( "detection time = %gms\n", t/((double)cvGetTickFrequency()*1000.) ); return fii; }
int main( int argc, char **argv ) { CvCapture *capture = 0; IplImage *frame = 0; int key = 0; int t; /* initialize camera */ capture = cvCaptureFromCAM( 0 ); /* always check */ if ( !capture ) { fprintf( stderr, "Cannot open initialize webcam!\n" ); return 1; } /* create a window for the video */ cvNamedWindow( "result", CV_WINDOW_AUTOSIZE ); while( key != 'q' ) { t = (double)cvGetTickCount(); printf("%g", t/((double)cvGetTickFrequency()*1000.)); /* get a frame */ frame = cvQueryFrame( capture ); t = (double)cvGetTickCount() - t; printf("get frame time = %g ms\n", t/((double)cvGetTickFrequency()*1000.)); /* always check */ if( !frame ) break; t = (double)cvGetTickCount(); printf("%g", t/((double)cvGetTickFrequency()*1000.)); /* display current frame */ cvShowImage( "result", frame ); t = (double)cvGetTickCount() - t; printf("show time = %g ms\n", t/((double)cvGetTickFrequency()*1000.)); /* exit if user press 'q' */ key = cvWaitKey( 1 ); } /* free memory */ cvDestroyWindow( "result" ); cvReleaseCapture( &capture ); return 0; }
CybRegionTrackInfo *CybHaarTracker::detect(IplImage *img) { double scale = 1.3; CybRegionTrackInfo *region= NULL; IplImage* gray = cvCreateImage(cvSize(img->width, img->height), 8, 1); IplImage* small_img = cvCreateImage(cvSize(cvRound(img->width/scale), cvRound(img->height/scale)), 8, 1); cvCvtColor(img, gray, CV_BGR2GRAY); cvResize(gray, small_img, CV_INTER_LINEAR); cvEqualizeHist(small_img, small_img); cvClearMemStorage(storage); if (cascade) { double t = (double)cvGetTickCount(); CvSeq* faces = cvHaarDetectObjects(small_img, cascade, storage, 1.1, 2, 0/*CV_HAAR_DO_CANNY_PRUNING*/, cvSize(30, 30) ); t = (double)cvGetTickCount() - t; if (faces && (faces->total > 0)) { CvRect* r = (CvRect*)cvGetSeqElem(faces, 0); region = new CybRegionTrackInfo((int)((r->x)*scale), (int)((r->x + r->width)*scale), (int)((r->y)*scale), (int)((r->y + r->height)*scale)); int meanx = (region->getMaxX() + region->getMinX())/2; int meany = (region->getMaxY() + region->getMinY())/2; if (dbg_mode) { cvLine(img, cvPoint(meanx, meany), cvPoint(meanx, meany), CV_RGB(50, 50 , 50), 4, 8, 0); cvRectangle(img, cvPoint((int)(region->getMinX()), (int)(region->getMinY())), cvPoint( (int)(region->getMaxX()), (int) (region->getMaxY())), CV_RGB(150, 150, 150), 2, 8, 0); } } } cvReleaseImage( &gray); cvReleaseImage( &small_img); return region; }