Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- void labeling()
- {
- Mat src;
- // Read image from file
- char fname[MAX_PATH];
- while (openFileDlg(fname))
- {
- src = imread(fname,CV_LOAD_IMAGE_GRAYSCALE);
- //Create a window
- namedWindow("My Window", 1);
- int width = src.cols;
- int height = src.rows;
- //generare paleta de culori
- 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(src.size(), CV_16SC1); //matricea de etichete
- Mat dst = Mat::zeros(src.size(), CV_8UC3); //matricea destinatie pt. afisare
- printf("%d %d", labels.rows, labels.cols);
- //Procesare (labeling) ….
- int label = 0;
- for (int i = 1; i < height - 1; i++)
- for (int j = 1; j < width - 1; j++)
- {
- if (src.at<uchar>(i, j) == 0 && labels.at<short>(i, j) == 0)
- {
- label++;
- short newLabel = label % 1000;
- labels.at<short>(i, j) = newLabel;
- queue<Point2i> Q;
- Q.push({ i, j });
- while (!Q.empty())
- {
- Point2i q = Q.front();
- Q.pop();
- for (int k = -1; k <= 1; k++)
- for (int m = -1; m <= 1;m++)
- {
- int x = q.x + k;
- int y = q.y + m;
- if(src.at<uchar>(x,y)== 0 && labels.at<short>(x, y) == 0)
- {
- labels.at<short>(x, y) = newLabel;
- Q.push({ x, y });
- }
- }
- }
- }
- }
- for (int i = 1; i < height - 1; i++)
- for (int j = 1; j < width - 1; j++)
- {
- Scalar color = colorLUT[labels.at<short>(i, j)]; // valabil pt. Met. 1 BFS
- dst.at<Vec3b>(i, j)[0] = (uchar)color[0];
- dst.at<Vec3b>(i, j)[1] = (uchar)color[1];
- dst.at<Vec3b>(i, j)[2] = (uchar)color[2];
- }
- //show the image
- imshow("My Window", src);
- imshow("Label photo", dst);
- // Wait until user press some key
- waitKey(0);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement