// 目标颜色 for (int i = 1; i < num_labels; ++i) { colors[i] = Vec3b(rng.uniform(0, 256), rng.uniform(0, 256), rng.uniform(0, 256)); }
// 抽取统计信息 Mat dst = image.clone(); for (int i = 1; i < num_labels; ++i) { // 中心位置 int cx = centroids.at<double>(i, 0); int cy = centroids.at<double>(i, 1);
// 统计信息 int 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);
num_labels, labels, stats, centers = cv.connectedComponentsWithStats(binary, connectivity=8, ltype=cv.CV_32S) colors = [] for i in range(num_labels): b = np.random.randint(0, 256) g = np.random.randint(0, 256) r = np.random.randint(0, 256) colors.append((b, g, r))
colors[0] = (0, 0, 0) image = np.copy(src) for t in range(1, num_labels, 1): x, y, w, h, area = stats[t] cx, cy = centers[t] cv.circle(image, (np.int32(cx), np.int32(cy)), 2, (0, 255, 0), 2, 8, 0) cv.rectangle(image, (x, y), (x+w, y+h), colors[t], 1, 8, 0) cv.putText(image, "num:" + str(t), (x, y), cv.FONT_HERSHEY_SIMPLEX, .5, (0, 0, 255), 1); print("label index %d, area of the label : %d"%(t, area))