AlexeyAB / darknet

YOLOv4 / Scaled-YOLOv4 / YOLO - Neural Networks for Object Detection (Windows and Linux version of Darknet )
http://pjreddie.com/darknet/
Other
21.65k stars 7.96k forks source link

Misleading color conversion after opencv imread #7904

Open StephanHasler opened 3 years ago

StephanHasler commented 3 years ago

https://github.com/AlexeyAB/darknet/blob/1be96802a030ae2216c7e19b7523e1e2f0948aaf/src/image_opencv.cpp#L120

Here a conversion RGB2BGR is done. This means from RGB to BGR. But imread delivers the image as BGR not RGB.

The code still does the correct thing as it just swaps the 1st with the 3rd channel. But using RGB2BGR is misleading. It gives the impression that you have RGB and want BGR. But you have BGR and want RGB, I guess.

Correct would be to use BGR2RGB.

Please check also the other transformations in image_opencv.cpp. I guess, whenever you like to convert from cv to internal, it should be BGR2RGB, e.g. loading. And whenever you like to convert from internal to cv, it should be RGB2BGR, e.g. drawing.

MambaWong commented 3 years ago

https://github.com/opencv/opencv/blob/b61a55eebf1e654106bde3222e926d0e5b8edbca/modules/imgproc/include/opencv2/imgproc.hpp#L544-L547

https://github.com/opencv/opencv/blob/b61a55eebf1e654106bde3222e926d0e5b8edbca/modules/imgproc/src/color.cpp#L193-L199

It's just a sign of whether or not the R/B channels need to be swapped

StephanHasler commented 3 years ago

Yes, as I sad the code is correct, but misleading.

As a user it is sometimes hard to find out whether a deep learning framework uses rgb or bgr internally. Darknet seems to use RGB. But this is hard to find out, if you use a wrong/misleading color conversion constant.

In general, besides being correct, code should reveal the intent of the developer. The contrary is done here.