PageRenderTime 208ms CodeModel.GetById 32ms RepoModel.GetById 0ms app.codeStats 0ms

/examples/03_OpenCV_and_blobs/Blobs_examples/OpenCVThresholdAndExtractBlobs/OpenCVThresholdAndExtractBlobs.cpp

http://icode-mda.googlecode.com/
C++ | 198 lines | 103 code | 56 blank | 39 comment | 2 complexity | 17f37f7caa922fe46cf2dfbf59a9275a MD5 | raw file
  1. // OpenCVThresholdAndExtractBlobs.cpp : Main function demonstrating blobs library usage.
  2. //
  3. // OpenCV functions
  4. #include <opencv/cv.h>
  5. #include <opencv/highgui.h>
  6. // Include Blob Functions
  7. #include "blob.h"
  8. #include "BlobResult.h"
  9. // For writing to cout
  10. #include <iostream> // For writing to streams (for example the cout stream, which we will discuss)
  11. #include <fstream> // Also for writing to streams
  12. #include <vector> // vector
  13. #include <math.h> // math
  14. using namespace std;
  15. int main(int argc, char* argv[])
  16. {
  17. if(argc!=3){
  18. cout << "Usage:\nOpenCVThresholdAndExtractBlobs.exe <input_file> <threshold>" << endl;
  19. return 0;
  20. }
  21. // Declare 4 IplImages
  22. IplImage* sourceImg;
  23. IplImage* colorThresh;
  24. IplImage* gray;
  25. IplImage* grayThresh;
  26. // Declare and set thresholds
  27. int threshold = 40;
  28. int maxValue = 255;
  29. int thresholdType = CV_THRESH_BINARY_INV;
  30. // Load source image
  31. sourceImg = cvLoadImage(argv[1], 1);
  32. // Load threshold from user
  33. threshold = atoi(argv[2]);
  34. // Clone RGB sourceImg
  35. colorThresh = cvCloneImage( sourceImg );
  36. // Initialize gray image, same size as sourceImg
  37. gray = cvCreateImage( cvSize(sourceImg->width, sourceImg->height), IPL_DEPTH_8U, 1 );
  38. // Convert RGB image to gray scale
  39. cvCvtColor( sourceImg, gray, CV_BGR2GRAY );
  40. // Clone gray scale image
  41. grayThresh = cvCloneImage( gray );
  42. // Show RGB and gray scale images
  43. cvNamedWindow( "source", 0 );
  44. cvShowImage( "source", sourceImg);
  45. cvNamedWindow( "gray", 0 );
  46. cvShowImage( "gray", gray );
  47. // Threshold RGB image
  48. cvThreshold( sourceImg, colorThresh, threshold, maxValue, thresholdType );
  49. // Threshold gray scale image
  50. cvThreshold( gray, grayThresh, threshold, maxValue, thresholdType );
  51. IplConvKernel* SE;
  52. SE = cvCreateStructuringElementEx(5, 7, 0, 0, CV_SHAPE_RECT);
  53. cvNamedWindow( "grayThreshbefore", 0 );
  54. cvShowImage( "grayThreshbefore", grayThresh );
  55. // Perform Morphological Ops (This is a "closing" operation)
  56. cvDilate(grayThresh,grayThresh, SE);
  57. cvErode(grayThresh,grayThresh, SE);
  58. cvNamedWindow( "colorThresh", 0 );
  59. cvShowImage( "colorThresh", colorThresh );
  60. cvNamedWindow( "grayThresh", 0 );
  61. cvShowImage( "grayThresh", grayThresh );
  62. cvWaitKey(0);
  63. cvDestroyWindow( "colorThresh" );
  64. cvDestroyWindow( "grayThresh" );
  65. // Now let's use the blobs library to analyze our binary image
  66. // Image containing binary blob image.
  67. IplImage *inputBlob = cvCreateImage(cvSize(grayThresh->width+2,grayThresh->height+2),IPL_DEPTH_8U,1);
  68. IplImage *blobMask = cvCreateImage(cvSize(grayThresh->width, grayThresh->height), IPL_DEPTH_8U, 1);
  69. //cvCopyMakeBorder(dummy, inputBlob, cvPoint(1,1), IPL_BORDER_CONSTANT);
  70. cvCopyMakeBorder(grayThresh, inputBlob, cvPoint(1,1), IPL_BORDER_CONSTANT);
  71. // Declare blob results
  72. CBlobResult blobs;
  73. // delare a single blob
  74. CBlob Blob;
  75. // Corners of blob and middle of blob
  76. int iMaxx, iMinx, iMaxy, iMiny, iMeanx, iMeany;
  77. // get the blobs from the image, with no mask, using a threshold of 100
  78. blobs = CBlobResult(inputBlob, NULL, 100, true );
  79. //filter the background and helper external frame
  80. //see note 3 http://opencv.willowgarage.com/wiki/cvBlobsLib
  81. blobs.Filter(blobs, B_INCLUDE, CBlobGetArea(), B_GREATER, 100);
  82. blobs.Filter(blobs, B_INCLUDE, CBlobGetMean(), B_GREATER, 1);
  83. cout << "Number of blobs " << blobs.GetNumBlobs() << "\n";
  84. // Loop through each blob found
  85. for (int blobIndex=0; blobIndex<blobs.GetNumBlobs(); ++blobIndex)
  86. {
  87. // get the blob info
  88. Blob = blobs.GetBlob(blobIndex);
  89. cvSetZero(blobMask);
  90. CvScalar color = CV_RGB(1,1,1);
  91. Blob.FillBlob(blobMask,color); //set all pixels in blob mask =1
  92. CvScalar numPixels = cvSum(blobMask); //number of pixels in blob
  93. // multiply input by blob mask
  94. cvMul(gray, blobMask, blobMask, 1); //now blob mask should be gray scale image
  95. CvScalar sumIntensity = cvSum(blobMask);
  96. // Mean value of pixels of blob
  97. double blobMean = sumIntensity.val[0] / numPixels.val[0];
  98. //grab the ellipse of it to get the appx size
  99. CvBox2D ellipse = Blob.GetEllipse();
  100. CvSize2D32f dim = ellipse.size;
  101. // major and minor axis of blob (length and width)
  102. double W = dim.width;
  103. double L = dim.height;
  104. // get max, and min co-ordinates
  105. iMaxx=(int)Blob.MaxX() - 1;
  106. iMinx=(int)Blob.MinX() - 1;
  107. iMaxy=(int)Blob.MaxY() - 1;
  108. iMiny=(int)Blob.MinY() - 1;
  109. // Area of Blob
  110. double BlobArea = Blob.Area();
  111. cout << "area of blob : " << BlobArea << endl;
  112. // size of full image
  113. int height = gray->height;
  114. int width = gray->width;
  115. // size of blob bounding rect
  116. int BlobxSize = iMaxx - iMinx;
  117. int BlobySize = iMaxy - iMiny;
  118. cout << "Blobsize : " << BlobxSize << ", " << BlobySize << endl;
  119. IplImage* blobImg = cvCreateImage(cvSize(BlobxSize,BlobySize),IPL_DEPTH_8U, 1);
  120. cvSetImageROI(gray,cvRect(iMinx,iMiny,BlobxSize,BlobySize));
  121. cvCopy(gray, blobImg);
  122. cvResetImageROI(gray);
  123. // Show blobmask
  124. cvNamedWindow("Window", 0); // Create a new window
  125. cvShowImage("Window", blobImg); // Display the image in the window
  126. cvWaitKey(0); // Wait for key to close the window
  127. cvDestroyWindow( "Window" ); // Destroy the window
  128. cvReleaseImage(&blobImg); //Free the memory
  129. // find the average of the blob (i.e. estimate its centre)
  130. iMeanx=(iMinx+iMaxx)/2;
  131. iMeany=(iMiny+iMaxy)/2;
  132. cout << "Blob UL corner : " << iMinx << " , " << iMiny << endl;
  133. } // End of blob loop
  134. //free all memory that was allocated
  135. cvReleaseImage(&sourceImg);
  136. cvReleaseImage(&colorThresh);
  137. cvReleaseImage(&gray);
  138. cvReleaseImage(&grayThresh);
  139. cvReleaseImage(&inputBlob);
  140. cvReleaseImage(&blobMask);
  141. return 0;
  142. }