Image grayscale for image processing

Image grayscale

   将彩色图像转化成为灰度图像的过程成为图像的灰度化处理。

The color of each pixel in a color image is determined by three components of R, G, and B, and each component has a median value of 255, so that a pixel can have more than 16 million (255 255 255) color variations . The grayscale image is a special color image with the same three components of R, G, and B (R=G=B), and the variation range of one pixel point is 255, so in digital image processing, each image is generally first Images in this format are converted to grayscale images to make subsequent images less computationally intensive. The description of the grayscale image still reflects the distribution and characteristics of the overall and local chromaticity and brightness levels of the entire image, just like the color image.

Common methods of image grayscale

average method

Take the mean value of the R, G, and B components, and the formula is as follows:
Gray ( x , y ) = ( R ( x , y ) + G ( x , y ) + B ( x , y ) ) / 3 Gray(x, y)=(R(x,y)+G(x,y)+B(x,y))/3Gray(x,y)=(R(x,y)+G(x,y)+B(x,y))/3

def grayByaverage(img):
    # 获取图像高度和宽度
    height = img.shape[0]
    width = img.shape[1]
    # 创建一幅图像
    grayimg = np.zeros((height, width, 3), np.uint8)
    # 图像最大值灰度处理
    for i in range(height):
        for j in range(width):
            # 获取图像R G B最大值
            gray = (int(img[i,j][0])+int(img[i,j][1])+int(img[i,j][2]))/3
            grayimg[i, j] = np.uint8(gray)
    return grayimg

Maximum method

Take the maximum value of the R, G, and B components, and the formula is as follows:
Gray ( x , y ) = max ( R ( x , y ) + G ( x , y ) + B ( x , y ) ) Gray(x, y)=max(R(x,y)+G(x,y)+B(x,y))Gray(x,y)=max(R(x,y)+G(x,y)+B(x,y))

def grayByaverage(img):
    # 获取图像高度和宽度
    height = img.shape[0]
    width = img.shape[1]
    # 创建一幅图像
    grayimg = np.zeros((height, width, 3), np.uint8)
    # 图像最大值灰度处理
    for i in range(height):
        for j in range(width):
            # 获取图像R G B最大值
            gray = (int(img[i,j][0])+int(img[i,j][1])+int(img[i,j][2]))/3
            grayimg[i, j] = np.uint8(gray)
    return grayimg

Weighted average method

According to the importance and other indicators, the three components are weighted and averaged with different weights. Since the human eye has the highest sensitivity to green and the lowest sensitivity to blue, a more reasonable grayscale image can be obtained by weighting and averaging the three components of RGB according to the following formula. The formula is as follows:
Gray ( x , y ) = 0.11 R ( x , y ) + 0.59 G ( x , y ) + 0.30 B ( x , y ) Gray(x,y)=0.11R(x,y)+0.59 G(x,y)+0.30B(x,y)Gray(x,y)=0.11R(x,y)+0.59G(x,y)+0.30B(x,y)

def grayByWeightedaverage(img):
    # 获取图像高度和宽度
    height = img.shape[0]
    width = img.shape[1]
    # 创建一幅图像
    grayimg = np.zeros((height, width, 3), np.uint8)
    for i in range(height):
        for j in range(width):
            # 获取图像R G B最大值
            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)
    return grayimg

The results of the three gray scales are as follows:
insert image description here
Analysis of the results:
Among the three gray scale images, the gray scale image obtained by the maximum value method is brighter; the gray scale image obtained by the weighted average method is soft, and the gray scale image obtained by the average value is darker. Others are darker.

overall code

import cv2
import numpy as np
import matplotlib.pyplot as plt

#图像灰度化
#最大值法
def grayBymax(img):
    # 获取图像高度和宽度
    height = img.shape[0]
    width = img.shape[1]
    # 创建一幅图像
    grayimg = np.zeros((height, width, 3), np.uint8)
    # 图像最大值灰度处理
    for i in range(height):
        for j in range(width):
            # 获取图像R G B最大值
            gray = max(img[i,j][0], img[i,j][1], img[i,j][2])
            grayimg[i, j] = np.uint8(gray)
    return grayimg


def grayByaverage(img):
    # 获取图像高度和宽度
    height = img.shape[0]
    width = img.shape[1]
    # 创建一幅图像
    grayimg = np.zeros((height, width, 3), np.uint8)
    # 图像最大值灰度处理
    for i in range(height):
        for j in range(width):
            # 获取图像R G B最大值
            gray = (int(img[i,j][0])+int(img[i,j][1])+int(img[i,j][2]))/3
            grayimg[i, j] = np.uint8(gray)
    return grayimg


def grayByWeightedaverage(img):
    # 获取图像高度和宽度
    height = img.shape[0]
    width = img.shape[1]
    # 创建一幅图像
    grayimg = np.zeros((height, width, 3), np.uint8)
    for i in range(height):
        for j in range(width):
            # 获取图像R G B最大值
            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)
    return grayimg


if __name__ == '__main__':
    img = cv2.imread('miao.jpg')
    opencv_gray=cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
    grayByaverage=grayByaverage(img)
    grayBymax=grayBymax(img)
    grayByWeightedaverage=grayByWeightedaverage(img)
    # # 显示图像
    # cv2.imshow("src", img)
    # cv2.imshow("gray", opencv_gray)
    # # 等待显示
    # cv2.waitKey(0)
    # cv2.destroyAllWindows()
    imgRGB = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
    plt.figure(figsize=(80, 80))
    plt.rcParams['font.sans-serif'] = [
        'FangSong']  # 支持中文标签
    fig, ax = plt.subplots(2, 2)
    ax[0, 0].imshow(imgRGB)
    ax[0, 1].imshow(grayByaverage)
    ax[1, 0].imshow(grayBymax)
    ax[1, 1].imshow(grayByWeightedaverage)
    ax[0, 0].title.set_text("原图")
    ax[0, 1].title.set_text("平均值法")
    ax[1, 0].title.set_text("最大值法")
    ax[1, 1].title.set_text("加权平均值法")
    ax[0,0].axis("off")
    ax[0,1].axis("off")
    ax[1,0].axis("off")
    ax[1,1].axis("off")
    fig.tight_layout()
    plt.savefig("gray.jpg")
    plt.show()

Reference article
Grayscale and binarization in image processing

Guess you like

Origin blog.csdn.net/weixin_42491648/article/details/131669756