In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
Color space and its OpenCV implementation is what, I believe that many inexperienced people do not know what to do, so this paper summarizes the causes of the problem and solutions, through this article I hope you can solve this problem.
Guide reading
This paper gives a conceptual introduction to the color space of the image, and visualizes the meaning of each channel in each color space by means of code.
The contents of the article include:
What is color space? What are the categories of color space? How to implement it in OpenCV? What is color space?
Color is a continuous phenomenon, which means that there are countless colors. However, human eyes and perception are limited. So, in order to identify these colors, we need a medium or a representation of these colors, which is called a color space. In technical terms, a color model or color space is a specific 3murD coordinate system and a subspace in the system, where each color is represented by a single point.
What are the types of color spaces?
At present, there are mainly five types of color models. However, I will only write some common ones (RGB, HSV, and HSL).
RGB (Red Green Blue) HSL (Hue Saturation Lightness) HSV (Hue Saturation Value) YUV (Luminance, blue-luminance, red-luminance) CMYK (Cyan, Magenta, Yellow, Key) RGB color space:
RGB color space is one of the famous color spaces represented by red, green and blue coordinates in three-dimensional coordinate system. In more technical terms, RGB describes color as a three-part tuple. Each part can take a value between 0 and 255, where the tuple (0jin0jin0) is black and the tuple (255255255) is white. Parts 0, 1, and 2 of the tuple represent the components of red, green, and blue, respectively.
Python implementation of RGB color space:
Here we import the necessary libraries, cv2 for color space conversion, NumPy for array operations, Matplotlib for displaying images, os for accessing the image directory, and tqdm for displaying the add-on bar.
Hsl_img = cv2.cvtColor (X [0], cv2.COLOR_BGR2HLS) # CONVERTING BGR COLOR SPACE INTO HSL COLOR SPACE # #
Hsl_img_1 = hsl_img.copy ()
Hsl_img_2 = hsl_img.copy ()
Hsl_img_3 = hsl_img.copy ()
Hsl_img_1 [:, 1] = 0 # HUE-- > ZERO #
Hsl_img_1 [:, 2] = 0
Hsl_img_2 [:,:, 0] = 0 # SATURATION-> ZERO #
Hsl_img_2 [:, 2] = 0
Hsl_img_3 [:,:, 0] = 0 # LIGHTNESS-> ZERO #
Hsl_img_3 [:, 1] = 0
Set up two empty lists Z and X to store labels with their respective images, and then specify the image size and path directory. After that, I defined two functions to return the flower type (assign_lable) and to access each image, read and resize it (make_train_data).
ZMagol X = [], []
IMG_SIZE=150
FLOWER_SUNFLOWER_DIR='../input/flowers-recognition/flowers/flowers/sunflower'
Def assign_label (img,flower_type):
Return flower_type
Def make_train_data (flower_type,DIR):
For img in tqdm (os.listdir (DIR)):
Label=assign_label (img,flower_type)
Path = os.path.join (DIR,img)
Img = cv2.imread (path,cv2.IMREAD_COLOR)
Img = cv2.resize (img, (IMG_SIZE,IMG_SIZE)) # Resizing the image
Load the image, and then convert the BGR color space to the RGB color space when OpenCV reads the image in BGR format, but Maplotlib uses the RGB format to display the image. This is why we need to convert the color space after reading the image to RGB.
Then make three copies of the fixed image, and set any two-color channel of each copy to zero, which is used to access the red, green and blue channels, respectively. If you let the 0 th color channel be 0, then you will only get the blue channel.
Make_train_data ('Sunflower',FLOWER_SUNFLOWER_DIR) # Loading Sunflower Data
Fix_img = cv2.cvtColor (X [0], cv2.COLOR_BGR2RGB) # CONVERTING BGR COLOR SPACE INTO RGB COLOR SPACE #
New_img_1 = fix_img.copy ()
New_img_2 = fix_img.copy ()
New_img_3 = fix_img.copy ()
New_img_1 [:, 0] = 0 # making R channel zero # For BLUE channel#
New_img_1 [:, 1] = 0 # making G channel zero
New_img_2 [:, 1] = 0####For RED color Channel####
New_img_2 [:, 2] = 0
New_img_3 [:,:, 0] = 0###For GREEN Channel####
New_img_3 [:, 2] = 0
Display the image:
F, axes = plt.subplots (1m 3, figsize = (15J 15))
List = [new_img_1,new_img_2,new_img_3]
I = 0
For ax in axes:
Ax.imshow (list [I])
ITunes 1
HSL color space:
The general meaning of HSL is hue, saturation and lightness. You can visualize HSL as a cylinder, as shown in figure 2 (a). There are different colors around the cylinder, such as green, yellow, red and so on (the color we really want). Saturation refers to the amount of color, while lightness refers to how dark or bright the color is. As you can see, the top of the cylinder is all white and the bottom is all black.
Figure 2:HSL color space
Python implementation of HSL color space:
Use the OpenCV function * * cvtColor () * * to convert the BGR color space to the HSL color space, where we need to transfer the image and from which color space to which color space we want to change the image. Then copy and make the two color channels zero so that each color channel is displayed separately.
Hsl_img = cv2.cvtColor (X [0], cv2.COLOR_BGR2HLS) # CONVERTING BGR COLOR SPACE INTO HSL COLOR SPACE # #
Hsl_img_1 = hsl_img.copy ()
Hsl_img_2 = hsl_img.copy ()
Hsl_img_3 = hsl_img.copy ()
Hsl_img_1 [:, 1] = 0 # HUE-- > ZERO #
Hsl_img_1 [:, 2] = 0
Hsl_img_2 [:,:, 0] = 0 # SATURATION-> ZERO #
Hsl_img_2 [:, 2] = 0
Hsl_img_3 [:,:, 0] = 0 # LIGHTNESS-> ZERO #
Hsl_img_3 [:, 1] = 0
Now display three different color channels →
F, axes = plt.subplots (1m 3, figsize = (15J 15))
List = [hsl_img_1,hsl_img_2,hsl_img_3]
I = 0
For ax in axes:
Ax.imshow (list [I])
ITunes 1
HSV color space:
The name HSV comes from the three coordinates of the color model, namely hue, saturation and value. It is also a cylindrical color model, with the radius of the cylinder representing saturation, the vertical axis representing the value, and the angle representing hue. For the observer, the hue is dominant, the saturation is the amount of white light mixed into the hue, value is the intensity of chrome, the lower value color becomes more similar to black, and the higher the value, the more like the color itself. By changing these parameters, we can generate different colors.
Figure 3:HSV color space
Python implementation of HSV color space:
Use the cvtColor () function to convert the color space to the HSV color space. Then copy and set the two channels to zero so that each channel is displayed separately.
Hsv_img = cv2.cvtColor (X [0], cv2.COLOR_BGR2HSV)
Hsv_img_1 = hsv_img.copy ()
Hsv_img_2 = hsv_img.copy ()
Hsv_img_3 = hsv_img.copy ()
Hsv_img_1 [:,:, 1] = 0#HUE-> ZERO
Hsv_img_1 [:, 2] = 0
Hsv_img_2 [:,:, 0] = 0#SATURATION-> ZERO
Hsv_img_2 [:, 2] = 0
Hsv_img_3 [:,:, 0] = 0#VALUE-> ZERO
Hsv_img_3 [:, 1] = 0
Display each color channel separately:
After reading the above, have you mastered the color space and its OpenCV implementation? If you want to learn more skills or want to know more about it, you are welcome to follow the industry information channel, thank you for reading!
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.