Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- void labelling(Mat img)
- {
- int label = 0;
- Scalar colorLUT[1000] = { 0 };
- Scalar color;
- for (int i = 1; i < 1000; i++)
- {
- Scalar color(rand() & 255, rand() & 255, rand() & 255);
- colorLUT[i] = color;
- }
- colorLUT[0] = Scalar(255, 255, 255); // fundalul va fi alb
- Mat labels = Mat::zeros(img.size(), CV_16SC1);
- Mat dst = Mat::zeros(img.size(), CV_8UC3);
- for (int i = 0; i < img.rows; i++)
- for (int j = 0; j < img.cols; j++)
- {
- if (img.at<uchar>(i, j) == 0 && labels.at<short>(i,j) == 0)
- {
- label++;
- std::queue <Point> que;
- labels.at<short>(i,j) = label;
- que.push({ i,j });
- while(!que.empty())
- {
- Point oldest = que.front();
- uchar neighbors[8];
- int di[8] = { -1,-1,-1,0,0,1,1 ,1 };
- int dj[8] = { -1,0,1,-1, 1,-1,0,1};
- int x = oldest.x;
- int y = oldest.y;
- for (int k = 0; k < 8; k++)
- {
- int vecin_i = x + di[k];
- int vecin_j = y + dj[k];
- if(vecin_i>=0 && vecin_j>=0 && vecin_i<img.rows && vecin_j <img.cols)
- if (img.at<uchar>(vecin_i,vecin_j) == 0 && labels.at<short>(vecin_i,vecin_j) == 0)
- {
- labels.at<short>(vecin_i,vecin_j) = label;
- que.push(Point(vecin_i,vecin_j));
- }
- }
- que.pop();
- }
- }
- }
- for (int i = 1; i < labels.rows - 1; i++)
- for (int j = 1; j < labels.cols - 1; j++)
- {
- Scalar color = colorLUT[labels.at<short>(i, j)]; // valabil pt. Met. 1 BFS
- dst.at<Vec3b>(i, j)[0] = color[0];
- dst.at<Vec3b>(i, j)[1] = color[1];
- dst.at<Vec3b>(i, j)[2] = color[2];
- }
- imshow("labelled", dst);
- waitKey();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement