图像增强:opencv去除图片的高光

1.算法原理: 对高光部位应该先检测,后用图像增强的办法去除高光,想办法用周围的色素值代替,本文采用图像修复的方法。
2. 算法步骤:
1.图像阈分割生成mask掩膜
2.opencv自带的图像修复函数,如下:

cv2.inpaint(src, inpaintMask, 3, cv2.INPAINT_TELEA)
src:目标修复图像;
inpaintMask:蒙版图(定位修复区域);
3:选取邻域半径;
cv2.INPAINT_TELEA:修复算法(INPAINT_TELEA:基于快速行进算法 算法效果较好
INPAINT_NS:基于流体动力学并使用了偏微分方程)

测试图片:
图像增强:opencv去除图片的高光_第1张图片
生成的mask图片:
图像增强:opencv去除图片的高光_第2张图片
最终的结果:
图像增强:opencv去除图片的高光_第3张图片
实现的代码:

import  cv2
import os,shutil
#找亮光位置
def create_mask(imgpath):
    image = cv2.imread(imgpath, cv2.IMREAD_GRAYSCALE)
    _, mask = cv2.threshold(image, 200, 255, cv2.THRESH_BINARY)
    return mask
#修复图片
def xiufu(imgpath,maskpath):
    src_ = cv2.imread(imgpath)
    mask = cv2.imread(maskpath, cv2.IMREAD_GRAYSCALE)
    #缩放因子(fx,fy)
    res_ = cv2.resize(src_,None,fx=0.6, fy=0.6, interpolation = cv2.INTER_CUBIC)
    mask = cv2.resize(mask,None,fx=0.6, fy=0.6, interpolation = cv2.INTER_CUBIC)
    dst = cv2.inpaint(res_, mask, 10, cv2.INPAINT_TELEA)
    return dst

if __name__=='__main__':
    rootpath = r"E:\data_set\img"
    masksavepath=r"E:\data_set\mask"
    savepath = r"E:\data_set\wcp\result"
    imgfiles = os.listdir(rootpath)
    for i in range(0, len(imgfiles)):
        path = os.path.join(rootpath, imgfiles[i])
        print(imgfiles[i])
        if os.path.isfile(path):
            if (imgfiles[i].endswith("jpg") or imgfiles[i].endswith("JPG")):
                maskpath =os.path.join(masksavepath, "mask_"+imgfiles[i])
                cv2.imwrite(maskpath, create_mask(path))
                dst=xiufu(path,maskpath)
                newname = 'xiufu_' + imgfiles[i].split(".")[0]
                cv2.imwrite(os.path.join(savepath, newname + ".jpg"), dst)
                shutil.copyfile(os.path.join(rootpath, imgfiles[i].split(".")[0] + ".xml"),
                                os.path.join(savepath, newname + ".xml"))

相关文献:
https://blog.csdn.net/song_esther/article/details/80157900

你可能感兴趣的:(图像增强:opencv去除图片的高光)