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 58 59 60 61 62 63 64 65 66
| def img_chuli_plt2(): import cv2 import random import numpy as np
path = './img/0.jpg' image = cv2.imread(path) gray_img = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
ori_h, ori_w = image.shape[:2] height, width = gray_img.shape[:2] image = cv2.resize(image, (int(ori_w / ori_h * 400), 400), interpolation=cv2.INTER_CUBIC) gray_img = cv2.resize(gray_img, (int(width / height * 400), 400), interpolation=cv2.INTER_CUBIC)
a, b = -0.5, 0 new_img1 = np.ones((gray_img.shape[0], gray_img.shape[1]), dtype=np.uint8) for i in range(new_img1.shape[0]): for j in range(new_img1.shape[1]): new_img1[i][j] = gray_img[i][j] * a + b
a, b = 1.5, 20 new_img2 = np.ones((gray_img.shape[0], gray_img.shape[1]), dtype=np.uint8) for i in range(new_img2.shape[0]): for j in range(new_img2.shape[1]): if gray_img[i][j] * a + b > 255: new_img2[i][j] = 255 else: new_img2[i][j] = gray_img[i][j] * a + b
a, b = 0.5, 0 new_img3 = np.ones((gray_img.shape[0], gray_img.shape[1]), dtype=np.uint8) for i in range(new_img3.shape[0]): for j in range(new_img3.shape[1]): new_img3[i][j] = gray_img[i][j] * a + b
a, b = 1, -50 new_img4 = np.ones((gray_img.shape[0], gray_img.shape[1]), dtype=np.uint8) for i in range(new_img4.shape[0]): for j in range(new_img4.shape[1]): pix = gray_img[i][j] * a + b if pix > 255: new_img4[i][j] = 255 elif pix < 0: new_img4[i][j] = 0 else: new_img4[i][j] = pix
new_img5 = 255 - gray_img
cv2.imshow('origin', image) cv2.imshow('gray', gray_img) cv2.imshow('a<0 and b=0', new_img1) cv2.imshow('a>1 and b>=0',new_img2) cv2.imshow('a<1 and b>=0',new_img3) cv2.imshow('a=1 and b><0',new_img4) cv2.imshow('a=-1 and b=255',new_img5) if cv2.waitKey(0) == 27: cv2.destroyAllWindows()
|