/opencv_demo_win32/opencv_demo_win32.cpp

https://bitbucket.org/raviyellani/opencv · C++ · 108 lines · 63 code · 14 blank · 31 comment · 6 complexity · 66460ebb5d265c2abce6fa00b03cfb91 MD5 · raw file

  1. // opencv_demo_win32.cpp : Defines the entry point for the console application.
  2. //
  3. #include "cv.h"
  4. #include "cxcore.h"
  5. #include "highgui.h"
  6. int main(int argc, char** argv[])
  7. {
  8. //defines the name of the input picture
  9. char * filename = "inputpic_stoer_.jpg";
  10. //Load the color image into an array
  11. //src points to the memory location where the input image is saved
  12. printf("Load Image \r\n");
  13. IplImage *src = cvLoadImage( filename, CV_LOAD_IMAGE_COLOR );
  14. //check if loading was successfull
  15. if(NULL==src)
  16. {
  17. printf("Error: Couldn't open the file");
  18. }
  19. else
  20. {
  21. //convert input image to greyscale:
  22. //cvCreateImage allocates memory for the greyscale picture
  23. //the hough transformation works with greyscale images only
  24. printf("Convert to greyscale\r\n");
  25. IplImage *gsrc = cvCreateImage( cvGetSize(src), IPL_DEPTH_8U, 1 );
  26. cvCvtColor(src,gsrc,CV_RGB2GRAY);
  27. printf("Save converted input picture\r\n");
  28. cvSaveImage( "converted_input.jpg", gsrc );
  29. //The hough transformation returns several circles
  30. //so you have to create store them somewhere
  31. //For this OpenCV implemented CvMemStorage
  32. CvMemStorage* storage = cvCreateMemStorage(0);
  33. //The hough transformation works better with a slightly blurred image
  34. cvSmooth(gsrc,gsrc, CV_GAUSSIAN, 5,5);
  35. //This function detects the circles, saves them in the memory storage
  36. //an returns the pointer to the first entry
  37. CvSeq* results = cvHoughCircles(gsrc,storage,CV_HOUGH_GRADIENT,2,src->width/10,100,300);
  38. //draw the found circles on the source image
  39. for(int i = 0;i<results->total;i++)
  40. {
  41. //get the first value from the memory storage
  42. float* p = (float*) cvGetSeqElem(results,i);
  43. CvPoint pt = cvPoint(cvRound(p[0]),cvRound(p[1]));
  44. //draw a red circle
  45. cvCircle(src,pt,cvRound(p[2]),CV_RGB(0xff,0x00,0x00),3);
  46. }
  47. //check if the container contains something with red, green or blue color
  48. for(int i = 0;i<results->total;i++)
  49. {
  50. //again, get the values from the memory storage
  51. float* p = (float*) cvGetSeqElem(results,i);
  52. CvPoint pt = cvPoint(cvRound(p[0]),cvRound(p[1]));
  53. //get the color of the pixel of the center of the detected circle
  54. CvScalar color= cvGet2D(src,pt.y,pt.x);
  55. //if blue (ChannelSequence in src-image is switched: BGR not RGB)
  56. if(color.val[0]>150)
  57. {
  58. cvCircle(src,pt,10,CV_RGB(0xff,0xff,0xff),2);
  59. cvCircle(src,pt,10,CV_RGB(0x00,0x00,0xff),-1);
  60. }
  61. //if green
  62. if(color.val[1]>150)
  63. {
  64. cvCircle(src,pt,8,CV_RGB(0xff,0xff,0xff),2);
  65. cvCircle(src,pt,8,CV_RGB(0x00,0xff,0x00),-1);
  66. }
  67. //if red
  68. if(color.val[2]>150)
  69. {
  70. cvCircle(src,pt,6,CV_RGB(0xff,0xff,0xff),2);
  71. cvCircle(src,pt,6,CV_RGB(0xff,0x00,0x00),-1);
  72. }
  73. }
  74. printf("Save changed source file\r\n");
  75. cvSaveImage( "detected.jpg", src );
  76. //Window functions does not work on windows CE
  77. #ifndef WINCE
  78. //*Show the pic*/
  79. printf("Show pics \r\n");
  80. cvNamedWindow("Source", CV_WINDOW_AUTOSIZE);
  81. cvShowImage("Source", src);
  82. //cvNamedWindow("Converted Source", CV_WINDOW_AUTOSIZE);
  83. //cvShowImage("Converted Source", gsrc);
  84. //wait for user to press a button
  85. cvWaitKey(0);
  86. cvDestroyWindow("Source");
  87. //cvDestroyWindow("Converted Source");
  88. #endif
  89. //release the memory that was used for the pictures
  90. cvReleaseImage(&src);
  91. cvReleaseImage(&gsrc);
  92. }
  93. return 0;
  94. }