Welcome to DOFBOT-Ultra repository
13. Grayscale Processing
Grayscale Processing
The process of converting a color image into a grayscale image is called image grayscale processing. In a color image, the color of each pixel is determined by three components: R, G, and B. Each component can take values from 0 to 255, so a single pixel can have over 16 million (256256256=16777216) color variations. A grayscale image is a special color image where the R, G, and B components are equal, and the variation range of a pixel point is 256 types. Therefore, in digital image processing, various image formats are generally converted to grayscale images to reduce the computational load of subsequent image processing. The description of grayscale images, like color images, still reflects the overall and local distribution and characteristics of chromaticity and brightness levels of the entire image.
Image grayscale processing. Grayscale processing is the process of converting a color image into a grayscale image. A color image is divided into three components: R, G, and B, displaying various colors such as red, green, and blue. Grayscale is the process of making the R, G, and B components equal. Pixels with larger grayscale values are brighter (maximum pixel value is 255, which is white), while those with smaller values are darker (minimum pixel value is 0, which is black).
The core idea of image grayscale is R = G = B, and this value is also called the grayscale value.
Image grayscale algorithms
1) Maximum method: Set the converted R, G, B values equal to the largest of the three values before conversion, i.e.: R=G=B=max(R, G, B). The grayscale image converted by this method has very high brightness.
2) Average method: Set the converted R, G, B values equal to the average of R, G, B before conversion, i.e.: R=G=B=(R+G+B)/3. The grayscale image produced by this method is relatively soft.
3) Weighted average method: Perform a weighted average of R, G, B values with certain weights, i.e.: Different weight values for R, G, and B produce different grayscale images. Since the human eye is most sensitive to green, followed by red, and least sensitive to blue, using these weights results in a more recognizable grayscale image. Generally, the following formula yields the best grayscale image effect: gray = r0.299 + g0.587 + b*0.114
The following code demonstrates four methods of grayscale processing:
Code path:
~/dofbot_opencv/scripts/3.draw_picture/01_gray.ipynb
x#Method 1 imread#Note: Sometimes the image may not display on the first run, but will on the second runimport cv2import matplotlib.pyplot as pltimg0 = cv2.imread('yahboom.jpg',0)img1 = cv2.imread('yahboom.jpg',1)# print(img0.shape)# print(img1.shape)# cv2.imshow('src',img0)# cv2.waitKey(0)#Original image# img_bgr2rgb1 = cv2.cvtColor(img1, cv2.COLOR_BGR2RGB)# plt.imshow(img_bgr2rgb1)#Grayscale imageimg_bgr2rgb0 = cv2.cvtColor(img0, cv2.COLOR_BGR2RGB) plt.imshow(img_bgr2rgb0) plt.show()
xxxxxxxxxx#Method 2 cvtColor#Note: Sometimes the image may not display on the first run, but will on the second runimport cv2import matplotlib.pyplot as pltimg = cv2.imread('image0.jpg',1)dst = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)# color space conversion 1 data 2 BGR gray#cv2.imshow('dst',dst)#cv2.waitKey(0)#Original image# img_bgr2rgb1 = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)# plt.imshow(img_bgr2rgb1)#Grayscale imageimg_bgr2rgb0 = cv2.cvtColor(dst, cv2.COLOR_BGR2RGB) plt.imshow(img_bgr2rgb0) plt.show()
xxxxxxxxxx#Method 3 Average methodimport cv2import numpy as npimport matplotlib.pyplot as pltimg = cv2.imread('yahboom.jpg',1)imgInfo = img.shapeheight = imgInfo[0]width = imgInfo[1]# RGB R=G=B = gray (R+G+B)/3dst = np.zeros((height,width,3),np.uint8)for i in range(0,height): for j in range(0,width): (b,g,r) = img[i,j] gray = (int(b)+int(g)+int(r))/3 dst[i,j] = np.uint8(gray)#cv2.imshow('dst',dst)#cv2.waitKey(0)#Original image# img_bgr2rgb1 = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)# plt.imshow(img_bgr2rgb1)#Grayscale imageimg_bgr2rgb0 = cv2.cvtColor(dst, cv2.COLOR_BGR2RGB)plt.imshow(img_bgr2rgb0) plt.show()
xxxxxxxxxx#Method 4 Weighted average method# gray = r*0.299+g*0.587+b*0.114import cv2import numpy as npimg = cv2.imread('yahboom.jpg',1)imgInfo = img.shapeheight = imgInfo[0]width = imgInfo[1]dst = np.zeros((height,width,3),np.uint8)for i in range(0,height): for j in range(0,width): (b,g,r) = img[i,j] b = int(b) g = int(g) r = int(r) gray = r*0.299+g*0.587+b*0.114 dst[i,j] = np.uint8(gray)#cv2.imshow('dst',dst)#cv2.waitKey(0)#Original image# img_bgr2rgb1 = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)# plt.imshow(img_bgr2rgb1)#Grayscale imageimg_bgr2rgb0 = cv2.cvtColor(dst, cv2.COLOR_BGR2RGB)plt.imshow(img_bgr2rgb0) plt.show()