EV5_Beeldherk_Bomen/src/experiments/algorithms/colourspace.py

83 lines
2.3 KiB
Python

import cv2
import matplotlib.pyplot as plt
import numpy as np
def imgStats(img):
mean = np.zeros(3)
std = np.zeros(3)
for i in range(img.shape[2]):
mean[i] = np.mean(img[:, :, i])
std[i] = np.std(img[:, :, i])
return mean, std
def histNormalize(hist):
max_val = np.amax(hist)
min_val = np.amin(hist)
for i in range(len(hist)):
hist[i][0] = (hist[i][0] - min_val)/(max_val - min_val)
DATASET_PATH = "C:\\Users\\Tom\\Desktop\\Files\\Repositories\\EV5_Beeldherk_Bomen\\res\\trees\\"
SAVE_PATH = "C:\\Users\\Tom\\Desktop\\Files\\Repositories\\EV5_Beeldherk_Bomen\\res\\essay\\"
IMAGE = "berk_sq1_original.png"
full_img = cv2.imread(DATASET_PATH + IMAGE, 1)
assert full_img is not None, "Path is wrong: " + DATASET_PATH + IMAGE
bgr_img = cv2.resize(full_img, (0, 0), fx=0.25, fy=0.25)
hsv_img = cv2.cvtColor(bgr_img, cv2.COLOR_BGR2HSV)
cv2.imshow('BGR', bgr_img)
BGR = np.concatenate((bgr_img[:, :, 0], bgr_img[:, :, 1], bgr_img[:, :, 2]),
axis=1)
cv2.imshow("BGR - Expanded", BGR)
cv2.imshow('HSV', hsv_img)
HSV = np.concatenate((hsv_img[:, :, 0], hsv_img[:, :, 1], hsv_img[:, :, 2]),
axis=1)
cv2.imshow("HSV - Expanded", HSV)
cv2.imwrite(SAVE_PATH + "2-5-1_bgr.png", bgr_img)
cv2.imwrite(SAVE_PATH + "2-5-1_bgr_expanded.png", BGR)
cv2.imwrite(SAVE_PATH + "2-5-1_hsv.png", hsv_img)
cv2.imwrite(SAVE_PATH + "2-5-1_hsv_expanded.png", HSV)
mean, std = imgStats(bgr_img)
print(mean)
print(std)
print()
mean, std = imgStats(hsv_img)
print(mean)
print(std)
print()
cv2.waitKey(0)
cv2.destroyAllWindows()
hist = cv2.calcHist([bgr_img], [0], None, [256], [0,256])
plt.plot(hist, label='B', c='b')
hist = cv2.calcHist([bgr_img], [1], None, [256], [0,256])
plt.plot(hist, label='G', c='g')
hist = cv2.calcHist([bgr_img], [2], None, [256], [0,256])
plt.plot(hist, label='R', c='r')
plt.title("BGR histogram")
plt.xlabel("Waarde van pixels [0-255]")
plt.ylabel("Hoeveelheid pixels")
plt.grid()
plt.legend()
plt.show()
hist = cv2.calcHist([hsv_img], [0], None, [256], [0,256])
plt.plot(hist, label='H')
hist = cv2.calcHist([hsv_img], [1], None, [256], [0,256])
plt.plot(hist, label='S')
hist = cv2.calcHist([hsv_img], [2], None, [256], [0,256])
plt.plot(hist, label='V')
plt.title("HSV histogram")
plt.xlabel("Waarde van pixels [0-255]")
plt.ylabel("Hoeveelheid pixels")
plt.grid()
plt.legend()
plt.show()