- Practical Computer Vision
- Abhinav Dadhich
- 366字
- 2021-06-30 18:54:42
Image color conversions
An image is made up pixels and is usually visualized according to the value stored. There is also an additional property that makes different kinds of image. Each of the value stored in a pixel is linked to a fixed representation. For example, a pixel value of ten can represent gray intensity value ten or blue color intensity value 10 and so on. It is therefore important to understand different color types and their conversion. In this section, we will see color types and conversions using OpenCV:
- Grayscale: This is a simple one channel image with values ranging from 0 to 255 that represent the intensity of pixels. The previous image can be converted to grayscale, as follows:
import cv2
# loads and read an image from path to file
img = cv2.imread('../figures/flower.png')
# convert the color to grayscale
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
# displays previous image
cv2.imshow("Image",gray)
# keeps the window open until a key is pressed
cv2.waitKey(0)
# clears all window buffers
cv2.destroyAllWindows()
The resulting image is as shown in the following screenshot:

- HSV and HLS: These are another representation of color representing H is hue, S is saturation, V is value, and L is lightness. These are motivated by the human perception system. An example of image conversion for these is as follows:
# convert the color to hsv
hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
# convert the color to hls
hls = cv2.cvtColor(img, cv2.COLOR_BGR2HLS)
This conversion is as shown in the following figure, where an input image read in BGR format is converted to each of the HLS (on left) and HSV (on right) color types:

- LAB color space: Denoted L for lightness, A for green-red colors, and B for blue-yellow colors, this consists of all perceivable colors. This is used to convert between one type of color space (for example, RGB) to others (such as CMYK) because of its device independence properties. On devices where the format is different to that of the image that is sent, the incoming image color space is first converted to LAB and then to the corresponding space available on the device. The output of converting an RGB image is as follows:

推薦閱讀
- Go Machine Learning Projects
- Hands-On Artificial Intelligence on Amazon Web Services
- Windows 7寶典
- SAP Business Intelligence Quick Start Guide
- 計算機組網技術
- Mastering GitLab 12
- 單片機技能與實訓
- Photoshop CS5圖像處理入門、進階與提高
- 網絡脆弱性掃描產品原理及應用
- Spark大數據商業實戰三部曲:內核解密|商業案例|性能調優
- Mastering OpenStack(Second Edition)
- Learning Cassandra for Administrators
- 穿越計算機的迷霧
- QTP自動化測試實踐
- 網頁設計與制作