## Digital image processing (16): The image color space conversion and processing image gray OpenCV

A gray image Principle

Second image color space conversion

3 OpenCV gray image processing

Reference material

# A gray image Principle

In the image processing algorithms are often required to convert a color image into a grayscale image. Converting the gray image is a grayscale image of a color image process. A color image typically includes R, G, B three components, respectively, showing equal colors red, green and blue, the color is gray-scale image R, G, B three component process. Each pixel having a grayscale image samples only one color, the gray level is a multi-color black and white depth is located between the large gray value pixels brighter, darker contrary, the maximum pixel value of 255 ( represents white), the pixel value of the minimum is 0 (black). It assumed that the color of a point by the RGB (R, G, B), which is common gradation processing algorithm:

Algorithm name                           Algorithm formula

$gray=\max (R,G,B)$

Floating-point grayscale processing

$gray=0.3R+0.59G+0.11B$

$gray=(30R+59G+11B)/100$

$gray=(28R+151G+77B)>>8$

$gray=(R,G,B)/3$

$gray=0.299R+0.587G+0.144B$

Wherein the gradation processing method is common to three RGB components may be summed and then averaged, the more accurate method is to set different weights, the gradation RGB components divided in different proportions. For example, the human eye senses lowest sensitivity blue, green is the most sensitive, so the RGB ratio in accordance with the weighted average 0.299,0.587,0.144 can get a more reasonable grayscale image

$gray=0.299R+0.587G+0.144B$

# Second image color space conversion

In daily life, most color image we see is RGB type, but in image processing, often need to use the gray image, binary image, HSV, HSI and other colors, OpenCV provides cvtColor () function these functions.

In OpenCV  cvtColor () function form as follows:

dst = cv2.cvtColor(src, code[, dst[, dstCn]])

src represents the input image, the need for the original image color space conversion;

dst indicates an output image, which is consistent with the size and depth of the src;

It represents a conversion code or identification code;

dstCn channel represents the target image, when a value of 0, and the code has src decisions.

The effect is a function of converting an image from one color space to another color space, wherein, the RGB refers to Red, Green and Blue, an image formed of three channels (channel); Gray represents only a gray value channel; hue comprising the HSV (hue), saturation (saturation) and the Value (brightness) of three channels. In OpenCV, the common color space conversion comprises identification CV_BGR2BGRA, CV_RGB2GRAY, CV_GRAY2RGB, CV_BGR2HSV, CV_BGR2XYZ, CV_BGR2HLS like.

The following is a call cvtColor () function to image color space conversion (BGR, RGB, GRAY, HSV , YCrCb, HLS, XYZ, LAB , and YUV)

Code as follows:

``````#encoding:utf-8
import cv2
import numpy as np
import matplotlib.pyplot as plt

#读取原始图像

#BGR转换为RGB
img_RGB = cv2.cvtColor(img_BGR, cv2.COLOR_BGR2RGB)

#灰度化处理
img_GRAY = cv2.cvtColor(img_BGR, cv2.COLOR_BGR2GRAY)

#BGR转HSV
img_HSV = cv2.cvtColor(img_BGR, cv2.COLOR_BGR2HSV)

#BGR转YCrCb
img_YCrCb = cv2.cvtColor(img_BGR, cv2.COLOR_BGR2YCrCb)

#BGR转HLS
img_HLS = cv2.cvtColor(img_BGR, cv2.COLOR_BGR2HLS)

#BGR转XYZ
img_XYZ = cv2.cvtColor(img_BGR, cv2.COLOR_BGR2XYZ)

#BGR转LAB
img_LAB = cv2.cvtColor(img_BGR, cv2.COLOR_BGR2LAB)

#BGR转YUV
img_YUV = cv2.cvtColor(img_BGR, cv2.COLOR_BGR2YUV)

#调用matplotlib显示处理结果
titles = ['BGR', 'RGB', 'GRAY', 'HSV', 'YCrCb', 'HLS', 'XYZ', 'LAB', 'YUV']
images = [img_BGR, img_RGB, img_GRAY, img_HSV, img_YCrCb,
img_HLS, img_XYZ, img_LAB, img_YUV]
for i in range(9):
plt.subplot(3, 3, i+1), plt.imshow(images[i], 'gray')
plt.title(titles[i])
plt.xticks([]),plt.yticks([])
plt.show()

cv2.waitKey(0)
cv2.destroyAllWindows()``````

Operating results as shown below:

# 3 OpenCV gray image processing

The following describes the maximum gradation processing , the average gradation processing and a weighted average gradation processing algorithms.

A method gray value image is equal to the color R, G, B maximum three components, the following formula:

$gray=\max (R,G,B)$

Code as follows:

``````#encoding:utf-8
import cv2
import numpy as np
import matplotlib.pyplot as plt

#读取原始图像

#获取图像高度和宽度
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])
#灰度图像素赋值 gray=max(R,G,B)
grayimg[i,j] = np.uint8(gray)

#显示图像
cv2.imshow("src", img)
cv2.imshow("gray", grayimg)

#等待显示
cv2.waitKey(0)
cv2.destroyAllWindows()``````

Run results are shown in FIG :( gradation processing bright side effects )

A method gray value image is equal to the color R, the average of the summation G, B gradation values ​​of the three components, which is calculated as follows:

$gray=(R,G,B)/3$

Code as follows:

``````#encoding:utf-8
import cv2
import numpy as np
import matplotlib.pyplot as plt

#读取原始图像

#获取图像高度和宽度
height = img.shape[0]
width = img.shape[1]

#创建一幅图像
grayimg = np.zeros((height, width, 3), np.uint8)
# print (grayimg)

#图像平均灰度处理方法
for i in range(height):
for j in range(width):
#灰度值为RGB三个分量的平均值
gray = (int(img[i,j][0]) + int(img[i,j][1]) + int(img[i,j][2]))  /  3
grayimg[i,j] = np.uint8(gray)

#显示图像
cv2.imshow("src", img)
cv2.imshow("gray", grayimg)

#等待显示
cv2.waitKey(0)
cv2.destroyAllWindows()``````

Operating results as shown below:

## 3.3 weighted average gradation processing

The method according to the importance of color, the weighted average of the three components with different weights. Gradation processing method is common to three RGB components may be summed and then averaged, the more accurate method is to set different weights, the gradation RGB components divided in different proportions. For example, the minimum sensitivity of the human eye senses blue, green is the most sensitive, so the RGB ratio in accordance with the weighted average 0.299,0.587,0.144 can get more reasonable grayscale image:

$gray=0.299R+0.587G+0.144B$

Code as follows:

``````#encoding:utf-8
import cv2
import numpy as np
import matplotlib.pyplot as plt

#读取原始图像

#获取图像高度和宽度
height = img.shape[0]
width = img.shape[1]

#创建一幅图像
grayimg = np.zeros((height, width, 3), np.uint8)
# print grayimg

#图像加权平均灰度处理方法
for i in range(height):
for j in range(width):
#灰度加权平均法
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)

#显示图像
cv2.imshow("src", img)
cv2.imshow("gray", grayimg)

#等待显示
cv2.waitKey(0)
cv2.destroyAllWindows()``````

Operating results as shown below:

# Reference material

### Guess you like

Origin blog.csdn.net/zaishuiyifangxym/article/details/89810403
Recommended
Ranking
Daily