Colour Classifier

So you have written that new, must have program. Let us see it here.
Post Reply
User avatar
stevedee
Posts: 450
Joined: Monday 20th March 2017 6:06pm

Colour Classifier

Post by stevedee » Wednesday 10th February 2021 8:23pm

I spent a few hours trying to get the remnants of the old standard C Library in opencv to play nice with Gambas, but no joy.

As I only wanted to create a relatively simple colour matching program, I've just done it in Gambas.
ColourRecognition.png
ColourRecognition.png (35.38 KiB) Viewed 1608 times
This is only really an experimental test bed for an idea I'm toying with. But I'm posting it here in case its of any interest to you.

The colour look-up csv file has hundreds of colours. I took the file from somewhere on Github...thanks whoever you are.

ColourClassifier.tar.gz
(341.44 KiB) Downloaded 69 times

User avatar
stevedee
Posts: 450
Joined: Monday 20th March 2017 6:06pm

Colour Classifier: determine resistor colours/values

Post by stevedee » Friday 12th March 2021 5:53pm

Don't know if this is of any interest, but it may be for anyone who enjoys experimenting.

Taking the basic colour look-up idea in the first post, I thought I'd explore the possibility of a real-world colour recognition application. Although I've known how to read the colour-codes on electrical resistors since I was 10 or 11, I wondered how difficult it would be to write some Gambas code to do the same job.

With real images of resistors (photos) you are at the mercy of digital noise and poor lighting. So my code includes a number of filtering techniques in an attempt to overcome such difficulties.
ResistorColourClassifier.png
ResistorColourClassifier.png (284.69 KiB) Viewed 1528 times
This code is not Showcase quality. Its particularly rough as I've been cutting chunks out and hacking new bits in. So it should really be seen as a test bed for trying out algorithms that might just improve results, rather than any attempt to write pretty code.

You will find a more detailed explanation in my blog post: http://captainbodgit.blogspot.com/2021/ ... ion-2.html

ColourClassifier.tar.gz
(13.9 MiB) Downloaded 56 times

User avatar
cogier
Site Admin
Posts: 739
Joined: Wednesday 21st September 2016 2:22pm
Location: Guernsey, Channel Islands

Re: Colour Classifier

Post by cogier » Saturday 13th March 2021 2:26pm

Hi Steve,

If you add the following code to the beginning of your Colour Classifier Form_Open() routine it will allow the Form to expand.
 With Me
    .Arrangement = Arrange.Vertical
    .Padding = 5
    .Invert = True
  End With

  PicBox.Expand = True

User avatar
stevedee
Posts: 450
Joined: Monday 20th March 2017 6:06pm

Re: Colour Classifier

Post by stevedee » Sunday 14th March 2021 4:56pm

cogier wrote:
Saturday 13th March 2021 2:26pm
If you add the following code...
Thanks Charlie, but this didn't quite work right.
But not to worry, I'm going to change the layout first and then take another look at this.

User avatar
cogier
Site Admin
Posts: 739
Joined: Wednesday 21st September 2016 2:22pm
Location: Guernsey, Channel Islands

Re: Colour Classifier

Post by cogier » Sunday 21st March 2021 4:02pm

I found this to be interesting. Your list of colours was relatively small, so I went and found this site https://www.w3schools.com/colors/. I had to copy the colours from the web pages and manipulate things to create a CSV file that would do the job. The CSV has 2148 named colours with Decimal, Hex and RGB details. The fact that there are 16,000,000 colours on a modern computer means that the hit rate is only around 1 in every 7500 colours. The attached program will find the nearest colours, which are often some way off the original. I have no idea where all the names came from e.g.Bubble Bath (tickle Me Pink)!

You can select a colour by name or move the selector around to pick a colour which will normally offer the closest match.

If the program does not suit your needs may be the (Made for Gambas!) CSV file will be of some help.

Image
NamedColour-0.0.1.tar.gz
(53.05 KiB) Downloaded 53 times

User avatar
stevedee
Posts: 450
Joined: Monday 20th March 2017 6:06pm

Re: Colour Classifier

Post by stevedee » Tuesday 23rd March 2021 10:43am

cogier wrote:
Sunday 21st March 2021 4:02pm
I found this to be interesting. Your list of colours was relatively small, so I went and found...

...The CSV has 2148 named colours with Decimal, Hex and RGB details. The fact that there are 16,000,000 colours on a modern computer means that the hit rate is only around 1 in every 7500 colours...
Hi Charlie and thanks for your input.

While its true that computers can currently represent millions of colours, people have a very limited capability to identify individual colours.

The 10 colours used for the resistor value code have been deliberately chosen so that they can be identified by the majority of the population (although a significant minority cannot reliably identify them). So for example, the majority would be able to tell the difference between a colour known as blue from a colour known as green, while a minority might not see a clear distinction between blue and green.

So with this in mind, I'm not sure that having more 'colour shades' available to the program would necessarily help with identification. The existing look-up table/file allows us to find the closest match from hundreds of 'colour shades'.

Note: I'm using the term 'colour shades' because a colour called (say) "antique ruby" may just be identified by someone as the colour red. I'm using 'colour bands' to mean a band of different colour shades that we would generally agree to be a basic colour (e.g. red, green, yellow & so on).


The program needs to be able to establish 10 colour 'bands' (e.g. one for 'colour shades' that meet the requirement for the red band of colours, and nine more for the others). It also needs another 9 bands for 'non-colours' between each of the resistor colour bands (e.g. a band between black & brown, a band between brown & red, a band between red & orange & so on).

As an example, this is probably how I would classify the top 24 colours from your post:-
CharlieColours.png
CharlieColours.png (12.63 KiB) Viewed 1388 times
...where the colours marked by dots have been classified as resistor colours and the non-dotted colours are just gaps or non-colours. (...Hmm, looking at this again, I'm not sure about orange!)

Also note that if you had a lookup file/table with thousands of colours, it would take a very long time to rename just the ones that matched the basic 10 resistor colours.

I think this basic classification should be possible with a lookup table of less than 1000 colour shades, although I haven't checked the existing file to look for gaps and clusters (i.e. there may be too few colour shades between {say} orange & yellow). With each colour shade from my existing csv file, I have to carefully assess whether I'm sure that it is definitely {say} a red and could not bee seen as an orange or a brown. So far I've only done this for colours that come up in my programs colour list (i.e when running with example images) that I think need re-classifying.

The main problem when using images of real resistors, is the colour shade/tint variations due to lighting. Generally, people are quite good at making adjustments/allowances for these effects, but at the moment, my program is not.

Post Reply