OpenCV学习(14)-二维离散卷积

# -*- coding: utf-8 -*-
import sys
import cv2
import numpy as np
from scipy import signal
import matplotlib.pyplot as plt


if __name__=="__main__":
   if len(sys.argv)>1 :
      img = cv2.imread(sys.argv[1],0)
   else:
      print('None')
   I=np.array([[1,2],[3,4]],np.float32)
   h1,w1=I.shape
   K=np.array([[-1,-2],[2,1]],np.float32)
   h2,w2=K.shape
   c_full=signal.convolve2d(I,K,mode='full')
   c_valid=signal.convolve2d(I,K,mode='valid')
   c_same=signal.convolve2d(I,K,mode='same')
   print(c_full)
   print(c_valid)
   print(c_same)
   kr,kc=1,1 #锚点
   c_same=c_full[h2-kr-1:h1+h2-kr-1,w2-kc-1:w2+w1-kc-1]
   print(c_same)
   
'''
result:
[[-1. -4. -4.]
 [-1. -5. -6.]
 [ 6. 11.  4.]]
[[-5.]]
[[-1. -4.]
 [-1. -5.]]
[[-1. -4.]
 [-1. -5.]]
'''

你可能感兴趣的:(opencv-图像平滑,python,opencv)