物体检测核心预处理器中的_rgb_to_grayscale()函数在Python中的中文标题
发布时间:2023-12-15 20:33:29
_rgb_to_grayscale()函数的中文标题:将RGB图像转为灰度图像
使用例子如下:
import cv2
def _rgb_to_grayscale(rgb_image):
"""
将RGB图像转为灰度图像
参数:
- rgb_image: RGB图像,形状为(height, width, 3)
返回值:
- 灰度图像,形状为(height, width)
使用例子:
python
import cv2
# 读取RGB图像
img_rgb = cv2.imread('input.jpg')
# 将RGB图像转为灰度图像
img_gray = _rgb_to_grayscale(img_rgb)
# 显示灰度图像
cv2.imshow('Grayscale Image', img_gray)
cv2.waitKey(0)
cv2.destroyAllWindows()
"""
gray_image = cv2.cvtColor(rgb_image, cv2.COLOR_RGB2GRAY)
return gray_image
# 读取RGB图像
img_rgb = cv2.imread('input.jpg')
# 将RGB图像转为灰度图像
img_gray = _rgb_to_grayscale(img_rgb)
# 显示灰度图像
cv2.imshow('Grayscale Image', img_gray)
cv2.waitKey(0)
cv2.destroyAllWindows()
在上述例子中,首先通过cv2.imread()函数读取一个RGB图像,然后调用_rgb_to_grayscale()函数将RGB图像转为灰度图像。最后使用cv2.imshow()函数展示灰度图像。
