做者|Kavya Musty
编译|Flin
来源|mediumpython
咱们常常扫描纸张把它们转换成图像。咱们有各类各样的工具能够在线加强这些图像,使它们的亮度更亮,并消除这些图像中的阴影。若是咱们能够手动去除阴影呢?咱们能够将任何图像做为灰度图像加载到咱们的代码中,并在几秒钟内得到输出,而无需任何应用程序的帮助。git
这能够经过使用基本的Numpy操做和一些open CV函数来实现。为了解释这个过程,咱们使用了下面的图片,它是用手机拍的。github
很明显,有一个阴影须要删除。让咱们开始吧。算法
import cv2 import numpy as np import matplotlib.pyplot as plt
那么,最大值滤波和最小值滤波究竟是什么?数组
让咱们在代码中实现这个概念。机器学习
def max_filtering(N, I_temp): wall = np.full((I_temp.shape[0]+(N//2)*2, I_temp.shape[1]+(N//2)*2), -1) wall[(N//2):wall.shape[0]-(N//2), (N//2):wall.shape[1]-(N//2)] = I_temp.copy() temp = np.full((I_temp.shape[0]+(N//2)*2, I_temp.shape[1]+(N//2)*2), -1) for y in range(0,wall.shape[0]): for x in range(0,wall.shape[1]): if wall[y,x]!=-1: window = wall[y-(N//2):y+(N//2)+1,x-(N//2):x+(N//2)+1] num = np.amax(window) temp[y,x] = num A = temp[(N//2):wall.shape[0]-(N//2), (N//2):wall.shape[1]-(N//2)].copy() return A
让咱们对该函数进行编码。函数
def min_filtering(N, A): wall_min = np.full((A.shape[0]+(N//2)*2, A.shape[1]+(N//2)*2), 300) wall_min[(N//2):wall_min.shape[0]-(N//2), (N//2):wall_min.shape[1]-(N//2)] = A.copy() temp_min = np.full((A.shape[0]+(N//2)*2, A.shape[1]+(N//2)*2), 300) for y in range(0,wall_min.shape[0]): for x in range(0,wall_min.shape[1]): if wall_min[y,x]!=300: window_min = wall_min[y-(N//2):y+(N//2)+1,x-(N//2):x+(N//2)+1] num_min = np.amin(window_min) temp_min[y,x] = num_min B = temp_min[(N//2):wall_min.shape[0]-(N//2), (N//2):wall_min.shape[1]-(N//2)].copy() return B
#B is the filtered image and I is the original image def background_subtraction(I, B): O = I - B norm_img = cv2.normalize(O, None, 0,255, norm_type=cv2.NORM_MINMAX) return norm_img
输出图像是原始图像加强后的结果。所实现的代码是在open CV中手动实现一些库函数以加强图像的拙劣尝试。带有图像的整个notebook能够在下面的Github连接中找到。工具
原文连接:https://medium.com/swlh/enhan...学习
欢迎关注磐创AI博客站:
http://panchuang.net/测试
sklearn机器学习中文官方文档:
http://sklearn123.com/
欢迎关注磐创博客资源汇总站:
http://docs.panchuang.net/