Advertisement
Guest User

Untitled

a guest
Dec 10th, 2019
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.44 KB | None | 0 0
  1. void labeling()
  2. {
  3. Mat src;
  4. // Read image from file
  5. char fname[MAX_PATH];
  6. while (openFileDlg(fname))
  7. {
  8. src = imread(fname,CV_LOAD_IMAGE_GRAYSCALE);
  9. //Create a window
  10. namedWindow("My Window", 1);
  11.  
  12. int width = src.cols;
  13. int height = src.rows;
  14.  
  15. //generare paleta de culori
  16. Scalar colorLUT[1000] = { 0 };
  17. Scalar color;
  18. for (int i = 1; i < 1000; i++)
  19. {
  20. Scalar color(rand() & 255, rand() & 255, rand() & 255);
  21. colorLUT[i] = color;
  22.  
  23. }
  24. colorLUT[0] = Scalar(255,255,255); // fundalul va fi alb
  25.  
  26. Mat labels = Mat::zeros(src.size(), CV_16SC1); //matricea de etichete
  27. Mat dst = Mat::zeros(src.size(), CV_8UC3); //matricea destinatie pt. afisare
  28.  
  29. printf("%d %d", labels.rows, labels.cols);
  30.  
  31. //Procesare (labeling) ….
  32. int label = 0;
  33. for (int i = 1; i < height - 1; i++)
  34. for (int j = 1; j < width - 1; j++)
  35. {
  36. if (src.at<uchar>(i, j) == 0 && labels.at<short>(i, j) == 0)
  37. {
  38. label++;
  39. short newLabel = label % 1000;
  40. labels.at<short>(i, j) = newLabel;
  41. queue<Point2i> Q;
  42. Q.push({ i, j });
  43. while (!Q.empty())
  44. {
  45. Point2i q = Q.front();
  46.  
  47. Q.pop();
  48. for (int k = -1; k <= 1; k++)
  49. for (int m = -1; m <= 1;m++)
  50. {
  51. int x = q.x + k;
  52. int y = q.y + m;
  53. if(src.at<uchar>(x,y)== 0 && labels.at<short>(x, y) == 0)
  54. {
  55. labels.at<short>(x, y) = newLabel;
  56. Q.push({ x, y });
  57. }
  58. }
  59.  
  60. }
  61. }
  62. }
  63.  
  64.  
  65.  
  66. for (int i = 1; i < height - 1; i++)
  67. for (int j = 1; j < width - 1; j++)
  68. {
  69. Scalar color = colorLUT[labels.at<short>(i, j)]; // valabil pt. Met. 1 BFS
  70. dst.at<Vec3b>(i, j)[0] = (uchar)color[0];
  71. dst.at<Vec3b>(i, j)[1] = (uchar)color[1];
  72. dst.at<Vec3b>(i, j)[2] = (uchar)color[2];
  73. }
  74.  
  75.  
  76. //show the image
  77. imshow("My Window", src);
  78. imshow("Label photo", dst);
  79.  
  80. // Wait until user press some key
  81. waitKey(0);
  82. }
  83.  
  84.  
  85. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement