我需要对大量图像进行一些快速阈值处理,每个RGB通道都有一个特定的范围,即去除(使黑色)不在[100; 110]中的所有R值,所有G值都不在[80; 85]和所有B值不在[120; 140]
使用到OpenCV的python绑定为我提供了一个快速阈值处理,但它将所有三个RGP通道阈值设置为单个值:
cv.Threshold(cv_im,cv_im,threshold+5, 100,cv.CV_THRESH_TOZERO_INV)
cv.Threshold(cv_im,cv_im,threshold-5, 100,cv.CV_THRESH_TOZERO)
或者,我尝试通过将图像从PIL转换为numpy来手动完成:
arr=np.array(np.asarray(Image.open(filename).convert('RGB')).astype('float'))
for x in range(img.size[1]):
for y in range(img.size[0]):
bla = 0
for j in range(3):
if arr[x,y][j] > threshold2[j] - 5 and arr[x,y][j] < threshold2[j] + 5 :
bla += 1
if bla == 3:
arr[x,y][0] = arr[x,y][1] = arr[x,y][2] = 200
else:
arr[x,y][0] = arr[x,y][1] = arr[x,y][2] = 0
虽然这是按预期工作的,但速度非常慢!
关于如何快速实现这一点的任何想法?
提前谢谢了,
Bjarke