Python opencv识别油表数字

图片:
在这里插入图片描述
代码:

# @time: 2022/2/14 10:29
# @Author: wangshubo
# @File: Ocr_digital_dect.py
# @description: 油表数字检测
# @author_email: '971490321@qq.com'
import cv2
import cv2 as cv
import numpy as np
from utilsW.utils import cvShow, ContrastChange


if __name__ == '__main__':
    #1, load image
    src = cv.imread("D:/images/numDect.jpg")
    cvShow("src", src)

    #2, 图像对比度增强
    ContrastImg = ContrastChange(src)
    cvShow("ContrastImg", ContrastImg)

    #3,灰度化,高斯模糊
    grayImg = cv.cvtColor(ContrastImg, cv.COLOR_BGR2GRAY)
    cvShow("grayImg", grayImg)
    blurImg = cv.GaussianBlur(grayImg, (5,5), 1)
    cvShow("blurImg", blurImg)

    #4, 阈值处理
    thresholdImg = cv.threshold(blurImg, 0, 255, cv.THRESH_BINARY_INV | cv.THRESH_OTSU)[1]
    cvShow("thresholdImg", thresholdImg)

    #5,找轮廓
    contours = cv.findContours(thresholdImg, cv.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)[0]
    drawImg = src.copy()
    cv.drawContours(drawImg, contours, -1, (0,0,255), 2)
    cvShow("drawImg", drawImg)
    words = []
    for c in contours:
        rect = list(cv.boundingRect(c))
        words.append(rect)

    #6, 按照每一个矩形框x取值进行排序
    contours = sorted(words, key=lambda s:s[0])[:5]
    for item in contours:
        x = item[0]
        y = item[1]
        w = item[2]
        h = item[3]
        sigleimg = src[y: y+h, x:x+w]
        cvShow("sigleimg", sigleimg)

函数cvShow

def cvShow(name, img):
    cv.imshow(name, img)
    cv.waitKey(0)
    # cv.destroyAllWindows()

函数ContrastChange

# 图像对比度增强
def ContrastChange(image):
    alpha = np.array([1.80])
    img = cv.multiply(image, alpha, image)
    return img
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值