Python 将图像中的红线转换为numpy列表

Python 将图像中的红线转换为numpy列表,python,opencv,tensorflow,python-imaging-library,Python,Opencv,Tensorflow,Python Imaging Library,我想测量此管道中检测到的物体与地面的高度差。较低的红线应为最小高度的标记。我想我可以先把下面的红线转换成一个numpy列表,但我该怎么做呢?红色圆圈是使用cv2.circle()函数绘制的 编辑: 多亏了ZdaR,我离解决问题更近了。这是他为使用python3而改写的解决方案: import cv2 import numpy as np def get_center(arr): return sum(arr)/len(arr) def get_cluster_centers(a

我想测量此管道中检测到的物体与地面的高度差。较低的红线应为最小高度的标记。我想我可以先把下面的红线转换成一个numpy列表,但我该怎么做呢?红色圆圈是使用
cv2.circle()
函数绘制的

编辑:

多亏了ZdaR,我离解决问题更近了。这是他为使用python3而改写的解决方案:

import cv2
import numpy as np


def get_center(arr):
    return sum(arr)/len(arr)


def get_cluster_centers(arr, tolerance):
    clusters = [[arr[0]]]

    for ele in arr[1:]:
        if abs(clusters[-1][0] - ele) < tolerance:
            clusters[-1].append(ele)
        else:
            clusters.append([ele])

    clusters_centers = map(get_center, clusters)
    return clusters_centers


img = cv2.imread("/home/artur/Desktop/0.png")

# Segment the red color
mask = cv2.inRange(img, np.array([0, 0, 255]), np.array([0, 0, 255]))

for i in mask:
    print(i)

# Let's iterate the middle column and get the distance between the two red lines.
half_width = int(mask.shape[1]/2)
middle_column = mask[:, half_width]

idx = np.where(middle_column == 255)

# Since the width of line is not 1 px so we need to cluster the pixels to get a single center value.
centers = list(get_cluster_centers(idx[0], 5))

if len(centers) == 2:
    print("Distance between lines:", centers[1] - centers[0], "px")
导入cv2
将numpy作为np导入
def get_中心(arr):
返回金额(arr)/长度(arr)
def get_集群_中心(arr,公差):
集群=[[arr[0]]]
对于arr[1:]中的ele:
如果abs(簇[-1][0]-ele)<公差:
群集[-1]。追加(ele)
其他:
clusters.append([ele])
集群\中心=地图(获取\中心,集群)
返回中心
img=cv2.imread(“/home/artur/Desktop/0.png”)
#分割红色
mask=cv2.inRange(img,np.array([0,0,255]),np.array([0,0,255]))
对于面具中的i:
印刷品(一)
#让我们迭代中间的列,得到两条红线之间的距离。
半宽度=int(掩码形状[1]/2)
中间列=遮罩[:,半宽度]
idx=np。其中(中间列==255)
#因为线条的宽度不是1像素,所以我们需要对像素进行聚类以获得单个中心值。
中心=列表(获取集群中心(idx[0],5))
如果len(中心)==2:
打印(“行间距:”,中心[1]-中心[0],“px”)

它借助图像的中间列测量上下红线之间的像素距离。我如何迭代所有列以确定这两条线之间的最小距离或更好的距离,即检测到的对象和较低的红线之间的距离?这个解决方案只考虑中间列,对吗?

您可以先从输入图像中分割红色,得到一个二值遮罩,然后假设您的红线位于输入图像的中心,我们取该图像的中心列,并在该列上迭代以查找红点位置,然后简单地查找以像素为单位的距离,如下所示:

import cv2
import numpy as np


def get_center(arr):
    return sum(arr)/len(arr)


def get_cluster_centers(arr, tolerance):
    clusters = [[arr[0]]]

    for ele in arr[1:]:
        if abs(clusters[-1][0] - ele) < tolerance:
            clusters[-1].append(ele)
        else:
            clusters.append([ele])

    clusters_centers = map(get_center, clusters)
    return clusters_centers


img = cv2.imread("/home/anmol/Downloads/HK3WM.png")

# Segment the red color
mask = cv2.inRange(img, np.array([0, 0, 255]), np.array([0, 0, 255]))

# Let's iterate the middle column and get the distance between the two red lines.
half_width = mask.shape[1]/2
middle_column = mask[:, half_width]

idx = np.where(middle_column == 255)

# Since the width of line is not 1 px so we need to cluster the pixels to get a single center value.
centers = get_cluster_centers(idx[0], 5)

if len(centers) == 2:
    print "Distance between lines:", centers[1] - centers[0], "px"
导入cv2
将numpy作为np导入
def get_中心(arr):
返回金额(arr)/长度(arr)
def get_集群_中心(arr,公差):
集群=[[arr[0]]]
对于arr[1:]中的ele:
如果abs(簇[-1][0]-ele)<公差:
群集[-1]。追加(ele)
其他:
clusters.append([ele])
集群\中心=地图(获取\中心,集群)
返回中心
img=cv2.imread(“/home/anmol/Downloads/HK3WM.png”)
#分割红色
mask=cv2.inRange(img,np.array([0,0,255]),np.array([0,0,255]))
#让我们迭代中间的列,得到两条红线之间的距离。
半宽度=遮罩形状[1]/2
中间列=遮罩[:,半宽度]
idx=np。其中(中间列==255)
#因为线条的宽度不是1像素,所以我们需要对像素进行聚类以获得单个中心值。
中心=获取群集中心(idx[0],5)
如果len(中心)==2:
打印“线间距:”,中心[1]-中心[0],“px”

PS:如果这还不能解释一些问题,我会很匆忙。请在评论中随意提问。

你所说的“将红线转换为numpy列表”到底是什么意思?您想要圆周上所有像素的(x,y)坐标数组吗?因为你知道圆心和半径,以及定义圆的方程式,你可以很容易地计算它们一个懒散的处理方法就是把圆圈(没有那么粗)画成一个黑色的图像,谢谢你的回答。请查看我文章的编辑部分。