03图片修补
2019-07-18 本文已影响0人
犬夜叉写作业
1、生成坏图
import cv2
import numpy as np
img = cv2.imread('image0.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('damaged.jpg',img)
cv2.imshow('image',img)
cv2.waitKey(0)
image.png
2、修图
先读取坏图,再用一个图片描绘坏图坏的地方,调用opencv的方法(inpaint)完成修补
#1 坏图 2 array 3 inpaint
import cv2
import numpy as np
img = cv2.imread('damaged.jpg',1)
cv2.imshow('src',img)
imgInfo = img.shape
height = imgInfo[0]
width = imgInfo[1]
paint = np.zeros((height,width,1),np.uint8)
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
cv2.imshow('paint',paint)
#1 src 2 mask 最终生成的目标图片
imgDst = cv2.inpaint(img,paint,3,cv2.INPAINT_TELEA)
cv2.imshow('image',imgDst)
cv2.waitKey(0)
paint
imgDst