识别图片中是否存在印章并提取出来

该代码实现了一个用于检测和提取红色印章的Python脚本,主要利用OpenCV库进行图像处理。通过将图像转换为HSV色彩空间并设置阈值来判断是否存在红色印章,如果存在则返回1,否则返回0。此外,脚本还提供了提取印章并保存为新图片的功能。
摘要由CSDN通过智能技术生成
#!/usr/bin/env python
# coding=utf-8

"""
@author: shenzh
@file: seal_pick.py
@date: 2022-4-25 9:05
@desc: 图片中的红色印章的提取识别
"""

import cv2
import numpy as np


##判别图片中是否存在红色印章(只能判别红色印章)
def ckeck_seal_exit(image):
    img = cv2.imread(image)
    img_hsv = cv2.cvtColor(img, cv2.COLOR_RGB2HSV)

    lower_blue = np.array([100, 30, 100])
    upper_blue = np.array([150, 255, 255])

    mask = cv2.inRange(img_hsv, lower_blue, upper_blue)

    res = cv2.bitwise_and(img, img, mask=mask)
    r, g, b = cv2.split(res)
    r_num = 0
    for i in b:
        for j in i:
            if j > 170:
                r_num += 1

    if r_num > 30:
        # print("该图片有红章")
        seal_result = 1  ##该图片有红章
    else:
        # print("该图片没有红章")
        seal_result = 0  ##该图片没有红章
    return seal_result


##红章的提取出来生成图片(只能提取出黑白颜色底的红色印章)
def pick_seal_image(image, image_out):
    np.set_printoptions(threshold=np.inf)
    image = cv2.imread(image)

    hue_image = cv2.cvtColor(image, cv2.COLOR_BGR2HSV)
    low_range = np.array([150, 103, 100])
    high_range = np.array([180, 255, 255])
    th = cv2.inRange(hue_image, low_range, high_range)
    index1 = th == 255

    img = np.zeros(image.shape, np.uint8)
    img[:, :] = (255, 255, 255)
    img[index1] = image[index1]  # (0,0,255)

    cv2.imwrite(image_out, img)


if __name__ == '__main__':
    image = 'G:/codes/file_light_code/files/seal.jpg'
    seal_result = ckeck_seal_exit(image)
    print(seal_result)
    image_out = 'G:/codes/file_light_code/files/pick_image.jpg'
    pick_seal_image(image, image_out)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值