Hi, I'm Ramu. Image processing implements the familiar grayscale.
Grayscale is often used as a pre-process for various image processing. If you want to learn image processing, be sure to know it. Grayscale is a method of expressing an image using black, white, and shades of gray, which are intermediate colors. Monochrome images have only two black and white colors, while grayscale images usually use 256 colors.
Each pixel is calculated by the following formula.
grayscale.py
import cv2
import matplotlib.pyplot as plt
import numpy as np
plt.gray()
def grayscale(img):
#Array creation for grayscale images
dst = np.zeros((img.shape[0], img.shape[1]))
#Grayscale
dst[:,:] = (0.2126*img[:,:,2] + 0.7152*img[:,:,1] + 0.0722*img[:,:,0]).astype(np.uint8)
return dst
#Image reading
img = cv2.imread('image.jpg')
#Grayscale
gray = grayscale(img)
#Save image
cv2.imwrite('result.jpg', gray)
#Image display
plt.imshow(gray)
plt.show()
The image on the left is the input image, and the image on the right is the output image. You have created a solid grayscale image.
If you have any questions, please feel free to contact us. imori_imori's Github has the official answer, so please check that as well. ..
Recommended Posts