def image_corrosion(img_src, img_dst): src = read_img(img_src) ## b.设置卷积核5*5 kernel = np.ones((5, 5), np.uint8) ## c.图像的腐蚀,默认迭代次数 erosion = cv2.erode(src, kernel) #腐蚀后,图像明显变黑很多 cv2.imwrite(img_dst, erosion)
def image_sharpen(img_src, img_dst): """ :param img_src:传入图片的路径 :param img_dst:模糊化后图片的路径 :return: """ image = read_img(img_src) kernel = np.array([[0, -1, 0], [-1, 5, -1], [0, -1, 0]], np.float32) # 锐化 dst = cv2.filter2D(image, -1, kernel=kernel) #滤波器 cv2.imwrite(img_dst, dst)
def gray_scale(img_src_path, img_dst_path): """ :param img_src_path:图片的源路径 :param img_dst_path:灰度化的图片路径 :return:img_dst_path """ img = read_img(img_src_path) gray = cv2.cvtColor( img, cv2.COLOR_BGR2GRAY) #RGB颜色空间以R(Red:红)、G(Green:绿)、B(Blue:蓝) cv2.imwrite(img_dst_path, gray) return img_dst_path
def image_dilate(img_src, img_dst): #膨胀类似与 '领域扩张' ,将图像的高亮区域或白色部分进行扩张,其运行结果图比原图的高亮区域更大 src = read_img(img_src) ## 设置卷积核5*5 kernel = np.ones((5, 5), np.uint8) ## 图像的腐蚀,默认迭代次数 erosion = cv2.erode(src, kernel) ## 图像的膨胀 dst = cv2.dilate(erosion, kernel) #在图像上进行从左到右,从上到下的平移,如果方框中存在白色,那么这个方框内所有的颜色都是白色 cv2.imwrite(img_dst, dst)
def image_blur(img_src, img_dst): """ :param img_src:传入图片的路径 :param img_dst:模糊化后图片的路径 :return: """ src = read_img(img_src) dst = cv2.blur(src, (15, 1)) #均值滤波 # cv2.imshow("blur_demo", dst) cv2.imwrite(img_dst, dst)
def binary_local_threshold(img_src,img_dst): """ :param img_src:传入图片的路径 :param img_dst:模糊化后图片的路径 :return: """ src = read_img(img_src) gray = cv2.cvtColor(src, cv2.COLOR_RGB2GRAY) #把输入图像灰度化 #自适应阈值化能够根据图像不同区域亮度分布,改变阈值 binary = cv2.adaptiveThreshold(gray, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C,cv2.THRESH_BINARY, 25, 10) # cv2.namedWindow("binary1", cv2.WINDOW_NORMAL) # cv2.imshow("binary1", binary) cv2.imwrite(img_dst, binary)
output) return output def gasuss_noise(image, mean=0, var=0.001): ''' 添加高斯噪声 mean : 均值 var : 方差 ''' image = np.array(image / 255, dtype=float) noise = np.random.normal(mean, var**0.5, image.shape) out = image + noise if out.min() < 0: low_clip = -1. else: low_clip = 0. out = np.clip(out, low_clip, 1.0) out = np.uint8(out * 255) cv2.imwrite( "D:/pythonProject/ImageRecognitionAndClassification/UnitTest/ImgReco/small_tool_6.png", out) return out img_path = "D:/pythonProject/ImageRecognitionAndClassification/UnitTest/ImgReco/small_tool_2.png" #gasuss_noise(image=read_img(img_path)) sp_noise(image=read_img(img_path), prob=0.1)