I have no idea why publish WebCam in delayed image
i'm work in ubuntu 20.04, ROS2 Foxy, C++ enviroment. what i'm trying to do is try to publish webcam and subscride it. problem is i succesfully publish webcam but not in realtime. image is publish every 2 seconds. i have no idea why this happen i'm post my publish, subscribe code right here.
this is publish node
#include <chrono>
#include <memory>
#include "cv_bridge/cv_bridge.h"
#include "rclcpp/rclcpp.hpp"
#include "sensor_msgs/msg/image.hpp"
#include "std_msgs/msg/header.hpp"
#include <opencv2/opencv.hpp>
#include <stdio.h>
#include <opencv2/core/types.hpp>
#include <opencv2/core/hal/interface.h>
#include <image_transport/image_transport.hpp>
#include <opencv2/imgproc/imgproc.hpp>
using namespace std::chrono_literals;
using namespace cv;
class MinimalPublisher : public rclcpp::Node {
public:
MinimalPublisher() : Node("minimal_publisher"), count_(0) {
auto qos_profile = rclcpp::QoS(rclcpp::KeepLast(10));
publisher_ = this->create_publisher<sensor_msgs::msg::Image>("topic", qos_profile);
timer_ = this->create_wall_timer(10ms, std::bind(&MinimalPublisher::timer_callback, this));
}
private:
void timer_callback() {
cv_bridge::CvImagePtr cv_ptr;
cv::VideoCapture cap(0);
cv::Mat img(cv::Size(1280, 720), CV_8UC3);
cap >> img;
sensor_msgs::msg::Image::SharedPtr msg = cv_bridge::CvImage(std_msgs::msg::Header(), "bgr8", img).toImageMsg();
publisher_->publish(*msg);
RCLCPP_INFO(this->get_logger(), "publishing");
}
rclcpp::TimerBase::SharedPtr timer_;
rclcpp::Publisher<sensor_msgs::msg::Image>::SharedPtr publisher_;
size_t count_;
};
int main(int argc, char *argv[]) {
printf("Starting...");
rclcpp::init(argc, argv);
rclcpp::spin(std::make_shared<MinimalPublisher>());
rclcpp::shutdown();
return 0;
}
this is subscriber node
#include <chrono>
#include <memory>
#include "cv_bridge/cv_bridge.h"
#include "rclcpp/rclcpp.hpp"
#include "sensor_msgs/msg/image.hpp"
#include "std_msgs/msg/header.hpp"
#include <opencv2/opencv.hpp>
#include <opencv2/highgui.hpp>
#include <stdio.h>
#include <opencv2/core/types.hpp>
#include <opencv2/core/hal/interface.h>
#include <image_transport/image_transport.hpp>
#include <opencv2/imgproc/imgproc.hpp>
using std::placeholders::_1;
class MinimalSubscriber : public rclcpp::Node {
public:
MinimalSubscriber()
: Node("minimal_subscriber")
{
auto qos_profile = rclcpp::QoS(rclcpp::KeepLast(10));
subscription_ = this->create_subscription<sensor_msgs::msg::Image>(
"topic", qos_profile, std::bind(&MinimalSubscriber::topic_callback, this, _1));
}
private :
void topic_callback(sensor_msgs::msg::Image::SharedPtr msg) const
{
RCLCPP_INFO(this->get_logger(), "In callback");
cv_bridge::CvImagePtr cv_ptr;
cv_ptr = cv_bridge::toCvCopy(msg,"bgr8");
cv::imshow("minimal_subscriber", cv_ptr->image);
cv::waitKey(1);
}
rclcpp::Subscription<sensor_msgs::msg::Image>::SharedPtr subscription_;
};
int main(int argc, char *argv[])
{
rclcpp::init(argc, argv);
rclcpp::spin(std::make_shared<MinimalSubscriber>());
rclcpp::shutdown();
return 0;
}