数字图像处理知识(2)

数字图像处理知识

一、 一个画笔修复的例子

import numpy as np
import cv2 as cv
import time
#导入要使用的各类包

#opencv Class for Mouse (Version for python) set mouse...
class Sketcher:
    def __init__(self, windowname, dests, colors_func):
        self.prev_point = None
        self.windowname = windowname
        # dests is a set of images: copy & mask
        self.dests = dests
        self.colors_func = colors_func
        self.dirty = False
        self.show()
        cv.setMouseCallback(self.windowname, self.on_mouse)

    def show(self):
        cv.imshow(self.windowname, self.dests[0])
        cv.imshow(self.windowname + ":mask", self.dests[1])

    # on mouse function
    def on_mouse(self, event, x, y, flags, param):
        # point store the current position of the mouse
        point = (x, y)
        if event == cv.EVENT_LBUTTONDOWN:
            # assignment of previous point
            self.prev_point = point
        elif event == cv.EVENT_LBUTTONUP:
            self.prev_point = None
        # cv.EVENT_FLAG_LBUTTON & flags 代表按住左键拖拽
        if self.prev_point and flags & cv.EVENT_FLAG_LBUTTON:
            # zip 把前后参数打包为元组
            for dst, color in zip(self.dests, self.colors_func()):
                cv.line(dst, self.prev_point, point, color, 5)
        # Record this dirt
        self.dirty = True
        self.prev_point = point
        self.show()
#主函数
def main():
    print("Usage: python inpaint <image_path>")
    print("Keys: ")
    print("t - inpaint using FMM")  # Fast Marching method
    print("n - inpaint using NS technique")
    print("r - reset the inpainting mask")
    print("ESC - exit")

    # Read image in color mode 读取图片
    img = cv.imread("Snowtext.jpg", cv.IMREAD_COLOR)

    # 没有找到图片则读取失败
    if img is None:
        print("Failed to read the image")
        return

    # Create the copy of the original image
    img_mask = img.copy()

    # Create a black mask of the image
    inpaintMask = np.zeros(img.shape[:2], np.uint8)
    # Create a Sketch
    # dests= img_mask, inpaintMask
    # color_func is a tuple : white with BGR and white on gray
    sketch = Sketcher('image', [img_mask, inpaintMask], lambda: ((255, 255, 255), 255))

    while True:
        ch = cv.waitKey()
        # Esc
        if ch == 27:
            break
        # 判断是否是名字为‘t’的图片
        if ch == ord('t'):
            t1 = time.time()
            res = cv.inpaint(src=img_mask, inpaintMask=inpaintMask, inpaintRadius=3, flags=cv.INPAINT_TELEA)
            res = np.hstack((img, res))
            t2 = time.time()
            cv.imshow('Inpaint with FMM', res) #显示图片
        # 判断是否是名字为‘n’的图片
        if ch == ord('n'):
            t1 = time.time()
            res = cv.inpaint(src=img_mask, inpaintMask=inpaintMask, inpaintRadius=3, flags=cv.INPAINT_NS)
            res = np.hstack((img, res))
            t2 = time.time()
            cv.imshow('Inpaint Output using NS Technique', res)
        # 创建名字为‘r’的图片
        if ch == ord('r'):
            # 复制一个图片的原因
            img_mask[:] = img
            inpaintMask[:] = 0
            sketch.show()
    print('Completed')
if __name__ == '__main__':
    main()
    cv.destroyAllWindows()

二、运行结果

Usage: python inpaint <image_path>
Keys:
t - inpaint using FMM
n - inpaint using NS technique
r - reset the inpainting mask
ESC - exit
在这里插入图片描述

三、实验小结

数字图像的修复技术对局部区域有数据丢失或损坏的数字图像按照某种特定规则进行处理,以恢复原图像的完整性。但是修复后的图像不可能完全与原图相同,只是用人眼能够接受的已知信息代替而已,而不是对未知信息的还原。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

三毛是she

感谢打赏,我会继续努力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值