图像类型错误:OpenCV Python

时间:2018-07-05 03:08:02

标签: python image opencv image-processing error-handling

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

  

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

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

  

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

我不知道要摆脱该错误我需要进行什么转换

这是发生错误的代码:

#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)

1 个答案:

答案 0 :(得分:3)

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

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

如何做到这一点?

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

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

错误1:

  

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

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

错误2:

  

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

由于数组的数据类型而发生此错误。形状是正确的,但它需要类型为int的数组。 dilatedfloat类型的2D数组。

那么您如何更改它?使用astype更改数组的数据类型:

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