Advertisement
Guest User

Untitled

a guest
Oct 15th, 2019
141
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.23 KB | None | 0 0
  1. void CallBackL4(int event, int x, int y, int flags, void *userdata)
  2. {
  3. Mat* H = (Mat*)userdata;
  4. if (event == EVENT_RBUTTONDOWN)
  5. {
  6. imshow("H", *H);
  7. int width = (*H).cols;
  8. int height = (*H).rows;
  9. Mat labels = Mat::zeros((*H).size(),CV_16UC1);
  10. Mat dst = Mat::zeros((*H).size(), CV_8UC1);
  11. queue<Point> que;
  12. double hue_avg = (*H).at<uchar>(y, x);
  13. printf("salut");
  14. int k = 1; //ethiceta curenta
  15. int N = 1; // numarul de pixeli din regiune
  16. que.push(Point(y, x)); // adauga element (seed point) in coada
  17. // acesta primeste eticheta k
  18. while (!que.empty())
  19. { // Retine poz. celui mai vechi element din coada
  20. Point oldest = que.front();
  21. que.pop(); // scoate element din coada
  22. int xx = oldest.y; // coordonatele lui
  23. int yy = oldest.x;
  24. // Pentru fiecare vecin al pixelului (xx, yy)
  25. // Daca abs(hue(vecin) – Hue_avg)<T si labels(vecin) == 0
  26. // Aduga vecin la regiunea curenta
  27. // labels(vecin) = k
  28. // Actualizeaza Hue_avg (medie ponderata)
  29. // Incrementeza N
  30.  
  31. for (int v = -1; v <= 1; v++)
  32. for (int u = -1; u <= 1; u++)
  33. {
  34. if (yy <= height - 1 && xx <= width - 1){
  35. if (abs((*H).at<uchar>(yy + v, xx +
  36.  
  37. u) - hue_avg) < 55 && labels.at<uchar>(yy + v, xx + u) == 0){
  38. que.push(Point(yy + v, xx +
  39.  
  40. u));
  41. labels.at<uchar>(yy + v, xx
  42.  
  43. + u) = k;
  44. hue_avg = (N * hue_avg +
  45.  
  46. (*H).at<uchar>(yy + v, xx + u)) / (N + 1);
  47. N++;
  48. }
  49. }
  50. }
  51. }
  52.  
  53. for (int i = 0; i < height;i++)
  54. for (int j = 0; j < width; j++)
  55. {
  56. if (labels.at<uchar>(i,j) != 0) {
  57. dst.at<uchar>(i, j) = 255;
  58. }
  59. }
  60. imshow("dst", dst);
  61.  
  62. }
  63. }
  64.  
  65. void L4_RG()
  66. {
  67. Mat src;
  68. Mat channels[3],hsv,H;
  69.  
  70. // Read image from file
  71. char fname[MAX_PATH];
  72. while (openFileDlg(fname))
  73. {
  74. src = imread(fname);
  75.  
  76. GaussianBlur(src, src, Size(5, 5), 0, 0);//filtru Gausian
  77. cvtColor(src, hsv, CV_BGR2HSV);//conversie bgr-> hsv
  78. split(hsv, channels);//separare hsv
  79. H = channels[0]*510/360;
  80.  
  81. //Create a window
  82. namedWindow("src", 1);
  83.  
  84. //set the callback function for any mouse event
  85. setMouseCallback("src", CallBackL4, &H);
  86.  
  87. //show the image
  88. imshow("src", src);
  89.  
  90. // Wait until user press some key
  91. waitKey(0);
  92. }
  93. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement