【问题标题】:TypeError: lowerb is not a numpy array, neither a scalarTypeError:lowerb 不是 numpy 数组,也不是标量
【发布时间】:2020-04-01 02:48:27
【问题描述】:

我需要使用cupy 而不是numpy 在GPU 中运行部分代码。所以,我只对这一行 # import numpy as np 做了注释,并用这一行代替了 import cupy as np

完整代码:

import cv2
# import numpy as np
import cupy as np
import time
cap = cv2.VideoCapture(0)
while (1):
    _, img = cap.read()


    if _ is True:
        hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
    else:
        continue

    # red color
    red_lower = np.array([0, 0, 0], np.uint8)
    red_upper = np.array([180,255,30], np.uint8)
    mask = cv2.inRange(hsv, red_lower, red_upper)
    # #
    kernal = np.ones((15, 15), "uint8")
    # # # # #
    mask = cv2.dilate(mask, kernal , iterations=1)
    mask = cv2.GaussianBlur(mask, (11, 11), cv2.BORDER_DEFAULT)
    (_, contours, hierarchy) = cv2.findContours(mask, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
    for pic, contour in enumerate(contours):
        area = cv2.contourArea(contour)
        if (area > 300):
            x, y, w, h = cv2.boundingRect(contour)
            img = cv2.rectangle(img, (x, y), (x + w, y + h), (0, 0, 0), 2)
            cv2.putText(img, "black Colour", (x, y), cv2.FONT_HERSHEY_SIMPLEX, 0.7, (0, 0, 0))


    cv2.imshow("Color Tracking", img)
    if cv2.waitKey(10) & 0xFF == ord('q'):
        cap.release()
        cv2.destroyAllWindows()
        break

如何修复此错误以便使用 cupy。

Traceback (most recent call last):
  File "/home/redhwan/learn.py", line 18, in <module>
    mask = cv2.inRange(hsv, red_lower, red_upper)
TypeError: lowerb is not a numpy array, neither a scalar

我认为我们不能将 numpy 的某些应用程序与 cupy 一起使用。

请问您有什么想法或建议?

【问题讨论】:

    标签: python-2.7 numpy hsv cupy opencv3.3


    【解决方案1】:

    感谢您的提问。

    Opencv 不适用于 cupy 数组,在使用 cupy.asnumpycupy.asarray 函数调用 Opencv 时,您将需要从 cupy 转换回来。

    【讨论】:

    • 使用 cupy.asnumpy 时输出: Traceback(最近一次调用最后一次):文件“/home/redhwan/learn.py”,第 22 行,在 中 mask = cv2.dilate(mask , kernal , iterations=1) TypeError: kernel is not a numpy array, not a scalar
    • 使用 cupy.asarray 时,输出: Traceback(最近一次调用最后一次):文件“/home/redhwan/learn.py”,第 18 行,在 mask = cv2.inRange( hsv, red_lower, red_upper) TypeError: lowerb 不是 numpy 数组,也不是标量
    猜你喜欢
    • 2018-12-18
    • 1970-01-01
    • 1970-01-01
    • 2018-01-08
    • 2018-04-24
    • 2018-02-02
    • 1970-01-01
    • 1970-01-01
    • 2018-02-18
    相关资源
    最近更新 更多