In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
How to use Python to give Miss Sister beauty, I believe that many inexperienced people do not know what to do about it. Therefore, this paper summarizes the causes and solutions of the problem. Through this article, I hope you can solve this problem.
01 color space of the image
Color image has more information than grayscale image. Each pixel of color image is usually represented by three components: red (R), green (G) and blue (B), each of which is between 0,255.
The different colors presented in the image are all made up of R, G and B colors. In OpenCV, the color image has three color channels, but the order of the channels can be changed. RGB, BRG, BGR, GBR and GRB are all possible.
When reading an image, we are not clear about the arrangement of the color channel of the image, so we need to fix the color channel of the image first, which requires calling the cvtColor () function of OpenCV.
The function of the cvtColor () function is to transform the color space of the image. The prototype is as follows:
Dst=cv2.cvtColor (src, code)
Parameter description:
Src: the input image is the original image to be transformed in color space, which can be the Mat class.
Code: the converted code or logo, that is, the image that determines what format to convert into what format, which will be described in more detail later.
The function output stores the image after color space transformation.
By calling the cvtColor () function, you can also convert a color image into a grayscale image. The sample code is shown in program 3-5. The code runs as shown in figure 3.9.
▲ color image 1.jpg
Program 3-5 color image to grayscale image example: color2gray.py
#-*-coding: UTF-8-*-import numpy as np import cv2 # defines the main () function def main (): img = cv2.imread ('1.jpg') img2 = cv2.cvtColor (img,cv2.COLOR_RGB2GRAY) # convert from color image to grayscale image cv2.imshow (' img2.bmp') Img2) cv2.waitKey (0) if _ _ name__ = ='_ main__': main ()
▲ figure 3.9 the running result of color2gray.py program
Note: the cvtColor () function can also change the order of the image color channels by changing the parameter cv2.COLOR_RGB2BRG and so on. In addition, you can also directly set the parameter to 0 when reading the image function imread, and directly read the color image as a grayscale image, img = cv2.imread ('1.jpg recording paper 0).
Channel separation and mixing of 02 color images
The grayscale image is a single channel, and the color image has R, G and B color channels. Therefore, in image processing, color channels are often separated, an array of channels is processed separately, and then merged into a color image.
In the actual code writing, we only need to call the split () and merge () functions in OpenCV to realize the separation and merging of image channels.
The function of the split () function is to separate a multi-channel matrix into a single-channel matrix. The prototype is as follows:
[, mv] = cv2.split (src)
Parameter description: the input parameter is the image matrix to be separated, and the output parameter is a Mat array.
The function of the merge () function is to combine multiple single-channel images into a multi-channel image. The prototype is as follows:
Dst=cv2.merge ([, dst])
Parameter description: the input parameters can be Mat array, and the output can be the merged image matrix.
03 example of channel separation and mixing program for color images
Input a color image, divide it into R, G, B channel images and display them by program 3-6. The color channel distribution of the image needs to be determined before segmentation, so the cvtColor () function is called to fix the color channel first. The sample code is shown in program 3-6, and the effect is shown in figure 3.10.
Program 3-6 color image channel separation example: colorsplit.py
#-*-coding: UTF-8-*-import numpy as np import cv2 # defines the main () function def main (): img = cv2.imread ('1.jpg') img2 = cv2.cvtColor (img,cv2.COLOR_BRG2RGB) r cv2.split (img2) # img is separated into three single-channel image cv2.imshow ("Red", r) cv2.imshow ("Green", g) cv2.imshow ("Blue") B) cv2.waitKey (0) if _ _ name__ = ='_ main__': main ()
▲ figure 3.10 the running result of colorsplit.py program
It can be seen that after the separation of the image channel, the image of different color channel shows different depth, and the image of a single channel presents the grayscale information of the color channel. Next, mix the three color channels and add a line of code to the code: img3 = cv2.merge ([bgrameg.r]), so that img3 returns to the original color image style, as shown in figure 3.11.
▲ figure 3.11 output of mixed images with three channels
04 binarization of color image
The binarization of the image is to set the gray value of the pixels on the image to 0 or 255, that is, the whole image shows an obvious black-and-white effect. The simplest steps for binarization of color images are as follows:
Hongmeng official Strategic Cooperation to build HarmonyOS Technology Community
The color image changes to grayscale.
Image threshold processing, that is, the pixel whose pixel value is higher than a certain threshold is assigned to 255, and vice versa.
Where the operation of the threshold calls the threshold () function of OpenCV.
The threshold () function is declared as follows:
Ret, dst = cv2.threshold (src, thresh, maxval, type)
Function function: realize the binarization of the fixed threshold of the image.
Parameter description:
Src: input images, only single-channel images, usually grayscale images.
Dst: output diagram.
Thresh: threshold.
Maxval: the value assigned when the pixel value exceeds the threshold (or is less than the threshold, as determined by type).
Type: the type of binarization operation, including five types, namely cv2.THRESH_BINARY, cv2.THRESH_BINARY_INV, cv2.THRESH_TRUNC, cv2.THRESH_TOZERO, and cv2.THRESH_TOZERO_INV.
For example, refer to procedure 3-7.
Program 3-7 color image binarization example: colorthreshold.py
#-*-coding: UTF-8-*-import numpy as np import cv2 # defines the main () function def main (): img = cv2.imread ('1.jpgstam0) thresh2,dst = cv2.threshold (img,127,255,cv2.THRESH_BINARY) # Image binarization cv2.imshow ("dst") Dst) cv2.waitKey (0) if _ _ name__ = ='_ main__': main ()
As shown in program 3-7, all pixels higher than 127 are set to 255 and those below are set to 0, and the output result shown in figure 3.12 is obtained.
▲ figure 3.12 colorthreshold.py program output result
05 traversal of color images
The traversal of the grayscale image obtains the pixels of the coordinate position by accessing the two-dimensional array. What about color images? The color image can be seen as a 3-dimensional array, which can be traversed in program 3-8.
Program 3-8 traversal color image example: color1.py
#-*-coding: UTF-8-*-import numpy as np import cv2 # defines the main () function def main (): img = cv2.imread ('1.jpg') height,width N = img.shape # get the width, height and dimension of the picture img2 = img.copy () # copy a new picture that is the same as img # traverse the picture in two dimensions: for i in range (height): for j in range (width): img2 [I, j] [0] = 0 # reassign the elements in the first channel to cv2.imshow ('img2.jpg' Img2) cv2.waitKey (0) if _ _ name__ = ='_ main__': main ()
Since the color information in the first channel has all changed to 0, the image display result is shown in figure 3.13.
▲ figure 3.13 the running result of color1.py program
When reading the image pixel values of different channels, we need to determine whether the channel arrangement of the image is RGB or BRG.
06 conversion between color image and grayscale image
After the previous study, we know that there are three ways to convert a color image into a grayscale image:
When imread reads the image, the parameter is directly set to 0, and the color image is automatically read into a grayscale image.
Call the cvtColor () function with the parameter set to cv2.COLOR_BGR2GRAY.
By calling the split () function, a color image can be separated into three single-channel grayscale images.
So is it possible to convert grayscale images into color images?
We know that the grayscale image is a single channel, and the color image is the RGB 3 color channel. So is it possible to artificially increase the channels of the image and forge the other two channels, while the other two channels can be assigned randomly? Program 3-9 made an attempt.
Program 3-9 adds image channel example: gray2color1.py
#-*-coding: UTF-8-*-import numpy as np import cv2 # defines the main () function def main (): img = cv2.imread ('gray1.jpg') gray = np.zeros ((512,512,3), np.uint8) # generate an empty color image height,width,n = img.shape # Image Pixel level traversal for i in range (height): for j in range (width): gray [I J] [0] = img [I, j] [0] gray [I, j] [1] = 0 gray [I, j] [2] = 0 cv2.imshow ('gray.jpg', gray) cv2.waitKey (0) = if _ name__ = =' _ main__': main ()
The above program creates a new 3-channel empty color image, and then puts the read grayscale image on the first channel of the new color image, that is, B channel, and the other two channels are assigned a value of 0, so the image as a whole appears blue. the program runs as shown in figure 3.14.
▲ figure 3.14 the running result of gray2color1.py program
The color of the image converted by the above method is very single. Is there a smarter way? In the period when the camera technology was not very mature, people colored the black-and-white photos and invented a pseudo-color image technology. In OpenCV, you can use a predefined Colormap (chromaticity diagram) to color the picture. The sample code can be found in program 3-10.
Program 3-10 pseudo-color image technology example: gray2color2.py
#-*-coding: UTF-8-*-import numpy as np import cv2 # defines the main () function def main (): img = cv2.imread ('gray1.jpg') im_color = cv2.applyColorMap (img, cv2.COLORMAP_JET) # Color cv2.imshow ("im_color.jpg", im_color) cv2.waitKey (0) if _ _ name__ = =' _ main__': main ()
The running result of the program is shown in figure 3.15. At present, pseudo-color images are mainly used to describe height, pressure, density, humidity and so on.
▲ figure 3.15 the running result of gray2color program
After reading the above, have you mastered how to use Python to give Miss Sister a beautiful face? 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.