In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-01 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
Editor to share with you how to achieve python RGB and YCBCR color space conversion, I believe that most people do not understand, so share this article for your reference, I hope you can learn a lot after reading this article, let's go to know it!
1. The relationship between grayscale value and brightness
Human beings can get most of the information needed to understand the scene from grayscale images, so watching black-and-white TV will not seriously affect people's understanding of the scene in the video. The brightness of the image is proportional to the pixel value. If you need to increase the brightness of the image, such as the gradual transition from black to white, you can increase the gray image pixel value of a single channel. Each pixel value of a grayscale image is generally saved with 8 bit, and the range of pixel values is 0-255.
The following example shows how the brightness changes as the pixel value of a grayscale image increases, assuming that the initial pixel value of the image is 0:
The above shows the change of skin tone as the pixel value of the lena image increases. The implementation of the code is relatively simple, read the picture, and then constantly increase the offset to each pixel value of the image:
Import numpy as npimport matplotlib.pyplot as pltimport imageioimage = imageio.imread ("lena.jpg") # set the increment of pixels per cycle shift = 6*np.ones (shape= (64,64) plt.figure () for i in range (1,17): plt.subplot (4,4, I) plt.imshow (image/255, cmap= "gray", vmin=0, vmax=1) plt.axis ("off") image = image + shift2, RGB color space and color control
RGB model is widely used in hardware devices, and more colors can be formed by the superposition of R (red), G (green) and B (blue). There is a linear transformation relationship between RGB color space and YCbCr color space and HSV color space, so as long as you have RGB images, you can get images in other color spaces.
R, G and B are used as three channels in an image. If the value of one of the two channels is 0, the color of the image will be controlled by the channel that is not zero.
For example:
There are three steps to achieve the above effect:
(1) create a 3-channel empty image
(2) add a single-channel image to the R channel of a 3-channel empty image.
(3) continuously increase the offset to the R channel pixel value of the 3-channel image.
# 1: create a 3-channel empty image = np.zeros (shape= (64, 64, 3)) r = imageio.imread ("lena.jpg") / lena.jpg 2: add a single-channel image to the R channel of the 3-channel empty image image [:,:, 0] = image [:,: 0] + rshift = 4*np.ones (shape= (64,64)) plt.figure () for i in range (1,17): plt.subplot (4,4) I) plt.imshow (image/255, vmin=0, vmax=1) plt.axis ("off") # (3) increasing the offset image [:,:, 0] = image [:,:, 0] + shift to the R channel pixel value of the 3-channel image
However, because the color of the final image is the superposition of the three R\ G\ B, and in reality is not only one of the colors, so it is difficult to control the color of the final image, so we need other color space.
3. YCbCr color space and its transformation relationship with RGB.
Y in the YCbCr color space is the luminance channel, Cb is the blue component, and Cr is the red component. It is more commonly used in TV systems, such as early black-and-white televisions using color TV signal lines, you can use luminance values alone; this function cannot be done in RGB color space, because we can't just use a channel in RGB as a luminance signal.
Because YCbCr and YUV color space are often similar, they are easy to be regarded as subordinate or equivalent. According to Wikipedia, YUV is an analog signal, while YCbCr is a digital signal.
There is a linear transformation relationship between YCbCr and RGB. The transformation matrix introduced in this paper comes from ITU.BT-601. The Trans form of the transformation matrix is as follows:
The implementation of the rgb2ycbcr () function requires only two steps: (1) to create a transformation matrix Trans; (2) to traverse each pixel of the image and calculate the matrix of the three channels respectively.
The following code shows how to transform from RGB space to YCBCR:
Def rgb2ycbcr (rgb_image): "" convert rgb into ycbcr "if len (rgb_image.shape)! = 3 or rgb_image.shape [2]! = 3: raise ValueError (" input image is not a rgb image ") rgb_image = rgb_image.astype (np.float32) # 1: create a transformation matrix And offset transform_matrix = np.array ([[0.257, 0.564, 0.098], [- 0.148,-0.291, 0.439], [0.439,-0.368,-0.071]]) shift_matrix = np.array ([16128]) Ycbcr_image = np.zeros (shape=rgb_image.shape) w, h, _ = rgb_image.shape # 2: traverse three channels of each pixel to transform for i in range (w): for j in range (h): ycbcr_image [I, j,:] = np.dot (transform_matrix, rgb_image [I, j,:]) + shift_matrix return ycbcr_image
If you want to require the inverse transformation, you only need to proceed according to the matrix inversion rule. It should be noted that when the inverse transformation occurs, the offset matrix also needs the inverse of the left multiplication transformation matrix Trans! The inverse transformation only needs to inverse the transform_matrix in rgb2ycbcr. Again, shift_matrix needs to be multiplied by the inverse of transform_matrix, instead of directly subtracting shift_matrix!
Def ycbcr2rgb (ycbcr_image): "" convert ycbcr into rgb "" if len (ycbcr_image.shape)! = 3 or ycbcr_image.shape [2]! = 3: raise ValueError ("input image is not a rgb image") ycbcr_image = ycbcr_image.astype (np.float32) transform_matrix = np.array ([[0.257, 0.564, 0.098], [- 0.148,-0.291] 0.439], [0.439,-0.368,-0.071]]) transform_matrix_inv = np.linalg.inv (transform_matrix) shift_matrix = np.array ([16,128,128]) rgb_image = np.zeros (shape=ycbcr_image.shape) w, h _ = ycbcr_image.shape for i in range (w): for j in range (h): rgb_image [I, j,:] = np.dot (transform_matrix_inv, ycbcr_image [I, j,:])-np.dot (transform_matrix_inv, shift_matrix) return rgb_image.astype (np.uint8)
The required package and drawing code are as follows, and the drawing uses the two functions defined above. First, change rgb to ycbcr, and then from ycbcr to rgb:
Import numpy as npimport imageioimport matplotlib.pyplot as pltrgb_image = imageio.imread ("lena.jpg") ycbcr_image = rgb2ycbcr (rgb_image) cycle_image = ycbcr2rgb (ycbcr_image) images = [rgb_image, ycbcr_image, cycle_image] titles = ["orignal", "ycbcr", "cycle"] for i in range (1, len (images) + 1): plt.subplot (1,3, I) plt.title (titles [I-1]) plt.imshow (images [I-1] / 255)
In the following image, on the left is the original rgb image, in the middle is the converted ycbcr space image, and on the right is the image transferred back to rgb space:
Finally, the conversion effect of the standard library provided by opencv is compared:
Import cv2rgb_image = imageio.imread ("lena.jpg") ycrcb_image = cv2.cvtColor (rgb_image, cv2.COLOR_RGB2YCR_CB) cycle_image = cv2.cvtColor (ycbcr_image, cv2.COLOR_YCR_CB2RGB) images = [rgb_image, ycrcb_image, cycle_image] titles = ["orignal", "ycrcb", "cycle"] for i in range (1, len (images) + 1): plt.subplot (1,3) I) plt.title (titles [I-1]) plt.imshow (images [I-1] / 255)
The results obtained by opencv are as follows:
The original rgb effect is very similar to the cycle (refactoring) effect, while the intermediate result is inconsistent because opencv uses "ycrcb" instead of "ycbcr".
The above is all the content of the article "how to convert RGB to YCBCR color space by python". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.