小噪音情况下的验证码提取(python-CV)

       因朋友请求,帮忙写了个CV的验证码提取,由于他的验证码中间噪音不是很高,所以各种滤波的降噪在代码中没有,所以想看降噪的朋友可能要失望了,话不多说,附上源码


import cv2
import numpy as np

height = 180
width = 600
threshold = 130  # 阈值


def threshold_demo(image):
    for i in range(height):
        for j in range(width):
            if image[i][j] > threshold:
                image[i][j] = 255
            else:
                image[i][j] = 0
    return image


def split_picture(imagepath):
    img = cv2.imread(imagepath)
    img = cv2.resize(img, (width, height))
    gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
    # 将图片的边缘变为白色
    # for i in range(width):
    #     gray[0, i] = 255
    #     gray[height-1, i] = 255
    # for j in range(height):
    #     gray[j, 0] = 255
    #     gray[j, width-1] = 255

    thresh1 = threshold_demo(gray)
    contours, hierarchy = cv2.findContours(thresh1, 2, 2)
    for i in range(len(contours)):
        cnt = contours[i]
        # 寻找每个轮廓的最小外接矩形
        x, y, w, h = cv2.boundingRect(cnt)
        area = cv2.contourArea(cnt)
        if w > 0.8*width or h > 0.8 * height:
            continue
        if w < width*0.083 or h < height*0.25:
            continue
        print((x, y, w, h))
        cv2.rectangle(img, (x, y), (x+w, y+h), (255, 0, 0), 2)
        cv2.drawContours(img, contours, i, (0, 255, 0), 2)

    cv2.imshow('img', img)
    cv2.imshow('gray', gray)
    cv2.imshow('thresh1', thresh1)
    cv2.waitKey(0)


if __name__ == '__main__':
    imagepath = '你的图片路径'
    split_picture(imagepath)

原图:

下面是测试效果:

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值