Skip to content

Commit ea81ac3

Browse files
committed
Fix image handling
1 parent 8128e77 commit ea81ac3

File tree

4 files changed

+26
-46
lines changed

4 files changed

+26
-46
lines changed

aegis_grpc/include/aegis_grpc/robot_read_service.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ class RobotReadServiceImpl final
106106
void FillProtoImage(
107107
const sensor_msgs::msg::Image& ros,
108108
proto_aegis_grpc::v1::Image* out,
109-
cv::Mat& resize_buffer);
109+
cv::Mat& resize_buffer);
110110

111111
std::shared_ptr<rclcpp::Node> node_;
112112
rclcpp::Subscription<geometry_msgs::msg::PoseStamped>::SharedPtr pose_sub_;

aegis_grpc/proto_aegis_grpc/v1/sensor_msgs.proto

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,6 @@ message JointState {
2525
message Image {
2626
uint32 height = 1;
2727
uint32 width = 2;
28-
enum Encoding {
29-
UNKNOWN = 0;
30-
BGR8 = 1;
31-
BAYER_RGGB8 = 2;
32-
}
33-
Encoding encoding = 3;
34-
uint32 step = 4;
35-
bytes data = 5;
28+
uint32 step = 3;
29+
bytes data = 4;
3630
}

aegis_grpc/python_client/aegis_grpc_client/grpc_client.py

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -219,16 +219,11 @@ def _joints_to_array(self, joints: sensor_msgs_pb2.JointState) -> np.ndarray:
219219
).T
220220

221221
def _image_to_array(self, img: sensor_msgs_pb2.Image) -> np.ndarray:
222-
arr = np.frombuffer(img.data, dtype=np.uint8)
223-
224-
if img.encoding == sensor_msgs_pb2.Image.BGR8:
225-
arr = arr.reshape(img.height, img.width, 3)
226-
elif img.encoding == sensor_msgs_pb2.Image.BAYER_RGGB8:
227-
arr = arr.reshape(img.height, img.width)
228-
else:
229-
raise ValueError(f"Unsupported image encoding: {img.encoding}")
230-
231-
return arr
222+
return np.moveaxis(
223+
np.frombuffer(img.data, dtype=np.uint8).reshape(img.height, img.width, 3),
224+
-1,
225+
0,
226+
)
232227

233228
# ==================== RobotControlService Methods ====================
234229

aegis_grpc/src/robot_read_service.cpp

Lines changed: 18 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ RobotReadServiceImpl::RobotReadServiceImpl(std::shared_ptr<rclcpp::Node> node)
1010
DeclareROSParameter("topic_wrench", std::string("/wrench"), "[str] Init; Sub: topic with the F/T data.");
1111
DeclareROSParameter("topic_joints", std::string("/joint_states"), "[str] Init; Sub: topic with the joint states.");
1212
DeclareROSParameter("topic_camera_scene", std::string("/cam_scene/rgb/image_rect"), "[str] Init; Sub: Camera scene image topic.");
13-
DeclareROSParameter("topic_camera_right", std::string("/cam_tool_right/image_raw"), "[str] Init; Sub: Camera scene image topic.");
14-
DeclareROSParameter("topic_camera_left", std::string("/cam_tool_left/image_raw"), "[str] Init; Sub:Camera scene image topic.");
13+
DeclareROSParameter("topic_camera_right", std::string("/cam_tool_right/image_color"), "[str] Init; Sub: Camera scene image topic.");
14+
DeclareROSParameter("topic_camera_left", std::string("/cam_tool_left/image_color"), "[str] Init; Sub:Camera scene image topic.");
1515
DeclareROSParameter("target_image_width", 64, "[int] Init; Target output image width in pixels.");
1616
DeclareROSParameter("target_image_height", 64, "[int] Init; Target output image height in pixels.");
1717
DeclareROSParameter("enable_image_resize", true, "[bool] Init; Enable resizing images before sending over gRPC.");
@@ -300,39 +300,30 @@ void RobotReadServiceImpl::FillProtoJointState(
300300
proto_aegis_grpc::v1::Image* out,
301301
cv::Mat& buffer)
302302
{
303-
cv_bridge::CvImageConstPtr cv_ptr;
304-
try {
305-
cv_ptr = cv_bridge::toCvCopy(ros, ros.encoding);
306-
} catch (cv_bridge::Exception& e) {
307-
RCLCPP_ERROR(node_->get_logger(), "cv_bridge: %s", e.what());
308-
return;
309-
}
303+
const cv::Mat src(
304+
ros.height,
305+
ros.width,
306+
CV_8UC3,
307+
const_cast<uint8_t*>(ros.data.data()),
308+
ros.step
309+
);
310310

311-
const cv::Mat* src = &cv_ptr->image;
311+
const cv::Mat* img = &src;
312312

313313
if (enable_image_resize_) {
314314
cv::resize(
315-
cv_ptr->image,
315+
src,
316316
buffer,
317317
cv::Size(target_img_width_, target_img_height_),
318-
0, 0, cv::INTER_LINEAR);
319-
src = &buffer;
320-
}
321-
322-
if (ros.encoding == "bayer_rggb8") {
323-
cv::cvtColor(*src, buffer, cv::COLOR_BayerRG2BGR);
324-
src = &buffer;
325-
out->set_encoding(proto_aegis_grpc::v1::Image::BGR8);
326-
} else if (ros.encoding == "bgr8") {
327-
out->set_encoding(proto_aegis_grpc::v1::Image::BGR8);
328-
} else {
329-
out->set_encoding(proto_aegis_grpc::v1::Image::UNKNOWN);
318+
0, 0, cv::INTER_LINEAR
319+
);
320+
img = &buffer;
330321
}
331322

332-
out->set_height(src->rows);
333-
out->set_width(src->cols);
334-
out->set_step(src->step);
335-
out->set_data(src->data, src->total() * src->elemSize());
323+
out->set_height(img->rows);
324+
out->set_width(img->cols);
325+
out->set_step(img->step);
326+
out->set_data(img->data, img->total() * img->elemSize());
336327
}
337328

338329

0 commit comments

Comments
 (0)