使用 OpenCV 和 ROS 时冻结的黑色图像和轨迹栏

Frozen black image and trackbar while using OpenCV and ROS

我正在尝试创建一个订阅图像主题的简单 ROS 节点,然后使用轨迹栏显示图像以允许用户确定对图像设置阈值所需的正确 HSV 值。

问题是 window 出现时没有图像。像这样:

window 无图像的屏幕截图:

一个有趣的行为是将 cv2.waitKey(30) 放在 main 中,就在 rospy.spin() 之前。然后我得到 window,有轨迹条,没有图像,没有任何东西是可点击的。像这样:

window 的屏幕截图具有不可点击的功能且没有图像:

我已经阅读了很多关于这个问题的文章,当人们使用 cv2.waitKey(delay) 时它似乎得到了修复,但无论我如何 fiddle 使用代码,这对我来说都不是这种情况。

我的系统是 Ubuntu GNOME 16.04 并且是 运行 ROS Kinetic。

#!/usr/bin/env python

"""Allows the user to calibrate for HSV filtering later."""

# Standard libraries
from argparse import ArgumentParser

# ROS libraries
import rospy
from cv_bridge import CvBridge
from sensor_msgs.msg import Image

# Other libraries
import cv2
import numpy as np


def nothing():
    """Does nothing."""

    pass


def calibrator(msg, args):
    """Updates the calibration window"""

    bridge = args
    raw = bridge.imgmsg_to_cv2(msg, desired_encoding="bgr8")
    hsv = cv2.cvtColor(raw, cv2.COLOR_BGR2HSV)

    # get info from track bar and appy to result
    h_low = cv2.getTrackbarPos('H_low', 'HSV Calibrator')
    s_low = cv2.getTrackbarPos('S_low', 'HSV Calibrator')
    v_low = cv2.getTrackbarPos('V_low', 'HSV Calibrator')
    h_high = cv2.getTrackbarPos('H_high', 'HSV Calibrator')
    s_high = cv2.getTrackbarPos('S_high', 'HSV Calibrator')
    v_high = cv2.getTrackbarPos('V_high', 'HSV Calibrator')

    # Normal masking algorithm
    lower = np.array([h_low, s_low, v_low])
    upper = np.array([h_high, s_high, v_high])

    mask = cv2.inRange(hsv, lower, upper)

    result = cv2.bitwise_and(raw, raw, mask=mask)

    cv2.imshow('HSV Calibrator', result)
    cv2.waitKey(30)


def main(node, subscriber):
    """Creates a camera calibration node and keeps it running."""

    # Initialize node
    rospy.init_node(node)

    # Initialize CV Bridge
    bridge = CvBridge()

    # Create a named window to calibrate HSV values in
    cv2.namedWindow('HSV Calibrator')

    # Creating track bar
    cv2.createTrackbar('H_low', 'HSV Calibrator', 0, 179, nothing)
    cv2.createTrackbar('S_low', 'HSV Calibrator', 0, 255, nothing)
    cv2.createTrackbar('V_low', 'HSV Calibrator', 0, 255, nothing)

    cv2.createTrackbar('H_high', 'HSV Calibrator', 50, 179, nothing)
    cv2.createTrackbar('S_high', 'HSV Calibrator', 100, 255, nothing)
    cv2.createTrackbar('V_high', 'HSV Calibrator', 100, 255, nothing)

    # Subscribe to the specified ROS topic and process it continuously
    rospy.Subscriber(subscriber, Image, calibrator, callback_args=(bridge))

    rospy.spin()


if __name__ == "__main__":
    PARSER = ArgumentParser()
    PARSER.add_argument("--subscribe", "-s",
                        default="/cameras/lmy_cam",
                        help="ROS topic to subcribe to (str)", type=str)
    PARSER.add_argument("--node", "-n", default="CameraCalibrator",
                        help="Node name (str)", type=str)
    ARGS = PARSER.parse_args()

    main(ARGS.node, ARGS.subscribe)

仅当我们在订阅者的回调函数中使用 cv2.imshow(winname, mat) 时才会出现此问题;其他 OpenCV 功能正常工作,但这似乎是一个错误。

克服这个问题的一种方法是像这样分析每一帧:

while not rospy.is_shutdown():
    data = rospy.wait_for_message(topic, topic_type)
    # do stuff
    cv2.imshow(winname, mat)
    cv2.waitKey(delay)

而不是这个:

def callback(data):
    # do stuff
    cv2.imshow(winname, mat)
    cv2.waitKey(delay)

rospy.subscribe(name, data_class, callback=callback)
rospy.spin()