我在 C++ 环境中使用 ROS 和 OpenCV,以便从 ROS 节点获取视频(灰度),通过 cv_bridge 转换数据(以便通过 OpenCV 详细说明),提取一些数据并将它们发布在一个主题上作为 ROS 消息。
我的问题是我不知道如何将数组发送frame
到main
函数以便详细说明!我无法详细说明,因为我需要区分不同帧的不同数据。这是我的代码:
#include <ros/ros.h>
#include <image_transport/image_transport.h>
#include <cv_bridge/cv_bridge.h>
#include <sensor_msgs/image_encodings.h>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <vector>
#include "rd_ctrl/proc_data.h"
#include <cv.h>
using namespace std;
namespace enc = sensor_msgs::image_encodings;
rd_ctrl::proc_data points;
ros::Publisher data_pub_;
static const char WINDOW[] = "Image window";
class ImageConverter
{
ros::NodeHandle nh_;
image_transport::ImageTransport it_;
image_transport::Subscriber image_sub_;
image_transport::Publisher image_pub_;
public:
ImageConverter()
: it_(nh_)
{
image_pub_ = it_.advertise("out", 1);
image_sub_ = it_.subscribe("/vrep/visionSensorData", 1, &ImageConverter::imageCb, this);
cv::namedWindow(WINDOW);
}
~ImageConverter()
{
cv::destroyWindow(WINDOW);
}
void imageCb(const sensor_msgs::ImageConstPtr& msg)
{
cv_bridge::CvImagePtr cv_ptr;
try
{
cv_ptr = cv_bridge::toCvCopy(msg, enc::BGR8);
}
catch (cv_bridge::Exception& e)
{
ROS_ERROR("cv_bridge exception: %s", e.what());
return;
}
CvMat frame= cv_ptr->image; //definition of "frame"
cvSmooth(&frame, &frame, CV_MEDIAN);
cvThreshold(&frame, &frame,200, 255,CV_THRESH_BINARY);
cv::imshow(WINDOW, cv_ptr->image);
cv::waitKey(3);
image_pub_.publish(cv_ptr->toImageMsg());
}
};
int main(int argc, char** argv)
{
ros::init(argc, argv, "image_proc");
ImageConverter ic;
ros::NodeHandle n;
//....elaboration of "frame" and production of the data "points"
data_pub_.publish(points);
data_pub_ = n.advertise<rd_ctrl::proc_data>("/data_im", 1);
ros::spin();
return 0;
}
我希望这个问题足够清楚。你能帮我吗?