1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
| import cv2 import numpy as np import matplotlib.pyplot as plt
def img_color(imgPath): original_img = cv2.imread(imgPath) img = cv2.resize(original_img,None,fx=0.8,fy=0.8, interpolation=cv2.INTER_AREA)
Make_border_img = cv2.copyMakeBorder(img, 30, 30, 0, 0, cv2.BORDER_CONSTANT, value=(0, 0, 0))
img_hsv = cv2.cvtColor(img,cv2.COLOR_BGR2HSV) colorless_hsv = img_hsv.copy() colorless_hsv[:,:,1] = 0.5 * colorless_hsv[:,:,1] colorless_img = cv2.cvtColor(colorless_hsv,cv2.COLOR_HSV2BGR)
darker_hsv = img_hsv.copy() darker_hsv[:,:,2] = 0.5 * darker_hsv[:,:,2] darker_img = cv2.cvtColor(darker_hsv,cv2.COLOR_HSV2BGR)
img_corrected = gamma_trans(img,0.5)
cv2.imshow("original_img",img) cv2.imshow("Make_border_img",Make_border_img) cv2.imshow('colorless_jpg',colorless_img) cv2.imshow('darker_jpg',darker_img) cv2.imshow('gamma_corrected_jpg',img_corrected)
hist_b = cv2.calcHist([img],[0],None,[256],[0,256]) hist_g = cv2.calcHist([img],[1],None,[256],[0,256]) hist_r = cv2.calcHist([img],[2],None,[256],[0,256])
plt.plot(hist_b, label='B', color='blue') plt.plot(hist_g, label='G', color='green') plt.plot(hist_r, label='R', color='red') plt.legend(loc='best') plt.xlim([0, 256]) plt.show()
def gamma_trans(img,gamma): gamma_table = [np.power(x/255.0,gamma)*255.0 for x in range(256)] gamma_table = np.round(np.array(gamma_table)).astype(np.uint8) return cv2.LUT(img,gamma_table)
if __name__ == "__main__": imgPath = 'src/python-opencv/a.jpg' img_color(imgPath)
|