not able to stream images from imshow in ROS

asked 2017-04-13 11:07:43 -0500

zubair gravatar image

updated 2017-04-17 09:43:20 -0500

hi all i want to show images when i rosrun my cpp node, (with realsense camera R200). i am using i was using imshow before with opencv libraries in eclipse to see images through my camera but now its not streaming me image threshold, depth image etc. i have little idea that i need to use cv_bridge . please help guys ,, i am including my code .

#include <ros ros.h=""> #include <stdlib.h> #include <iostream> #include <opencv2 highgui="" highgui.hpp=""> #include <opencv2 opencv.hpp=""> #include <opencv2 imgproc="" imgproc.hpp=""> #include <opencv2 core="" core.hpp=""> #include <librealsense rs.hpp=""> #include <librealsense rscore.hpp=""> #include <cv_bridge cv_bridge.h=""> #include <image_transport image_transport.h="">

using namespace cv; using namespace std;

typedef struct rs_context rs_context; typedef struct rs_device rs_device;

void imageCallback(const sensor_msgs::ImageConstPtr& msg) {

        rs::context ctx;

        // Access the first available RealSense device
        rs::device * dev = ctx.get_device(0);

        // Configure color stream to run at VGA resolution at 30 frames per second
        dev->enable_stream(rs::stream::color, 640, 480, rs::format::bgr8, 30);

        // We must also configure depth stream
        dev->enable_stream(rs::stream::depth, 640, 480, rs::format::z16, 30);

        // Start streaming
        dev->start();

        // Determine depth value corresponding to one meter
        //const uint16_t one_meter = static_cast<uint16_t>(1.0f / dev->get_depth_scale());


        // Creating OpenCV Matrix from a color image
        Mat color(Size(640, 480), CV_8UC3, (void*)dev->get_frame_data(rs::stream::color), Mat::AUTO_STEP);

        // Creating OpenCV Matrix from a depth image
        Mat depth(Size(640, 480), CV_16U, (void*)dev->get_frame_data(rs::stream::depth), Mat::AUTO_STEP);


        float scale = dev->get_depth_scale();




        // Display in a GUI
        namedWindow("Display Image depth", WINDOW_AUTOSIZE );
        namedWindow("Display Image", WINDOW_AUTOSIZE );
        namedWindow("Control", CV_WINDOW_AUTOSIZE); //create a window called "Control"

        int iLowH = 0;
        int iHighH = 179;

        int iLowS = 0;
        int iHighS = 255;

        int iLowV = 0;
        int iHighV = 255;

        //Create trackbars in "Control" window
        createTrackbar("LowH", "Control", &iLowH, 179); //Hue (0 - 179)
        createTrackbar("HighH", "Control", &iHighH, 179);

        createTrackbar("LowS", "Control", &iLowS, 255); //Saturation (0 - 255)
        createTrackbar("HighS", "Control", &iHighS, 255);

        createTrackbar("LowV", "Control", &iLowV, 255);//Value (0 - 255)
        createTrackbar("HighV", "Control", &iHighV, 255);

        int iLastX = -1;
        int iLastY = -1;


        //Create a black image with the size as the camera output
        Mat imgLines = Mat::zeros(color.size(), CV_8UC3 );;



        while(cv::waitKey(30) <= 0)
            {
                dev->wait_for_frames();
                if (!dev->is_streaming())
            {
                cout << "[ERROR] Disable streaming" << endl;
            }
            if (!dev->is_stream_enabled(rs::stream::color))
            {
                cout << "[ERROR] Disable color streaming" << endl;
            }

            color.data = (unsigned char*)dev->get_frame_data(rs::stream::color);
            depth.data = (unsigned char*)dev->get_frame_data(rs::stream::depth);
            imshow("Display Image", color);
            imshow("depth image", depth);


            // Retrieve depth data, which was previously configured as a 640 x 480 image of 16-bit depth values
            const uint16_t * depth_frame = reinterpret_cast<const uint16_t *>(dev->get_frame_data(rs::stream::depth));

            Mat imgHSV;

            cvtColor(color, imgHSV, COLOR_BGR2HSV); //Convert the captured frame from BGR to HSV

            Mat imgThresholded;

            inRange(imgHSV, Scalar(iLowH, iLowS, iLowV), Scalar(iHighH, iHighS, iHighV), imgThresholded); //Threshold the image

            //morphological opening (removes small objects from the foreground)
            erode(imgThresholded, imgThresholded, getStructuringElement(MORPH_ELLIPSE, Size(5,5)) );
            dilate( imgThresholded, imgThresholded, getStructuringElement(MORPH_ELLIPSE, Size(5,5)) );

            //morphological closing (removes small holes from the ...
(more)
edit retag flag offensive close merge delete

Comments

also, i can se that if i am in ros my camera initialization is wrong,, please how can i initialise and get streams from realsense camera and then convert to MAT

zubair gravatar image zubair  ( 2017-04-13 11:54:18 -0500 )edit

reformat your message, please. Headers are broken.

eugene.katsevman gravatar image eugene.katsevman  ( 2017-04-14 18:00:16 -0500 )edit

Some hints on how to ask a good question: 1) try to write correctly, at least read your own text before posting to get rid of something like " i am using i was using" 2) do proper formatting 3) only post RELEVANT code and not just dump everything you have.

NEngelhard gravatar image NEngelhard  ( 2017-04-17 11:10:52 -0500 )edit