画像内のサブウィンドウのエントロピー機能を計算しようとしています。ここに私が書いたコードがあります:
def genHist(img):
hist = np.histogram(img, np.arange(0, 256), normed=True)
return hist[0]
def calcEntropy(hist):
logs = np.nan_to_num(np.log2(hist))
hist_loghist = hist * logs
entropy = -1 * hist_loghist.sum()
return entropy
img = cv2.imread("lena.jpg", 0)
result = np.zeros(img.shape, dtype=np.float16)
h, w = img.shape
subwin_size = 5
for y in xrange(subwin_size, h-subwin_size):
for x in xrange(subwin_size, w-subwin_size):
subwin = img[y-subwin_size:y+subwin_size, x-subwin_size:x+subwin_size]
hist = genHist(subwin) # Generate histogram
entropy = calcEntropy(hist) # Calculate entropy
result[y, x] = entropy
実際、それは機能します。しかし、問題は速度が遅すぎることです。速くするアイデアはありますか?