import cv2 as cv
import matplotlib.pyplot as plt
import numpy as np
def cv_show(name,img):
cv.imshow(name,img)
cv.waitKey(0)
cv.destroyAllWindows()
def cv_hist(img):
hist = cv.calcHist([img], [0], None, [256], [0, 256])
print(hist.shape)
plt.hist(img.ravel(), 256)
plt.show()
def cv_histr():
img = cv.imread('D://cat.png')
color = ('b', 'g', 'r')
for i, col in enumerate(color):
histr = cv.calcHist([img], [i], None, [256], [0, 256])
plt.plot(histr, color=col)
plt.xlim([0, 256])
plt.show()
def cv_mask():
img = cv.imread('D://cat.png', 0)
mask = np.zeros(img.shape[:2], np.uint8)
print(mask.shape)
mask[100:300, 100:400] = 255
masked_img = cv.bitwise_and(img,img,mask=mask)
hist_full = cv.calcHist([img], [0], None, [256], [0, 256])
hist_mask = cv.calcHist([img], [0], mask, [256], [0, 256])
plt.subplot(221), plt.imshow(img, 'gray')
plt.subplot(222), plt.imshow(mask, 'gray')
plt.subplot(223), plt.imshow(masked_img, 'gray')
plt.subplot(224), plt.plot(hist_full), plt.plot(hist_mask)
plt.xlim([0, 256])
plt.show()
def cv_equ():
img = cv.imread('D://clahe.png', 0)
plt.hist(img.ravel(), 256)
plt.show()
equ = cv.equalizeHist(img)
plt.hist(equ.ravel(), 256)
plt.show()
res = np.hstack((img,equ))
cv_show('res',res)
def cv_equ1():
img = cv.imread('D://clahe.png', 0)
plt.hist(img.ravel(), 256)
plt.show()
equ = cv.equalizeHist(img)
plt.hist(equ.ravel(), 256)
plt.show()
res = np.hstack((img, equ))
cv_show('res', res)
clahe = cv.createCLAHE(clipLimit=2.0, tileGridSize=(8, 8))
res_clahe = clahe.apply(img)
res = np.hstack((img, equ, res_clahe))
cv_show('res',res)
img = cv.imread('D://cat.png',0)
cv_equ1()