Advertisement
Guest User

Lab5 - PI

a guest
Mar 22nd, 2018
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.77 KB | None | 0 0
  1. void labelObjects() {
  2.     char fileName[MAX_PATH];
  3.     Mat src;
  4.     while (openFileDlg(fileName)) {
  5.         src = imread(fileName, CV_LOAD_IMAGE_GRAYSCALE);
  6.  
  7.         Mat labels = Mat::zeros(src.rows, src.cols, CV_8UC1);
  8.  
  9.         uchar label = 0;
  10.         for (int i = 1; i < src.rows - 1; i++) {
  11.             for (int j = 1; j < src.cols - 1; j++) {
  12.                 if (src.at<uchar>(i, j) == 0 && labels.at<uchar>(i, j) == 0) {
  13.                     label++;
  14.                     std::queue<Point2i> Q;
  15.                     labels.at<uchar>(i, j) = label;
  16.                     Q.push(Point2i(j, i));
  17.                     while (!Q.empty()) {
  18.                         Point2i q = Q.front();
  19.                         Q.pop();
  20.                         int dj[8] = { -1, 0, 1, 1, 1, 0, -1, -1 };
  21.                         int di[8] = { -1, -1, -1, 0, 1, 1, 1, 0 };
  22.                    
  23.                         for (int k = 0; k < 8; k++) {  
  24.                             int ii = q.y + di[k];
  25.                             int jj = q.x + dj[k];
  26.                             if (jj >= 0 && jj < src.cols) {
  27.                                 if (ii >= 0 && ii < src.rows) {
  28.                                     // traversarea in latime pentru componente conexe
  29.                                     if (src.at<uchar>(ii, jj) == 0 && labels.at<uchar>(ii, jj) == 0) {
  30.                                         labels.at<uchar>(ii, jj) = label;
  31.                                         Q.push(Point2i(jj, ii));
  32.                                     }
  33.                                 }
  34.                             }
  35.                         } // for for neighbours
  36.                     } // for while
  37.                 } // if
  38.             } // for j
  39.         } // for i
  40.  
  41.         Mat dst = Mat::zeros(labels.rows, labels.cols, CV_8UC3);
  42.         Vec3b color;
  43.         std::default_random_engine gen;
  44.         std::uniform_int_distribution<int> d(0, 255);
  45.         // color the figures
  46.         for (int k = 0; k <= label; k++) {
  47.             color = Vec3b(d(gen), d(gen), d(gen));
  48.             for (int i = 0; i < labels.rows; i++) {
  49.                 for (int j = 0; j < labels.cols; j++) {
  50.                     if (labels.at<uchar>(i, j) == k) {
  51.                         if (k == 0) {
  52.                             dst.at<Vec3b>(i, j) == Vec3b(255, 255, 255);
  53.                         }
  54.                         else {
  55.                             dst.at<Vec3b>(i, j) = color;
  56.                         }
  57.  
  58.                     }
  59.                 }
  60.             }
  61.         }
  62.         imshow("labels", dst);
  63.         waitKey();
  64.     }
  65.  
  66. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement