基于python Opencv和PIL库 完成对小物块的轮廓提取和颜色识别


前言


最近做了个识别物块颜色的小demo,迫不及待分享出来。

一、Opencv是什么:

OpenCV是一个基于BSD许可(开源)发行的跨平台计算机视觉和机器学习软件库,可以运行在Linux、Windows、Android和Mac OS操作系统上。 它轻量级而且高效——由一系列 C 函数和少量 C++ 类构成,同时提供了Python、Ruby、MATLAB等语言的接口,实现了图像处理和计算机视觉方面的很多通用算法。

二、代码

1.引入库

import cv2 as cv
from PIL import Image
from matplotlib import pyplot as plt
import copy

2.读入数据

filepath = r"C:\Users\38429\Desktop\PicFiles\color_blocks\r\WIN_20210610_12_53_56_Pro.jpg"
src1 = cv.imread(filepath)  # 读入图片放进src中
img1 = Image.open(filepath)

3.对图像进行预处理(滤波去噪,灰度处理,阈值分割)

    image1 = copy.deepcopy(image)
    blured = cv.bilateralFilter(image, 0, 50, 50)
    
    gray = cv.cvtColor(image, cv.COLOR_BGR2GRAY)
    ret, binary = cv.threshold(gray, 0, 255, cv.THRESH_BINARY | cv.THRESH_OTSU)

3.用find、draw contours函数提取小物块轮廓并画出,getpixel函数拾取轮廓内像素值

contours, hierarchy = cv.findContours(binary, cv.RETR_TREE, cv.CHAIN_APPROX_SIMPLE)

    for i, contour in enumerate(contours):
        x, y, w, h = cv.boundingRect(contours[1])
    RGB = img1.getpixel((x + 100, y + 100))

    #  draw rectangle
    for i in range(0, len(contours)):
        drawContours = cv.drawContours(image1, contours, i, (0, 0, 255), 2)

    result = cv.rectangle(image, (x, y), (x + w, y + h - 50), (RGB[2], RGB[1], RGB[0]), 3)

4.在结果图像上标识

    r = RGB[0]
    g = RGB[1]
    b = RGB[2]
    Text = ['Blue', 'Green', 'Red', 'Orange', 'Purple']
    if r < b and g < b:
        # print('蓝色')
        t = Text[0]
    elif r < g and b < g:
        # print('绿色')
        t = Text[1]
    elif b < g < 60 < r:
        # print('红色')
        t = Text[2]
    elif r > g >= 60 and b < r:
        # print('橙色')
        t = Text[3]
    elif g < b < r:
        # print('紫色')
        t = Text[4]

    cv.putText(image, text=t, org=(x, y - 30), fontFace=2, fontScale=2, color=(RGB[2], RGB[1], RGB[0]), thickness=2)

5.利用matplotlib库使整个处理过程可视化

    #  BGR2RGB
    pltBlured = cv.cvtColor(blured, cv.COLOR_BGR2RGB)
    pltGray = cv.cvtColor(gray, cv.COLOR_BGR2RGB)
    pltBinary = cv.cvtColor(binary, cv.COLOR_BGR2RGB)
    pltDrawcontours = cv.cvtColor(drawContours, cv.COLOR_BGR2RGB)
    pltResult = cv.cvtColor(result, cv.COLOR_BGR2RGB)

    #  plt.subplot
    plt.suptitle('RGB:(%s,%s,%s),%s' % (RGB[0], RGB[1], RGB[2], t))
    plt.subplot(231), plt.imshow(img1)  # colormap 默认为RGB空间,需要将处理完的图2RGB
    plt.title('Input Image'), plt.xticks([]), plt.yticks([])
    plt.subplot(232), plt.imshow(pltBlured)
    plt.title('blured|EPF'), plt.xticks([]), plt.yticks([])
    plt.subplot(233), plt.imshow(pltGray)
    plt.title('gray'), plt.xticks([]), plt.yticks([])
    plt.subplot(234), plt.imshow(pltBinary)
    plt.title('binary|OTSU'), plt.xticks([]), plt.yticks([])
    plt.subplot(235), plt.imshow(pltDrawcontours)
    plt.title('contours'), plt.xticks([]), plt.yticks([])
    plt.subplot(236), plt.imshow(pltResult)
    plt.title('Result'), plt.xticks([]), plt.yticks([])
    plt.show()

6.Run

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

在这里插入图片描述

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值