【问题标题】:Image Type Error: OpenCV Python图像类型错误:OpenCV Python
【发布时间】:2018-07-05 03:08:02
【问题描述】:

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

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

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

错误:(-215) image.type() == (((0) & ((1

我不知道需要什么转换才能摆脱错误

这是发生错误的代码:

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

【问题讨论】:

  • 返回第一个错误是因为图像形状有3个分量,高度宽度和通道,第三个描述颜色分量的数量。您正在创建的“图像”没有第三种形状,因此 cvtColor 将无法使用它。尝试使用 np.expand_dims 添加第三个组件
  • @VaibhavMehrotra 当我打印 image.shape 时,我传递给 cvtColor 的膨胀图像只有高度和宽度,那么当图像已经是灰度时,为什么 HoughLinesP 会产生错误?
  • @OcheGeorge 检查变量的数据类型。

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


【解决方案1】:

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

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

如何做到这一点?

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

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

错误一:

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

只要您传入的图像形状错误,就会出现此错误。在导致此错误的行中,cv2.COLOR_BGR2GRAY 期望图像是 3D 数组,但是当您使用 dilated.shape 检查它时,它会返回两个值的元组,类似于 (558L, 796L),它不是 3D 数组。您正在传入一个 2D 数组,该函数需要一个 3D 数组。 cv2.COLOR_BGR2GRAY 的结果是一个二维数组。

错误2:

错误:(-215) image.type() == (((0) & ((1

由于数组的数据类型而发生此错误。形状是正确的,但它需要一个 int 类型的数组。 dilated 是一个float 类型的二维数组。

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-01-09
    • 2018-06-04
    • 1970-01-01
    • 2020-01-18
    • 1970-01-01
    • 1970-01-01
    • 2011-02-21
    • 1970-01-01
    相关资源
    最近更新 更多