前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >图像修复

图像修复

作者头像
裴来凡
发布2022-05-29 09:40:19
4320
发布2022-05-29 09:40:19
举报
文章被收录于专栏:图像处理与模式识别研究所
代码语言: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

本文参与 腾讯云自媒体同步曝光计划,分享自微信公众号。
原始发表:2021-10-18,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 图像处理与模式识别研究所 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档