当调用 image.find_blobs() 函数识别多个颜色区域时,返回的是一个 blob 列表(list)。若直接访问 blob.x(),实际是访问列表中第一个 blob 的 x 坐标;用户希望了解:如何正确遍历该列表,依次获取每个 blob 的 x 坐标。
blobs = img.find_blobs(thresholds, pixels_threshold=100, area_threshold=100)
# 方法1:for 循环遍历
for b in blobs:
print("Blob x:", b.x())
# 方法2:带索引遍历
for i, b in enumerate(blobs):
print(f"Blob {i+1} x:", b.x())
# 方法3:提取所有 x 坐标到列表
x_coords = [b.x() for b in blobs]
print("所有 x 坐标:", x_coords)这段代码是这样的:
for blobs in img.find_blobs([thresholds[2]], x_stride=4,y_stride=4, area_threshold=20, pixels_threshold=20): img.draw_rectangle(blobs.rect()) img.draw_cross(blobs.cx(),blobs.cy()) if blobs: for b in blobs: print("Blob cx:",b.cx(),"Blob cy:",b.cy())
运行后系统报错:TypeError:'blob' object isn't iterable
这是什么原因,应该如何修改
语法错误,建议参考IDE左上角:文件-示例中的代码