Advertisement
Guest User

Untitled

a guest
Oct 15th, 2019
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.80 KB | None | 0 0
  1. // Laborator 3 - lucrarea 4
  2.  
  3. Point Pstart, Pend;
  4.  
  5. void callBack(int event, int x, int y, int flags, void *userdata)
  6. {
  7. Mat* H = (Mat*)userdata;
  8. if (event == EVENT_RBUTTONDOWN)
  9. {
  10. int height = (*H).rows;
  11. int width = (*H).cols;
  12. Mat labels = Mat::zeros((*H).size(), CV_8UC1);
  13. Mat dst = Mat::zeros((*H).size(), CV_8UC1);
  14.  
  15. std::queue<Point> que;
  16. int w = 1;
  17. float hue_avg = (*H).at<uchar>(y, x);
  18. int hue_std = 5;
  19. float k1 = 2.8;
  20. // float T = hue_std * k1;
  21. float T = 15;
  22. /*
  23. for (int i = x - w; i <= x + w; i++)
  24. {
  25. for (int j = y - w; j <= y + w; j++)
  26. {
  27. hue_avg += (*H).at<uchar>(i, j);
  28. printf("hue_avg = %d", hue_avg);
  29. }
  30. }
  31. */
  32.  
  33. printf("hue avg = %f", hue_avg);
  34.  
  35. int k = 1; //ethiceta curenta
  36. int N = 1; // numarul de pixeli din regiune
  37.  
  38. que.push(Point(x, y)); // adauga element (seed point) in coada
  39. // acesta primeste eticheta k
  40. while (!que.empty())
  41. { // Retine poz. celui mai vechi element din coada
  42. Point oldest = que.front();
  43. que.pop(); // scoate element din coada
  44. int xx = oldest.x; // coordonatele lui
  45. int yy = oldest.y;
  46.  
  47. for (int i = yy - w; i <= yy + w; i++)
  48. {
  49. for (int j = xx - w; j <= xx + w; j++)
  50. {
  51. if (inside(i, j, height, width))
  52. {
  53. if ((labels.at<uchar>(i, j) == 0) && (abs((*H).at<uchar>(i, j) - hue_avg) < T))
  54. {
  55. que.push(Point(j, i));
  56. labels.at<uchar>(i, j) = k;
  57. hue_avg = (N * hue_avg + (*H).at<uchar>(i, j)) / (N + 1);
  58. N++;
  59. }
  60. }
  61. }
  62. }
  63. }
  64. for (int i = 0; i < height; i++)
  65. {
  66. for (int j = 0; j < width; j++)
  67. {
  68. if (labels.at<uchar>(i, j) == 1)
  69. {
  70. dst.at<uchar>(i, j) = 255;
  71. }
  72. else
  73. {
  74. dst.at<uchar>(i, j) = 0;
  75. }
  76. }
  77. }
  78. imshow("Imaginea destinatie inainte de postprocesare", dst);
  79.  
  80. // creare element structural de dimensiune 3x3 de tip patrat (V8)
  81. Mat element = getStructuringElement(MORPH_RECT, Size(3, 3));
  82.  
  83. // dilatare cu acest element structural (aplicata n)
  84. dilate(dst, dst, element, Point(-1, -1), 3);
  85.  
  86. // dilatare cu acest element structural (aplicata n)
  87. erode(dst, dst, element, Point(-1, -1), 3);
  88.  
  89. imshow("Imaginea destinatie", dst);
  90. }
  91. }
  92.  
  93. void regionGrowing()
  94. {
  95. char fname[MAX_PATH];
  96. while (openFileDlg(fname))
  97. {
  98. Mat src = imread(fname);
  99. //imshow("orignial", src);
  100. Mat hue = Mat(src.rows, src.cols, CV_8UC1);
  101. Mat labels = Mat::zeros(hue.size(), CV_8UC1);
  102. vector<Mat> hsv;
  103.  
  104. GaussianBlur(src, src, Size(5, 5), 0, 0);
  105.  
  106. hsv = BGR2HSV(src);
  107. hue = hsv[0];
  108.  
  109. //Create a window
  110. namedWindow("My Window", 1);
  111.  
  112. //set the callback function for any mouse event
  113. setMouseCallback("My Window", callBack, &hue);
  114.  
  115. //show the image
  116. imshow("My Window", src);
  117.  
  118. // Wait until user press some key
  119. waitKey(0);
  120. }
  121. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement