图像类型错误:OpenCV Python

乔治鹅

运行代码时出现图像类型错误。我知道HoughLinesP需要灰度图像,但是当我尝试将源图像转换为灰度图像时,出现以下错误(1):

错误:(-215)深度== 0 || 深度== 2 || 深度== 5在函数cv :: cvtColor中

如果我运行HoughLinesP而不转换为灰度,则会出现以下错误(2):

错误:函数cv :: HoughLinesProbabilistic中的(-215)image.type()==((((0)&((1 << 3)-1))+(((1)-1)<< 3))

我不知道要摆脱这个错误我需要什么转换

这是发生错误的代码:

#extract largest component from image.
components, output, stats, centroids = cv2.connectedComponentsWithStats(threshold_img, connectivity=4)
sizes = stats[:, -1]

max_label = 1
max_size = sizes[1]
for i in range(2, components):
    if sizes[i] > max_size:
        max_label = i
        max_size = sizes[i]
biggestComponent = np.zeros(output.shape)
biggestComponent[output == max_label] = 255
biggestComponent = biggestComponent - cv2.erode(biggestComponent, np.ones((5,5), np.uint8))
dilated = cv2.dilate(biggestComponent, np.ones((3,3), dtype=np.uint8))

#-------------------------ERROR(1)----------------------------#
dilated = cv2.cvtColor(dilated, cv2.COLOR_BGR2GRAY)

#obtaining corners using houghlines
def find_intersection(line1, line2):
    # extract points
    x1, y1, x2, y2 = line1[0]
    x3, y3, x4, y4 = line2[0]
    # compute determinant
    Px = ((x1*y2 - y1*x2)*(x3-x4) - (x1-x2)*(x3*y4 - y3*x4))/  \
        ((x1-x2)*(y3-y4) - (y1-y2)*(x3-x4))
    Py = ((x1*y2 - y1*x2)*(y3-y4) - (y1-y2)*(x3*y4 - y3*x4))/  \
        ((x1-x2)*(y3-y4) - (y1-y2)*(x3-x4))
    return Px, Py

def segment_lines(lines, delta):
    h_lines = []
    v_lines = []
    for line in lines:
        for x1, y1, x2, y2 in line:
            if abs(x2-x1) < delta: # x-values are near; line is vertical
                v_lines.append(line)
            elif abs(y2-y1) < delta: # y-values are near; line is horizontal
                h_lines.append(line)
    return h_lines, v_lines

def cluster_points(points, nclusters):
    criteria = (cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER, 10, 1.0)
    _, _, centers = cv2.kmeans(points, nclusters, None, criteria, 10, cv2.KMEANS_PP_CENTERS)
    return centers

#-------------------------ERROR(2)----------------------------#
# run the Hough transform
lines = cv2.HoughLinesP(dilated, rho=1, theta=np.pi/180, threshold=100, maxLineGap=20, minLineLength=50)
杰鲁·卢克

需要对图像及其属性有一些基本的了解。

在OpenCV中,图像基本上是数组。在进行任何类型的转换之前,请首先确保是否有可能。

如何做到这一点?

  1. 了解其属性,例如其形状(是2D还是3D)。使用shape属性检查形状
  2. 了解其数据类型(intfloat
  3. 为了将其转换为合适的数据类型,请使用astype()并传入要放入数组的数据类型。

回到您的问题!(我实际上运行了您的整个代码以得出此结论)

错误1:

错误:(-215)深度== 0 || 深度== 2 || 深度== 5在函数cv :: cvtColor中

每当您传入的图像形状错误时,就会发生此错误。在导致此错误的行中,cv2.COLOR_BGR2GRAY期望图像是3D数组,但是当您使用进行检查时dilated.shape,它将返回两个类似这样的值的元组(558L, 796L),它不是3D数组。您正在传递2D数组,函数需要3D数组。结果cv2.COLOR_BGR2GRAY是2D数组。

错误2:

错误:函数cv :: HoughLinesProbabilistic中的(-215)image.type()==((((0)&((1 << 3)-1))+(((1)-1)<< 3))

由于数组的数据类型而发生此错误。形状正确,但是需要一个type数组intdilated是type的2D数组float

那你怎么改变呢?使用以下命令更改数组的数据类型astype

lines = cv2.HoughLinesP(dilated.astype(np.uint8), rho=1, theta=np.pi/180, threshold=100, maxLineGap=20, minLineLength=50)

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章