(摄像头视频处理)将ROS节点转为opencv 图像----cv_bridge 顺便显示两个相机

#include 
#include
#include
#include
#include
#include
#include 
#include 
#include    //这个只有opencv2才有,所以安装一个,然后用绝对路径

using namespace std;
using namespace cv;
bool try_use_gpu = false;
//static const std::string OPENCV_WINDOW = "Image window";
Mat img1;
Mat img2;
vector imgs;
string result_name = "dst1.jpg";
class ImageConverter
{
ros::NodeHandle nh_;
image_transport::ImageTransport it_;
image_transport::Subscriber image_sub1_;
image_transport::Subscriber image_sub2_;
image_transport::Publisher image_pub_;

public:
ImageConverter()
: it_(nh_)
{
// Subscrive to input video feed and publish output video feed
image_sub1_ = it_.subscribe("/1/usb_cam/image_raw", 1, &ImageConverter::imageCb1, this);
image_sub2_ = it_.subscribe("/2/usb_cam/image_raw", 1, &ImageConverter::imageCb2, this);
image_pub_ = it_.advertise("/image_converter/output_video", 1);

//namedWindow(OPENCV_WINDOW);
}

~ImageConverter()
{
//destroyWindow(OPENCV_WINDOW);
}

///////1/
void imageCb1(const sensor_msgs::ImageConstPtr& msg)
{
cv_bridge::CvImagePtr cv_ptr1;
    try
    {
    cv_ptr1 = cv_bridge::toCvCopy(msg, sensor_msgs::image_encodings::BGR8);
    }
    catch (cv_bridge::Exception& e)
    {
    ROS_ERROR("cv_bridge exception: %s", e.what());
    return;
    }
//imshow("1111",cv_ptr1->image);
img1= cv_ptr1->image;
waitKey(3);
}
//2////////////////



///////2
//***
void imageCb2(const sensor_msgs::ImageConstPtr& msg)
{
cv_bridge::CvImagePtr cv_ptr2;
    try
    {
    cv_ptr2 = cv_bridge::toCvCopy(msg, sensor_msgs::image_encodings::BGR8);
    }
    catch (cv_bridge::Exception& e)
    {
    ROS_ERROR("cv_bridge exception: %s", e.what());
    return;
    }
//Mat img2;
//imshow("2222",img1);
img2= cv_ptr2->image;
waitKey(3);

//***/
imshow("1111",img1);
imshow("2222",img2);
////////////////////
//**

///**/
}
};

int main(int argc, char** argv)
{
ros::init(argc, argv, "video_stitching");
ImageConverter ic;
ros::Rate loop_rate(10);  //5/100
///////video_stitching

//imshow("1111",cv_ptr1->image);
/**
if (!img2.empty()) 
{
imshow("3333", img2);
} 
else
{
cout << "image3 is empty" << endl;
}
**/
ros::spin();

return 0;
}

你可能感兴趣的:(double,camera,opencv)