image - 类型错误:参数 'array' 的预期 Ptr<cv::UMat>

标签 image opencv computer-vision cv2

以下是我编写的代码:执行此代码时出现以下错误- **

x,y,w,h = cv2.boundingRect(contours) 类型错误:预期 Ptr 对于参数“数组”

我正在尝试使矩形超出我的轮廓

**

from cv2 import cv2
import numpy as np

cam = cv2.VideoCapture(0)
kernel_open = np.ones((7,7), np.uint8)
kernel_close = np.ones((15,15), np.uint8)


while True:
    ret, frame = cam.read()

    frame = cv2.resize(frame, (340,220))

    

    # Our image is in BGR format. We need to conver it into HSV
    imgHSV = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)

    # Now we'll apply masking to capture green colour
    lower_lim = np.array([33, 80, 40])
    upper_lim = np.array([102, 255, 255])

    mask = cv2.inRange(imgHSV, lower_lim, upper_lim)

    # As we are getting noises so we'll remove the noise by morphological transformation- Opening and Closing
    mask_open = cv2.morphologyEx(mask, cv2.MORPH_OPEN, kernel_open)
    mask_close = cv2.morphologyEx(mask_open, cv2.MORPH_CLOSE, kernel_close)

    # Till now we have identified the colour of the image we want
    # Now we'll make boundary or contours around our required colour image
    contours, hierarchy = cv2.findContours(mask_close.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)
    cv2.drawContours(frame, contours, -1, (0,0,255), 3)


    # Now we'll draw rectangles
    for i in range(len(contours)):
        x,y,w,h = cv2.boundingRect(contours)
        cv2.rectangle(frame, (x,y), (x+w, y+h), (255,0,0), 3)
        


    cv2.imshow('Original Image', frame)
    cv2.imshow('Mask Closed Image', mask_close)

    if cv2.waitKey(1) == 27:
        break

cam.release()
cv2.destroyAllWindows()

Error while running the above code

最佳答案

将您的 mask_close 数组转换为无符号 8 位整数数组 cv2.findContours((mask_close.astype(np.uint8).copy(),cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)

关于image - 类型错误:参数 'array' 的预期 Ptr<cv::UMat>,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62992240/

相关文章:

javascript - 使用 JavaScript 缩放至全窗口宽度

java - 在java中添加/加载图像

python - 如何根据与其他匹配项的接近程度来过滤外围匹配项?

opencv - 从灰度图像中提取像素值

c++ - 如何从多条直线画一条曲线?

c# - 检测人脸位置

c# - 如何在不写入文件的情况下将 DataVisualization.Charting.Chart 的图像转换为 iTextSharp.text.Image?

c++ - 使用两个不同 LMDB 的多标签

python - 使用faster_rcnn_nas_coco模型训练时设置 "second_stage_batch_size*"是什么意思?

php - 从另一台服务器获取图像