#!/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)
识别图片中是否存在印章并提取出来
于 2022-04-25 16:43:29 首次发布
该代码实现了一个用于检测和提取红色印章的Python脚本,主要利用OpenCV库进行图像处理。通过将图像转换为HSV色彩空间并设置阈值来判断是否存在红色印章,如果存在则返回1,否则返回0。此外,脚本还提供了提取印章并保存为新图片的功能。
1万+

被折叠的 条评论
为什么被折叠?



