日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程语言 > python >内容正文

python

Python笔记-安装python虚拟环境及配置opencv及通过opencv识别颜色

發(fā)布時間:2025/3/15 python 18 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。