Using the geometry of confocal curves for passing through a door, implementation.

blob.cpp 1.8KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. #include <iostream>
  2. #include <opencv2/core/core.hpp>
  3. #include <opencv2/highgui/highgui.hpp>
  4. #include <cvblob.h>
  5. using namespace cv;
  6. using namespace cvb;
  7. using namespace std;
  8. int main(int argc, char* argv[]) {
  9. if (argc < 3) {
  10. cout << "Usage: " << argv[0] << " IMAGE_FILE H" << endl;
  11. // H in [0, 360]
  12. return EXIT_FAILURE;
  13. }
  14. int H = atoi(argv[2]);
  15. CvBlobs blobs;
  16. Mat img;
  17. img = imread(argv[1], CV_LOAD_IMAGE_COLOR);
  18. if (!img.data) {
  19. return EXIT_FAILURE;
  20. }
  21. int cols = img.cols;
  22. int rows = img.rows;
  23. // Changing the color space to HSV
  24. cvtColor(img, img, CV_BGR2HSV);
  25. // Threshold the frame for yellow
  26. inRange(img, Scalar(H / 2 - 10, 0, 0), Scalar(H / 2 + 10, 255, 255), img);
  27. // Filtering the frame
  28. medianBlur(img, img, 7);
  29. // Finding the blobs
  30. IplImage *img_label = cvCreateImage(cvSize(img.cols, img.rows), IPL_DEPTH_LABEL, 1);
  31. IplImage img_ipl = (IplImage) img;
  32. cvLabel(&img_ipl, img_label, blobs);
  33. // cout << "Before filter: " << blobs.size() << endl;
  34. // Filtering the blobs
  35. // cvFilterByArea(blobs, 60, 500);
  36. // cout << "After filter: " << blobs.size() << endl;
  37. cout << blobs.size() << " blobs found" << endl;
  38. for (CvBlobs::const_iterator it = blobs.begin(); it != blobs.end(); ++it) {
  39. double moment10 = it->second->m10;
  40. double moment01 = it->second->m01;
  41. double area = it->second->area;
  42. //Variable for holding position
  43. int x;
  44. int y;
  45. //Calculating the current position
  46. x = moment10 / area;
  47. y = moment01 / area;
  48. //Printing the position information
  49. cout << "X: " << x - cols / 2 << endl << "Y: " << y - rows / 2 << endl;
  50. }
  51. cvReleaseBlobs(blobs);
  52. img.release();
  53. return EXIT_SUCCESS;
  54. }