【无标题】

一、OpenCV图片的简单操作

1.灰度图片转换
1)原图
在这里插入图片描述

灰度化

图片读取
src = cv2.imread("YuKaguraNe.png")
img = src.copy()

灰度化
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

效果:
在这里插入图片描述

2.彩色图像(RGB)转为HSV、HSI 格式
1) HSV是一种比较直观的颜色模型,所以在许多图像编辑工具中应用比较广泛,这个模型中颜色的参数分别是:色调(H, Hue),饱和度(S,Saturation),明度(V, Value)。

hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)

在这里插入图片描述
HSI色彩空间是从人的视觉系统出发,用色调(Hue)、**色饱和度(Saturation或Chroma)和亮度(Intensity或Brightness)**来描述色彩

def rgb2hsi(image):
    b, g, r = cv.split(image)                    # 读取通道
    r = r / 255.0                                # 归一化
    g = g / 255.0
    b = b / 255.0
    eps = 1e-6                                   # 防止除零

    img_i = (r + g + b) / 3                      # I分量

    img_h = np.zeros(r.shape, dtype=np.float32)
    img_s = np.zeros(r.shape, dtype=np.float32)
    min_rgb = np.zeros(r.shape, dtype=np.float32)
    # 获取RGB中最小值
    min_rgb = np.where((r <= g) & (r <= b), r, min_rgb)
    min_rgb = np.where((g <= r) & (g <= b), g, min_rgb)
    min_rgb = np.where((b <= g) & (b <= r), b, min_rgb)
    img_s = 1 - 3*min_rgb/(r+g+b+eps)                                            # S分量

    num = ((r-g) + (r-b))/2
    den = np.sqrt((r-g)**2 + (r-b)*(g-b))
    theta = np.arccos(num/(den+eps))
    img_h = np.where((b-g) > 0, 2*np.pi - theta, theta)                           # H分量
    img_h = np.where(img_s == 0, 0, img_h)

    img_h = img_h/(2*np.pi)                                                       # 归一化
    temp_s = img_s - np.min(img_s)
    temp_i = img_i - np.min(img_i)
    img_s = temp_s/np.max(temp_s)
    img_i = temp_i/np.max(temp_i)

    image_hsi = cv.merge([img_h, img_s, img_i])
    return image_hsi

在这里插入图片描述

二、车牌数字分割

1.引入库

# 导入必要的包/库
import numpy as np
import cv2
import os

2.函数定义
1)图片栈

def stackImages(scale, imgArray):
    """
        将多张图像压入同一个窗口显示
        :param scale:float类型,输出图像显示百分比,控制缩放比例,0.5=图像分辨率缩小一半
        :param imgArray:元组嵌套列表,需要排列的图像矩阵
        :return:输出图像
    """
    rows = len(imgArray)
    cols = len(imgArray[0])

    rowsAvailable = isinstance(imgArray[0], list)

    # 用空图片补齐
    for i in range(rows):
        tmp = cols - len(imgArray[i])
        for j in range(tmp):
            img = np.zeros((imgArray[0][0].shape[0], imgArray[0][0].shape[1]), dtype='uint8')
            imgArray[i].append(img)

    # 判断维数
    if rows>=2:
        width = imgArray[0][0].shape[1]
        height = imgArray[0][0].shape[0]

    else:
        width = imgArray[0].shape[1]
        height = imgArray[0].shape[0]

    if rowsAvailable:
        for x in range(0, rows):
            for y in range(0, cols):
                if imgArray[x][y].shape[:2] == imgArray[0][0].shape[:2]:
                    imgArray[x][y] = cv2.resize(imgArray[x][y], (0, 0), None, scale, scale)
                else:
                    imgArray[x][y] = cv2.resize(imgArray[x][y], (imgArray[0][0].shape[1], imgArray[0][0].shape[0]),
                                                None, scale, scale)
                if len(imgArray[x][y].shape) == 2:
                    imgArray[x][y] = cv2.cvtColor(imgArray[x][y], cv2.COLOR_GRAY2BGR)
        imageBlank = np.zeros((height, width, 3), np.uint8)
        hor = [imageBlank] * rows
        hor_con = [imageBlank] * rows
        for x in range(0, rows):
            hor[x] = np.hstack(imgArray[x])
        ver = np.vstack(hor)
    else:
        for x in range(0, rows):
            if imgArray[x].shape[:2] == imgArray[0].shape[:2]:
                imgArray[x] = cv2.resize(imgArray[x], (0, 0), None, scale, scale)
            else:
                imgArray[x] = cv2.resize(imgArray[x], (imgArray[0].shape[1], imgArray[0].shape[0]), None, scale, scale)
            if len(imgArray[x].shape) == 2: imgArray[x] = cv2.cvtColor(imgArray[x], cv2.COLOR_GRAY2BGR)
        hor = np.hstack(imgArray)
        ver = hor
    return ver

