Welcome to the Chapter 4 supplementary materials! Inside this chapter, you reviewed the building blocks of an image — the pixel.
You also learned how to access individual pixel values by supplying specific (x, y)-coordinates of an image. However, as you may have found out, you need to be careful when supplying these (x, y)-coordinates.
Be careful with coordinate ordering
There are three considerations that you need to keep in mind when using OpenCV, especially if you’re just starting out:
- The Python programming language is zero indexed. This means that we start counting from zero rather than one. If you’re coming from a MATLAB background, this might cause you some confusion.
- The origin (0, 0) of an image starts at the top-left corner of the image and increases as we move to the right and down. For some readers, this origin may seem counterintuitive, so you’ll want to make sure you embed this caveat in your memory now.
- Even though we specify pixels in terms of (x, y)-coordinates, when it comes to writing code, we access the individual pixel values as
image[y, x]
. Why does the y-coordinate come first? Well, keep in mind that an image is defined as a matrix. We define a matrix in terms of number of rows and number of columns. Therefore, to access an individual pixel located at (x, y), we first supply y, the row number, followed by x, the column number — this is by far the most common mistake I see when first getting started with OpenCV, so be sure to keep this in mind.
Why does OpenCV store images in BGR order rather than RGB?
Now that you understand a bit more about coordinate ordering, let’s discuss a different type of ordering — channel ordering.
If you have previous experience in the image processing field or have used photo editing software such as Photoshop or GIMP, you may already know that pixels are normally specified in RGB order: Red, Green, and Blue.
If that’s the case, why does OpenCV store channels in reverse BGR order?
What sense does that make?
Like many long-standing open source projects, the answer might surprise you:
“Because of the Roman horse’s ass!” as stated by Dr. Gary Bradski, founder of OpenCV.
You can read the full story here:
https://www.learnopencv.com/why-does-opencv-use-bgr-color-format/
There are a few technical reasons, but in general, the rationale as to why developers chose the BGR format is because specifying color values in BGR rather than RGB was more popular at the time — that’s all.
Overall, BGR is used for historical reasons and now we have to live with it. It can be a bit frustrating when you’re just getting started, but after developing a few Python + OpenCV scripts of your own, you’ll quickly adjust to it.