opencv 顏色識別(3)

import cv2
import numpy as np
from matplotlib import pyplot as plt
'''讀取照片'''
image=cv2.imread('E:\jre\y23.jpg')
HSV=cv2.cvtColor(image,cv2.COLOR_BGR2HSV)
#def getpos(event,x,y,flags,param):
    #if event==cv2.EVENT_LBUTTONDOWN:
#b=HSV[0,0]#因爲從零開始計數的(像素爲120*180)初始值(0.0)
#print(b[0],b[1],b[2])#輸出一個座標點的顏色

'''
if b[0]>=156 and b[0]<=180:#判斷H
    if b[1] >= 43 and b[1] <= 255:判斷S
        if b[2] >= 46 and b[2] <= 255:判斷
            print("識別成功:紅色")
'''
z=range(2592)#可以寫成z=range(0,20)
d=range(1944)
n=0
for x in z:
    if n == 1:  # 加一個標誌位判斷
        break  # 如果標誌位置一則跳出
    for y in d:
        b = HSV[x,y]
        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("識別成功:含有藍色像素")
                    n=1
                break

#if HSV[2,3]==[178 ,255 ,204]:
#    print("紅色")
#cv2.imshow("imageHSV",HSV)#顯示轉化爲HSV後的圖片
cv2.imshow('image',image)#顯示img
#cv2.setMouseCallback("imageHSV",getpos)#
cv2.waitKey(0)
#print (image(10,10,10))

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章