PageRenderTime 203ms CodeModel.GetById 29ms RepoModel.GetById 3ms app.codeStats 0ms

/opencv_demo_thumb/opencv_demo_thumb.cpp

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