[英]return an array from a function member of a class in C++
I'm using ROS and OpenCV in C++ environment in order to acquire a video (gray-scale) from a ROS node, convert the data through cv_bridge (in order to elaborate it through OpenCV), extract some data and publish them on a topic as ROS messages. 我在C ++环境中使用ROS和OpenCV,以便从ROS节点获取视频(灰度级),通过cv_bridge转换数据(以便通过OpenCV进行详细说明),提取一些数据并将其发布到主题上作为ROS消息。
My problem is that I don't know how to send the array frame
to the main
function in order to elaborate it! 我的问题是我不知道如何将数组frame
发送给main
功能! I cannot elaborate out of it, because I need to distinguish between different data of different frames. 我无法详细说明,因为我需要区分不同帧的不同数据。 This is my code: 这是我的代码:
#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;
}
I hope that the question is clear enough. 我希望这个问题足够清楚。 Can you help me please? 你能帮我吗?
Now if I understand you correctly you want to call imageCb
and have the frame it creates passed back to main
. 现在,如果我理解正确,您想调用imageCb
并将其创建的帧传递回main
。 If that is indeed the case then you can modify imageCb
as follows: 如果确实如此,则可以如下修改imageCb
:
void imageCb(const sensor_msgs::ImageConstPtr& msg, cv::Mat frame )
You will need to create the frame in main
and pass it imageCb
: 您将需要在main
创建框架并将其传递给imageCb
:
cv::Mat frame ;
ic.imageCb( ..., frame ) ;
This will use the copy constructor, it will just copy the header and use a pointer to the actual data, so it won't be very expensive. 这将使用copy构造函数,它将仅复制标头并使用指向实际数据的指针,因此它不会非常昂贵。
This previous thread has a much more detailed elaboration on cv::Mat copy constructor. 上一个线程对cv :: Mat复制构造函数进行了更详细的阐述。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.