pip install opencv-python # 仅安装主模块
pip install opencv-contrib-python # 包含主模块和额外贡献模块
import cv2
img=cv2.imread('/data/cvdata/aero1.jpg')
from IPython.display import Image
import numpy as np
from matplotlib import pyplot as plt
plt.imshow(img )
plt.xticks = []
plt.yticks = []
plt.show()
# img = cv2.imread('gradient.png',0)
ret,thresh1 = cv2.threshold(img,127,255,cv2.THRESH_BINARY)
ret,thresh2 = cv2.threshold(img,127,255,cv2.THRESH_BINARY_INV)
ret,thresh3 = cv2.threshold(img,127,255,cv2.THRESH_TRUNC)
ret,thresh4 = cv2.threshold(img,127,255,cv2.THRESH_TOZERO)
ret,thresh5 = cv2.threshold(img,127,255,cv2.THRESH_TOZERO_INV)
titles = ['Original Image','BINARY','BINARY_INV','TRUNC','TOZERO','TOZERO_INV']
images = [img, thresh1, thresh2, thresh3, thresh4, thresh5]
for i in range(6):
plt.subplot(3,2,i+1),plt.imshow(images[i],'gray')
plt.title(titles[i])
plt.xticks = []
plt.yticks = []
plt.show()
import numpy as np
from matplotlib import pyplot as plt
kernel = np.ones((5,5),np.float32)/25
dst = cv2.filter2D(img,-1,kernel)
plt.subplot(111),plt.imshow(img),plt.title('Original')
plt.xticks = []
plt.yticks = []
plt.show()
plt.subplot(111),plt.imshow(dst),plt.title('Averaging')
plt.xticks = []
plt.yticks = []
plt.show()