图像处理(Image Process)对输入的图像作某种变换,输出仍然是图像,不涉及或者很少涉及图像内容的分析。例如:图像增强、图像去噪、图像压缩、图像恢复、二值图像处理,以及基于阈值的图像分割等等。
图像分析:对图像内容进行分析,提取有意义的特征,以便于后续的处理。
计算机视觉(CV, Computer Vision):对图像分析得到的特征进行分析,提取场景的语义表示,让计算机具有人眼和人脑的能力。
图像的读取、色彩空间和灰度
import cv2 import numpy as np import matplotlib.pyplot as plt def img_show(): # 原图 img = cv2.imread("img-input/station-320-180.png", cv2.IMREAD_COLOR) img_source = cv2.cvtColor(img, cv2.COLOR_BGR2RGB) # 灰度图 img_gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) img_gray_source = cv2.cvtColor(img_gray, cv2.COLOR_BGR2RGB) images = [img, img_source, img_gray, img_gray_source] for i in range(4): plt.subplot(2, 2, i+1) plt.imshow(images[i]) plt.savefig("img-output/colorspace_gray.png") plt.show() if __name__ == '__main__': img_show()
result:第一行是原图,第二行是灰度图,第一列是BGR色彩空间,第二列是RGB色彩空间。可见,要得到灰度图,要进行两次转换。
除此之外,也可以直接读取图片为灰度图,但由于默认BGR colorspace,仍然需要进行一次转换,转换为RGB色彩空间:
img = cv2.imread("img-input/station-320-180.png", 0) rows, cols = img.shape source = cv2.cvtColor(img, cv2.COLOR_BGR2RGB) images = [img, source] for i in range(2): plt.subplot(2, 1, i+1) plt.imshow(images[i]) plt.show()
result:两张二维图像,没有色彩通道这一维度,1. 默认的BGR,转换为RGB便是常见的灰度图。
阈值Image Thresholding:
简单的阈值操作是,For every pixel, the same threshold value is applied. If the pixel value is smaller than the threshold, it is set to 0, otherwise it is set to a maximum value.
二值化(binarization)或阈值化(threshold):旨在提取图像中的目标物体,将背景以及噪声区分开来。通常会设定一个阈值T,通过T将图像的像素划分为两类:大于T的像素群和小于T的像素群。
案例如下,需要注意的是,以下案例是在RGB空间的灰度图上进行thresholding操作的。
def thresholding_apply(): img = cv2.imread("img-input/station-320-180.png", 0) rows, cols = img.shape img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB) ret, thresh1 = cv2.threshold(img, 127, 255, cv2.THRESH_BINARY) ret, thresh2 = cv2.threshold(img, 127, 255, cv2.THRESH_BINARY_INV) ret, thresh3 = cv2.threshold(img, 127, 255, cv2.THRESH_TRUNC) ret, thresh4 = cv2.threshold(img, 127, 255, cv2.THRESH_TOZERO) ret, thresh5 = cv2.threshold(img, 127, 255, cv2.THRESH_TOZERO_INV) images = [img, thresh1, thresh2, thresh3, thresh4, thresh5] titles = ['Original Image','BINARY','BINARY_INV','TRUNC','TOZERO','TOZERO_INV'] for i in range(6): plt.subplot(3, 2, i+1) plt.title(titles[i]) plt.xticks([]) plt.yticks([]) plt.imshow(images[i]) plt.savefig("img-output/thresholding_rgb.png") plt.show() if __name__ == '__main__': thresholding_apply()
result:原图为灰度图,binary是把像素值小于127的置为0,大于127置为1,binary_inv则相反。tozero是大于127的不变,小于127置为0,tozero_inv则相反。trunc则是小于127不变,大于127渐变。
稍微复杂的阈值操作暂略。
图像的形态学操作(Morphological operations):Morphological transformations are some simple operations based on the image shape. It is normally performed on binary images. It needs two inputs, one is our original image, second one is called structuring element or kernel which decides the nature of operation.
参考:https://docs.opencv.org/4.x/d9/d61/tutorial_py_morphological_ops.html
腐蚀和膨胀(Erosion and Dilate)
梯度算子(Morphological Gradient)
开运算和闭运算(Opening and Closing)
顶帽运算和黑帽运算(Top Hat and Black Hat)
标签:plt,img,127,cv2,图像处理,灰度,图像 From: https://www.cnblogs.com/zhaoke271828/p/17145236.html