1.导入CV模块
import cv2 as cv
2.读取单通道灰度图及处理
#读取图片
color_img = cv.imread("C:/Users/Administrator/Desktop/New_Study/IMAGE/flower.jpg")
print(color_img.shape)
cv.namedWindow('color_img',cv.WINDOW_NORMAL)
cv.imshow("Flower",color_img)
#读取单通道灰度图
gray_img = cv.imread("C:/Users/Administrator/Desktop/New_Study/IMAGE/flower.jpg",cv.IMREAD_GRAYSCALE)
print(gray_img.shape)
cv.namedWindow('gray_img',cv.WINDOW_NORMAL)
cv.imshow("Flower_Gray",gray_img)
#保存单通道灰度图,打开任然是三通道
#cv.IMWRITE_JPEG_QUALITY指定JPG的质量,范围0-100,默认95,越高画质越好,文件越大
#cv.IMWRITE_PNG_CONPRESSION指定PNG质量,范围0-9,默认3,越高文件越小,画质越差
cv.imwrite("C:/Users/Administrator/Desktop/New_Study/IMAGE/flower_gray.jpg",
gray_img,(cv.IMWRITE_JPEG_QUALITY,80))
reload_gray = cv.imread("C:/Users/Administrator/Desktop/New_Study/IMAGE/flower_gray.jpg")
print(reload_gray.shape)
cv.namedWindow('reload_gray',cv.WINDOW_NORMAL)
cv.imshow("reload_gray",reload_gray)
cv.waitKey()
3.放缩、裁剪等处理
#读取图片
flower = cv.imread("C:/Users/Administrator/Desktop/New_Study/IMAGE/flower.jpg")
cv.namedWindow('flower',cv.WINDOW_NORMAL)
cv.imshow('flower',flower)
#图像缩放200x200
flower_200x200 = cv.resize(flower,(200,200))
cv.namedWindow('flower_200x200',cv.WINDOW_NORMAL)
cv.imshow('flower_200x200',flower_200x200)
#不直接指定缩放大小,通过fx,fy 指定缩放比例,0.5就是原长宽的一半,插值法默认是cv.INTER_LINEAR,这里采用最近邻
flower_100x100 = cv.resize(flower_200x200,(0,0),fx=0.5,fy=0.5,interpolation=cv.INTER_NEAREST)
cv.namedWindow('flower_100x100',cv.WINDOW_NORMAL)
cv.imshow('flower_100x100',flower_100x100)
#在上图基础上,上下左右各贴50,50,0,0的像素边框,生成300x300的图像
flower_border =cv.copyMakeBorder(flower,50,50,0,0,cv.BORDER_CONSTANT,value=[0,255,0])
cv.namedWindow('flower_border',cv.WINDOW_NORMAL)
cv.imshow('flower_border',flower_border)
#对图像局部进行裁剪
"""
图像坐标系:[X,Y]
"""
patch_flower = flower[200:400,0:-200]
cv.namedWindow('patch_flower',cv.WINDOW_NORMAL)
cv.imshow('patch_flower',patch_flower)
#防止图像展示窗口卡死
cv.waitKey()
#保存处理过的图像
cv.imwrite('C:/Users/Administrator/Desktop/New_Study/IMAGE/Result_img/flower_200x200.jpg',flower_200x200)
cv.imwrite('C:/Users/Administrator/Desktop/New_Study/IMAGE/Result_img/flower_100x100.jpg',flower_100x100)
cv.imwrite('C:/Users/Administrator/Desktop/New_Study/IMAGE/Result_img/flower_border.jpg',flower_border)
cv.imwrite('C:/Users/Administrator/Desktop/New_Study/IMAGE/Result_img/patch_flower.jpg',patch_flower)
4.饱和度处理
#减小饱和度会让图片损失鲜艳,变得更灰
flower = cv.imread("C:/Users/Administrator/Desktop/New_Study/IMAGE/flower.jpg")
cv.namedWindow('flower',cv.WINDOW_NORMAL)
cv.imshow('flower',flower)
colorless_hsv = flower.copy()
colorless_hsv[:,:,1] = 0.5*colorless_hsv[:,:,1]
colorless_img = cv.cvtColor(colorless_hsv,cv.COLOR_HSV2BGR)
cv.namedWindow('colorless_img',cv.WINDOW_NORMAL)
cv.imshow('colorless_img',colorless_img)
cv.waitKey()
5.亮度处理
#读取图片
flower = cv.imread("C:/Users/Administrator/Desktop/New_Study/IMAGE/flower.jpg")
cv.namedWindow('flower',cv.WINDOW_NORMAL)
cv.imshow('flower',flower)
#较小饱和度,图像损失鲜艳,变得灰暗
colorless_hsv = flower.copy()
colorless_hsv[:,:,1] = 0.5*colorless_hsv[:,:,1]
colorless_img = cv.cvtColor(colorless_hsv,cv.COLOR_HSV2BGR)
cv.namedWindow('colorless_img',cv.WINDOW_NORMAL)
cv.imshow('colorless_img',colorless_img)
#减小明度为原来的一半
darker_hsv = flower.copy()
darker_hsv[:,:,2] = 0.5*darker_hsv[:,:,2]
darker_img = cv.cvtColor(darker_hsv,cv.COLOR_HSV2BGR)
cv.namedWindow('darker_img',cv.WINDOW_NORMAL)
cv.imshow('darker_img',darker_img)
cv.waitKey()
cv.imwrite('C:/Users/Administrator/Desktop/New_Study/IMAGE/Result_img/flower.jpg',flower)
特别注明:本文属于Python OpenCV学习笔记,不以盈利为目的,纯手工码字不容易,若整理的笔记中,对您有所助益,麻烦点个赞或者收藏,万分感谢!如有构成侵权的地方,请联系作者删除,谢谢合作!