Advertisement
hemalchevli

Opencv Test

Jun 22nd, 2013
618
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.03 KB | None | 0 0
  1. #include "cv.h"
  2. #include "highgui.h"
  3. #include <stdio.h>
  4.  
  5. //fps clac
  6. #define h 280
  7. #define w 320
  8.  
  9. using namespace cv;
  10. using namespace std;
  11.  
  12.  
  13. int main( int argc, char** argv )
  14. {
  15.  
  16.     int c;
  17.     CvCapture* capture = 0;
  18.     Mat image,gray_image,sub_Image;
  19.     //FPS variables
  20.     time_t start,end;
  21.     Point org;
  22.     org.x = 10;
  23.     org.y = 50;
  24.     int fonttype = 2; //0 to 8  FONT_HERSHEY_SIMPLEX , FONT_HERSHEY_PLAIN , FONT_HERSHEY_DUPLEX ,
  25.     //        FONT_HERSHEY_COMPLEX , FONT_HERSHEY_TRIPLEX , FONT_HERSHEY_COMPLEX_SMALL ,
  26.     //        FONT_HERSHEY_SCRIPT_SIMPLEX or FONT_HERSHEY_SCRIPT_COMPLEX
  27.     int scale = 1; //0.1 to 5
  28.     int colour = 0; //0 to 255
  29.     int thickness = 2; //0 to 10
  30.     int lineType = 8;
  31.     ostringstream str;
  32.     double sec;//perviously double
  33.     double fps;//perviously double
  34.     //fps end
  35.  
  36.  
  37.     if( argc == 1 || (argc == 2 && strlen(argv[1]) == 1 && isdigit(argv[1][0])))
  38.         capture = cvCaptureFromCAM( 0 );
  39.  
  40.     cvNamedWindow( "Input", CV_WINDOW_AUTOSIZE );
  41.  
  42.     int param1=200, param2=100,min=0,max=0,sigma=1;
  43.     //createTrackbar(trackbarname, windowname, int* value, int count, TrackbarCallback onChange CV_DEFAULT(0), void* userdata CV_DEFAULT(0))
  44.     cvCreateTrackbar("Parameter1", "Input", &param1, 500, NULL);
  45.     cvCreateTrackbar("Parameter2", "Input", &param2, 500, NULL);
  46.     cvCreateTrackbar("Min Raduis", "Input", &min, 10, NULL);
  47.     cvCreateTrackbar("Max Radius", "Input", &max, 10, NULL);
  48.     cvCreateTrackbar("Blur", "Input", &sigma, 10, NULL);
  49.  
  50.     ///Wait for cam to stabilize , press ESC when ready
  51.     for(;;)
  52.     {
  53.         image = cvQueryFrame( capture );
  54. //        cvAddS(image, cvScalar(bright-128,bright-128,bright-128), image);
  55.  
  56.         //flip(image,image,1);
  57.         imshow( "Input", image );
  58.         c = cvWaitKey(10);
  59.         if( (char)c == 27 )
  60.             break;
  61.     }
  62.  
  63.    // time(&start);
  64.     //int counter=0;
  65.  
  66.     for(;;)
  67.     {
  68.  
  69.         image = cvQueryFrame( capture );
  70.         //if( !image ) break;
  71.  
  72.         //subImage = image(cvRect(10,10,50,50));
  73.         //x y width and height
  74.         image = image(cvRect(190,180,250,250));
  75.  
  76.         //Downsample image if needed
  77.         //pyrDown( dst, tmp, Size( dst.cols/2, dst.rows/2 ));
  78.         ///Do processign here
  79.  
  80.         //Flip image optional
  81.         //flip(image,image,1);
  82.         //convert to grayscale
  83.         cvtColor( image, gray_image, CV_BGR2GRAY );
  84.  
  85.         //blur to remove noise
  86.         GaussianBlur( image, image, Size(9,9), sigma, sigma );
  87.  
  88.         ///Detect circles Start
  89.         vector<Vec3f> circles;
  90.  
  91.         // Apply the Hough Transform to find the circles
  92.         HoughCircles( gray_image, circles, CV_HOUGH_GRADIENT, 2, gray_image.rows/8, param1,param2, min,max );
  93.         //HoughCircles( gray_image, circles, CV_HOUGH_GRADIENT, 2, gray_image.rows/8, 200, 100, 0,0 );
  94.  
  95.         // Draw the circles detected
  96.         for( size_t i = 0; i < circles.size(); i++ )
  97.         {
  98.             Point center(cvRound(circles[i][0]), cvRound(circles[i][1]));
  99.             int radius = cvRound(circles[i][2]);
  100.             // circle center
  101.             circle( image, center, 3    , Scalar(0,255,0), -1, 8, 0 );
  102.             // circle outline
  103.             circle( image, center, radius, Scalar(0,0,255), 3, 8, 0 );
  104.         }
  105.         ///Circle detect end
  106.  
  107.         ///processing end
  108. //        cvShowImage( "Input", image ); //does not work with Mat
  109.  
  110.         /*
  111.         //fps
  112.         time(&end);
  113.         ++counter;
  114.         sec=difftime(end,start);
  115.         fps=counter/sec;
  116.         //prints on console
  117.  
  118.         //printf("\n%lf",fps); //prints on console
  119.         //printf("\n%d",(int)fps);
  120.  
  121.         //print on image
  122.  
  123.         ostringstream str; //prints on itself
  124.        str << "FPS: " << (int)fps;
  125.         putText( image,  str.str() , org, fonttype, scale, colour, thickness, lineType);
  126.         //fps end
  127.         */
  128.         imshow( "Input", image );
  129.         //c = cvWaitKey(10);
  130.         c = cvWaitKey(15);
  131.         if( (char)c == 27 )
  132.             break;
  133.     }
  134.  
  135.     return 0;
  136. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement