這篇博客將介紹如何通過(guò)OpenCV中圖像修復(fù)的技術(shù)——cv2.inpaint() 去除舊照片中的小噪音、筆劃等。并提供一個(gè)可交互式的程序,利用OpenCV的快速行進(jìn)和流體力學(xué)倆種修復(fù)算法對(duì)自己的圖片進(jìn)行修復(fù)。
# 圖像修復(fù)交互式案例——通過(guò)水流填充算法來(lái)修復(fù)被破壞的圖像區(qū)域;
# 使用倆種方法進(jìn)行修復(fù)
# cv2.INPAINT_TELEA (Fast Marching Method 快速行進(jìn)算法),對(duì)位于點(diǎn)附近、邊界法線附近和邊界輪廓上的像素賦予更多權(quán)重。一旦一個(gè)像素被修復(fù),它將使用快速行進(jìn)的方法移動(dòng)到下一個(gè)最近的像素。
# cv2.INPAINT_NS 流體力學(xué)算法,使用了流體力學(xué)的一些方法,基本原則是啟發(fā)式的,首先沿著邊從已知區(qū)域移動(dòng)到未知區(qū)域(因?yàn)檫吺沁B續(xù)的)。它在匹配修復(fù)區(qū)域邊界處的漸變向量的同時(shí),繼續(xù)等高線(連接具有相同強(qiáng)度的點(diǎn)的線,就像等高線連接具有相同高程的點(diǎn)一樣)。
# USAGE
# python inpaint.py D:/deepLearning/py-demo/20210808/images/ml.jpg
# 按下鼠標(biāo)左鍵,添加點(diǎn)、線,按下鼠標(biāo)右鍵,添加矩形框,以制作被污染的需要修復(fù)圖像
# 按下空格鍵:執(zhí)行修復(fù)功能
# 按下r鍵:重置待修復(fù)的mask
# 按下esc鍵,退出
import cv2
import numpy as np
class Sketcher:
def __init__(self, windowname, dests, colors_func):
self.prev_pt = None # 線起始點(diǎn)
self.drag_start = None # 矩形起點(diǎn)
self.drag_rect = None # 矩形(左上角,右下角)坐標(biāo)
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:
# 第一次初始化時(shí)設(shè)定pt,往后保留上一個(gè)點(diǎn)作為矩形起點(diǎn)
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
@property
def dragging(self):
return self.drag_rect is not None
def main():
import sys
try:
fn = sys.argv[1]
except:
fn = 'images/ml_.jpg'
img = cv2.imread(fn)
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()
參考 https://docs.opencv.org/3.0-beta/doc/py_tutorials/py_photo/py_inpainting/py_inpainting.html#inpainting
到此這篇關(guān)于OpenCV圖像修復(fù)cv2.inpaint()的使用的文章就介紹到這了,更多相關(guān)OpenCV圖像修復(fù)cv2.inpaint()內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!