"Digital Image Processing-OpenCV/Python" serial (16) Color space conversion of images
This book’s JD discount purchase link: https://item.jd.com/14098452.html
This book’s CSDN exclusive serial column: https://blog.csdn.net /youcans/category_12418787.html
Chapter 3 Color Space Conversion of Images
The images we encounter in our daily lives are usually in color. In digital image processing, color images have many different representation, storage and processing methods for different needs.
Summary of this chapter
- Introduce commonly used color spaces and learn the conversion and processing methods of color images.
- Understand the brightness, saturation and contrast of an image by adjusting color balance.
- Introduce pseudo-color images and learn methods to extend grayscale images and multi-modal data into color images.
3.1 Color space conversion of images
3.1.1 Image color space
Color space refers to a model system that uses multiple color components to form a coordinate system to represent various colors.
Color images can be mapped to a color space as needed for description. In different industrial environments or machine vision applications, different color spaces are used.
The RGB model is an additive color system derived from the three primary colors of red, green, and blue. It is used in cathode ray tube (CRT) monitors, digital scanners, digital cameras, and display devices. It is one of the most widely used color models. . In addition, digital media art usually uses HSV color space, while machine vision and image processing use HSI and HSL color spaces extensively.
3.1.2 Image color space conversion
The function cv.cvtColor is used to convert an image from one color space to another.
function prototype
cv.cvtColor(src, code [, dst, dstCn]) → dst
The function cv.cvtColor can convert the color channel order of a color image, convert a color image to a grayscale image, or convert an image between RGB space and other color spaces.
Parameter Description
- src: input image, which is a multi-dimensional Numpy array, the data type is CV_8U, CV_16U or CV_32F.
- code: color space conversion code, see ColorConversionCodes in the official documentation for details.
- COLOR_BGR2RGB: Convert BGR channels to RGB sequentially.
- COLOR_BGR2GRAY: Convert BGR color image to grayscale image.
- COLOR_BGR2HSV: Convert BGR image to HSV image.
- dst: Output image with the same size and depth as src.
- dstCn: The number of channels of the output image. The default value is 0, which means automatic calculation.
Attention issues
(1) When OpenCV uses the RGB model to represent color images, it uses the BGR format and stores it as a multi-dimensional array in the order of B/G/R. Libraries such as PIL, PyQt, and Matplotlib use RGB format.
(2) Grayscale images are single-channel images, which are two-dimensional Numpy arrays in OpenCV and Matplotlib.
(3) The range of pixel values in the image is determined by the bit depth of the pixel. A commonly used image and video format is an 8-bit unsigned integer (CV_8U), with a value ranging from 0 to 255.
(4) Image format conversion is usually a linear transformation, and the bit depth of the pixel will not affect the transformation result; however, when performing nonlinear calculations or transformations, the input image needs to be normalized to an appropriate The correct result can be obtained by the value range. For example, CV_8U may lose some information due to low data accuracy. Using CV_16U or CV_32F can solve this problem.
(5) When converting an image from a grayscale image to an RGB image, the conversion rule is: R=G=B=gray.
(6) The function cv.cvtColor can provide more than 150 conversion types, which can be queried through the following program.
print([i for i in dir(cv) if i.startswith(‘COLOR_’)])
[Routine 0301] Image color space conversion
This routine contains several commonly used image color space conversions.
# 【0301】图像的颜色空间转换
import cv2 as cv
from matplotlib import pyplot as plt
if __name__ == '__main__':
# 读取原始图像
imgBGR = cv.imread("../images/Lena.tif", flags=1) # 读取为彩色图像
imgRGB = cv.cvtColor(imgBGR, cv.COLOR_BGR2RGB) # BGR 彩色图像转换为 RGB图像
imgGRAY = cv.cvtColor(imgBGR, cv.COLOR_BGR2GRAY) # BGR 彩色图像转灰度图像
imgHSV = cv.cvtColor(imgBGR, cv.COLOR_BGR2HSV) # BGR 彩色图像转 HSV 图像
imgYCrCb = cv.cvtColor(imgBGR, cv.COLOR_BGR2YCrCb) # BGR 彩色图像转 YCrCb图像
imgHLS = cv.cvtColor(imgBGR, cv.COLOR_BGR2HLS) # BGR 彩色图像转 HLS 图像
imgXYZ = cv.cvtColor(imgBGR, cv.COLOR_BGR2XYZ) # BGR 彩色图像转 XYZ 图像
imgLAB = cv.cvtColor(imgBGR, cv.COLOR_BGR2LAB) # BGR 彩色图像转 LAB 图像
imgYUV = cv.cvtColor(imgBGR, cv.COLOR_BGR2YUV) # BGR 彩色图像转 YUV 图像
# 调用Matplotlib显示处理结果
titles = ['BGR', 'RGB', 'GRAY', 'HSV', 'YCrCb', 'HLS', 'XYZ', 'LAB', 'YUV']
images = [imgBGR, imgRGB, imgGRAY, imgHSV, imgYCrCb,
imgHLS, imgXYZ, imgLAB, imgYUV]
plt.figure(figsize=(10, 8))
for i in range(9):
plt.subplot(3, 3, i+1), plt.imshow(images[i], 'gray')
plt.title("{}. {}".format(i+1, titles[i]))
plt.xticks([]), plt.yticks([])
plt.tight_layout()
plt.show()
Running results:
Using Matplotlib to display images in different color spaces is shown in Figure 3-1. Note that the image display of the routine is not the real color effect of different color spaces, but the result of the Numpy matrix obtained after the image is converted to color space and displayed in RGB format using Matplotlib.
Figure 3-1 Using Matplotlib to display images in different color spaces
JD discount purchase link for this book: https://item.jd.com/14098452.html
Copyright statement:
youcans@xupt original work, the original link must be marked when reprinting: (https://blog.csdn.net/youcans/article/details/133693135) Crated:2023-10-10
Copyright 2023 youcans, XUPT
Welcome to follow this book’s CSDN exclusive serialization column
"Digital Image Processing-OpenCV/Python" serialization: https://blog.csdn.net/youcans/category_12418787.html