timus 1008. Image encoding

本文介绍了一种处理黑白图像的算法,该算法能够实现两种不同表示方式之间的双向转换。通过使用BFS(宽度优先搜索)算法,实现了从一组像素坐标到路径指令序列的转换,反之亦然。代码中详细展示了如何利用队列和向量来组织和处理图像数据。

题目大意是一个黑白图像有两种表示方式,给出一种方式,你要输出另一种方式

 

解题报告:

1) 注意是双向转换.所以在输入时必须判断是哪一种情况. 可以应用cin>>first,然后cin.get()!='/n'来判断是不是第一种情况

2) 是一个BFS.

 

#!/usr/bin/env python3 # -*- coding: utf-8 -*- import cv2 import torch import rospy import numpy as np from ultralytics import YOLO from time import time from std_msgs.msg import Header from sensor_msgs.msg import Image from yolov8_ros_msgs.msg import BoundingBox, BoundingBoxes class Yolo_Dect: def __init__(self): # load parameters weight_path = rospy.get_param('~weight_path', '') image_topic = rospy.get_param( '~image_topic', '/d435/color/image_raw') pub_topic = rospy.get_param('~pub_topic', '/yolov8/BoundingBoxes') self.camera_frame = rospy.get_param('~camera_frame', '') conf = rospy.get_param('~conf', '0.5') self.visualize = rospy.get_param('~visualize', 'True') # which device will be used if (rospy.get_param('/use_cpu', 'false')): self.device = 'cpu' else: self.device = 'cuda' self.model = YOLO(weight_path) self.model.fuse() self.model.conf = conf self.color_image = Image() self.getImageStatus = False # Load class color self.classes_colors = {} # image subscribe self.color_sub = rospy.Subscriber(image_topic, Image, self.image_callback, queue_size=1, buff_size=52428800) # output publishers self.position_pub = rospy.Publisher( pub_topic, BoundingBoxes, queue_size=1) self.image_pub = rospy.Publisher( '/yolov8/detection_image', Image, queue_size=1) # if no image messages while (not self.getImageStatus): rospy.loginfo("waiting for image.") rospy.sleep(2) def image_callback(self, image): self.boundingBoxes = BoundingBoxes() self.boundingBoxes.header = image.header self.boundingBoxes.image_header = image.header self.getImageStatus = True self.color_image = np.frombuffer(image.data, dtype=np.uint8).reshape( image.height, image.width, -1) self.color_image = cv2.cvtColor(self.color_image, cv2.COLOR_BGR2RGB) results = self.model(self.color_image, show=False, conf=0.3) self.dectshow(results, image.height, image.width) cv2.waitKey(3) def dectshow(self, results, height, width): self.frame = results[0].plot() print(str(results[0].speed['inference'])) fps = 1000.0/ results[0].speed['inference'] cv2.putText(self.frame, f'FPS: {int(fps)}', (20,50), cv2.FONT_HERSHEY_SIMPLEX, 0.6, (0, 255, 0), 2, cv2.LINE_AA) for result in results[0].boxes: boundingBox = BoundingBox() boundingBox.xmin = np.int64(result.xyxy[0][0].item()) boundingBox.ymin = np.int64(result.xyxy[0][1].item()) boundingBox.xmax = np.int64(result.xyxy[0][2].item()) boundingBox.ymax = np.int64(result.xyxy[0][3].item()) boundingBox.Class = results[0].names[result.cls.item()] boundingBox.probability = result.conf.item() self.boundingBoxes.bounding_boxes.append(boundingBox) self.position_pub.publish(self.boundingBoxes) self.publish_image(self.frame, height, width) if self.visualize : cv2.imshow('YOLOv8', self.frame) def publish_image(self, imgdata, height, width): image_temp = Image() header = Header(stamp=rospy.Time.now()) header.frame_id = self.camera_frame image_temp.height = height image_temp.width = width image_temp.encoding = 'bgr8' image_temp.data = np.array(imgdata).tobytes() image_temp.header = header image_temp.step = width * 3 self.image_pub.publish(image_temp) def main(): rospy.init_node('yolov8_ros', anonymous=True) yolo_dect = Yolo_Dect() rospy.spin() if __name__ == "__main__": main() 减小输出图像尺寸
12-10
ros2 run learning_service service_object_server [INFO] [1752123589.000416759] [service_object_server]: Receiving video frame Traceback (most recent call last): File "/opt/ros/galactic/lib/python3.8/site-packages/cv_bridge/core.py", line 99, in encoding_to_cvtype2 return getCvType(encoding) RuntimeError: Unrecognized image encoding [yuv422_yuy2] During handling of the above exception, another exception occurred: Traceback (most recent call last): File "/home/wheeltec/dev_ws1/install/learning_service/lib/learning_service/service_object_server", line 33, in <module> sys.exit(load_entry_point('learning-service==0.0.0', 'console_scripts', 'service_object_server')()) File "/home/wheeltec/dev_ws1/install/learning_service/lib/python3.8/site-packages/learning_service/service_object_server.py", line 75, in main rclpy.spin(node) # 循环等待ROS2退出 File "/opt/ros/galactic/lib/python3.8/site-packages/rclpy/__init__.py", line 196, in spin executor.spin_once() File "/opt/ros/galactic/lib/python3.8/site-packages/rclpy/executors.py", line 712, in spin_once raise handler.exception() File "/opt/ros/galactic/lib/python3.8/site-packages/rclpy/task.py", line 239, in __call__ self._handler.send(None) File "/opt/ros/galactic/lib/python3.8/site-packages/rclpy/executors.py", line 418, in handler await call_coroutine(entity, arg) File "/opt/ros/galactic/lib/python3.8/site-packages/rclpy/executors.py", line 343, in _execute_subscription await await_or_execute(sub.callback, msg) File "/opt/ros/galactic/lib/python3.8/site-packages/rclpy/executors.py", line 107, in await_or_execute return callback(*args) File "/home/wheeltec/dev_ws1/install/learning_service/lib/python3.8/site-packages/learning_service/service_object_server.py", line 56, in listener_callback image = self.cv_bridge.imgmsg_to_cv2(data, 'bgr8') # 将ROS的图像消息转化成OpenCV图像 File "/opt/ros/galactic/lib/python3.8/site-packages/cv_bridge/core.py", line 169, in imgmsg_to_cv2 dtype, n_channels = self.encoding_to_dtype_with_channels(img_msg.encoding) File "/opt/ros/galactic/lib/python3.8/site-packages/cv_bridge/core.py", line 104, in encoding_to_dtype_with_channels return self.cvtype2_to_dtype_with_channels(self.encoding_to_cvtype2(encoding)) File "/opt/ros/galactic/lib/python3.8/site-packages/cv_bridge/core.py", line 101, in encoding_to_cvtype2 raise CvBridgeError(e) cv_bridge.core.CvBridgeError: Unrecognized image encoding [yuv422_yuy2]
07-11
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值