• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar
  • Skip to footer

Learn OpenCV

OpenCV, PyTorch, Keras, Tensorflow examples and tutorials

  • Home
  • Getting Started
    • Installation
    • PyTorch
    • Keras & Tensorflow
    • Resource Guide
  • Courses
    • Opencv Courses
    • CV4Faces (Old)
  • Resources
  • AI Consulting
  • About

Why does OpenCV use BGR color format ?

Avatar Satya Mallick
September 27, 2015 17 Comments
Computer Vision Stories

September 27, 2015 By 17 Comments

One of the elements of good design is the principle of least astonishment ( a.k.a principle of least surprise). A good intuitive design makes the user not think.

When you see a handle on a door, you want to pull it. When you see a door with a metal plate, you want to push it. Those are the expectations of a user of the door and when the designer of a door respects these expectations, a user can use the door without exerting any mental energy. But if the design is inconsistent with a user’s expectation, a simple act of opening a door can become a mental burden.

OpenCV is a very useful library, but when it comes to the principle of least surprise, it is an unqualified disaster. When I am writing Computer Vision code in MATLAB, I rarely refer to the documentation. While working with OpenCV, I have the docs permanently open in a browser tab.

OpenCV seems to have been built with the principle of maximum surprise in mind! And it all begins with the default BGR pixel format. It does not play well with libraries that use the standard RGB pixel format. E.g. when you try to display an image using matplotlib after reading it using imread in OpenCV, the result looks funny.

“Why not use RGB when everybody else and their aunts use RGB?”

This is the question I asked Dr. Gary Bradski, founder of OpenCV, when I met him at a conference a few months back, although I left out the part about everybody and their aunts using RGB. He answered my question with another question.

“Why is the the US standard railroad gauge 4 feet, 8.5 inches?”

The answer to that question of course is “Because of Roman horse’s ass!”

For people who do not know this not-so-true story, I have copied the following from snopes

The US standard railroad gauge (width between the two rails) is 4 feet, 8.5 inches. That’s an exceedingly odd number. Why was that gauge used?

Because that’s the way they built them in England, and the US railroads were built by English expatriates.

Why did the English build them like that? Because the first rail lines were built by the same people who built the pre-railroad tramways, and that’s the gauge they used.

Why did “they” use that gauge then? Because the people who built the tramways used the same jigs and tools that they used for building wagons which used that wheel spacing.

Okay! Why did the wagons have that particular odd wheel spacing? Well, if they tried to use any other spacing, the wagon wheels would break on some of the old, long distance roads in England, because that’s the spacing of the wheel ruts.

So who built those old rutted roads? The first long distance roads in Europe (and England) were built by Imperial Rome for their legions. The roads have been used ever since. And the ruts in the roads? Roman war chariots first formed the initial ruts, which everyone else had to match for fear of destroying their wagon wheels. Since the chariots were made for (or by) Imperial Rome, they were all alike in the matter of wheel spacing.

The United States standard railroad gauge of 4 feet, 8.5 inches derives from the original specification for an Imperial Roman war chariot. Specifications and bureaucracies live forever. So the next time you are handed a specification and wonder what horse’s ass came up with it, you may be exactly right, because the Imperial Roman war chariots were made just wide enough to accommodate the back ends of two war horses. Thus, we have the answer to the original question.

Now the twist to the story . . .

There’s an interesting extension to the story about railroad gauges and horses’ behinds. When we see a Space Shuttle sitting on its launch pad, there are two big booster rockets attached to the sides of the main fuel tank. These are solid rocket boosters, or SRBs. The SRBs are made by Thiokol at their factory in Utah. The engineers who designed the SRBs might have preferred to make them a bit fatter, but the SRBs had to be shipped by train from the factory to the launch site. The railroad line from the factory had to run through a tunnel in the mountains. The SRBs had to fit through that tunnel. The tunnel is slightly wider than the railroad track, and the railroad track is about as wide as two horses’ behinds.

So, the major design feature of what is arguably the world’s most advanced transportation system was determined over two thousand years ago by the width of a Horse’s Ass!

Why did they choose BGR color space in OpenCV ?

The reason the early developers at OpenCV chose BGR color format is that back then BGR color format was popular among camera manufacturers and software providers. E.g. in Windows, when specifying color value using COLORREF they use the BGR format 0x00bbggrr.

BGR was a choice made for historical reasons and now we have to live with it. In other words, BGR is the horse’s ass in OpenCV.

Subscribe

If you liked this article, please subscribe to our newsletter and receive a free
Computer Vision Resource guide. In our newsletter we share OpenCV tutorials and examples written in C++/Python, and Computer Vision and Machine Learning algorithms and news.

Subscribe Now

Tags: BGR

Filed Under: Computer Vision Stories

About

AvatarI am an entrepreneur with a love for Computer Vision and Machine Learning with a dozen years of experience (and a Ph.D.) in the field.

In 2007, right after finishing my Ph.D., I co-founded TAAZ Inc. with my advisor Dr. David Kriegman and Kevin Barnes. The scalability, and robustness of our computer vision and machine learning algorithms have been put to rigorous test by more than 100M users who have tried our products. Read More…

Getting Started

  • Installation
  • PyTorch
  • Keras & Tensorflow
  • Resource Guide

Resources

Download Code (C++ / Python)

ENROLL IN OFFICIAL OPENCV COURSES

I've partnered with OpenCV.org to bring you official courses in Computer Vision, Machine Learning, and AI.
Learn More

Recent Posts

  • How to use OpenCV DNN Module with Nvidia GPU on Windows
  • How to use OpenCV DNN Module with NVIDIA GPUs
  • Code OpenCV in Visual Studio
  • Install OpenCV on Windows – C++ / Python
  • Face Recognition with ArcFace

Disclaimer

All views expressed on this site are my own and do not represent the opinions of OpenCV.org or any entity whatsoever with which I have been, am now, or will be affiliated.

GETTING STARTED

  • Installation
  • PyTorch
  • Keras & Tensorflow
  • Resource Guide

COURSES

  • Opencv Courses
  • CV4Faces (Old)

COPYRIGHT © 2021 - BIG VISION LLC

Privacy Policy | Terms & Conditions

We use cookies to ensure that we give you the best experience on our website. If you continue to use this site we will assume that you are happy with it. Privacy policyAccept