图像类型错误:OpenCV Python

2024-10-04 07:24:17 发布

您现在位置:Python中文网/ 问答频道 /正文

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

error: (-215) depth == 0 || depth == 2 || depth == 5 in function cv::cvtColor

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

error: (-215) image.type() == (((0) & ((1 << 3) - 1)) + (((1)-1) << 3)) in function cv::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)

Tags: 错误nplinecv2maxlinesx1x2
1条回答
网友
1楼 · 发布于 2024-10-04 07:24:17

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

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

怎么做到的?

  1. 了解它的特性,比如它的形状(是二维还是三维)。使用shape属性检查形状。在
  2. 了解其数据类型(intfloat
  3. 为了将其转换为合适的数据类型,请使用astype()并传入数组所属的数据类型。在

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

错误1:

error: (-215) depth == 0 || depth == 2 || depth == 5 in function cv::cvtColor

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

错误2:

error: (-215) image.type() == (((0) & ((1 << 3) - 1)) + (((1)-1) << 3)) in function cv::HoughLinesProbabilistic

发生此错误的原因是数组的数据类型。形状正确,但它需要int类型的数组。dilatedfloat类型的2D数组。在

那你怎么改呢?使用astype更改数组的数据类型:

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

相关问题 更多 >