当前位置:网站首页>Two image enhancement methods: image point operation and image graying

Two image enhancement methods: image point operation and image graying

2022-06-21 08:15:00 Huawei cloud developer community

Abstract : This paper mainly explains the grayscale processing of image point operation , The commonly used grayscale processing methods are introduced in detail , And share the image color space conversion , And the implementation of three gray conversion algorithms .

This article is shared from Huawei cloud community 《[Python From zero to one ] forty-three . Image point operation and image graying processing in image enhancement and operation 》, author :eastmount.

One . Concept of image point operation

Image point operation (Point Operation) For an input image , An output image will be generated , The gray value of each pixel of the output image is determined by the input pixel . Point operation is actually a gray-scale to gray-scale mapping process , Through mapping transformation to enhance or weaken the gray level of the image . You can also calculate the gray histogram of the image 、 linear transformation 、 Nonlinear transformation and image skeleton extraction . It has no operational relationship with adjacent pixels , It is a simple and effective image processing method [1].

The gray-scale transformation of the image can selectively highlight the features of interest in the image or suppress the unwanted features in the image , So as to improve the quality of the image , Highlight the details of the image , Improve the contrast of the image . It can also effectively change the histogram distribution of the image , 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

Set the input image to A(x,y), The output image is B(x,y), Then the point operation can be expressed as :

There are differences between image point operation and geometric operation , The spatial position relationship between pixels in the image will not be changed . At the same time with local ( field ) There are also differences in operations , Input pixels and output pixels correspond to each other .

Two . Image graying

Image graying is the process of converting a color image into a grayed image . Color images usually include R、G、B The three components , It shows various colors such as red, green and blue , Graying is to make the color image R、G、B The process of three equal components . Each pixel in a grayscale image has only one sample color , Its grayscale is a multi-level color depth between black and white , Pixels with large gray value are brighter , On the contrary, it is darker , The maximum pixel value is 255( Said the white ), The minimum pixel value is 0( According to black ).

Suppose the color of a point is determined by RGB(R,G,B) form , Common gray processing algorithms are shown in table 11-1 Shown :

surface 11-1 in Gray Represents the color after grayscale processing , Then the original RGB(R,G,B) Replace the color evenly with the new color RGB(Gray,Gray,Gray), Thus, the color picture is transformed into gray image . A common way is to put RGB Sum the three components and then take the average , But a more accurate way is to set different weights , take RGB The components are divided into gray levels according to different proportions . For example, human eyes have the lowest sensitivity to blue , The most sensitive is green , So it will RGB according to 0.299、0.587、0.144 Proportional weighted average can get a more reasonable gray image , As formula 11-2 Shown [4-6].

In everyday life , Most of the color images we see are RGB type , But in image processing , You often need to use grayscale images 、 Binary image 、HSV、HSI Equal color ,OpenCV Provides cvtColor() Function to implement these functions . Its function prototype is as follows :

  • dst = cv2.cvtColor(src, code[, dst[, dstCn]])
    – src Represents the input image , The original image that needs color space transformation
    – dst Output image , Its size and depth are similar to src Agreement
    – code A code or identifier that represents a transformation
    – dstCn Indicates the number of target image channels , Its value is 0 when , Then there are src and code decision

This function is used to convert an image from one color space to another , among ,RGB Refer to Red、Green and Blue, An image consists of these three channels (channel) constitute ;Gray It means that there is only one channel with gray value ;HSV contain Hue( tonal )、Saturation( saturation ) and Value( brightness ) Three channels .

stay OpenCV in , Common color space conversion logos include CV_BGR2BGRA、CV_RGB2GRAY、CV_GRAY2RGB、CV_BGR2HSV、CV_BGR2XYZ、CV_BGR2HLS etc. . Here is the call cvtColor() Function to gray image processing code .

# -*- coding: utf-8 -*-
# By:Eastmount
import cv2  
import numpy as np  

# Read the original picture 
src = cv2.imread('luo.png')

# Image graying 
grayImage = cv2.cvtColor(src,cv2.COLOR_BGR2GRAY)

# Display images 
cv2.imshow("src", src)
cv2.imshow("result", grayImage)

# Wait for the display 
cv2.waitKey(0)
cv2.destroyAllWindows()

The output result is shown in the figure 11-1 Shown , The left is colored “ Xiao Luoluo ” Original picture , On the right is the grayscale image after grayscale processing of the color image . among , Grayscale image sets the three color variables of a pixel to be equal (R=G=B), In this case, the value is called gray value .

Again , You can call the following core code to convert the color image into HSV Color space , The output result is shown in the figure 11-2 Shown .

  • grayImage = cv2.cvtColor(src, cv2.COLOR_BGR2HSV)

The following code compares nine common color spaces , Include BGR、RGB、GRAY、HSV、YCrCb、HLS、XYZ、LAB and YUV, And circularly display the processed image .

# -*- coding: utf-8 -*-
# By:Eastmount
import cv2  
import numpy as np  
import matplotlib.pyplot as plt

