TypeError:预期的Ptr< cv :: UMat>对于参数“数组” [英] TypeError: Expected Ptr<cv::UMat> for argument 'array'

查看:537
本文介绍了TypeError:预期的Ptr< cv :: UMat>对于参数“数组”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

The following is the code I have written: I am getting the following error while executing this code- **

x,y,w,h = cv2.boundingRect(contours)TypeError:预期的Ptr< cv :: UMat>
用于参数数组

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

I am trying to make the rectangle outside my contours

**

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

推荐答案

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

Cast your mask_close array as an unsigned 8-bit integer array cv2.findContours((mask_close.astype(np.uint8).copy(),cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)

这篇关于TypeError:预期的Ptr&lt; cv :: UMat&gt;对于参数“数组”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