Ask Your Question
0

how to display video we get from cv_bridge?

asked 2018-12-02 10:22:51 -0500

PRASHANT gravatar image

i am getting some errror in doing processing of that mutiple images . so i want to know whether i am doing right or not.

edit retag flag offensive close merge delete

Comments

1

can you share more detail like your code? we can't say anything without more detail.

Hamid Didari gravatar image Hamid Didari  ( 2018-12-02 22:28:40 -0500 )edit

1 Answer

Sort by ยป oldest newest most voted
0

answered 2018-12-03 10:38:53 -0500

Farid gravatar image

You may want to give this code a try to have a live stream from either your IP camera (Android phone) or a low cost USB camera.

cameraPub.cpp

#include <ros/ros.h>
#include <image_transport/image_transport.h>
#include <opencv2/highgui/highgui.hpp>
#include <cv_bridge/cv_bridge.h>
#include <camera_info_manager/camera_info_manager.h>
#include <sstream> 

using namespace std;
using namespace cv;
using namespace ros;

int main(int argc, char** argv)
{
  // Check if video source has been passed as a parameter

  init(argc, argv, "cameraPub");
  NodeHandle nh;

    if(argc != 2)
    {
        cerr << endl << "Usage: rosrun ORB_SLAM2 cameraPub camera_name(webcam or phone?)" << endl;
        ros::shutdown();
        return 1;
    }

    stringstream ss;
    string camera_name;
    ss << argv[1];
    ss >> camera_name;

  //string camera_name = "logitech_B525";
  //string camera_name = "Android";

  image_transport::ImageTransport it(nh);
  image_transport::Publisher pub_cam_msg = it.advertise(camera_name+"/image_raw", 1);
  Publisher pub_cam_info = nh.advertise<sensor_msgs::CameraInfo>(camera_name+"/camera_info", 1);


    // default camera
    VideoCapture cap(0);
    // camera_info -> setting file (.yaml)
    const string camurl = "file:///path/to/settingFile/logitech_B525.yaml";

    if (camera_name ==  "phone")
    {
        // IP webcam
        cout<<"IP camera selected!"<<endl;
        const string vsa = "http://192.168.43.26:8080/video?x.mjpeg";
        VideoCapture cap(vsa);

        // camera_info -> setting file (.yaml)
        const string camurl = "file:///path/to/settingFile/Huawei_P10.yaml";
    }
    else
    {
        cout<<"Default Camera!"<<endl;
    }

  // Check video is open
  if (!cap.isOpened())
  {
    cerr<<"Could not open video!!"<<endl;
    nh.shutdown();
    return 1;
  }
  cout << "\nCamera from " << camera_name << " ON ------------>>>>>>>>>>>>" << endl;

  Mat frame;

  sensor_msgs::ImagePtr cam_msg;

  camera_info_manager::CameraInfoManager caminfo(nh, camera_name, camurl);

  sensor_msgs::CameraInfo ci;

  Rate loop_rate(5);

  while (nh.ok()) {

    cap >> frame;

    if(!frame.empty())
    {
        cam_msg = cv_bridge::CvImage(std_msgs::Header(), "bgr8", frame).toImageMsg();

        ci.header.stamp = ros::Time::now(); //
        ci=caminfo.getCameraInfo();

        pub_cam_msg.publish(cam_msg);
        pub_cam_info.publish(ci);

            imshow("video stream",frame);
            waitKey(1); // 30 ms */
    }
    else
    {
    cout << "EMPTY FRAME!" << endl;
    }

    spinOnce();
  }
  return 0;
}
edit flag offensive delete link more

Your Answer

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

Add Answer

Question Tools

1 follower

Stats

Asked: 2018-12-02 10:22:51 -0500

Seen: 258 times

Last updated: Dec 03 '18