opencv筆記(8):圖像修補

生活就像大海,我就像一條鹹魚,在浩瀚的海洋中邊浪邊學,這是opencv筆記系列中的「圖像修補」。bash

世間萬圖,皆可修補。小編邊浪邊學,順帶以很鹹魚的方式把它們記錄下來。ui

首先,對原圖像進行一些小小的「損壞」spa

import cv2
import numpy as np
img = cv2.imread('test1.jpg',1)
for i in range(200,300):
    img[i,200] = (255,255,255)
    img[i,200+1] = (255,255,255)
    img[i,200-1] = (255,255,255)
for i in range(150,250):
    img[250,i] = (255,255,255)
    img[250+1,i] = (255,255,255)
    img[250-1,i] = (255,255,255)
cv2.imwrite('damge.jpg',img)
cv2.imshow('image',img)
cv2.waitKey(0)
複製代碼

破壞後的圖像顯示以下,可見人物左邊的天空,有一小塊被「破壞」了code

接下來讀取這張圖片,進行修補cdn

img = cv2.imread('damge.jpg',1)
height = img.shape[0]
width = img.shape[1]
paint = np.zeros((height,width,1),np.uint8)
cv2.imshow('img',img)
for i in range(200,300):
    paint[i,200] = 255
    paint[i,200+1] = 255
    paint[i,200-1] = 255
for i in range(150,250):
    paint[250,i] = 255
    paint[250+1,i] = 255
    paint[250-1,i] = 255
imgDst = cv2.inpaint(img,paint,3,cv2.INPAINT_TELEA)
cv2.imshow('dst',imgDst)
cv2.waitKey(0)
複製代碼

修補後圖像顯示以下,已經和原圖像沒啥兩樣了blog

更多可來個人公衆號:圖片

相關文章
相關標籤/搜索