【问题标题】:Why doesn't OpenCV's inRange function convert my HSV image to Binary?为什么 OpenCV 的 inRange 函数不能将我的 HSV 图像转换为二进制?
【发布时间】:2018-02-18 14:25:27
【问题描述】:

我正在尝试将从相机捕获的视频转换为二进制文件,其阈值由轨迹栏控制。

在我的代码中,在使用inRange() 函数进行阈值处理之前,我已将 RGB 视频转换为 HSV。

当我运行我的代码并更改阈值时,什么都没有发生,我只得到 HSV 图像而不是二进制图像。

谁能告诉我如何解决这个问题?

import serial
import numpy as np
import cv2

# dummy function
def nothing(x):
    pass

# Track bar window
cv2.namedWindow('thresh')

# Track bars
cv2.createTrackbar('lH','thresh', 0, 180, nothing)
cv2.createTrackbar('uH', 'thresh', 0, 180, nothing)

cv2.createTrackbar('lS', 'thresh', 0, 255, nothing)
cv2.createTrackbar('uS', 'thresh', 0, 255, nothing)

cv2.createTrackbar('lV', 'thresh', 0, 255, nothing)
cv2.createTrackbar('uV', 'thresh', 0, 255, nothing)

# capture video
cap = cv2.VideoCapture(0)




while True:
    # Read from camera
    source, frame = cap.read()
    if not source:
        break

    # converting image color to HSV color space
    cv2.cvtColor(frame, cv2.COLOR_RGB2HSV, frame, 0)

    # Getting values from track bars
    lH = cv2.getTrackbarPos('lH','thresh')
    uH = cv2.getTrackbarPos('uH', 'thresh')
    lS = cv2.getTrackbarPos('lS', 'thresh')
    uS = cv2.getTrackbarPos('uS', 'thresh')
    lV = cv2.getTrackbarPos('lV', 'thresh')
    uV = cv2.getTrackbarPos('uV', 'thresh')

    lowerb = (lH, lS, lV)
    upperb = (uH, uS, uV)
    cv2.inRange(frame, lowerb, upperb, frame)
    cv2.flip(frame, 1, frame)
    cv2.imshow('thresh', frame)

    if cv2.waitKey(1) & 0xFF == ord('q'):
        break

cap.release()
cv2.destroyAllWindows()

【问题讨论】:

  • 你试过了吗:frame = cv2.inRange(...)frame = cv2.flip(...)
  • @Miki 我试过 frame = cv2.inRange() 但似乎 cv2.inRange() 没有返回任何东西,我得到的只是一张黑色图像,无论我改变多少阈值我仍然得到黑色图像。然后在尝试 frame = cv2.inRange() 时,我做了 print(frame),打印结果是一个用零填充的矩阵。
  • 你确定你得到了正确的 lH、uH 等值吗?
  • 是的,我打印了 lH、uH 和其他类似的变量,它们的值与轨迹栏中显示的相同。我什至在 jpg 图片上硬编码了特定值,但 inRange 函数仍然无法将图片更改为二进制。
  • 试试lowerb = np.array([lH, lS, lV]),和upperb一样

标签: python opencv image-processing


【解决方案1】:

好的,我在 cmets 的 @Miki 的帮助下找到了解决方案。

代码改动很少:

lowerb = np.array([lH, lS, lV], np.uint8)
upperb = np.array([uH, uS, uV], np.uint8)

还有

frame = cv2.inRange(frame, lowerb, upperb)

上述代码更改解决了问题。

完整的工作代码如下

import serial
import numpy as np
import cv2

# dummy function
def nothing(x):
    pass

# Track bar window
cv2.namedWindow('thresh')

# Track bars
cv2.createTrackbar('lH','thresh', 0, 180, nothing)
cv2.createTrackbar('lS', 'thresh', 0, 255, nothing)
cv2.createTrackbar('lV', 'thresh', 0, 255, nothing)
cv2.createTrackbar('uH', 'thresh', 0, 180, nothing)
cv2.createTrackbar('uS', 'thresh', 0, 255, nothing)
cv2.createTrackbar('uV', 'thresh', 0, 255, nothing)

# capture video
cap = cv2.VideoCapture(0)

while True:
    # Read from camera
    source, frame = cap.read()
    if not source:
        break

    # converting image color to HSV color space
    cv2.cvtColor(frame, cv2.COLOR_RGB2HSV, frame, 0)

    # Getting values from track bars
    lH = cv2.getTrackbarPos('lH', 'thresh')
    uH = cv2.getTrackbarPos('uH', 'thresh')
    lS = cv2.getTrackbarPos('lS', 'thresh')
    uS = cv2.getTrackbarPos('uS', 'thresh')
    lV = cv2.getTrackbarPos('lV', 'thresh')
    uV = cv2.getTrackbarPos('uV', 'thresh')

    lowerb = np.array([lH, lS, lV], np.uint8)
    upperb = np.array([uH, uS, uV], np.uint8)

    frame = cv2.inRange(frame, lowerb, upperb)
    cv2.flip(frame, 1, frame)
    cv2.imshow('thresh', frame)

    if cv2.waitKey(1) & 0xFF == ord('q'):
        break

cap.release()
cv2.destroyAllWindows()

【讨论】:

    猜你喜欢
    • 2019-06-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-09-28
    • 1970-01-01
    • 2012-10-29
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多