Python循环遍历图像的每个像素

时间:2017-08-14 17:57:44

标签: python for-loop

如何让下面的循环更快?它是逐个像素的循环,当我运行它时,我得到的只是for循环和if-else块之后的两个print语句的字符串。

for i in xrange(0, nCols, 1):
    p = img[i, row] #image[column, row]

    if withinRange(p): #if the pixel is a certain color, or within a certain range of colors
        pixelCount += 1
        #print "count after the initial row loop is: ", pixelCount

print 'time after initial row loop ', time.time()

if pixelCount == 0: # In other words, no pixels were found in this whole row 
                    #- take the average middle row for each line of text
    for i in xrange(0, nCols, 1):
        img[i, row] = 255

else:
    start = row
print 'time after start = row loop', time.time()

有没有人有关于我如何优化这个的提示?

withinRange()函数是一个布尔值,所有它看的是颜色强度(I' m处理灰度图像)是否在两个特定值之间,例如,如果img [x,y]介于242和254之间,则为真。

我看了几个似乎相似的答案,但不是这个。 Faster way to loop through every pixel of an image in Python?

我目前正在使用scikit图像,但我愿意使用OpenCV。这是一个相当基本的操作,只需检查某些条件,然后操纵像素强度的值。

0 个答案:

没有答案