图像的基本操作 ===================== 读取并显示图像 ------------------ .. code-block:: python import cv2 image = cv2.imread("cloud.jpg") cv2.imshow( # 窗口名称 "云朵", # cv2.imread("cloud.jpg")读取的图片 image ) # 按下任意键 cv2.waitKey() # 销毁窗口 cv2.destroyAllWindows() 保存图像 ---------------------------- .. code-block:: python import cv2 image = cv2.imread("cloud.jpg") cv2.imwrite( # 图片保存路径 "/home/chenjie/Downloads/cloud.jpg", # 图片数据 image ) 获取图像的属性 ---------------------------- .. code-block:: python import cv2 # 读取彩色图像 image = cv2.imread("cloud.jpg") # (292,219,3) <- (像素行,像素列,通道数) print("shape = ", image.shape) # 像素个数 = 像素列数 * 像素行数 * 通道数 print("size = ", image.size) # dtype 图像的数据类型 print("dtype = ", image.dtype) # 读取灰度图像 image_gray = cv2.imread("cloud.jpe", 0) .. note:: ``像素`` 是构成数字图像的基本单位 获取像素的BGR值 ------------------------- .. code-block:: python import cv2 image = cv2.imread("tree.jpg") # 读取坐标位置为100x100的像素 px = image[100,100] # 获取像素的BGR信息 print("100x100像素的BGR值为:", px) blue = image[100,100, 0] # B通道的值 green = image[100,100,1] # G通道的值 red = image[100,100,2] # R通道的值 .. note:: 通常使用一个三维数组来表示一幅图像某个像素点的RGB值。 修改图像中某个区域的像素为白色 -----------------------------------------------