图像修复

2022-05-29 09:40:19 浏览数 (1)

代码语言:javascript复制
import cv2
import numpy as np
class Sketcher:
    def __init__(self, windowname,dests,colors_func):
        self.prev_pt=None#线起始点
        self.drag_start=None#矩形起点
        self.drag_rect=None#矩形(左上角,右下角)坐标
        self.windowname=windowname
        self.dests=dests
        self.colors_func=colors_func
        self.dirty=False
        self.drawing=False
        self.mode=False
        self.show()
        cv2.setMouseCallback(self.windowname,self.on_mouse)
    def show(self):
        cv2.imshow(self.windowname,self.dests[0])
    def on_mouse(self,event,x,y,flags,param):
        pt=(x,y)
        if event==cv2.EVENT_LBUTTONDOWN:
            self.prev_pt=pt
            self.drawing=True
        elif event==cv2.EVENT_RBUTTONDOWN:
            #第一次初始化时设定pt,往后保留上一个点作为矩形起点
            if self.drag_start==None:
                self.drag_start = pt
        if self.prev_pt and flags & cv2.EVENT_FLAG_LBUTTON:
            for dst, color in zip(self.dests,self.colors_func()):
                cv2.line(dst,self.prev_pt,pt,color,5)
            self.dirty=True
            self.prev_pt=pt
            self.show()
        if self.drag_start and flags & cv2.EVENT_FLAG_RBUTTON:
            xo,yo=self.drag_start
            x0,y0=np.minimum([xo,yo], [x,y])
            x1,y1=np.maximum([xo,yo], [x,y])
            self.drag_rect=None
            if x1 - x0>0 and y1-y0>0:
                self.drag_rect=(x0,y0,x1,y1)
                for dst,color in zip(self.dests,self.colors_func()):
                    cv2.rectangle(dst, (x0,y0),(x1,y1),color,-1)
                self.dirty=True
                self.drag_start=None
                self.drag_rect=None
                self.show()
            else:
                self.drag_start=pt
def main():
    img=cv2.imread('C:/Users/xpp/Desktop/Lena.png')
    if img is None:
        print('Failed to load image file:',fn)
        sys.exit(1)
    img_mark=img.copy())
    mark=np.zeros(img.shape[:2],np.uint8
    sketch = Sketcher('img',[img_mark,mark],lambda: ((255,255,255), 255))
    while True:
        ch=cv2.waitKey()
        if ch==27:
            break
        if ch==ord(' '):
            cv2.imshow('mask',mark)
            fmmres=cv2.inpaint(img_mark,mark,3,cv2.INPAINT_TELEA)
            nsres=cv2.inpaint(img_mark,mark,3,cv2.INPAINT_NS)
            cv2.imshow('inpaint fmm res',fmmres)
            cv2.imshow('inpaint ns res',nsres)
        if ch==ord('r'):
            img_mark[:]=img
            mark[:]=0
            sketch.show()
    print('Done')
if __name__=='__main__':
    main()
    cv2.destroyAllWindows()

算法:图像修复是去除旧照片中的小噪音、笔划等并提供一个可交互式的程序的技术。

  • cv2.INPAINT_TELEA (Fast Marching Method快速行进算法),对位于点附近、边界法线附近和边界轮廓上的像素赋予更多权重。
  • cv2.INPAINT_NS(Fluid Dynamics Method流体力学算法),首先沿着边从已知区域移动到未知区域,在匹配修复区域边界处的渐变向量的同时,继续等高线。
  • OpenCV未实现的:(Content-Aware Fill 内容感知填充算法),这是Adobe Photoshop中使用的一种高级修复技术。

链接:https://docs.opencv.org/3.0-beta/doc/py_tutorials/py_photo/py_inpainting/py_inpainting.html#inpainting

0 人点赞