连通域标记后的图像计算各区域面积和坐标python代码
时间: 2024-05-12 16:19:12 浏览: 26
以下是使用Python和OpenCV库计算连通域标记后的图像中各区域的面积和坐标的示例代码:
```python
import cv2
# 读取图像
img = cv2.imread('labeled_image.png', cv2.IMREAD_GRAYSCALE)
# 连通域标记
num_labels, labels, stats, centroids = cv2.connectedComponentsWithStats(img)
# 输出各区域的面积和坐标
for i in range(1, num_labels):
area = stats[i, cv2.CC_STAT_AREA]
x, y, w, h, _ = stats[i]
print(f"Label {i}: area={area}, position=({x}, {y})")
```
其中,`cv2.connectedComponentsWithStats()`函数返回的四个参数分别是:
- `num_labels`:图像中连通域的数量(包括背景)。
- `labels`:一个与输入图像大小相同的数组,每个像素点的值表示所属的连通域标签。
- `stats`:一个大小为 `(num_labels, 5)` 的数组,每一行包含一个连通域的信息,包括左上角坐标、宽度、高度和面积。
- `centroids`:一个大小为 `(num_labels, 2)` 的数组,每一行包含一个连通域的质心坐标。
相关问题
连通域标记后的图像计算各区域坐标python代码
以下是一个示例代码,用于计算标记后图像中每个连通域的坐标:
```python
import cv2
import numpy as np
# 读取图像并进行连通域标记
img = cv2.imread('image.png', 0)
ret, thresh = cv2.threshold(img, 127, 255, cv2.THRESH_BINARY)
output = cv2.connectedComponentsWithStats(thresh, 8, cv2.CV_32S)
# 获取连通域的数量和各种统计信息
num_labels = output[0]
labels = output[1]
stats = output[2]
centroids = output[3]
# 循环遍历每个连通域并计算其坐标
for i in range(num_labels):
x = stats[i, cv2.CC_STAT_LEFT]
y = stats[i, cv2.CC_STAT_TOP]
w = stats[i, cv2.CC_STAT_WIDTH]
h = stats[i, cv2.CC_STAT_HEIGHT]
cx = centroids[i, 0]
cy = centroids[i, 1]
print("Label {}: Top left={},{} Bottom right={},{} Centroid={},{}".format(i, x, y, x + w, y + h, cx, cy))
```
在这个示例代码中,我们首先读取了一张灰度图像,并使用OpenCV的`connectedComponentsWithStats`函数对其进行连通域标记。然后,我们获取了标记后图像中的连通域数量、标记数组、统计信息和质心坐标。最后,我们循环遍历每个连通域,并计算其左上角和右下角坐标以及质心坐标,并将其打印出来。
区域生长计算连通域的质心点算法代码
以下是一种使用Python实现的区域生长算法来计算连通域质心点的代码:
```python
import numpy as np
def region_growing(image, threshold):
"""
使用区域生长算法来计算图像中的连通域
:param image: 原始图像,二维numpy数组
:param threshold: 生长阈值
:return: 连通域列表,每个连通域包含其像素坐标和质心坐标
"""
# 初始化
rows, cols = image.shape
visited = np.zeros((rows, cols), dtype=bool)
regions = []
# 区域生长算法
for i in range(rows):
for j in range(cols):
if not visited[i, j]:
region = []
queue = [(i, j)]
visited[i, j] = True
while queue:
x, y = queue.pop(0)
region.append((x, y))
if x > 0 and not visited[x-1, y] and abs(image[x, y] - image[x-1, y]) < threshold:
queue.append((x-1, y))
visited[x-1, y] = True
if x < rows-1 and not visited[x+1, y] and abs(image[x, y] - image[x+1, y]) < threshold:
queue.append((x+1, y))
visited[x+1, y] = True
if y > 0 and not visited[x, y-1] and abs(image[x, y] - image[x, y-1]) < threshold:
queue.append((x, y-1))
visited[x, y-1] = True
if y < cols-1 and not visited[x, y+1] and abs(image[x, y] - image[x, y+1]) < threshold:
queue.append((x, y+1))
visited[x, y+1] = True
if len(region) > 0:
region = np.array(region)
centroid = np.mean(region, axis=0)
regions.append({'pixels': region, 'centroid': centroid})
return regions
```
在此代码中,我们使用一个二维numpy数组来表示原始图像,并使用阈值作为生长条件。算法遍历图像的每个像素,并将它与其相邻的像素进行比较。如果它们之间的差异小于阈值,则将其添加到同一连通域中,并将其标记为已访问。最终,我们计算每个连通域的质心坐标作为其特征,并将其保存在一个列表中返回。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![py](https://img-home.csdnimg.cn/images/20210720083646.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)