Friday 12 February 2021

Playing with colour recognition #1

A Python tutorial on Colour Recognition recently grabbed my attention.


It looked like an interesting playground to step into, so... any good Gambaser, I decided to write some code.

The article in question was actually centered around opencv and illustrated some techniques using the opencv API with Python. Any coloured area clicked by the mouse would be compared to a look-up table of colours in a CSV file.

After getting the Python code to run and having played around with it for a while, I wondered if I could replicate it with Gambas by using the opencv C API. Although I knew that support for the C API had been dropped some time ago, my understanding was that the code was still in the libraries.

my Gambas code

However, after several hours of disappointment, I gave up and just started coding in Gambas without it.

In fact, none of the openvc stuff was required for this simple program.

All my Gambas program did initially was to load a test image and allow the user to pick a pixel via the mouse while looking-up the nearest colour from a CSV file.

The biggest chunk of code just loads the CSV file into a collection and then compares the sum of the RGB channels with each of the 800+ colours detailed in the CSV file.

Public Function ColourLookup(iColours As Integer[]) As String
Dim index As Integer
Dim aFields As String[]
Dim iRecords As Integer
Dim colRecords As New Collection
Dim intRedDiff As Integer
Dim intGreenDiff As Integer
Dim intBlueDiff As Integer
Dim intLowestDifference As Integer = 10000
Dim strColourMatch As String = "Larks Vomit Green"

  myCSVfile = New CsvFile(strRefColours)
  Do Until myCSVfile.Eof
    colRecords[myCSVfile.Line] = myCSVfile.Read()   'read every line into a collection
    Inc iRecords                                    'count the number of records
    aFields = myCSVfile.Fields      'get the field headers
    Me.Text = "csv records: " & CStr(iRecords) & ", fields: " & CStr(aFields.Count)
  'find closest colour match
  For index = 1 To iRecords
    intRedDiff = Abs(iColours[0] - colRecords[CStr(index + 1)]["r"])
    intGreenDiff = Abs(iColours[1] - colRecords[CStr(index + 1)]["g"])
    intBlueDiff = Abs(iColours[2] - colRecords[CStr(index + 1)]["b"])
    If intLowestDifference > intRedDiff + intGreenDiff + intBlueDiff Then
      intLowestDifference = intRedDiff + intGreenDiff + intBlueDiff
      strColourMatch = colRecords[CStr(index + 1)]["colourName"]
  lblColourMatch.Text = "closest colour match: " & strColourMatch

Someone on GitHub called codebrainz (Matthew Brush) had a lot of patience putting together the colour CSV file...


 ...all I had to do was add the headers.

So my initial Gambas code doesn't do very much, but if you want to download the project, I've posted it over at GambasONE:

an application for this madness

Just to set myself a real-life application challenge, I thought it might be interesting to make a program that could decode the colours used on electrical resistors to denote their value. The basic code looks like this:-

When viewed left-to-right, the first two colours are digits, while the third represents the number of zeroes.

The colour code roughly follows the colours of the rainbow:-












For the sample image above; yellow = 4, violet = 7 and orange = 3 zeroes

So the resistor value is 47000 Ohms.

what's next?

The next step is to take a slice through the centre of the resistor image and convert the colours to numbers. After that, I need to start working on real resistor images. This should happen in my next post.


Playing with colour recognition #2:

No comments:

Post a Comment