代码在git
相机中的坏点就是那些和周围不一样的点,就是那些数值极大或者极小值点,你可以理解一张曲面的山峰或者山谷,人群中也是一样,那些与大众不一样的人就是"坏人",衡量好坏用他与周围的差值,
abs[V(好人)-v(坏人)]
if (abs(p1 - p0) > self.thres) and (abs(p2 - p0) > self.thres) and (abs(p3 - p0) > self.thres) \
and (abs(p4 - p0) > self.thres) and (abs(p5 - p0) > self.thres) and (abs(p6 - p0) > self.thres) \
and (abs(p7 - p0) > self.thres) and (abs(p8 - p0) > self.thres):
threds=30
那个最优秀的人就是坏人,不,是坏pixel
p6 | p7 | p8 |
p4 | p0 | p5 |
p1 | p2 | p3 |
代码
#!/usr/bin/python
import numpy as np
class DPC:
'Dead Pixel Correction'
def __init__(self, img, thres, mode, clip):
self.img = img
self.thres = thres
self.mode = mode
self.clip = clip
def padding(self):
#在四周放两个0 从(1080,1920) --->(1084,1924)
img_pad = np.pad(self.img, (2, 2), 'reflect')
return img_pad
def clipping(self):
#np.clip是一个截取函数,用于截取数组中小于或者大于某值的部分,并使得被截取部分等于固定值
#限定在()0,1023
np.clip(self.img, 0, self.clip, out=self.img)
return self.img
def execute(self):
img_pad = self.padding()
raw_h = self.img.shape[0]
raw_w = self.img.shape[1]
dpc_img = np.empty((raw_h, raw_w), np.uint16)
for y in range(img_pad.shape[0] - 4):
for x in range(img_pad.shape[1] - 4):
p0 = img_pad[y + 2, x + 2]
p1 = img_pad[y, x]
p2 = img_pad[y, x + 2]
p3 = img_pad[y, x + 4]
p4 = img_pad[y + 2, x]
p5 = img_pad[y + 2, x + 4]
p6 = img_pad[y + 4, x]
p7 = img_pad[y + 4, x + 2]
p8 = img_pad[y + 4, x + 4]
if (abs(p1 - p0) > self.thres) and (abs(p2 - p0) > self.thres) and (abs(p3 - p0) > self.thres) \
and (abs(p4 - p0) > self.thres) and (abs(p5 - p0) > self.thres) and (abs(p6 - p0) > self.thres) \
and (abs(p7 - p0) > self.thres) and (abs(p8 - p0) > self.thres):
if self.mode == 'mean':
p0 = (p2 + p4 + p5 + p7) / 4
elif self.mode == 'gradient':
dv = abs(2 * p0 - p2 - p7)
dh = abs(2 * p0 - p4 - p5)
ddl = abs(2 * p0 - p1 - p8)
ddr = abs(2 * p0 - p3 - p6)
if (min(dv, dh, ddl, ddr) == dv):
p0 = (p2 + p7 + 1) / 2
elif (min(dv, dh, ddl, ddr) == dh):
p0 = (p4 + p5 + 1) / 2
elif (min(dv, dh, ddl, ddr) == ddl):
p0 = (p1 + p8 + 1) / 2
else:
p0 = (p3 + p6 + 1) / 2
dpc_img[y, x] = p0
self.img = dpc_img
return self.clipping()