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

这里以window为例:

安装python虚拟环境:

安装virtuallenv:

pip install virtualenv

使用cmd进入指定后目录新建虚拟环境:

virtualenv env1

这样会生成虚拟环境:

这里使用PyCharm进行配置:

配置好解释器即可。

 

虚拟环境一般package是空的,不会向我上面那样。下面是安装opencv相关的包:

使用腾讯的源安装,这个比较快:

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

这样就安装好了,就和我上面一样了。

下面是准备好一个像素的素材,来搞个颜色识别的demo

原理是转HSV,在进行识别,以前用C++写过相关原理了,在此不在具体说明

源码如下:

import cv2

if __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("黑")
        pass
    elif (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("灰")
        pass
    elif (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("白")
        pass
    elif ((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("红")
        pass
    elif (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("橙")
        pass
    elif (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("黄")
        pass
    elif (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("绿")
        pass
    elif (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("青")
        pass
    elif (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("蓝")
        pass
    elif (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("紫")
        pass

    array = matHsv[0:1][0:1][0:3]
    print("over")
    pass

程序运行截图如下:

第一张

第二张

第三张

 

 

©️2020 CSDN 皮肤主题: 数字20 设计师: CSDN官方博客 返回首页
实付0元
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、C币套餐、付费专栏及课程。

余额充值