本文主要是介绍OpenCV联通组件扫描,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
注:黑色背景
void ccl_demo()
{QString appPath = QCoreApplication::applicationDirPath();QString imagePath = appPath + "/rice.png";Mat img = cv::imread(imagePath.toStdString());if (img.empty()) {return;}namedWindow("input", WINDOW_AUTOSIZE);imshow("input", img);//高斯模糊:降噪GaussianBlur(img, img, Size(3, 3),0);//To gray imageMat gray, binary;cvtColor(img, gray, COLOR_BGR2GRAY);imshow("gray", gray);//OTSUdouble m_otsu = threshold(gray, binary, 0, 255, THRESH_BINARY | THRESH_OTSU);imshow("BINARY", binary);ccl_stats_demo(binary);}
void ccl_stats_demo(Mat &image)
{Mat labels = Mat::zeros(image.size(), CV_32S);Mat stats, centrolds;int num_labels = connectedComponentsWithStats(image,labels,stats,centrolds,8, CV_32S, CCL_DEFAULT);vector<Vec3b> colorTable(num_labels);RNG rng(12345);//background colorcolorTable[0] = Vec3b(0, 0, 0);for (int i = 1; i < num_labels; i++){colorTable[i] = Vec3b(rng.uniform(0, 256), rng.uniform(0, 256), rng.uniform(0, 256));}Mat result = Mat::zeros(image.size(), CV_8UC3);int w = result.cols;int h = result.rows;for (int row = 0; row < h; row++){for (int col = 0; col < w; col++){int label = labels.at<int>(row, col);result.at<Vec3b>(row, col) = colorTable[label];}}for (int i = 1; i < num_labels; i++){//centerint cx = centrolds.at<double>(i, 0);int cy = centrolds.at<double>(i, 1);//rectangleint x = stats.at<int>(i, CC_STAT_LEFT);int y = stats.at<int>(i, CC_STAT_TOP);int w = stats.at<int>(i, CC_STAT_WIDTH);int h = stats.at<int>(i, CC_STAT_HEIGHT);int area = stats.at<int>(i, CC_STAT_AREA);circle(result, Point(cx, cy), 3, Scalar(0, 0, 255), 2, 8, 0);Rect box(x, y, w, h);rectangle(result, box, Scalar(0, 255, 0), 2, 8, 0);putText(result, format("%d", area), Point(x,y), FONT_HERSHEY_PLAIN, 1.0, Scalar(0, 255, 255), 1);}putText(result, format("number:%d", num_labels-1), Point(10, 10), FONT_HERSHEY_PLAIN, 1.0, Scalar(0, 0, 255), 1);imshow("CCL demo", result);
}
这篇关于OpenCV联通组件扫描的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!