You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

107 lines
2.4 KiB

7 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. // main.cpp : Defines the entry point for the console application.
  2. //
  3. #if _MSC_VER > 1000
  4. #include "stdafx.h"
  5. #include <conio.h>
  6. #endif // _MSC_VER > 1000
  7. #include <opencv/cv.h>
  8. #include <opencv/highgui.h>
  9. #include <stdio.h>
  10. // Main blob library include
  11. #include "BlobResult.h"
  12. char wndname[] = "Blob Extraction";
  13. char tbarname1[] = "Threshold";
  14. char tbarname2[] = "Blob Size";
  15. // The output and temporary images
  16. IplImage* originalThr = 0;
  17. IplImage* original = 0;
  18. IplImage* displayedImage = 0;
  19. int param1, param2;
  20. // threshold trackbar callback
  21. void on_trackbar( int dummy )
  22. {
  23. if(!originalThr)
  24. {
  25. originalThr = cvCreateImage(cvGetSize(original), IPL_DEPTH_8U,1);
  26. }
  27. if(!displayedImage)
  28. {
  29. displayedImage = cvCreateImage(cvGetSize(original), IPL_DEPTH_8U,3);
  30. }
  31. // threshold input image
  32. cvThreshold( original, originalThr, param1, 255, CV_THRESH_BINARY );
  33. // get blobs and filter them using its area
  34. CBlobResult blobs;
  35. int i;
  36. CBlob currentBlob;
  37. // find blobs in image
  38. blobs = CBlobResult( originalThr, NULL, 255 );
  39. blobs.Filter( blobs, B_EXCLUDE, CBlobGetArea(), B_LESS, param2 );
  40. // display filtered blobs
  41. cvMerge( originalThr, originalThr, originalThr, NULL, displayedImage );
  42. for (i = 0; i < blobs.GetNumBlobs(); i++ )
  43. {
  44. currentBlob = blobs.GetBlob(i);
  45. (&currentBlob)->FillBlob( displayedImage, CV_RGB(255,0,0));
  46. }
  47. cvShowImage( wndname, displayedImage );
  48. }
  49. int main( int argc, char** argv )
  50. {
  51. char filename[1024] = "pic6.png\0";
  52. if(argc == 2)
  53. {
  54. strcpy(filename, argv[1]);
  55. }
  56. param1 = 100;
  57. param2 = 2000;
  58. // open input image
  59. original = cvLoadImage(filename, 0);
  60. cvNamedWindow("input");
  61. cvShowImage("input", original );
  62. cvNamedWindow(wndname, 0);
  63. cvCreateTrackbar( tbarname1, wndname, &param1, 255, on_trackbar );
  64. cvCreateTrackbar( tbarname2, wndname, &param2, 30000, on_trackbar );
  65. // Call to update the view
  66. for(;;)
  67. {
  68. int c;
  69. // Call to update the view
  70. on_trackbar(0);
  71. c = cvWaitKey(0);
  72. if( c == 27 )
  73. break;
  74. }
  75. cvReleaseImage( &original );
  76. cvReleaseImage( &originalThr );
  77. cvReleaseImage( &displayedImage );
  78. cvDestroyWindow( wndname );
  79. return 0;
  80. }