In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly introduces the Python image point operation and grayscale processing example analysis, has a certain reference value, interested friends can refer to, I hope you can learn a lot after reading this article, the following let Xiaobian with you to understand.
one。 The concept of image point operation
Image point operation (Point Operation) means that for an input image, an output image will be produced, and the gray value of each pixel of the output image is determined by the input pixel. Point operation is actually a mapping process from grayscale to grayscale, which enhances or weakens the grayscale of the image through mapping transformation. It can also calculate the gray histogram, linear transformation, non-linear transformation and image skeleton extraction. It has no operational relationship with adjacent pixels, so it is a simple and effective image processing method [1].
The grayscale transformation of the image can improve the quality of the image, highlight the details of the image and improve the contrast of the image by selectively highlighting the features of interest or suppressing the unwanted features in the image. It can also effectively change the histogram distribution of the image and make the pixel value distribution of the image more uniform [2-3]. It has many applications in practice:
Photometric calibration
Contrast enhancement
Contrast expansion
Display calibration
Contour line determination
If the input image is A (xfocus y) and the output image is B (xperience y), then the point operation can be expressed as follows:
There is a difference between image point operation and geometric operation, which will not change the spatial position relationship between pixels in the image. At the same time, it is also different from the local (domain) operation, and the input pixels correspond to the output pixels one by one.
two。 Image graying processing
Image graying is the process of converting a color image into a grayscale image. The color image usually includes three components: r, G and B, showing various colors such as red, green and blue respectively. Graying is the process of making the R, G and B components of the color image equal. Each pixel in a grayscale image has only one sample color, and its grayscale is a multi-level color depth between black and white. The pixels with large gray values are brighter, and vice versa. The maximum pixel value is 255 (for white). The minimum pixel value is 0 (for black).
Suppose the color of a point is made up of RGB (RMagneGMagneB). Common grayscale processing algorithms are shown in Table 11-1:
The Gray in Table 11-1 represents the color after grayscale processing, and then the original RGB (RMagneGMagneB) color is uniformly replaced with the new color RGB (Gray,Gray,Gray), thus converting the color picture into a grayscale image. A common method is to sum the three components of RGB and then take the average value, but a more accurate method is to set different weights and divide the RGB components into gray levels according to different proportions. For example, the sensory blue of human eyes has the lowest sensitivity and the highest sensitivity is green, so the weighted average of RGB according to 0.299, 0.587 and 0.144 can get a more reasonable grayscale image, as shown in formula 11-2 [4-6].
In daily life, most of the color images we see are RGB, but in the process of image processing, we often need to use grayscale images, binary images, HSV, HSI and other colors. OpenCV provides cvtColor () function to achieve these functions. The function prototype is as follows:
Dst = cv2.cvtColor (src, code [, dst [, dstCn]])
-src represents the input image and the original image that needs to be transformed in color space
-dst represents the output image, and its size and depth are the same as src
-code represents the code or identity of the transformation
-dstCn represents the number of channels in the target image. When the value is 0, it is decided by src and code
The function is to convert an image from one color space to another, where RGB refers to Red, Green and Blue, an image consists of these three channels (channel); Gray represents only one channel for grayscale values; and HSV contains three channels: Hue (hue), Saturation (saturation) and Value (luminance).
In OpenCV, common color space conversion logos include CV_BGR2BGRA, CV_RGB2GRAY, CV_GRAY2RGB, CV_BGR2HSV, CV_BGR2XYZ, CV_BGR2HLS and so on. The following is the code that calls the cvtColor () function to gray the image.
#-*-coding: utf-8-*-# By:Eastmountimport cv2 import numpy as np # read the original picture src = cv2.imread ('luo.png') # Image Grey processing grayImage = cv2.cvtColor (src,cv2.COLOR_BGR2GRAY) # display Image cv2.imshow ("src", src) cv2.imshow ("result", grayImage) # waiting for cv2.waitKey (0) cv2.destroyAllWindows ()
The output is shown in figure 11-1. On the left is the original image of the color "Xiao Luo", and on the right is the grayscale image after the color image is grayed out. The grayscale map sets the three color variables of a pixel to equal (R=G=B), which is called the grayscale value.
Similarly, the following core code can be called to convert the color image to the HSV color space, and the output is shown in figure 11-2.
GrayImage = cv2.cvtColor (src, cv2.COLOR_BGR2HSV)
The following code compares nine common color spaces, including BGR, RGB, GRAY, HSV, YCrCb, HLS, XYZ, LAB, and YUV, and loops through the processed image.
#-*-coding: utf-8-*-# By:Eastmountimport cv2 import numpy as np import matplotlib.pyplot as plt# reads the original image img_BGR = cv2.imread ('luo.png') img_RGB = cv2.cvtColor (img_BGR, cv2.COLOR_BGR2RGB) # BGR converts to RGBimg_GRAY = cv2.cvtColor (img_BGR, cv2.COLOR_BGR2GRAY) # Grey processing img_HSV = cv2.cvtColor (img_BGR Cv2.COLOR_BGR2HSV) # BGR to HSVimg_YCrCb = cv2.cvtColor (img_BGR, cv2.COLOR_BGR2YCrCb) # BGR to YCrCbimg_HLS = cv2.cvtColor (img_BGR, cv2.COLOR_BGR2HLS) # BGR to HLSimg_XYZ = cv2.cvtColor (img_BGR, cv2.COLOR_BGR2XYZ) # BGR to XYZimg_LAB = cv2.cvtColor (img_BGR, cv2.COLOR_BGR2LAB) # BGR to LABimg_YUV = cv2.cvtColor (img_BGR Cv2.COLOR_BGR2YUV) # BGR to YUV# call matplotlib to display the processing result titles = ['BGR',' RGB', 'GRAY',' HSV', 'YCrCb',' HLS', 'XYZ',' LAB', 'YUV'] images = [img_BGR, img_RGB, img_GRAY, img_HSV, img_YCrCb, img_HLS, img_XYZ, img_LAB, img_YUV] for i in range (9): plt.subplot (3) 3, iTun1), plt.imshow (images [I], 'gray') plt.title (titles [I]) plt.xticks ([]), plt.yticks ([]) plt.show ()
The running result is shown in figure 11-3:
three。 Image Grey processing based on Pixel Operation
The previous description of calling the cvtColor () function in OpenCV to achieve image grayscale processing, and then explain the image grayscale processing methods based on pixel operation, mainly the maximum gray processing, average gray processing and weighted average gray processing methods.
1. Maximum grayscale processing method
The gray value of this method is equal to the maximum value of the three components of color image R, G and B. the formula is as follows:
The brightness of the grayscale image after grayscale processing is very high, and the implementation code is as follows.
#-*-coding: utf-8-*-# By:Eastmountimport cv2 import numpy as np import matplotlib.pyplot as plt# reads the original image img = cv2.imread ('luo.png') # gets the image height and width height = img.shape [0] width = img.shape [1] # create an image grayimg = np.zeros ((height, width, 3) Np.uint8) # Image maximum grayscale processing for i in range (height): for j in range (width): # get the maximum image R G B value gray=max (img [iLJ] [0], img [iLJ] [1], img [iLJ] [2]) # grayscale pixel assignment gray=max (RMGMAE B) grayimg [I J] = np.uint8 (gray) # display image cv2.imshow ("src", img) cv2.imshow ("gray", grayimg) # waiting for cv2.waitKey (0) cv2.destroyAllWindows ()
The output result is shown in figure 11-4, and the grayscale of the processing effect is too bright.
two。 Average gray processing method
The grayscale value of this method is equal to the summation average value of the gray values of the three components of the color image R, G and B. the calculation formula is shown in the formula (11-4).
The implementation code of the average gray processing method is as follows.
#-*-coding: utf-8-*-# By:Eastmountimport cv2 import numpy as np import matplotlib.pyplot as plt# reads the original image img = cv2.imread ('luo.png') # gets the image height and width height = img.shape [0] width = img.shape [1] # create an image grayimg = np.zeros ((height, width, 3) Np.uint8) # Image average grayscale processing method for i in range (height): for j in range (width): # the gray value is the average of the three components of RGB gray = (int (img [iMagnej] [0]) + int (img [iMagnej] [1]) + int (img [iMagnej] [2])) / 3 grayimg [iMagazine j] = np.uint8 (gray) # display image cv2.imshow ("src") Img) cv2.imshow ("gray", grayimg) # waiting for cv2.waitKey (0) cv2.destroyAllWindows () to be displayed
The output is shown in figure 11-5:
3. Weighted average gray processing method
According to the importance of color, the three components are weighted and averaged with different weights. Because human eyes are most sensitive to green and least sensitive to blue, a reasonable grayscale image can be obtained by weighted averaging the three components of RGB according to the following formula.
The implementation code of the weighted average grayscale processing method is as follows:
#-*-coding: utf-8-*-# By:Eastmountimport cv2 import numpy as np import matplotlib.pyplot as plt# reads the original image img = cv2.imread ('luo.png') # gets the image height and width height = img.shape [0] width = img.shape [1] # create an image grayimg = np.zeros ((height, width, 3) Np.uint8) # Image average grayscale processing method for i in range (height): for j in range (width): # Grayscale weighted average method gray = 0.30 * img [iMagin j] [0] + 0.59 * img [iMagin j] [1] + 0.11 * img [iMagin j] [2] grayimg [iMagee j] = np.uint8 (gray) # display Image cv2.imshow ("src", img) cv2.imshow ("gray") Grayimg) # wait for cv2.waitKey (0) cv2.destroyAllWindows ()
The output is shown in figure 11-6:
Thank you for reading this article carefully. I hope the article "sample Analysis of Image Point Operation and Grey processing in Python" shared by the editor will be helpful to you. At the same time, I also hope you will support us and pay attention to the industry information channel. More related knowledge is waiting for you to learn!
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.