Ask Your Question
1

Converting Kinect RGB image to OpenCV gives wrong colours

asked 2011-03-05 02:18:10 -0500

Aslund gravatar image

Hey everyone

I have been playing a bit with the kinect camera using the openni driver. When running openni_node.launch and viewing the rgb image using rviz, then everything looks perfect, but when I create a node that has to the purpose to read the rgb image, convert it to a cv image and displaying it using imshow, then the colours are all wrong. Red is blue, blue is red, green is something yellow and yellow is light blue. I have been trying to use RGB8 and BGR8 encodings, but nether of them shows any difference. A sample of my code is shown below, inspired from the example in the cv_bridge tutorial:

void processRGB::processImage( const sensor_msgs::Image::ConstPtr& img ) {
    CvImagePtr cv_ptr;

    try {

        cv_ptr = toCvCopy(img, enc::BGR8); //enc::RGB8 also used

    } catch (cv_bridge::Exception& e) {

        ROS_ERROR("cv_bridge exception: %s", e.what());
        return;
    }

    imshow("Kinect RGB image", cv_ptr->image);
    waitKey(3);
}

Does anyone know what might cause this problem? I am using the newest version of Diamondback.

Regards

Sebastian Aslund

edit retag flag offensive close merge delete

Comments

What image topic are you subscribed to?
fergs gravatar imagefergs ( 2011-03-05 02:38:20 -0500 )edit

4 Answers

Sort by ยป oldest newest most voted
2

answered 2011-03-05 02:36:16 -0500

Pi Robot gravatar image

Hi Sebastian,

Here's a similar piece bit of Python code that works for me with my Kinect. Like you, I am using the latest version of Diamondback and the latest Ni stack. The steps I am using are:

$ roslaunch openni_camera openni_node.launch

$ rosrun pi_head_tracking_tutorial ros_to_cv.py

where of course the second line will be different for you. Here is the ros_to_cv.py code. If you run it as a node, you'll also have to change the manifiest name in the roslib.load_manifiest('pi_head_tracking_tutorial') line.

#!/usr/bin/env python

import roslib
roslib.load_manifest('pi_head_tracking_tutorial')
import sys
import rospy
import cv
from std_msgs.msg import String
from sensor_msgs.msg import Image
from cv_bridge import CvBridge, CvBridgeError

class vision_node:
    def __init__(self):
        rospy.init_node('vision_node')

        self.cv_window_name = "OpenCV Image"

        cv.NamedWindow(self.cv_window_name, 1)
        self.bridge = CvBridge()
        self.image_sub = rospy.Subscriber("/camera/rgb/image_color", Image,\
        self.callback)

    def callback(self, data):
        try:
          cv_image = self.bridge.imgmsg_to_cv(data, "bgr8")
        except CvBridgeError, e:
          print e

        cv.ShowImage(self.cv_window_name, cv_image)
        cv.WaitKey(3)

def main(args):
      vn = vision_node()
      try:
        rospy.spin()
      except KeyboardInterrupt:
        print "Shutting down vison node."
        cv.DestroyAllWindows()

if __name__ == '__main__':
    main(sys.argv)
edit flag offensive delete link more
0

answered 2011-03-09 20:28:04 -0500

Miguel Prada gravatar image

Hi Sebastian,

I had the same issue and found out that using toCvShare instead of toCvCopy resulted in an image with good colors. Maybe you can use that until this issue is fixed.

edit flag offensive delete link more

Comments

I will try, thanks
Aslund gravatar imageAslund ( 2011-03-25 07:43:25 -0500 )edit
0

answered 2011-03-05 08:42:54 -0500

Aslund gravatar image

I managed to make it work and it returns the image nicely, posted a mail on the mailing list pointing out that there is apparently an error in the cv_bridge for diamondback, as using the tutorial for cturtle also returned a nice image.

edit flag offensive delete link more

Comments

I've been using cv_bridge under diamondback without issue -- so I don't think there is a bug in the cv_bridge itself, perhaps you could post the entire source code of the node you have that fails.
fergs gravatar imagefergs ( 2011-03-05 08:58:57 -0500 )edit
0

answered 2011-03-05 03:15:08 -0500

Aslund gravatar image

I cant see any difference between your code and my own, besides the language used, so I have tried running your code but I get the following error:

  • [rosbuild] Building package ros_kinect_template
  • [rosbuild] Cached build flags older than manifests; calling rospack to get flags
  • [rosbuild] Including /opt/ros/diamondback/stacks/ros_comm/clients/rospy/cmake/rospy.cmake
  • [rosbuild] Including /opt/ros/diamondback/stacks/ros_comm/clients/roslisp/cmake/roslisp.cmake
  • [rosbuild] Including /opt/ros/diamondback/stacks/ros_comm/clients/cpp/roscpp/cmake/roscpp.cmake
  • -- Configuring done CMake Error: CMake can not determine linker language for target:rosCv
  • CMake Error: Cannot determine link language for target "rosCv".

rosCv was a quick node name for your code, I am not used to python in any way, so I do not know if I missed something. I added rospy to my manifest file. I added ros_to_cv.py to my src folder, copied your code, changed the second line to roslib.load_manifest('ros_kinect_temp late') and in my cmake list wrote rosbuild_add_executable(rosCv src/ros_to_cv.py)


If someone else have an idea to what might be wrong, then do not hesitate to write :)

Regards

Sebastian

edit flag offensive delete link more

Comments

You don't need to build python code -- there is no need for rosbuild_add_executable. Instead, just make the python script executable (using chmod).
fergs gravatar imagefergs ( 2011-03-05 04:58:02 -0500 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

Stats

Asked: 2011-03-05 02:18:10 -0500

Seen: 3,001 times

Last updated: Mar 09 '11