1#色彩空间转化
def color_space_demo(image): gray = cv.cvtColor(image, cv.COLOR_BGR2GRAY) cv.imshow("gray", gray) hsv = cv.cvtColor(image, cv.COLOR_BGR2HSV) cv.imshow("hsv", hsv) yuv = cv.cvtColor(image, cv.COLOR_BGR2YUV) cv.imshow("yuv", yuv) Ycrcb = cv.cvtColor(image, cv.COLOR_BGR2YCrCb) cv.imshow("ycrcb", Ycrcb)
2几种常见模糊
#3模糊 import cv2 as cv import numpy as np #均值模糊 def blur_demo(image): dst = cv.blur(image, (5, 5))#参数1为水平方向模糊数值,参数2为竖直方向 cv.imshow("blur_demo", dst) #中值模糊--可以用于去除椒盐去噪 def median_blur_demo(image): dst = cv.medianBlur(image, 5) cv.imshow("median_blur_demo", dst) #*定义 def custom_blur_demo(image): #kernel = np.ones([5, 5], np.float32)/25 kernel = np.array([[0, -1, 0],[-1, 5, -1],[0, -1, 0]], np.float32) dst = cv.filter2D(image, -1, kernel=kernel) cv.imshow("custom_blur_demo", dst) print("--------- Hello Python ---------") src = cv.imread("D:/vcprojects/images/demo.png") cv.namedWindow("input image", cv.WINDOW_AUTOSIZE) cv.imshow("input image", src) custom_blur_demo(src) cv.waitKey(0) cv.destroyAllWindows()
3高斯模糊
#高斯模糊--重要,有效,滤镜效果,语法:st = cv.GaussianBlur(src, (0, 0), 15) src = cv.imread("D:/vcprojects/images/example.png") cv.namedWindow("input image", cv.WINDOW_AUTOSIZE) cv.imshow("input image", src) t1 = cv.getTickCount() #gaussian_noise(src) t2 = cv.getTickCount() time = (t2 - t1)/cv.getTickFrequency() print("time consume : %s"%(time*1000)) dst = cv.GaussianBlur(src, (0, 0), 15) cv.imshow("Gaussian Blur", dst) #高斯双边--美颜磨皮效果 #均值迁移--羽化效果 import cv2 as cv import numpy as np def bi_demo(image):#高斯双边 dst = cv.bilateralFilter(image, 0, 100, 15) cv.imshow("bi_demo", dst) def shift_demo(image):#均值迁移 dst = cv.pyrMeanShiftFiltering(image, 10, 50) cv.imshow("shift_demo", dst) print("--------- Hello Python ---------") src = cv.imread("C:/Users/wml/Desktop/wml/ym.jpg") cv.namedWindow("input image", cv.WINDOW_AUTOSIZE) cv.imshow("input image", src) # bi_demo(src) shift_demo(src) cv.waitKey(0)
cv.destroyAllWindows()