Why does OpenCV use BGR color format ?【转】
转自:http://www.learnopencv.com/why-does-opencv-use-bgr-color-format/
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
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.
Filed Under: Computer Vision Stories Tagged With: BGR
-
Quite funny. Thanks for the post.
- autocyz
Actually,I never thought about it. Thanks for your story !
- chebhou
funny and informative , thanks
-
Having
worked at Thiokol, I can tell you that they tell the story about the
size of the boosters regularly and with some serious gusto.-
That’s funny!
-
- Pal Szasz
Isn’t 0x00bbggrr actually RGB on little-endian machines?
- leolodreamland
i just wasted ten minutes of my life to not get any kind of answer…
- bafu
Interesting and useful information, thank you for sharing!
- yalaudah
Haha finally understood the mystery behind this seemingly arbitrary choice. Thanks for the post!
- RockSteady
What a waste of resources, every time you need to call cvtColor RGB2BGR to use imshow.
- Govind Singh
LOL
Why does OpenCV use BGR color format ?【转】的更多相关文章
- OpenCV使用BGR而非RGB格式
日常开发和交流时,我们习惯将图片的颜色通道按照RGB(red, green, blue)进行排列.闲来无聊,在把玩卷积神经网络模型时发现OpenCV并没有按照这个顺序读入图片.好奇地搜索一下OpenC ...
- opencv颜色提取color filting
# -*- coding: utf-8 -* import numpy as np import cv2 cap = cv2.VideoCapture(0) while True: _ , frame ...
- opencv-4-成像系统与Mat图像颜色空间
opencv-4-成像系统与Mat图像颜色空间 opencvc++qtmat 目标 知道 opencv 处理图像数据的格式 介绍 mat 基础内容 知道 BGR 颜色 显示 颜色转换 BGR 到 灰度 ...
- WPF实现背景透明磨砂,并通过HandyControl组件实现弹出等待框
前言:上一个版本的Winform需要改成WPF来做界面,第一次接触WPF,在转换过程中遇到的需求就是一个背景透明模糊,一个是类似于 加载中…… 这样的等待窗口,等后台执行完毕后再关掉.在Winform ...
- 快速 图片颜色转换迁移 Color Transfer Opencv + Python
Super fast color transfer between images About a month ago, I spent a morning down at the beach, w ...
- 快速遍历OpenCV Mat图像数据的多种方法和性能分析 | opencv mat for loop
本文首发于个人博客https://kezunlin.me/post/61d55ab4/,欢迎阅读! opencv mat for loop Series Part 1: compile opencv ...
- opencv Mat基础
Mat Mat由两部分构成 matrix header pointer to the matrix containing the pixel values Mat is basically a cla ...
- 【OpenCV开发】imread和imwrite的类型以及第三个参数关于图片压缩质量等
本片参考博客:http://blog.csdn.net/poem_qianmo/article/details/20537737 基于OpenCV3.0,与原博客有出入. 在OpenCV1.0时代,基 ...
- imread opencv
''' Mat cv::imread ( const String & filename, int flags = IMREAD_COLOR ) Python: retval = cv.imr ...
随机推荐
- Delphi下使用指针的简单总结
由于最近公司太忙,好久没有更新我的BLOG了.原来想着写写关于HOOK驱动的文章,可是最后想想好久已经没有做驱动的东西了,怕写出来有错误,于是作罢.开发游戏也有一段时间了,发现使用DELPHI来开发网 ...
- HDU4753 Fishhead’s Little Game——2013 ACM/ICPC Asia Regional Nanjing Online
今天比赛又是做得好水的.被狂虐啊. 比赛两个多小时一直没出题,遒遒最先交的若干发都wa了.T_T 我独自在一遍苦思了1006这个题,还好最后把这个题目A掉了,不然又是深坑队友. 题目的意思我就不多说了 ...
- HDU5266-pog loves szh III
题目 给出一棵\(n\)个点的树,从1到\(n\)编号,\(m\)次询问\({LCA} _{v\in[L,R]}\). \(n,m\le 3\times 10^5\) 分析 我的做法是直接对LCA进 ...
- 已知UIScrollView放大后的Frame和放大之前的Frame计算放大的瞄点坐标
有时候在缩放后,需要知道该次缩放是在哪个坐标开始缩放的.如上篇已知缩放的点,然后在该点对其缩放.本篇其实是逆运算 (x,y)就是当初在该点进行缩放 化简之后很简单,代码如下: func getZoom ...
- Android性能优化:布局优化 详细解析(含<include>、<ViewStub>、<merge>讲解 )
1. 影响的性能 布局性能的好坏 主要影响 :Android应用中的页面显示速度 2. 如何影响性能 布局影响Android性能的实质:页面的测量 & 绘制时间 1个页面通过递归 完成测量 & ...
- BZOJ1997 [Hnoi2010]Planar 【2-sat】
题目链接 BZOJ1997 题解 显然相交的两条边不能同时在圆的一侧,\(2-sat\)判一下就好了 但这样边数是\(O(m^2)\)的,无法通过此题 但是\(n\)很小,平面图 边数上界为\(3n ...
- redis的自带VM(虚拟内存)
Redis支持采用VM技术,以达到当数据超过设置的可使用的物理内存的时候能够正常运行.当数据超过物理内存的时候,把一部分数据写入磁盘中的一块空间来代替物理内存. vm-enabled no ...
- 利用MailSniper越权访问Exchange邮箱
0x01 概述 Microsoft Exchange用户可以授权给其他用户对其邮箱文件夹进行各种级别的访问.例如,用户可以授予其他用户读取访问其收件箱中里面的电子邮件,但是要是用户(或Exchange ...
- HAOI2017游记
HACF的最终成绩已经出炉,但是事情还没有结束. 好多想说的,不知道从何说起,就按照时间顺序说吧. 考前 考前大概一周半就开始复习了,一些比较重要的算法,比如KDT,单纯性,线性基等等没有再继续学,所 ...
- 1.UiDevice API 详细介绍
1.UiDevice按键与keycode使用 返回值 方法名 说明 boolean pressBack() 模拟短按返回back键 boolean pressDPadCenter() 模拟按轨迹球中点 ...