灰度是将图像从其他颜色空间(例如RGB, CMYK, HSV等)转换为灰色阴影的过程。它在完全黑色和完全白色之间变化。
灰度的重要性–
- 尺寸缩小:例如在RGB图像中, 有三个颜色通道并具有三个维度, 而灰度图像是一维的。
- 降低模型复杂度:考虑在10x10x3像素的RGB图像上训练神经文章。输入层将有300个输入节点。另一方面, 同一神经网络只需要100个输入节点即可获得灰度图像。
- 为了使其他算法起作用:有许多算法被定制为仅适用于灰度图像, 例如OpenCV库中预先实现的Canny边缘检测功能仅适用于灰度图像。
以下是对图像进行灰度处理的代码-
# importing opencv
import cv2
# Load our input image
image = cv2.imread( 'C:\\Documents\\full_path\\tomatoes.jpg' )
cv2.imshow( 'Original' , image)
cv2.waitKey()
# We use cvtColor, to convert to grayscale
gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
cv2.imshow( 'Grayscale' , gray_image)
cv2.waitKey( 0 )
# window shown waits for any key pressing event
cv2.destroyAllWindows()
输入图片:
输出如下:
更快的代码–
# Faster method
import cv2
# The second argument zero specifies that
# image is to be read in grayscale mode.
img = cv2.imread( 'C:\\Documents\\full_path\\tomatoes.jpg' , 0 )
cv2.imshow( 'Grayscale' , img)
cv2.waitKey()
cv2.destroyAllWindows()
首先, 你的面试准备可通过以下方式增强你的数据结构概念:Python DS课程。
评论前必须登录!
注册