Advertisement
Guest User

Untitled

a guest
Oct 22nd, 2019
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.89 KB | None | 0 0
  1. void CallBackL4(int event, int x, int y, int flags, void* param)
  2. {
  3. Mat* H = (Mat*)param;
  4. if (event == CV_EVENT_LBUTTONDOWN)
  5. {
  6. Mat labels = Mat::zeros((*H).size(), CV_8U);
  7. queue <Point> que;
  8.  
  9. int k = 1;
  10. int N = 1;
  11. //float hue_avg = 1;
  12. que.push(Point(x, y));
  13. labels.at<uchar>(y, x) = k;
  14.  
  15. int width = (*H).cols;
  16. int height = (*H).rows;
  17. int T = 15;
  18. double hue_avg = (*H).at<uchar>(y, x);
  19. while (!que.empty()){
  20. Point oldest = que.front();
  21. que.pop();
  22.  
  23. int xx = oldest.x;
  24. int yy = oldest.y;
  25. for (int i = xx - 1; i <= xx + 1; i++){
  26. for (int j = yy - 1; j <= yy + 1; j++){
  27. if (i >= 1 && i <= (*H).rows - 1 && j >= 1 && j <= (*H).cols - 1){
  28. if (labels.at<uchar>(i, j) == 0 && abs((*H).at<uchar>(i, j) - hue_avg) < T){
  29. que.push(Point(i, j));
  30. labels.at<uchar>(i, j) = k;
  31. hue_avg = (N*hue_avg + (*H).at<uchar>(i, j)) / (N + 1);
  32. N++;
  33. }
  34. }
  35. }
  36. }
  37. }
  38. Mat dst = labels.clone();
  39. for (int i = 0; i < labels.rows; i++){
  40. for (int j = 0; j < labels.cols; j++){
  41. if (labels.at<uchar>(i, j) == 1){
  42. dst.at<uchar>(i, j) = 255;
  43. }
  44. }
  45. }
  46.  
  47. Mat el1 = getStructuringElement(MORPH_CROSS, Size(5, 5));
  48. erode(dst, dst, el1, Point(-1, -1), 2);
  49. dilate(dst, dst, el1, Point(-1, -1), 4);
  50. erode(dst, dst, el1, Point(-1, -1), 2);
  51. imshow("dst", dst);
  52. //printf("%d", hue_avg);
  53. }
  54. }
  55. void L4_R6(){
  56. Mat src;
  57. Mat hsv;
  58. // Read image from file
  59. char fname[MAX_PATH];
  60. while (openFileDlg(fname))
  61. {
  62. src = imread(fname);
  63.  
  64. GaussianBlur(src, src, Size(5, 5), 0, 0);
  65.  
  66. cvtColor(src, hsv, CV_BGR2HSV);
  67.  
  68. Mat channels[3];
  69.  
  70. split(hsv, channels);
  71.  
  72. Mat Hue;
  73. Hue = channels[0] * (255 / 180);
  74.  
  75. Mat dst(channels[0].rows, channels[0].cols, CV_8UC1);
  76.  
  77. namedWindow("src", 1);
  78.  
  79. setMouseCallback("src", CallBackL4, &Hue);
  80.  
  81. imshow("src", src);
  82.  
  83. waitKey();
  84. }
  85. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement