当前位置:   article > 正文

opencv Python 图片颜色识别_colour-checker-detection-l-seg

colour-checker-detection-l-seg

https://data-flair.training/blogs/project-in-python-colour-detection/

想做颜色识别,于是找到了一个这样的教程。
发觉对于颜色的精确识别很困难,本文的方法是,找到鼠标敲击处的图片RGB值,在一定容差范围内与已知颜色表进行匹配,从而得到颜色名。

# -*- coding: utf-8 -*-
import argparse
import cv2
import numpy as np
import pandas as pd


"""directly give an image path from the command prompt"""
ap = argparse.ArgumentParser()
ap.add_argument('-i','--image',required=True,help='Image Path')
args = vars(ap.parse_args())
img_path = args['image']
"""reading image with opencv"""
img = cv2.imread(img_path)
"""declaring global variables (are used later on)"""
clicked = False
r = g = b = xpos = ypos = 0
"""reading csv file with pandas and giving names to each column"""
index = ['color','color_name','hex','R','G','B']
csv = pd.read_csv('colors.csv', names=index, header=None)


def draw_function(event, x, y, flags, parm):
    if event == cv2.EVENT_LBUTTONDBLCLK: #鼠标左击
        global b, g, r, xpos, ypos, clicked
        # global parameters, should be in the same file
        clicked = True
        xpos = x
        ypos = y
        b, g, r = img[y, x]
        b = int(b)
        g = int(g)
        r = int(r)


def getColorName(R, G, B):
    minimum = 10000
    for i in range(len(csv)):
        d = abs(R - int(csv.loc[i,"R"])) + abs(G - int(csv.loc[i,"G"])) \
            + abs(B - int(csv.loc[i,"B"]))
        if (d <= minimum):
            minimum = d
            cname = csv.loc[i, 'color_name']
    return cname


"""set a mouse callback event on a window"""
cv2.namedWindow('image')
cv2.setMouseCallback('image', draw_function) #鼠标事件响应

"""display image on the window"""
while(1):
    cv2.imshow('image', img)
    if (clicked):
        cv2.rectangle(img, (20, 20), (750, 60), (b,g,r), -1)
        """creating text string to display (color name and RGB valuse)"""
        text = getColorName(r, g, b) + "R=" + str(r) + "G=" + str(g) + "B=" + str(b)
        cv2.putText(img, text, (50,50), 2, 0.8, (255,255,255),2, cv2.LINE_AA)
        """For very light colours we will display text in black color"""
        if (r+g+b>=600):
            cv2.putText(img, text, (50,50), 2, 0.8, (0,0,0), cv2.LINE_AA)
        clicked = False
    if cv2.waitKey(20) & 0xFF == 27:
        break
cv2.destroyWindow()
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65

argparse.ArgumentParser() 用于添加参数
pandas 读取csv文件很方便

cv2一些函数:

cv2.rectangle(img, (bbox.left, bbox.top), (bbox.right, bbox.bottom), (0,0,255), 2)
  • 1

靠确定对角线来画矩形。
各参数依次是:图片,左上角坐标,右下角坐标,填充颜色,线粗细(-1则为填充)

cv2.putText(img, str(i), (123,456)), font, 2, (0,255,0), 3)
  • 1

各参数依次是:图片,添加的文字,左上角坐标,字体,字体大小,颜色,字体粗细

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/爱喝兽奶帝天荒/article/detail/836919
推荐阅读
相关标签
  

闽ICP备14008679号