为什么OpenCV的inRange功能不能将我的HSV图像转换为二进制?

时间:2015-11-10 13:44:48

标签: python opencv image-processing

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

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

当我运行我的代码并更改阈值时,没有任何反应,我只得到HSV图像,而不是二进制图像。

有人可以告诉我如何解决这个问题吗?

enter image description here

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

1 个答案:

答案 0 :(得分:0)

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

代码中几乎没有变化:

String[] names = {"aa", ..........., "bb"};
for (int i = 0; i < names.length; i++) {
    if (names[i].toLowerCase().startsWith(query.toLowerCase()))
        c.addRow(new Object[]{i, names[i]});
}

以及

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

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

完整的工作代码在

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