Converts a color RGB image to a grayscale image, where the pixel value is converted to
Grayscale Value =0.2989 * R + 0.5870 * G + 0.1140 * B
The original color RGB image is a three-channel, converted into a single-channel grayscale image
You wrote a Python program that implements the conversion task:
#-*-Coding:utf-8-*-
# convert RGB color image to grayscale image
import cv2
import numpy Lena
= Cv2.imread (' F:\CVImages\ Lena.jpg ')
img = Lena
height = img.shape[0]
width = img.shape[1]
channels = img.shape[2]
value = [0] * 3
gray_img = Numpy.zeros ([height, width], numpy.uint8) for
row in range (height): A for column in
range (Widt h):
for Chan in range (channels):
Value[chan] = img[row, column, chan]
R = value[2]
G = value[1]
b = value[0]
# new_value = 0.2989 * r + 0.5870 * G + 0.1140 * B
new_value = 0.2989 * R + 0.5870 * G + 0.1140 * B # Convert to grayscale pixel
gray_img[row, column] = New_value
cv2.imshow (' Original image ', img)
cv2.waitkey (0)
Cv2.imshow (' Gray image ', gray_img)
cv2.waitkey (0)
cv2.imwrite (' gray_lena.jpg ', gray_img)