Advertisement
Guest User

Untitled

a guest
Dec 13th, 2019
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.10 KB | None | 0 0
  1. Mat rgb;
  2. // downsample and use it for processing
  3. pyrDown(large, rgb );
  4. pyrDown(rgb, rgb);
  5. pyrDown(rgb, rgb);
  6. //pyrDown()
  7.  
  8. Mat small;
  9. cvtColor(rgb, small, CV_BGR2GRAY);
  10. // morphological gradient
  11. Mat grad;
  12. Mat morphKernel = getStructuringElement(MORPH_ELLIPSE, Size(10, 10));
  13. morphologyEx(small, grad, MORPH_GRADIENT, morphKernel);
  14. // binarize
  15. Mat bw;
  16. threshold(grad, bw, 0.0, 255.0, THRESH_BINARY | THRESH_OTSU);
  17. // connect horizontally oriented regions
  18.  
  19. Mat connected;
  20. morphKernel = getStructuringElement(MORPH_RECT, Size(1, 5));
  21. morphologyEx(bw, connected, MORPH_CLOSE, morphKernel);
  22. // find contours
  23. Mat mask = Mat::zeros(bw.size(), CV_8UC1);
  24. vector<vector<Point>> contours;
  25. vector<Vec4i> hierarchy;
  26. findContours(connected, contours, hierarchy, CV_RETR_CCOMP, CV_CHAIN_APPROX_SIMPLE, Point(0, 0));
  27. // filter contours
  28.  
  29. int biggest = getMaxAreaContourId(contours);
  30.  
  31. std::vector<int> uniqueColors;
  32. std::vector<Mat> ROI_store;
  33.  
  34. std::map<cv::Mat, int> colorMap;
  35.  
  36. for (int idx = 0; idx >= 0; idx = hierarchy[idx][0])
  37. {
  38. Rect rect = boundingRect(contours[idx]);
  39. Mat maskROI(mask, rect);
  40. maskROI = Scalar(0, 0, 0);
  41. // fill the contour
  42.  
  43.  
  44. drawContours(mask, contours, idx, Scalar(255, 255, 255), CV_FILLED);
  45. // ratio of non-zero pixels in the filled region
  46. double r = (double)countNonZero(maskROI) / (rect.width*rect.height);
  47.  
  48. cout << "hello mary louZ" << endl;
  49. cout << "RGB width : " << rgb.cols<< endl;
  50. cout << "Rect width : " << rect.width << endl;
  51.  
  52. if (r > .35
  53. /*
  54. // may work on ios but not here
  55. && (rgb.cols /4 < rect.width < rgb.cols / 2)
  56. && (rgb.rows / 4 < rect.height < rgb.rows / 2)
  57. */
  58.  
  59. && (inRange(rgb.cols / 4, rgb.cols / 2, rect.width))
  60.  
  61. //|| inRange(rgb.rows / 4, rgb.rows / 2, rect.height))
  62. //&& (rgb.cols / 4 < rect.width) && (rect.width < rgb.cols / 2)
  63. //&& (rgb.rows / 4 < rect.height) && (rect.height < rgb.rows / 2)
  64.  
  65. //&& (800 > rect.width) && (800 > rect.height)
  66. //&& ( 10 <= rect.width - rect.height <= 10)
  67.  
  68. )
  69. {
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement