Advertisement
Guest User

Untitled

a guest
Oct 16th, 2019
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.15 KB | None | 0 0
  1. void lab3(){
  2. char fname[MAX_PATH];
  3. int hue_mean = 16;
  4. int hue_std = 5;
  5. while (openFileDlg(fname))
  6. {
  7. Mat rgb, hsv;
  8. rgb = imread(fname, CV_LOAD_IMAGE_COLOR);
  9. GaussianBlur(rgb, rgb, Size(5, 5), 0, 0);
  10. int r = rgb.rows;
  11. int c = rgb.cols;
  12. cvtColor(rgb, hsv, CV_BGR2HSV);
  13. Mat channels[3];
  14. split(hsv, channels);
  15. Mat dst(r, c, CV_8UC1);
  16. Mat H = channels[0].clone() * 255 / 180;
  17.  
  18. for (int i = 0; i < r; i++)
  19. {
  20. for (int j = 0; j < c; j++)
  21. {
  22.  
  23. if ((H.at<uchar>(i, j) > (hue_mean - 2.5 * hue_std)) && ((H.at<uchar>(i, j) < (hue_mean + 2.5 * hue_std))))
  24. {
  25.  
  26. dst.at<uchar>(i, j) = 255;
  27. }
  28. else
  29. dst.at<uchar>(i, j) = 0;
  30.  
  31. }
  32. }
  33.  
  34. // x=0 marginea stanga a imaginii - o numim y0
  35.  
  36. Mat element1 = getStructuringElement(MORPH_RECT, Size(3, 3));
  37. imshow("src", rgb);
  38.  
  39. erode(dst, dst, element1, Point(-1, -1), 2);
  40.  
  41. dilate(dst, dst, element1, Point(-1, -1), 4);
  42.  
  43. erode(dst, dst, element1, Point(-1, -1), 2);
  44. imshow("dst", dst);
  45.  
  46. //Labeling("contur", dst, false);
  47. //line(dst, p1, p2, Scalar(255,255,255), 8, 8);
  48. MyLabeling("contur cu linie", dst, false);
  49. imshow("linie", dst);
  50.  
  51. waitKey();
  52. }
  53. }
  54.  
  55. //lab4------------------------------------------------------
  56. /*
  57. void regionCallback(int event, int x, int y, int flags, void* userdata)
  58. {
  59. Mat* H = (Mat*)userdata;
  60. if (event == CV_EVENT_LBUTTONDOWN)
  61. {
  62. int height = (*H).rows;
  63. int width = (*H).cols;
  64. Mat labels = Mat::zeros((*H).size(), CV_16UC1);
  65. Mat dst = Mat::zeros((*H).size(), CV_16UC1);
  66. queue <Point> que;
  67.  
  68. int k = 1;
  69. int N = 1;
  70. double hue_avg = (*H).at<uchar>(y,x);
  71. que.push(Point(y, x));
  72. labels.at<uchar>(y, x) = k;
  73.  
  74. for (int i = x - 1; i <= x + 1; i++){
  75. for (int j = y - 1; j <= y + 1; j++){
  76. if (i >= 0 && i <= (*H).rows && j >= 0 && j <= (*H).cols){
  77. hue_avg += (*H).at<uchar>(i, j);
  78. }
  79. }
  80. }
  81. hue_avg = hue_avg / 9;
  82. int T = 10;
  83. while (!que.empty()){
  84. Point oldest = que.front();
  85. que.pop();
  86.  
  87. int xx = oldest.y;
  88. int yy = oldest.x;
  89. for (int i = xx - 1; i <= xx + 1; i++){
  90. for (int j = yy - 1; j <= yy + 1; j++){
  91. if (i >= 1 && i <= (*H).rows - 1 && j >= 1 && j <= (*H).cols - 1){
  92. if (labels.at<uchar>(yy + i, xx + j) == 0 && abs((*H).at<uchar>(yy + i, xx + j) - hue_avg) < T){
  93. que.push(Point(yy + i, xx + j));
  94. labels.at<uchar>(yy + i, xx + j) = k;
  95. hue_avg = (N*hue_avg + ((*H).at<uchar>(yy + i, xx + j)) / (N + 1));
  96. N++;
  97. }
  98. }
  99. }
  100. }
  101. }
  102.  
  103. for (int i = 0; i < labels.rows; i++){
  104. for (int j = 0; j < labels.cols; j++){
  105. if (labels.at<uchar>(i, j) == 1){
  106. dst.at<uchar>(i, j) = 255;
  107. }
  108. }
  109. }
  110.  
  111. imshow("dst", dst);
  112. waitKey();
  113.  
  114. //imshow(dst)
  115.  
  116. }
  117. }
  118.  
  119.  
  120.  
  121. void regionGrowing(){
  122. Mat src;
  123. // Read image from file
  124. char fname[MAX_PATH];
  125. while (openFileDlg(fname))
  126. {
  127. src = imread(fname);
  128. GaussianBlur(src, src, Size(5, 5), 0, 0);
  129.  
  130. Mat hsvImg = src.clone();
  131. cvtColor(src, hsvImg, CV_BGR2Luv);
  132.  
  133. Mat channels[3];
  134. split(hsvImg, channels);
  135.  
  136. int height = src.rows;
  137. int width = src.cols;
  138.  
  139. for (int i = 0; i < height; i++){
  140. for (int j = 0; j < width; j++){
  141. channels[0].at<uchar>(i, j) = channels[0].at<uchar>(i, j) * 255 / 180;
  142. }
  143. }
  144.  
  145. int hH[256] = { 0 };
  146.  
  147. for (int i = 0; i < height; i++){
  148. for (int j = 0; j < width; j++){
  149. hH[channels[0].at<uchar>(i, j)]++;
  150. }
  151. }
  152.  
  153. Mat src2 = channels[1];
  154.  
  155. namedWindow("src2", 1);
  156. setMouseCallback("regionGrowing", CallBackL4, &src2);
  157. imshow("src2", src2);
  158. waitKey(0);
  159. }
  160. }
  161. */
  162. void CallBackL4(int event, int x, int y, int flags, void *userdata)
  163. {
  164. Mat* H = (Mat*)userdata;
  165. if (event == EVENT_RBUTTONDOWN)
  166. {
  167. imshow("H", *H);
  168. int width = (*H).cols;
  169. int height = (*H).rows;
  170. Mat labels = Mat::zeros((*H).size(), CV_8U);
  171. queue <Point> que;
  172. double hue_avg = (*H).at<uchar>(y, x);
  173. int T = 15;
  174. int k = 1;
  175. int N = 1;
  176. que.push(Point(x,y));
  177. labels.at<uchar>(y, x) = k;
  178. while (!que.empty())
  179. {
  180. Point oldest = que.front();
  181. que.pop();
  182. int xx = oldest.x;
  183. int yy = oldest.y;
  184.  
  185.  
  186. for (int i = yy - 1; i <= yy + 1; i++)
  187. for (int j = xx - 1; j <= xx + 1; j++)
  188. {
  189. if (i >= 1 && i <= (*H).rows - 1 && j >= 1 && j <= (*H).cols - 1)
  190. {
  191. if (labels.at<uchar>(i, j) == 0 && abs((*H).at<uchar>(i, j) - hue_avg) < T)
  192. {
  193. que.push(Point(j, i));
  194. labels.at<uchar>(i, j) = k;
  195. hue_avg = (N*hue_avg + (*H).at<uchar>(i, j)) / (N + 1);
  196. N++;
  197. }
  198.  
  199. }
  200. }
  201. }
  202.  
  203. Mat dst = labels.clone();
  204.  
  205. for (int i = 0; i < height; i++)
  206. for (int j = 0; j < width; j++)
  207. {
  208. if (labels.at<uchar>(i, j) != 0) {
  209. dst.at<uchar>(i, j) = 255;
  210. }
  211. }
  212. imshow("dst", dst);
  213.  
  214. }
  215. }
  216. void regionGrowing()
  217. {
  218. Mat src;
  219. Mat hsv, channels[3], H;
  220. char fname[MAX_PATH];
  221. while (openFileDlg(fname))
  222. {
  223. src = imread(fname);
  224. GaussianBlur(src, src, Size(5, 5), 0, 0);
  225.  
  226. cvtColor(src, hsv, CV_BGR2HSV);
  227. split(hsv, channels);
  228. H = channels[0] * 510 / 360;
  229.  
  230. namedWindow("src", 1);
  231. setMouseCallback("src", CallBackL4, &H);
  232. imshow("src", src);
  233. waitKey(0);
  234.  
  235. }
  236.  
  237. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement