# image - 利用当前颜色区间快速查找像素位置

``````
def inRange(self, tgColor, excColor, jump):
if tgColor[0]> excColor[0] - jump and tgColor[0] <excColor[0] + jump and tgColor[1]> excColor[1] - jump and tgColor[1] <excColor[1] + jump and tgColor[2]> excColor[2] - jump and tgColor[2] <excColor[2] + jump:
return True
return False

for iy in xrange(self.pilImage.size[1]):
for ix in xrange(self.pilImage.size[0]):
if self.inRange(data[ix, iy], self.targetColor, self.jump):

``````

OpenCV带有一个函数英镑 。 你传递最小和最大像素值，并获得一个二进制图像，它的像素为白色和失败像素。

``````
>>> import cv2
>>> import numpy as np
>>> x = np.random.randint(1,10, (5,5))
>>> x
array([[9, 5, 1, 3, 1],
[7, 7, 2, 1, 7],
[9, 1, 4, 7, 4],
[3, 6, 6, 7, 2],
[3, 4, 2, 3, 1]])
>>> y = cv2.inRange(x,4,8)
>>> y
array([[ 0, 255, 0, 0, 0],
[255, 255, 0, 0, 255],
[ 0, 0, 255, 255, 255],
[ 0, 255, 255, 255, 0],
[ 0, 255, 0, 0, 0]], dtype=uint8)

>>> z = np.transpose(np.where(y>0))
>>> z
array([[0, 1],
[1, 0],
[1, 1],
[1, 4],
[2, 2],
[2, 3],
[2, 4],
[3, 1],
[3, 2],
[3, 3],
[4, 1]])

``````