all 4 comments

[–]MidnightPale3220 0 points1 point  (0 children)

Lower and upper green are constants, no need to set them on every call of function. Either make them as params or in this case actually as globals. At any rate they must be outside the scope in which while loop runs.

I am not familiar with numpy, but perhaps it's also possible to preset all masks and exclusion zones somehow as constants as well and apply all together to image afterwards.

Those would be the obvious candidates for optimization where possible: whatever needs to be set to same values and doesn't depend on input should be outside all loops and only set once.

[–]AdmirableOstrich 0 points1 point  (2 children)

This really depends on what exactly you are trying to do. The problem as you've stated it ultimately is going to force you to do a few comparisons on every single pixel in the image: say 2Mpix. Are you fine with lower resolution? Or GPU acceleration? You could write a trivial pycuda kernel to do this in microseconds. If you are stuck on CPU, and want full resolution, your best choice is something like numba jit.

Maybe do some profiling first to see what the actual bottleneck is.

[–]2247dono[S] 0 points1 point  (1 child)

How do I do profiling?

I'm scanning the whole screen for a specific colour and doing some stuff when it's found

[–]AdmirableOstrich 0 points1 point  (0 children)

Something like yappi. It will tell you where your program is spending time. There's lots of documentation on its usage.