pythoninputblobrgbsimplecv

Input print to score system


I have code that compares the x y coordinates with another and prints the RGB values to the terminal. How can I use the RGB values as input for a scoring system?

img = Image('/home/pi/Desktop/DartProj/DartConvert/dart1blob.jpg')
blobs = img.findBlobs()
first_blob = blobs[0] if blobs else None
if first_blob:
    pixcol = Image('/home/pi/Desktop/DartProj/Score/ColourScoreBoard.jpg')
    colrgb =  pixcol[first_blob.x, first_blob.y]
print colrgb

The code above prints the RGB values to the terminal, eg. (63.0, 71.0, 204.0) - a shade of blue. How can I make that particular RGB value equal a score of 20 (and other RGB values to other scores).

Any help, guidance, links etc would be appreciated, beginner here.


Solution

  • Define a mapping between the rgb tuples and the score. Below is an example, you'd have to implement the full mapping with the rgb values you know you have on your board.

    rgbmap = { (255, 255, 255): 60, (0, 0, 0): 20, (0, 0, 255): 2}
    
    def getscoreforrgb(rgb):
        return rgbmap[rgb]
    
    print "score for 255, 255, 255 is ", rgbmap[(255, 255, 255)]
    print "score for 0, 0, 0 is ", getscoreforrgb((0, 0, 0))
    

    You can take advantage of the fact a tuple is hashable and use it as a dictionary key. You can wrap it in a function or just access the dict directly, up to you (both methods shown).

    Obviously you get an exception if that tuple has no entry in the map but you'd have to wrap the code in a try/except.