import numpy as np
import cv2 as cv
import matplotlib.pyplot as plt
img = cv.imread("./images/002.jpg",0)
plt.imshow(img,cmap= plt.cm.gray)
""" 画原图的灰度直方图"""
hist = cv.calcHist([img],[0],None,[256],[0,256])
""" 创建掩膜 """
mask = np.zeros(img.shape[:2],np.uint8)
mask[400:1400,200:1000] = 1
# plt.imshow(mask,cmap=plt.cm.gray)
# plt.show()
""" 做与运算 """
mask_img = cv.bitwise_and(img,img,mask=mask)
# plt.imshow(mask_img,cmap=plt.cm.gray)
# plt.show()
""" 画图 """
plt.figure(1)
plt.subplot(2,2,1,title="original")
plt.imshow(img,cmap= plt.cm.gray)
plt.subplot(2,2,2,title="ori_hist")
plt.plot(hist)
plt.subplot(2,2,3,title="mask")
plt.imshow(mask,cmap=plt.cm.gray)
plt.subplot(2,2,4,title="mask_img")
plt.imshow(mask_img,cmap=plt.cm.gray)
plt.show()