Shua-Kang / ros_pytorch_yolov5

23 stars 8 forks source link

Color problem of detection results #12

Closed 646145192 closed 2 years ago

646145192 commented 2 years ago

Hi! When I run your program, the wrong color appears. Do you know the reason? The rgb information of the camera looks correct in rviz, but the color is reversed in yolo. I am using a realsense D455 camera. Looking forward to your reply! 2022-06-20 17-19-44 的屏幕截图

Shua-Kang commented 2 years ago

It is because the detect.py thinks the image received is BGR format. So, at detect.py line 264

img = img[:, :, ::-1].transpose(2, 0, 1)  # BGR to RGB, to 3x416x416

it will convert the image to RGB format for inference.

And when publishing, it will publish BGR version.(line 349)

img_msg = self.bridge.cv2_to_imgmsg(im0, encoding="bgr8")

Try to modify line 264 to img = img[:, :, ::-1] img.transpose(2, 0, 1) and modify line 349 to "rgb8" and I think you can get the right color.

646145192 commented 2 years ago

I tried your method but the following problem occurred 2022-06-20 21-20-09 的屏幕截图 If I change line 264 img = img.transpose(2, 0, 1) and change line 349 img_msg = self.bridge.cv2_to_imgmsg(im0, encoding="rgb8") It works fine but the color issue persists

Shua-Kang commented 2 years ago

line 264 should be

img = img[:, :, ::-1]

.transpose(2,0,1) is used for covert bgr to rgb.

646145192 commented 2 years ago

if i use img = img[:, :, ::-1] as line 264 error persists image It seems that the image is not the right size without transpose

Shua-Kang commented 2 years ago

You are right and I made a mistake. [:, :, ::-1] is used for covert bgr to rgb and transpose is for the right shape. Add code before line 349 to see if rviz have any changes.

im0 = im0[:, :, ::-1]
img_msg = self.bridge.cv2_to_imgmsg(im0, encoding="rgb8")
646145192 commented 2 years ago

it's still the wrong color... whether i use img = img[:, :, ::-1].transpose(2, 0, 1) or img = img.transpose(2, 0, 1) in line 264 and I have add im0 = im0[:, :, ::-1]before 349

Shua-Kang commented 2 years ago

This is too weird. Did the topic you watch is /detections_image ?

646145192 commented 2 years ago

/detections_image_topic

646145192 commented 2 years ago

not exactly the same as yours

Shua-Kang commented 2 years ago

:joy: I will have a test tomorrow.

646145192 commented 2 years ago

ok thank you very much

Shua-Kang commented 2 years ago

I have updated the repo. I think it is right now. Just have a try Snipaste_2022-06-20_23-35-29 !

646145192 commented 2 years ago

Now the color is normal,thank you very much!