一
图片处理,给图片修改对比度,把一个颜色很暗的图片通过对比度调节变得清晰
导入一张图片,然后修改图片的对比度,通过修改add weight函数中的三个参数获取不同的对比度
add weight(图片名称,参数m,blank,1-m(用1减去m),参数n)
m=2,n=-10,图片对比度调节效果比较好
//
import cv2
import numpy as np
#导入图片处理用到的库
image ="10087-90.jpg" #图片名称
img = cv2.imdecode(np.fromfile(image, dtype=np.uint8), flags=cv2.IMREAD_COLOR) #imdecode从内存来读取图片的位置信息
rows, cols, channels = img.shape
blank = np.zeros([rows, cols, channels], img.dtype)
dst = cv2.addWeighted(img, 2, blank, -1, -10)
cv2.imwrite(r'D:\Dataset\image\10087-90-duibi.jpg',dst)
二
图片反锐化掩码
突出图片细节和边缘
原理是先把图像进行滤波,让图像变得模糊,图像高平成分就会减少,然后用滤波后图像和原图做减法,留下的都是图像高频成分,最后在用原图和做减法后的结果相加,这样图片中低频成分基本不变,高频成分得到加强,突出边缘和图像细节
radius,amount两个参数调节来改变锐化效果
radius=5,amount=10,图片处理效果比较好。
import cv2
from skimage.filters import unsharp_mask #导入包
image = cv2.imread('10299-90-rui-duibi-rui-duibi.jpg') #导入图片
result_1 = unsharp_mask(image, radius=1, amount=1) #输出三个结果,参数不一样
result_2 = unsharp_mask(image, radius=5, amount=2)
result_3 = unsharp_mask(image, radius=5, amount=8)
cv2.imwrite(r"D:\Dataset\image\10299-90-rui-duibi-rui-duibi-rui.jpg",result_3*255)#反锐化结果需要*255
unsharp_mask函数实现,
import numpy as np
from ..util.dtype import img_as_float
from .._shared import utils
from .._shared.filters import gaussian
def _unsharp_mask_single_channel(image, radius, amount, vrange):
blurred = gaussian(image, sigma=radius, mode='reflect')
result = image + (image - blurred) * amount
if vrange is not None:
return np.clip(result, vrange[0], vrange[1], out=result)
return result
def unsharp_mask(image, radius=1.0, amount=1.0,
preserve_range=False, *, channel_axis=None):
vrange = None # Range for valid values; used for clipping.
float_dtype = utils._supported_float_type(image.dtype)
if preserve_range:
fimg = image.astype(float_dtype, copy=False)
else:
fimg = img_as_float(image).astype(float_dtype, copy=False)
negative = np.any(fimg < 0)
if negative:
vrange = [-1., 1.]
else:
vrange = [0., 1.]
if channel_axis is not None:
result = np.empty_like(fimg, dtype=float_dtype)
for channel in range(image.shape[channel_axis]):
sl = utils.slice_at_axis(channel, channel_axis)
result[sl] = _unsharp_mask_single_channel(
fimg[sl], radius, amount, vrange)
return result
else:
return _unsharp_mask_single_channel(fimg, radius, amount, vrange)
三
批量化处理
同时用一和二的步骤处理图片,并且文件数量大,一张一张太慢,直接输入目标文件夹和导出文件夹直接完成批量化处理。
import numpy as np
import os
import cv2
from skimage.filters import unsharp_mask
path = 'D:\Dataset\BH Image Data\data1jpg' # 图片路径
savepath = r'D:\Dataset\BH Image Data\data1jpg_rd' # 存储路径
def read_path(path,savepath):
files = os.listdir(path)
for file in files:
path_name = path + "/" + file # 源文件路径
image = cv2.imread(path_name)
result = unsharp_mask(image, radius=5, amount=8)
result1=result*255
print(file)
rows, cols, channels = result1.shape
blank = np.zeros([rows, cols, channels], result1.dtype)
dst = cv2.addWeighted(result1, 2, blank, -1, -10)
cv2.imwrite(savepath + '/' + file, dst)
print('numrous='+str(len(files)))
print('读取文件结束')
if __name__ == '__main__':
read_path(path, savepath)
批量化处理学习到的点:
cv2调用输入函数时候,输入路径,cv2.imwrite()存储图片时候,括号内必须是两个参数,第一个是存储路径,存储路径后续必须加上存储时候想要图片的名字,第二个是存储图片的名字,不是前面的名字,而是在上面代码中,所存储图片的代码,类似于dst。
读取文件内所有的文件,通过files=os.listdir(path),path是想要输入的文件夹,遍历所有图片可以通过
for file in files: 这样每一个file就是文件夹内图片名字。
看一个文件的长度用len()函数,如果想要输出长度的化,print(’ 想要写的字 ',+str(len())),通过str()来转换为字符串的形式。
理解不到位,随时改!!