qboticslabs / Chefbot_ROS_pkg

This is ROS package mentioned in the book "Learning Robotics using Python". This package contain drivers, Launch files for working with Chefbot, mentioned on this book|New version of code for ROS Kinetic: https://github.com/PacktPublishing/Learning-Robotics-using-Python-Second-Edition
http://learn-robotics.com
65 stars 73 forks source link

Problem viewing color image using opencv #5

Closed pnambiar closed 7 years ago

pnambiar commented 7 years ago

I have trouble viewing color images from R200 realsense camera using the python-opencv interface. The window is blank when I run this script. When I comment out'cv2.namedWindow("Image window", 1)', it shows the first image.

-P

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)

print 'start bridge and subscribe'

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)

qboticslabs commented 7 years ago

Hi

So are you able to display the color image by commenting the named Window line?

NamedWindow API will be only used if you have any interaction using mouse or keyboard, otherwise, it's fine to comment it.

Also, try changing the string inside this two function. Let me know what you are getting now

cv2.namedWindow("Image", 1) cv2.imshow("Image", frame)

pnambiar commented 7 years ago

No. That did not fix the problem

qboticslabs commented 7 years ago

Do you able to see the color image without Namedwindow?

pnambiar commented 7 years ago

just the first frame. then it does not move to the next frame

qboticslabs commented 7 years ago

Can you change the cv2.waitKey(0) to cv2.waitKey(30), now it's waiting for infinitely for the keypress.

pnambiar commented 7 years ago

Thanks. That fixed it.