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

时间:2018-07-09 15:09:06

标签: python opencv ros

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

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

没有图像的窗口的屏幕截图:

screenshot of window without image

一个有趣的行为是将cv2.waitKey(30)中的main放在rospy.spin()的正前方。然后,我得到带有跟踪栏的窗口,没有图像,并且没有可单击的东西。像这样:

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

screenshot of window with unclickable features and no image

我已经阅读了很多有关此问题的内容,当人们使用cv2.waitKey(delay)时,它似乎已解决,但是无论我如何摆弄代码,对我来说情况并非如此。

我的系统是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)

1 个答案:

答案 0 :(得分:1)

仅当我们在订户的回调函数中使用push_back时,才会出现此问题;其他OpenCV函数正常工作,但这似乎是一个错误。

克服此问题的一种方法是通过分析每个框架,如下所示:

cv2.imshow(winname, mat)

代替此:

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