I have a function where instead of returning a cv::Mat I want to pass in a reference to an existing data structure. I am trying to reduce unnecessary copying of cv::Mat Function currently looks like:
cv::Mat HSV::apply(const cv::Mat& input) {
cv::Mat image, output;
cv::cvtColor(input, image, cv::COLOR_BGR2HSV);
cv::inRange(image, m_Low, m_High, output);
return output;
}
I am trying to convert it to -
void HSV::apply(const cv::Mat& input) {
cv::Mat image, output;
cv::cvtColor(input, image, cv::COLOR_BGR2HSV);
cv::inRange(image, m_Low, m_High, output);
}
Now in another node, I used to call this function as follows:
Detection::Detection(ros::NodeHandle& nh)
: m_Filter_(std::make_unique<HSV>()), {
m_ImageSub = nh.subscribe("/image_raw", 10, &Detector::inputCb, this);
}
void Detector::inputCb(const sensor_msgs::Image::ConstPtr& msg) {
cv::Mat image = ImageConverter::convertToCvMat(msg);
cv::Mat finalImage = m_Filter_->apply(image);
}
How can I call the function now if I am passing it by reference?
I don't think your converted function is correct. It should be
That function would be called like this
However please note that named return value optimization (NRVO) means that no copying occurs when you
return output;so this might be an unnecessary change.