数字图像与机器视觉基础补充(2)

图片预处理

读取图片

# 车牌路径
file_path="./car/"
# 读取所有车牌
cars = os.listdir(file_path)
cars.sort()

src = cv2.imread(file_path+car)
img = src.copy()

在这里插入图片描述

去除螺丝

用蓝色覆盖,二值化时可以直接去除

cv2.circle(img, (145, 20), 10, (255, 0, 0), thickness=-1)
cv2.circle(img, (430, 20), 10, (255, 0, 0), thickness=-1)
cv2.circle(img, (145, 170), 10, (255, 0, 0), thickness=-1)
cv2.circle(img, (430, 170), 10, (255, 0, 0), thickness=-1)
cv2.circle(img, (180, 90), 10, (255, 0, 0), thickness=-1)

在这里插入图片描述

转灰度

gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

在这里插入图片描述

二值化

# 二值化
adaptive_thresh = cv2.adaptiveThreshold(gray, 255, cv2.ADAPTIVE_THRESH_MEAN_C, cv2.THRESH_BINARY_INV, 333, 1)

在这里插入图片描述

闭运算

去除螺丝的痕迹

kernel = np.ones((5, 5), int)
morphologyEx = cv2.morphologyEx(adaptive_thresh, cv2.MORPH_CLOSE, kernel)

在这里插入图片描述

字符边界

contours, hierarchy = cv2.findContours(morphologyEx, cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE)

绘制边界

img_1 = img.copy()
cv2.drawContours(img_1, contours, -1, (0, 0, 0), -1)

在这里插入图片描述

预处理结果

在这里插入图片描述

切割字符

预处理图转灰度

 gray_1 = cv2.cvtColor(img_1, cv2.COLOR_BGR2GRAY)

计算每一列的黑色和白色数量以及最大值

	# 每一列的白色数量
    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.95:  # 0.95这个参数请多调整,对应下面的0.05
                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.05 * 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)

结果

在这里插入图片描述

结果

在这里插入图片描述
其它车牌亦是差不多的

参考

https://blog.csdn.net/m0_38024433/article/details/78650024

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Ivan@Xiang

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值