积分图?
即是当前位置的像素值是为左上角到当前位置的像素总和,用途有计算haar特征、滤波等
程序代码:
def integral(img):
rows,cols=img.shape[0],img.shape[1]
integral_img=np.zeros((rows,cols)).astype("uint32")
for i in range(rows):
for j in range(cols):
sum1=0
for k in range(i+1):
for p in range(j+1):
sum1+=img[k,p]
integral_img[i,j]=sum1
这样的写法很不符合python,for循环能避免尽量避免
numpy 用一句可以 实现:
intergal_image=np.cumsum(np.cumsum(img,0),1)
卷积?
图像卷积即是滤波,有三种方式:valid、same 、full
卷积后的图大小=1+(图像大小-卷积核大小+2*p)/s
代码如下:
def conv(image,filter_mask,padding):
h,w=image.shape[:]
if padding==False:
result=[]
for i in range(0,h-len(filter_mask)+1):
r=[]
for j in range(0,w-len(filter_mask)+1):
r.append(image[i,j]*filter_mask[0,0]+image[i,j+1]*filter_mask[0,1]
+image[i,j+2]*filter_mask[0,2]+image[i+1,j]*filter_mask[1,0]+
image[i+1,j+1]*filter_mask[1,1]+image[i+1,j+2]*filter_mask[1,2]
+image[i+2,j]*filter_mask[2,0]+image[i+2,j+1]*filter_mask[2,1]
+image[i+2,j+2]*filter_mask[2,2])
result.append(r)
if padding==True:
image_new=np.zeros((h+2,w+2))
image_new[0:h,0:w]=image
result=[]
for i in range(0,h+2-len(filter_mask)+1):
r=[]
for j in range(0,w+2-len(filter_mask)+1):
r.append(image_new[i,j]*filter_mask[0,0]+image_new[i,j+1]*filter_mask[0,1]
+image_new[i,j+2]*filter_mask[0,2]+image_new[i+1,j]*filter_mask[1,0]+
image_new[i+1,j+1]*filter_mask[1,1]+image_new[i+1,j+2]*filter_mask[1,2]
+image_new[i+2,j]*filter_mask[2,0]+image_new[i+2,j+1]*filter_mask[2,1]
+image_new[i+2,j+2]*filter_mask[2,2])
result.append(r)
return np.array(result)