Skip to content

Commit

Permalink
Merge pull request #54 from rogy-AquaLab/compressed_image
Browse files Browse the repository at this point in the history
画像を圧縮して送受信
  • Loading branch information
H1rono authored Aug 4, 2024
2 parents 67dba9a + 648e384 commit 4ff44cb
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 8 deletions.
17 changes: 12 additions & 5 deletions app/imshow/imshow/imshow.py
Original file line number Diff line number Diff line change
@@ -1,25 +1,32 @@
import cv2
import numpy as np
import rclpy
from cv_bridge import CvBridge
from rclpy.node import Node
from sensor_msgs.msg import Image
from sensor_msgs.msg import CompressedImage


class Imshow(Node):
def __init__(self):
super().__init__("imshow")
self.subscription = self.create_subscription(
Image,
CompressedImage,
"camera_image",
self.listener_callback,
10,
)
self.bridge = CvBridge()

def listener_callback(self, msg):
frame = self.bridge.imgmsg_to_cv2(msg, "bgr8")
cv2.imshow("Camera", frame)
cv2.waitKey(1)
# CompressedImageデータをNumPy配列に変換
np_arr = np.frombuffer(msg.data, np.uint8)
# NumPy配列から画像をデコード
frame = cv2.imdecode(np_arr, cv2.IMREAD_COLOR)
if frame is not None:
cv2.imshow("Camera", frame)
cv2.waitKey(1)
else:
self.get_logger().error("Failed to decode image")


def main(args=None):
Expand Down
7 changes: 4 additions & 3 deletions device/camera_reader/camera_reader/camera.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@
import rclpy
from cv_bridge import CvBridge
from rclpy.node import Node
from sensor_msgs.msg import Image
from sensor_msgs.msg import CompressedImage
from std_msgs.msg import Header


class Camera(Node):
def __init__(self):
super().__init__("camera")
self.publisher_ = self.create_publisher(Image, "camera_image", 10)
self.publisher_ = self.create_publisher(CompressedImage, "camera_image", 10)
self.timer = self.create_timer(0.01, self.timer_callback)
param = self.declare_parameter("camera_id", 0)
camera_id = (
Expand All @@ -32,7 +32,8 @@ def _generate_header(self) -> Header:
def timer_callback(self):
ret, frame = self.cap.read()
if ret:
msg = self.bridge.cv2_to_imgmsg(frame, "bgr8", header=self._generate_header())
msg = self.bridge.cv2_to_compressed_imgmsg(frame)
msg.header = self._generate_header()
self.publisher_.publish(msg)
else:
self.get_logger().error("Failed to capture image")
Expand Down

0 comments on commit 4ff44cb

Please sign in to comment.