import cv2 import numpy as np import math # RGB色彩空间 def Rgb(path): img = cv2.imread(path, 1) b = img[:, :, 0] g = img[:, :, 1] r = img[:, :, 2] cv2.imwrite('./media/RGB/b.jpg', b) cv2.imwrite('./media/RGB/g.jpg', g) cv2.imwrite('./media/RGB/r.jpg', r) # HSV色彩空间 def Hsv(path): img = cv2.imread(path, 1) hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV) h = hsv[:, :, 0] s = hsv[:, :, 1] v = hsv[:, :, 2] cv2.imwrite("./media/HSV/h.jpg", h) cv2.imwrite("./media/HSV/s.jpg", s) cv2.imwrite("./media/HSV/v.jpg", v) def Un(path): img = cv2.imread(path, 0) result = ~img cv2.imwrite("./media/UN/un.jpg", result) def Add(paths): # 只做了两个图像叠加,多个怎么做 img1 = cv2.imread(paths[0], 1) img2 = cv2.imread(paths[1], 1) img = cv2.add(img1, img2) cv2.imwrite("./media/ADD/add.jpg", img) def Subtract(paths): img1 = cv2.imread(paths[0], 1) img2 = cv2.imread(paths[1], 1) img = cv2.subtract(img1, img2) cv2.imwrite("./media/SUBTRACT/subtract.jpg", img) # 形态学操作 def OpenAndClose(path): # 二值转换 img = cv2.imread(path, 1) gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) retval, src = cv2.threshold(gray, 127, 255, cv2.THRESH_BINARY) # 定义十字形结构元素 kernel = cv2.getStructuringElement(cv2.MORPH_CROSS, (10, 10), (-1, -1)) # 对二值图进行开运算和闭运算操作 im_op = cv2.morphologyEx(src, cv2.MORPH_OPEN, kernel) im_cl = cv2.morphologyEx(src, cv2.MORPH_CLOSE, kernel) cv2.imwrite('./media/OPANDCL/op.png', im_op) cv2.imwrite('./media/OPANDCL/cl.png', im_cl) # 边缘检测和图像增强 def Sharp(path): CRH = cv2.imread(path, 1) gradient = np.zeros_like(CRH) CRH = CRH.astype('float') h, w = CRH.shape[:2] for x in range(h - 1): for y in range(w - 1): gx = abs(CRH[x + 1, y] - CRH[x, y]) gy = abs(CRH[x, y + 1] - CRH[x, y]) gradient[x, y] = gx + gy sharp = CRH + gradient sharp = np.where(sharp > 255, 255, sharp) sharp = np.where(sharp < 0, 0, sharp) gradient = gradient.astype('uint8') sharp = sharp.astype('uint8') cv2.imwrite('./media/SHARP/gradient.png', gradient) cv2.imwrite('./media/SHARP/sharp.png', sharp) # 镜像 def Turn(path): img = cv2.imread(path) # 水平镜像 horizontal = cv2.flip(img, 1, dst=None) # 垂直镜像 vertical = cv2.flip(img, 0, dst=None) # 对角镜像 cross = cv2.flip(img, -1, dst=None) cv2.imwrite("./media/TURN/horizontal.jpg", horizontal) cv2.imwrite("./media/TURN/vertical.jpg", vertical) cv2.imwrite("./media/TURN/cross.jpg", cross) # 腐蚀 def Erosion(path): src = cv2.imread(path, cv2.IMREAD_UNCHANGED) # 10x10的交叉型结构元 kernel = cv2.getStructuringElement(cv2.MORPH_CROSS, (10, 10), (-1, -1)) erosion = cv2.erode(src, kernel) cv2.imwrite("./media/EROSION/erosion.jpg", erosion) # 膨胀 def Dilation(path): src = cv2.imread(path, cv2.IMREAD_UNCHANGED) # 10x10的交叉结构元 kernel = cv2.getStructuringElement(cv2.MORPH_CROSS, (10, 10), (-1, -1)) dilation = cv2.dilate(src, kernel) cv2.imwrite("./media/DILATION/dilation.png", dilation) # 噪声及滤波 def Noise(path): image = cv2.imread(path, cv2.IMREAD_GRAYSCALE) output = np.zeros(image.shape, np.uint8) output2 = np.zeros(image.shape, np.uint8) # 叠加噪声 for i in range(image.shape[0]): for j in range(image.shape[1]): if image[i][j] < 40: # 添加食盐噪声 output[i][j] = 255 elif image[i][j] > 200: # 添加胡椒噪声 output[i][j] = 0 # 不添加噪声 else: output[i][j] = image[i][j] # 均值滤波 for i in range(output.shape[0]): for j in range(output.shape[1]): ji = 1.0 for n in range(-1, 2): if 0 <= i < output.shape[0] and 0 <= j + n < output.shape[1]: ji = ji * output[i][j + n] output2[i][j] = int(ji ** (1 / 3)) cv2.imwrite("./media/NOISE/noise.jpg", output) cv2.imwrite("./media/NOISE/meanFilter.jpg", output2) # # 扩展平移缩放 # def moveandchange(path): # img = cv2.imread(path, 1) # l, w, h = img.shape # # 扩展,使用双线性插值法 # img = cv2.resize(img, (0, 0), fx=2, fy=2, interpolation=cv2.INTER_LINEAR) # height, width, channel = img.shape # # 移动 # M = np.float32([[1, 0, 30], [0, 1, 60]]) # img = cv2.warpAffine(img, M, (width, height)) # # 旋转 # rows, cols, depth = img.shape # M = cv2.getRotationMatrix2D((cols / 2, rows / 2), 45, 1) # dst = cv2.warpAffine(img, M, (width, height)) # # cv2.imwrite('./media/CHANGE/out.png', dst) # # 仿射变换 # def affine(path): # img = cv2.imread(path) # rows, cols = img.shape[: 2] # # 设置图像仿射变化矩阵 # post1 = np.float32([[50, 50], [200, 50], [50, 200]]) # post2 = np.float32([[10, 100], [200, 50], [100, 250]]) # M = cv2.getAffineTransform(post1, post2) # # 图像仿射变换 # result = cv2.warpAffine(img, M, (rows, cols)) # cv2.imwrite("./media/AFFINE/affine.jpg", result)