Color RGB image to grayscale image

Source: Internet
Author: User

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)


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.