[英]Fastest way to sum the values of the pixels above a threshold in an image with Python
我试图找到最好的方法来检索大于某个阈值的像素值的总和。 例如,如果我的阈值是 253,我得到 10 个像素是 254,另外 10 个像素是 255,我希望得到10*254 + 10*255 = 5090
- 超过像素的总强度临界点。
我找到了一种方法来使用np.histogram
:
import cv2, time
import numpy as np
threshold = 1
deltaImg = cv2.imread('image.jpg')
t0=time.time()
histogram = np.histogram(deltaImg,256-threshold,[threshold,256])
histoSum = sum(histogram[0]*histogram[1][:-1])
print(histoSum)
print("time = %.2f ms" % ((time.time()-t0)*1000))
这个作品和我得到的像素衣被合计是比选择的阈值更大的总和。 但是,我不确定这是最好/最快的方法。 显然,阈值越大,动作就越快。
有没有人知道如何使用更快的算法获得正确的结果?
干得好:
import numpy as np
image = np.random.randint(0,256,(10,10))
threshold = 1
res = np.sum(image[image > threshold])
这个操作:
%%timeit
res = np.sum(image[image >=threshold])
5.43 µs ± 137 ns per loop (mean ± std. dev. of 7 runs, 100000 loops each)
需要5.43 µs ± 137 ns per loop (mean ± std. dev. of 7 runs, 100000 loops each)
。
虽然 OP 的方法从根本上是不准确的,但其基本思想仍然可以用来制作一种对整数数组(例如灰度图像)有效的方法:
def sum_gt_hist(arr, threshold):
values = np.arange(threshold, np.max(arr) + 1)
hist, edges = np.histogram(arr, values + 0.5)
return sum(values[1:] * hist)
然而,这并不理想,因为它比应有的更复杂( np.histogram()
是一个相对复杂的函数,它计算的中间信息比需要的要多得多)并且仅适用于整数值。
@sehan2 的回答中提出了一种更简单且仍然纯粹的 NumPy 方法:
import numpy as np
def sum_gt_np(arr, threshold):
return np.sum(arr[arr > threshold])
虽然以上将是首选的 NumPy-only 解决方案,但使用基于 Numba 的简单解决方案可以获得更快的执行(和内存效率):
import numba as nb
@nb.njit
def sum_gt_nb(arr, threshold):
arr = arr.ravel()
result = 0
for x in arr:
if x > threshold:
result += x
return result
使用代表图像的随机 100x100 数组对上述内容进行基准测试,将得到:
import numpy as np
np.random.seed(0)
arr = np.random.randint(0, 256, (100, 100)) # generate a random image
threshold = 253 # set a threshold
funcs = sum_gt_hist, sum_gt_np, sum_gt_nb
for func in funcs:
print(f"{func.__name__:16s}", end=' ')
print(func(arr, threshold), end=' ')
%timeit func(arr, threshold)
# sum_gt_hist 22397 355 µs ± 8.67 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)
# sum_gt_np 22397 10.1 µs ± 438 ns per loop (mean ± std. dev. of 7 runs, 100000 loops each)
# sum_gt_nb 22397 1.19 µs ± 33.5 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each)
这表明sum_gt_nb()
比sum_gt_np()
快得多,而sum_gt_hist()
又比sum_gt_hist()
快得多。
我不知道这是否是最快的,但它很简单。 在 Python/OpenCV 中,仅将低于阈值的像素设为零,保持高于阈值的值作为原始值。 然后简单地计算不为零的值。
我创建了一个简单的斜坡图像,宽度为 100 像素,从顶部的 255 到底部的 0,以 1 灰度级为增量。
输入:
import cv2
import numpy as np
# read image
img = cv2.imread('ramp.png')
print(img.shape)
# convert img to gray
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
# threshold to zero below threshold, but keep values above threshold
# note: to count all values of 254 and 255, use threshold at 253
thresh = cv2.threshold(gray, 253, 255, cv2.THRESH_TOZERO)[1]
# sum non-zero pixel values
sum = np.sum(thresh[np.where(thresh != 0)])
print("actual count:", sum)
# compute the expected count
sum2 = 100*254+100*255
print("computed count:", sum2)
# show results
cv2.imshow('thresh', thresh)
cv2.waitKey(0)
cv2.destroyAllWindows()
actual count: 50900 computed count: 50900
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.