无法使用opencv imshow查看彩色图像

时间:2017-02-08 15:54:52

标签: python opencv ros realsense

我无法使用python-opencv界面从R200 realsense相机查看彩色图像。 运行此脚本时,窗口为空白。 当我注释掉'cf2.namedWindow(“图像窗口”,1)'时,它会显示第一张图片。

import roslib
import sys
import rospy
import cv2
from std_msgs.msg import String
from geometry_msgs.msg import Twist
from sensor_msgs.msg import Image
from rospy.numpy_msg import numpy_msg
#from rospy_tutorials.msg import Floats
from cv_bridge import CvBridge, CvBridgeError
import numpy as np
import math;
import cv2;
#import matplotlib.pyplot as plt;
import sys;
#import caffe;
import socket;
#from sklearn import datasets;
import subprocess;

import message_filters
from rospy.numpy_msg import numpy_msg
import time
#####################

import os.path


class image_converter:

# Initializing variables, publishers and subscribers
  def __init__(self):
    print 'show window'
    cv2.namedWindow("Image window", 1)

    self.bridge = CvBridge()

    self.image_sub = rospy.Subscriber("/camera/rgb/image_color", Image, self.callback)


  # The main callback that processes the color and depth data.      
  def callback(self,color):


    start = time.time()
    # acquiring color and depth data (from ROS-python tutorial)
    try:

      frame = self.bridge.imgmsg_to_cv2(color, "bgr8")
    except CvBridgeError, e:
      print e

    frame = np.array(frame, dtype=np.uint8)

    cv2.imshow("Image window", frame)
    print 'test'
    cv2.waitKey(0)


def main(args):
  ic = image_converter()
  rospy.init_node('image_converter', anonymous=True)
  try:
    rospy.spin()
  except KeyboardInterrupt:
    print "Shutting down"
  cv2.destroyAllWindows()

if __name__ == '__main__':
main(sys.argv)

1 个答案:

答案 0 :(得分:0)

您确定namedWindow标志值吗?我看到你在那里使用了一个int,但是在文档http://www.experiglot.com/2006/12/11/how-to-use-nested-if-statements-in-excel-with-and-or-not/中,你可以为C ++和python找到正确的标志。

我通常在C ++中使用OpenCV,所以我没有经过测试,但它应该像这样工作:

cv2.namedWindow( "Display window", cv2.WINDOW_AUTOSIZE );

如果方便您的需要,您还可以定义这样的show函数:

def show(image, name="Image"):
    '''
    Routine to display the image.
    '''
    cv2.namedWindow(name, cv2.WINDOW_NORMAL)
    cv2.imshow(name, image)
    cv2.waitKey(0)

该功能非常简单,但我自己并没有对其进行编码,因此这是第二个代码段的here