我正在尝试计算图像中子窗口的熵特征。这是我写的代码:
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
实际上,它有效。但问题是它的速度,太慢了。你有什么想法让它快吗?