我的图像中有一堆坏点。在python中,我有一个用于保存最终图像的numpy数组,还有另一个具有相同形状的boolean numpy数组,用于指示需要填充哪些像素。
我想通过取周围8个像素的平均值来填充坏点,但前提是它们实际上只保存数据。例如,如果我有这个(N表示那里没有数据,?是要填充的像素):
1 2 N
N ? 2
N N 5
我将填写?与(1 + 2 + 2 + 5)/ 4。
现在,我使用for循环执行此操作,如下所示。 outFrame保留最终图像,而填充的是布尔数组,指示已填充了哪些像素:
# Loop through each pixel in the image
for row in range(height):
for col in range(width):
# Check to see if the pixel needs to be filled in
if not populated[row,col]:
# Check for boundary conditions
xmin = max(0,col-1)
xmax = min(width-1,col+1)
ymin = max(0,row-1)
ymax = min(height-1,row+1)
# Find the 8 surrounding values
vals = outFrame[ymin:ymax+1,xmin:xmax+1]
mask = populated[ymin:ymax+1,xmin:xmax+1]
# Find the average of only the populated pixels
if vals[mask].size != 0:
outFrame[row,col] = np.mean(vals[mask])
显然,python循环很慢,但我无法弄清任何numpy索引魔术来实现此行为。有没有办法在python中快速完成此功能?
编辑:我尝试如下使用opencv修复功能:
mask = (1*~populated).astype(np.uint8)
outFrame = cv2.inpaint(outFrame,mask,3,cv2.INPAINT_NS) # This is very slow
但是,这比我的原始方法慢10倍(我的方法需要3-6秒,而修复方法需要60秒)。我的死像素非常多,我认为这是使用此方法的速度很慢的原因]
编辑:这是示例图像。原始图片:https://imgur.com/a/ax3nOAK插补后:https://imgur.com/a/2apOe7p
很难帮助您,因为您没有提供包含所有import
语句和代码的[[最小完整可验证示例,并且代码显示了如何打开图像,甚至指示您是否正在使用< [OpenCV或PIL或skimage。另外,您没有为第二个文件提供所有需要绘画的点的遮罩,也不知道您实际要实现的目标,所以目前,我只是在尝试提供一种看起来像样的方法。对我来说,它得到的结果与您显示的结果类似。
#!/usr/bin/env python3
import cv2
import numpy as np
# Load image and make into Numpy array
im = cv2.imread('holey.png')
# Use morphology to find maximum pixel in each 3x3 square
kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (3,3))
morph = cv2.morphologyEx(im, cv2.MORPH_DILATE, kernel)
# Save result
cv2.imwrite('result.png', morph)