2)单字分割识别
通过检测每一列的黑色点和白色点的比例,大于阈值则切割图片

#读取图片
    print("正在处理"+file_path+car)
    src = cv2.imread(file_path+car)
    img = src.copy()

    #灰度转化
    gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
    #二值化将除了白色的值全部变为黑色
    ret,thresh = cv2.threshold(gray, 254, 255, cv2.THRESH_BINARY )
    #腐蚀杂边
    kernel2 = cv2.getStructuringElement(cv2.MORPH_RECT, (2,2))
    close1 = cv2.erode(thresh,kernel2, iterations = 1)
    #膨胀修复
    kernel3 = cv2.getStructuringElement(cv2.MORPH_RECT, (5, 5))
    gray_1 = cv2.dilate(close1, None, iterations = 1)


    # 每一列的白色数量
    white = []
    # 每一列的黑色数量
    black = []
    # 区域高度取决于图片高
    height = gray_1.shape[0]
    # 区域宽度取决于图片宽
    width = gray_1.shape[1]
    # 最大白色数量
    white_max = 0
    # 最大黑色数量
    black_max = 0
    # 计算每一列的黑白色像素总和
    for i in range(width):
        s = 0  # 这一列白色总数
        t = 0  # 这一列黑色总数
        for j in range(height):
            if gray_1[j][i] == 255:
                 s += 1
            if gray_1[j][i] == 0:
                t += 1
        white_max = max(white_max, s)
        black_max = max(black_max, t)
        white.append(s)
        black.append(t)
        
    def find_end(start):
        end = start + 1
        for m in range(start + 1, width - 1):
            # 基本全黑的列视为边界
            if black[m] >= black_max * 0.98:  # 阈值参数可以根据图片的处理具体修改
                end = m
                break
        return end

    # 临时变量
    n = 1
    # 起始位置
    start = 1
    # 结束位置
    end = 2
    # 分割结果数量
    num=0
    # 分割结果
    res = []
    # 保存分割结果路径,以图片名命名
    output_path= output_dir + car.split('.')[0]
    if not os.path.exists(output_path):
        os.makedirs(output_path)
        # 从左边网右边遍历
    while n < width - 2:
        n += 1

        # 找到白色即为确定起始地址
        # 不可以直接 white[n] > white_max
        if white[n] > 0.02 * white_max:
            start = n
            # 找到结束坐标
            end = find_end(start)
            # 下一个的起始地址
            n = end

            # 确保找到的是符合要求的,过小不是车牌号
            if end - start > 10:
                # 分割
                char = gray_1[1:height, start - 5:end + 5]
                # 保存分割结果到文件
                cv2.imwrite(output_path+'/' + str(num) + '.jpg',char)
                num+=1
                # 重新绘制大小
                char = cv2.resize(char, (300, 300), interpolation=cv2.INTER_CUBIC)
                # 添加到结果集合
                res.append(char)
                
    res2 = (res[:2], res[2:4], res[4:6], res[6:])
    # 显示结果
    imgStack = stackImages(0.5, res2)
    cv2.imshow("imgStack", imgStack)
    cv2.waitKey(0)

3.图片预处理

    #灰度转化
    gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
    #二值化将除了白色的值全部变为黑色
    ret,thresh = cv2.threshold(gray, 254, 255, cv2.THRESH_BINARY )
    #腐蚀杂边
    kernel2 = cv2.getStructuringElement(cv2.MORPH_RECT, (2,2))
    close1 = cv2.erode(thresh,kernel2, iterations = 1)
    #膨胀修复
    kernel3 = cv2.getStructuringElement(cv2.MORPH_RECT, (5, 5))
    gray_1 = cv2.dilate(close1, None, iterations = 1)

4.分割效果
原图:
在这里插入图片描述

分割:
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值