0
$\begingroup$

Rosanswers logo

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)

Originally posted by pnambiar on ROS Answers with karma: 120 on 2017-02-08

Post score: 0

$\endgroup$

2 Answers 2

0
$\begingroup$

Rosanswers logo

Remove self.bridge = CvBridge() statement from init(self) and move it to callback() function. On each callback, you need to reinitialize CvBridge object


Originally posted by vibhor with karma: 26 on 2017-04-02

This answer was ACCEPTED on the original site

Post score: 1

$\endgroup$
0
$\begingroup$

Rosanswers logo

This thing works for me. For getting an Image stream.

Code:

import rospy
from sensor_msgs.msg import Image 
from cv_bridge import CvBridge 
import cv2 
import numpy as np
 
def callback(data):
  br = CvBridge()
  rospy.loginfo("receiving video frame")
  current_frame = br.imgmsg_to_cv2(data)
  cv_image_array = np.array(current_frame, dtype = np.dtype('f8'))
  cv_image_norm = cv2.normalize(cv_image_norm, cv_image_array, 0, 1, cv2.NORM_MINMAX)
  cv2.imshow("camera", cv2.cvtColor(current_frame,cv2.COLOR_BGR2RGB))
  print(cv_image_norm.shape)
  cv2.waitKey(1)

def receive_message():
  rospy.init_node('video_sub_py', anonymous=True)
  rospy.Subscriber('__YOUR_TOPIC_NAME', Image, callback) # check name by rostopic list
  rospy.spin()
  cv2.destroyAllWindows()
  
if __name__ == '__main__':
  receive_message()

Originally posted by Ranjit Kathiriya with karma: 1622 on 2020-11-20

This answer was NOT ACCEPTED on the original site

Post score: 2

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.