目前我正试图在图像中检测python的白色条纹。这是我已经编写的代码:
def getPoints(self, image):
threshold = 50
whiteMin = 100 #Minimal average of the brightness in a lane
minWidth = 1 #Min and Max width of a lane
maxWidth = 8
Points = []
loadedImage = image.convert('L').load()
for y in xrange(239, 479, 5):
previousBrightness = loadedImage[127,y]
lastDown = -1
brightness = 0
for x in xrange(128, 511, 1):
currentBrightness = loadedImage[x,y]
difference = currentBrightness - previousBrightness
if difference > threshold: #if the current pixel is brighter than the previous and the difference is bigger than the threshold, set a flag
lastDown = x
brightness = currentBrightness
elif -difference > threshold:
#check if the stripe has the correct width and the average brightness is high enough
if lastDown != -1 and x - lastDown < maxWidth and minWidth < x - lastDown and brightness / (x - lastDown) > whiteMin:
Points.append(((x - lastDown) / 2 + lastDown,y))
lastDown = -1
brightness = 0
elif lastDown != -1:
brightness += currentBrightness
previousBrightness = currentBrightness
return Points
图像是PIL图像对象。
我尝试过使用 getdata()命令,使用 getpixel()获取像素,首先裁剪图像或使用numpy -array,创建副本,滚动副本并从第一个数组中减去它。没有一个主题比我目前的版本更快。
我知道你可以使用例如PyPy来加速python代码,但是我问我是否可以编写没有它们的更快版本。
你们中有人可能是个好主意还是找到了一个瓶子?