import os import sys import cv2 as cv import numpy as np import scipy.cluster from annotations import DataFile,computeBoundingBox def createHistogram(img): # Convert BGR to HSV hsv = cv.cvtColor(img, cv.COLOR_BGR2HSV) # H in range(0,180) # S in range(0,256) # V in range(0,256) planes=cv.split(hsv) hhist=cv.calcHist(planes,[0],None,[256],(0,180),accumulate=False) shist=cv.calcHist(planes,[1],None,[256],(0,256),accumulate=False) vhist=cv.calcHist(planes,[2],None,[256],(0,256),accumulate=False) width=512 height=400 binSize=width//256 histImage = np.zeros((height, width, 3), dtype=np.uint8) cv.normalize(hhist, hhist, alpha=0, beta=height, norm_type=cv.NORM_MINMAX) cv.normalize(shist, shist, alpha=0, beta=height, norm_type=cv.NORM_MINMAX) cv.normalize(vhist, vhist, alpha=0, beta=height, norm_type=cv.NORM_MINMAX) for i in range(1, 256): cv.line(histImage, ( binSize*(i-1), height - int(round(hhist[i-1][0])) ), ( binSize*(i), height - int(round(hhist[i][0])) ), ( 255, 0, 0), thickness=2) cv.line(histImage, ( binSize*(i-1), height - int(round(shist[i-1][0])) ), ( binSize*(i), height - int(round(shist[i][0])) ), ( 0, 255, 0), thickness=2) cv.line(histImage, ( binSize*(i-1), height - int(round(vhist[i-1][0])) ), ( binSize*(i), height - int(round(vhist[i][0])) ), ( 0, 0, 255), thickness=2) cv.imshow('Source image', img) cv.imshow('calcHist Demo', histImage) cv.waitKey() def quantize(img): arr=np.reshape(img,(-1,3)).astype(np.float) colors=np.array([[0,0,0],[255,255,255],[193,165,116]],np.float) print(colors) (centers,distortion)=scipy.cluster.vq.kmeans(arr,colors) print(centers) return centers def computeClosest(x,centers): res=centers[0] d=np.linalg.norm(res-x) for c in centers: d_=np.linalg.norm(c-x) if d_