Fast loop algorithm open cv python

Edwin B. :

i created an algorithm with two for-loops for my gray image. Every pixel above the value 180 and on the left side of the image gets a new value.

This algorithm is very slow and takes a few seconds.

Is there a faster way to do this job?

   for x in range(img.shape[0]):
        for y in range(img.shape[1]):
            if(img[x,y]>180 or y>450):
                img[x,y]=255
William Miller :

You should employ vectorized numpy operations, something like

img[img > 180] = 255
img[:,450:] = 255

Should do exactly what your loop does, but much more quickly. To apply both conditions together you can do

img[:,450:][img[:,450:] > 180] = 255

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=387160&siteId=1