# Read the original image 
img_BGR = cv2.imread('luo.png')

img_RGB = cv2.cvtColor(img_BGR, cv2.COLOR_BGR2RGB)     #BGR Convert to RGB
img_GRAY = cv2.cvtColor(img_BGR, cv2.COLOR_BGR2GRAY)   # graying 
img_HSV = cv2.cvtColor(img_BGR, cv2.COLOR_BGR2HSV)     #BGR turn HSV
img_YCrCb = cv2.cvtColor(img_BGR, cv2.COLOR_BGR2YCrCb) #BGR turn YCrCb
img_HLS = cv2.cvtColor(img_BGR, cv2.COLOR_BGR2HLS)     #BGR turn HLS
img_XYZ = cv2.cvtColor(img_BGR, cv2.COLOR_BGR2XYZ)     #BGR turn XYZ
img_LAB = cv2.cvtColor(img_BGR, cv2.COLOR_BGR2LAB)     #BGR turn LAB
img_YUV = cv2.cvtColor(img_BGR, cv2.COLOR_BGR2YUV)     #BGR turn YUV

# call matplotlib Display processing results 
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, i+1), plt.imshow(images[i], 'gray')  
   plt.title(titles[i])  
   plt.xticks([]),plt.yticks([])  
plt.show()

The running results are shown in the figure 11-3 Shown :

3、 ... and . Image graying processing based on pixel operation

The call... Was described earlier OpenCV in cvtColor() Function to realize the processing of image grayscale , Next, we will explain the image graying processing method based on pixel operation , Mainly the maximum gray processing 、 Average gray processing and weighted average gray processing methods .

1. Maximum gray processing method

The gray value of this method is equal to that of color image R、G、B The maximum of the three components , The formula is as follows :

The brightness of the grayscale image after grayscale processing is very high , The implementation code is as follows .

# -*- coding: utf-8 -*-
# By:Eastmount
import cv2  
import numpy as np  
import matplotlib.pyplot as plt

# Read the original image 
img = cv2.imread('luo.png')

# Get 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 gray processing 
for i in range(height):
    for j in range(width):
        # Get images R G B Maximum 
        gray = max(img[i,j][0], img[i,j][1], img[i,j][2])
        # Gray image pixel assignment  gray=max(R,G,B)
        grayimg[i,j] = np.uint8(gray)

# Display images 
cv2.imshow("src", img)
cv2.imshow("gray", grayimg)
# Wait for the display 
cv2.waitKey(0)
cv2.destroyAllWindows()

The output result is shown in the figure 11-4 Shown , The gray level of its processing effect is too bright .

2. Average gray processing method

The gray value of this method is equal to that of color image R、G、B The sum average of the gray values of the three components , The calculation formula is as follows (11-4) Shown :

The gray level average processing method is as follows .

# -*- coding: utf-8 -*-
# By:Eastmount
import cv2  
import numpy as np  
import matplotlib.pyplot as plt

# Read the original image 
img = cv2.imread('luo.png')

# Get 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 gray processing method 
for i in range(height):
    for j in range(width):
        # Gray value is RGB The average of the three components 
        gray = (int(img[i,j][0]) + int(img[i,j][1]) + int(img[i,j][2]))  /  3
        grayimg[i,j] = np.uint8(gray)

# Display images 
cv2.imshow("src", img)
cv2.imshow("gray", grayimg)

# Wait for the display 
cv2.waitKey(0)
cv2.destroyAllWindows()

The output result is shown in the figure 11-5 Shown :

3. Weighted average gray processing method

This method is based on the importance of color , Weighted average the three components with different weights . Because the human eye is most sensitive to green , Blue is the least sensitive , therefore , The following formula is correct RGB Three component weighted average can get a more reasonable gray image .

The implementation code of weighted average gray processing method is as follows :

# -*- coding: utf-8 -*-
# By:Eastmount
import cv2  
import numpy as np  
import matplotlib.pyplot as plt

# Read the original image 
img = cv2.imread('luo.png')

# Get 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 gray processing method 
for i in range(height):
    for j in range(width):
        # Gray weighted average method 
        gray = 0.30 * img[i,j][0] + 0.59 * img[i,j][1] + 0.11 * img[i,j][2]
        grayimg[i,j] = np.uint8(gray)

# Display images 
cv2.imshow("src", img)
cv2.imshow("gray", grayimg)

# Wait for the display 
cv2.waitKey(0)
cv2.destroyAllWindows()

The output result is shown in the figure 11-6 Shown :

Four . summary

This paper mainly explains the grayscale processing of image point operation , The commonly used grayscale processing methods are introduced in detail , And share the image color space conversion , And the implementation of three gray conversion algorithms . Through gray processing , We can effectively convert color images into gray images , Provide support for subsequent edge extraction and other processing , It is also possible to realize the simplest color image to black-and-white effect of image processing software , I hope it will be of some help to you .

 

Click to follow , The first time to learn about Huawei's new cloud technology ~

原网站

版权声明
本文为[Huawei cloud developer community]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202221502060987.html