Processing time slow when multiple callback
I am running Ubuntu 18.04.
I have ros cpp, AsynchSpinner with multiple callback function. All callback doing same thing for different image topic, dublicate callbacks. In callback function, i am doing image preprocess operation using OpenCV. When i have only one calback function, preprocess operation took ~4 ms. But when i used multiple callback function, preprocessing took ~6.5 ms for each image. What causing this situation ? How can i fix?
I expected to callbacks function work concurently.
My main function, i am define ros::AsyncSpinner with 5 thread.
Detect vision_detect(nh);
ros::AsyncSpinner spinner(5);
spinner.start();
ros::waitForShutdown()
Then, in constructer of Detect, subscribe 2 callback function;
sub_img0_ = it_.subscribe(topic_sub_image0_, sub_queue_size_,
&Detect::CameraCallback0, this);
sub_img1_ = it_.subscribe(topic_sub_image1_, sub_queue_size_,
&Detect::CameraCallback1, this);
CameraCallback0 and CameraCallback1 doing same thing, not sharing any variable.
void Detect::CameraCallback0(const sensor_msgs::Image::ConstPtr &msg_image_in) {
...
auto t_start = std::chrono::high_resolution_clock::now();
std::vector<float> curInput = PrepareImage(img_orig);
auto t_end = std::chrono::high_resolution_clock::now();
float total = std::chrono::duration<float, std::milli>(t_end - t_start).count();
std::cout << "Time taken for preprocess is " << total << " ms." << std::endl;
....
}
And here is my PrepareImage function, if i am subscribe only one callback function, PrepareImage function took ~4.5 ms, but when i am used, pasted code, PrepareImage took ~6.5 ms.
Here is prepare image function;
std::vector<float> Detect::PrepareImage(const cv::Mat &img) const {
float scale = std::min(float(w) / img.cols, float(h) / img.rows);
auto scaleSize = cv::Size(img.cols * scale, img.rows * scale);
cv::Mat rgb;
cv::cvtColor(img, rgb, CV_BGR2RGB);
cv::Mat resized;
cv::resize(rgb, resized, scaleSize, 0, 0, cv::INTER_CUBIC);
cv::Mat cropped(h, w, CV_8UC3, 127);
cv::Rect rect((w - scaleSize.width) / 2, (h - scaleSize.height) / 2,
scaleSize.width, scaleSize.height);
resized.copyTo(cropped(rect));
cv::Mat img_float;
if (c == 3)
cropped.convertTo(img_float, CV_32FC3, 1 / 255.0);
else
cropped.convertTo(img_float, CV_32FC1, 1 / 255.0);
std::vector<cv::Mat> input_channels(c);
cv::split(img_float, input_channels);
std::vector<float> result(h * w * c);
auto data = result.data();
int channelLength = h * w;
for (int i = 0; i < c; ++i) {
std::memcpy(data, input_channels[i].data, channelLength * sizeof(float));
data += channelLength;
}
return result;
}
Please post your code. We cannot guess at what might be the issue.
Sorry, updated my question.
what is
it_
? I suspect its an image transport object, but which one? Have you checked inside that if there's something going on that takes some cycles?Thank you for advice, yes, it_ is image_transport::ImageTransport object. I checked line by line preprocessing function, OpenCV .convertTo() and cv::split function not working stable when used multiple callback.
You'll need to profile the code to figure it out.