Python笔记-安装python虚拟环境及配置opencv及通过opencv识别颜色
生活随笔
收集整理的這篇文章主要介紹了
Python笔记-安装python虚拟环境及配置opencv及通过opencv识别颜色
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
這里以window為例:
安裝python虛擬環(huán)境:
安裝virtuallenv:
pip install virtualenv使用cmd進(jìn)入指定后目錄新建虛擬環(huán)境:
virtualenv env1這樣會生成虛擬環(huán)境:
這里使用PyCharm進(jìn)行配置:
配置好解釋器即可。
?
虛擬環(huán)境一般package是空的,不會向我上面那樣。下面是安裝opencv相關(guān)的包:
使用騰訊的源安裝,這個比較快:
pip install opencv-python -i https://mirrors.cloud.tencent.com/pypi/simple pip install opencv-contrib-python -i https://mirrors.cloud.tencent.com/pypi/simple這樣就安裝好了,就和我上面一樣了。
下面是準(zhǔn)備好一個像素的素材,來搞個顏色識別的demo
原理是轉(zhuǎn)HSV,在進(jìn)行識別,以前用C++寫過相關(guān)原理了,在此不在具體說明
源碼如下:
import cv2if __name__ == '__main__':matSrc = cv2.imread('C:\\Users\\d5000\\Desktop\\pic\\1.png', cv2.IMREAD_UNCHANGED)matHsv = cv2.cvtColor(matSrc, cv2.COLOR_BGR2HSV)if (matHsv[0][0][0] >= 0 and matHsv[0][0][0] <= 180) and (matHsv[0][0][1] > 0 and matHsv[0][0][1] <= 255) and (matHsv[0][0][2] >= 0 and matHsv[0][0][2] <= 46):print("黑")passelif (matHsv[0][0][0] >= 0 and matHsv[0][0][0] <= 180) and (matHsv[0][0][1] > 0 and matHsv[0][0][1] <= 43) and (matHsv[0][0][2] >= 46 and matHsv[0][0][2] <= 220):print("灰")passelif (matHsv[0][0][0] >= 0 and matHsv[0][0][0] <= 180) and (matHsv[0][0][1] > 0 and matHsv[0][0][1] <= 30) and (matHsv[0][0][2] >= 221 and matHsv[0][0][2] <= 255):print("白")passelif ((matHsv[0][0][0] >= 0 and matHsv[0][0][0] <= 10) or (matHsv[0][0][0] >= 156 and matHsv[0][0][0] < 180)) and (matHsv[0][0][1] > 43 and matHsv[0][0][1] <= 255) and (matHsv[0][0][2] >= 46 and matHsv[0][0][2] <= 255):print("紅")passelif (matHsv[0][0][0] >= 11 and matHsv[0][0][0] <= 25) and (matHsv[0][0][1] > 43 and matHsv[0][0][1] <= 255) and (matHsv[0][0][2] >= 46 and matHsv[0][0][2] <= 255):print("橙")passelif (matHsv[0][0][0] >= 26 and matHsv[0][0][0] <= 34) and (matHsv[0][0][1] > 43 and matHsv[0][0][1] <= 255) and (matHsv[0][0][2] >= 46 and matHsv[0][0][2] <= 255):print("黃")passelif (matHsv[0][0][0] >= 35 and matHsv[0][0][0] <= 77) and (matHsv[0][0][1] > 43 and matHsv[0][0][1] <= 255) and (matHsv[0][0][2] >= 46 and matHsv[0][0][2] <= 255):print("綠")passelif (matHsv[0][0][0] >= 78 and matHsv[0][0][0] <= 99) and (matHsv[0][0][1] > 43 and matHsv[0][0][1] <= 255) and (matHsv[0][0][2] >= 46 and matHsv[0][0][2] <= 255):print("青")passelif (matHsv[0][0][0] >= 100 and matHsv[0][0][0] <= 155) and (matHsv[0][0][1] > 43 and matHsv[0][0][1] <= 255) and (matHsv[0][0][2] >= 46 and matHsv[0][0][2] <= 255):print("藍(lán)")passelif (matHsv[0][0][0] >= 125 and matHsv[0][0][0] <= 155) and (matHsv[0][0][1] > 43 and matHsv[0][0][1] <= 255) and (matHsv[0][0][2] >= 46 and matHsv[0][0][2] <= 255):print("紫")passarray = matHsv[0:1][0:1][0:3]print("over")pass程序運行截圖如下:
第一張
第二張
第三張
?
?
總結(jié)
以上是生活随笔為你收集整理的Python笔记-安装python虚拟环境及配置opencv及通过opencv识别颜色的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Python笔记-flask执行后台程序
- 下一篇: Python笔记-centos7使用ad