identify color sequence with OpenCV code example

Example 1: identify color sequence with OpenCV

res = []
for cnt in contours:
    if cv2.contourArea(cnt) > 100:
        x,y,w,h = cv2.boundingRect(cnt)
        cx,cy = x+w/2, y+h/2
        color = hsv[cy,cx,0]

        if (color < 10 or color > 170):
            res.append([cx,cy,'R'])
        elif(50 < color < 70):
            res.append([cx,cy,'G'])
        elif(20 < color <40):
            res.append([cx,cy,'Y'])
        elif(110 < color < 130):
            res.append([cx,cy,'B'])

res = sorted(res,key = lambda res : res[0])
colors = [x[2] for x in res]
print colors

Example 2: identify color sequence with OpenCV

import numpy as np
import cv2

img  = cv2.imread('sofqn.png')
gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
hsv = cv2.cvtColor(gray,cv2.COLOR_BGR2HSV)

Tags:

Misc Example