Ask Your Question

Revision history [back]

Hey, this code takes about 1.5 s to run on a modern laptop and I believe the code is as concise as possible.

However, there are definitely ways to speed up the process, IF there are properties about the image that you know about.

For e.g., if your background has the same color, then you can decide before, if you want the background to be black or white. This will save computer time, because it reduces the number of instruction from 6 to 1.

Or for e.g., if you know that your image will have a higher number of blacks than white, use the conditional statement in a way that can short circuit with the 6 conditions.

--> instead of: if (a == b AND a == c).... , --> do: if (a != b || ...