OpenCV(Open Source Computer Vision)是一个计算机视觉库,包含对图片或视频执行操作的各种功能。它最初由Intel开发,但后来由Willow Garage维护,现在由ITSEZ维护。这个库是跨平台的,它可以在多种编程语言上使用,如Python、C++等。
让我们讨论一下可视化图像的不同方法,我们将以不同的格式表示图像,如灰度、RGB比例、热图、边缘图、光谱图等。
RGB图像:
RGB图像由三个不同通道的线性组合表示,分别是R(红色)、G(绿色)和B(蓝色)。对于单通道,此颜色空间中的像素强度由0到255之间的值表示。因此,由像素表示的一种颜色的可能性数量约为1600万[255 x 255 x 255]。
# Python program to read image as RGB # Importing cv2 and matplotlib module import cv2 import matplotlib.pyplot as plt # reads image as RGB img = cv2.imread( 'g4g.png' ) # shows the image plt.imshow(img) |
输出:
灰度图像:
灰度图像只包含一个通道。该颜色空间中的像素强度由0到255之间的值表示。因此,由像素表示的一种颜色的可能性是256。
# Python program to read image as GrayScale # Importing cv2 module import cv2 # Reads image as gray scale img = cv2.imread( 'g4g.png' , 0 ) # We can alternatively convert # image by using cv2color img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) # Shows the image cv2.imshow( 'image' , img) cv2.waitKey( 0 ) cv2.destroyAllWindows() |
输出:
YCrCb颜色空间:
Y代表亮度或亮度分量,Cb和Cr代表色度分量。Cb代表蓝色差异(蓝色分量和亮度分量的差异)。Cr代表红色差异(红色成分和亮度成分的差异)。
# Python program to read image # as YCrCb color space # Import cv2 module import cv2 # Reads the image img = cv2.imread( 'g4g.png' ) # Convert to YCrCb color space img = cv2.cvtColor(img, cv2.COLOR_BGR2YCrCb) # Shows the image cv2.imshow( 'image' , img) cv2.waitKey( 0 ) cv2.destroyAllWindows() |
输出:
HSV颜色空间:
H: 色调代表主导波长。 S: 饱和度代表颜色的深浅程度。 五: 值表示强度。
# Python program to read image # as HSV color space # Importing cv2 module import cv2 # Reads the image img = cv2.imread( 'g4g.png' ) # Converts to HSV color space img = cv2.cvtColor(img, cv2.COLOR_BGR2HSV) # Shows the image cv2.imshow( 'image' , img) cv2.waitKey( 0 ) cv2.destroyAllWindows() |
输出:
实验室颜色空间:
L- 代表轻盈。 A—— 颜色成分从绿色到洋红色不等。 B- 颜色成分从蓝色到黄色。
# Python program to read image # as LAB color space # Importing cv2 module import cv2 # Reads the image img = cv2.imread( 'g4g.png' ) # Converts to LAB color space img = cv2.cvtColor(img, cv2.COLOR_BGR2LAB) # Shows the image cv2.imshow( 'image' , img) cv2.waitKey( 0 ) cv2.destroyAllWindows() |
输出:
图像的边缘映射:
边缘映射可以通过各种过滤器获得,比如拉普拉斯, 索贝尔 等。在这里,我们使用拉普拉斯生成边映射。
# Python program to read image # as EdgeMap # Importing cv2 module import cv2 # Reads the image img = cv2.imread( 'g4g.png' ) laplacian = cv2.Laplacian(img, cv2.CV_64F) cv2.imshow( 'EdgeMap' , laplacian) cv2.waitKey( 0 ) cv2.destroyAllWindows() |
输出:
图像的热图:
在热图表示法中,矩阵中包含的单个值表示为颜色。
# Python program to visualize # Heat map of image # Importing matplotlib and cv2 import matplotlib.pyplot as plt import cv2 # reads the image img = cv2.imread( 'g4g.png' ) # plot heat map image plt.imshow(img, cmap = 'hot' ) |
输出:
光谱图像地图:
光谱图像贴图获得场景图像中每个像素的光谱。
# Python program to visualize # Spectral map of image # Importing matplotlib and cv2 import matplotlib.pyplot as plt import cv2 img = cv2.imread( 'g4g.png' ) plt.imshow(img, cmap = 'nipy_spectral' ) |
输出: