Untitled

mail@pastecode.io avatar
unknown
c_cpp
2 years ago
1.3 kB
3
Indexable
Never
void etichetareBFS() {
	char fname[MAX_PATH];
	while (openFileDlg(fname)) {
		Mat src = imread(fname, IMREAD_UNCHANGED);
		Mat dst(src.rows, src.cols, CV_8UC3, Scalar(255, 255, 255));

		int label = 0;
		Mat labels(src.rows, src.cols, CV_32SC1, Scalar(0));


		int di[8] = { -1,-1,-1, 0, 0, 1, 1, 1 };
		int	dj[8] = { -1, 0, 1,-1, 1,-1, 0, 1 };

		for (int i = 0; i < src.rows; i++) {
			for (int j = 0; j < src.cols; j++) {
				uchar pixel = src.at<uchar>(i, j);
				if (pixel == 0 && labels.at<int>(i,j) == 0) {
					label++;
					uchar b = d(gen);
					uchar g = d(gen);
					uchar r = d(gen);
					dst.at<Vec3b>(i, j) = Vec3b(b, g, r);
					std::queue<Point2i> Q;
					labels.at<int>(i, j) = label;
					Q.push({ i,j });
					while (!Q.empty()) {
						Point2i p = Q.front();
						Q.pop();
						for (int k = 0; k < 8; k++) {
							if (isInside(src, p.x + di[k], p.y + dj[k])) {
								if (src.at<uchar>(p.x + di[k], p.y + dj[k]) == 0 && labels.at<int>(p.x + di[k], p.y + dj[k]) == 0) {
									labels.at<int>(p.x + di[k], p.y + dj[k]) = label;
									dst.at<Vec3b>(p.x + di[k], p.y + dj[k]) = Vec3b(b, g, r);
									Q.push({ p.x + di[k], p.y + dj[k] });
								}
							}
						}
					}
				}
			}
		}
		imshow("Initial", src);
		imshow("Etichetat", dst);
		waitKey(0);
	}
}