opencv识别图片颜色(1)

一般对颜色空间的图像进行有效处理都是在HSV空间进行的,然后对于基本色中对应的HSV分量需要给定一个严格的范围,下面是通过实验计算的模糊范围(准确的范围在网上都没有给出)。

H:  0 — 180

S:  0 — 255

V:  0 — 255

此处把部分红色归为紫色范围:


上图引用:https://blog.csdn.net/taily_duan/article/details/51506776 

参考了一下程序:原文:https://blog.csdn.net/spw_1201/article/details/78310349 

https://blog.csdn.net/weixin_39490421/article/details/85998984

import cv2
import numpy as np
from matplotlib import pyplot as plt
image=cv2.imread('./src/q5.png')
HSV=cv2.cvtColor(image,cv2.COLOR_BGR2HSV)
def getpos(event,x,y,flags,param):
    if event==cv2.EVENT_LBUTTONDOWN:
        print(HSV[y,x])
#th2=cv2.adaptiveThreshold(imagegray,255,cv2.ADAPTIVE_THRESH_MEAN_C,cv2.THRESH_BINARY,11,2)
cv2.imshow("imageHSV",HSV)
cv2.imshow('image',image)
cv2.setMouseCallback("imageHSV",getpos)
cv2.waitKey(0)
#print (image(10,10,10))

废话不多说上源码:

import cv2
import numpy as np
from matplotlib import pyplot as plt
image=cv2.imread('E:\jre\eee.jpg')
HSV=cv2.cvtColor(image,cv2.COLOR_BGR2HSV)
#def getpos(event,x,y,flags,param):
    #if event==cv2.EVENT_LBUTTONDOWN:
b=HSV[2,3]
print(b[0],b[1],b[2])#输出一个坐标点的颜色
'''
if b[0]>=156 and b[0]<=180:
    if b[1] >= 43 and b[1] <= 255:
        if b[2] >= 46 and b[2] <= 255:
            print("识别成功:红色")
'''
if b[0] >= 100 and b[0] <= 124:
    if b[1] >= 43 and b[1] <= 255:
        if b[2] >= 46 and b[2] <= 255:
            print("识别成功:蓝色")

#if HSV[2,3]==[178 ,255 ,204]:
#    print("红色")
cv2.imshow("imageHSV",HSV)
cv2.imshow('image',image)#显示img
#cv2.setMouseCallback("imageHSV",getpos)#
cv2.waitKey(0)
#print (image(10,10,10))

 

你可能感兴趣的:(python,opencv)