Skip to content
Advertisement

python – check presence of predefined color ranges in image

I would need to verify if and how much of certain predefined color ranges are present in a image file.

each color range is defined by 6 variables and a counter in a colorRange class:

JavaScript

The image can either be a file, or loaded from the camera. Following code loads the image from the camera buffer:

JavaScript

what I would need to do is to scan every n-th pixel (5 is a good starting value), and compare it to every color range. if it falls into that color range, then add +1 to that specific colorCounter. In the end I go over the Counter of each color range and calculate the presence of that color range in %. The goal is to check the % of analized pixels that fall into each color range. The sum of the colorranges can be greater than 100 since a pixel can fall into multiple colorranges as tehy can be overlapping. (for example a color ragne could be all the reds, and another could be only the dark reds.. a dark red pixel would fall into both ranges, a bright red only in the first one.)

The way I would do it is to check every fifth pixel one by one, convert its rgb value to HSL, and then compare it with all the color ranges. (where if hS > hE then it is wrapping around in the reds)

But it seems a very complicated way to do it and was wondering if there are some premade functions that can do this, or at least partially do this.

So the question is: How can this be done in a smart way?

UPDATE:

this is what I have so far:

JavaScript

Advertisement

Answer

I followed the advice of @Mark Setchell and came up with this solution:

The color class:

JavaScript

The adopted solution:

JavaScript
User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement