当前位置:网站首页>【Opencv实战】这个印章“神器”够牛,节省了时间提高了效率,厉害~(附完整源码)
【Opencv实战】这个印章“神器”够牛,节省了时间提高了效率,厉害~(附完整源码)
2022-06-10 22:42:00 【程序员梨子】
前言
作者 :“程序员梨子”
**文章简介 **:本篇文章主要是写了opencv的提取印章文字小程序!
**文章源码免费获取 : 为了感谢每一个关注我的小可爱每篇文章的项目源码都是无
偿分享滴
点这里蓝色这行字体自取,需要什么源码记得说标题名字哈!私信我也可!
欢迎小伙伴们 点赞、收藏、留言
正文

这期分享的是使用opencv提取印章,很多时候我们需要电子版的章,所以今天就带大家使用代码
提取出来!
Photoshop虽然强大,但是奈何小编不会使啊,昨天就有一个小伙伴问我能不能帮忙,这不?PS虽
然我不会,但是我会写代码呀!这可难不倒我!安排安排~
(特别提醒:所有爱好设计和喜欢做图的小伙伴们,切记千万不要帮着老板或者朋友PS伪造公
章,刑法第280条特别指出,伪造证件印章,是可以追究刑事责任的,违法的事情不要做哦。)
教程送,源码就文末或者前文的蓝色字体自己免费拿哈——来咯!

1)效果展示
原图——

效果图——

2)源码展示
import cv2
import numpy as np
class Seal:
def __init__(self, img_path):
"""
初始化图片
:param img_path: 原始图片路径 """
self.image = cv2.imread(img_path)
self.img_shape = self.image.shape
self.file_name = img_path.split('.')[0].split('\\')[-1]
def unify_img_size(self):
"""
统一图片的大小
:return:返回一张未处理的目标图片 """
img_w = 650 if self.img_shape[1] > 600 else 400
self.image = cv2.resize(self.image, (img_w, int(img_w * self.img_shape[0] / self.img_shape[1])), interpolation=cv2.IMREAD_COLOR)
impng = cv2.cvtColor(self.image.copy(), cv2.COLOR_RGB2RGBA)
return impng
def img_binaryzation(self,hue_image, low_range, high_range, imgpng):
th = cv2.inRange(hue_image, low_range, high_range)
element = cv2.getStructuringElement(cv2.MORPH_RECT, (1, 1))
th = cv2.dilate(th, element)
index1 = th == 255
print_img = np.zeros(imgpng.shape, np.uint8)
print_img[:, :, :] = (255, 255, 255, 0)
print_img[index1] = imgpng[index1] # (0,0,255)
return print_img
def img_enhance(self):
imgpng = self.unify_img_size()
hue_image = cv2.cvtColor(self.image, cv2.COLOR_BGR2HSV) # 处理图像色调
low_range = np.array([130, 43, 46]) # 设下边界
high_range = np.array([180, 255, 255]) # 设上边界
print1 = self.img_binaryzation(hue_image, low_range, high_range, imgpng)
low_range = np.array([0, 43, 46])
high_range = np.array([9, 255, 255])
print2 = self.img_binaryzation(hue_image, low_range, high_range, imgpng)
imgreal = cv2.add(print2, print1)
white_px = np.asarray([255, 255, 255, 255])
(row, col, _) = imgreal.shape
for r in range(row):
for c in range(col):
px = imgreal[r][c]
if all(px == white_px):
imgreal[r][c] = imgpng[r][c]
return imgreal
def extension_img(self):
"""
边缘检测,截取并输出结果
:return:
"""
imgreal = self.img_enhance()
# 扩充图片防止截取部分
print4 = cv2.copyMakeBorder(imgreal, 50, 50, 50, 50, cv2.BORDER_CONSTANT, value=[255, 255, 255, 0])
print2gray = cv2.cvtColor(print4, cv2.COLOR_RGBA2GRAY)
_, grayfirst = cv2.threshold(print2gray, 254, 255, cv2.THRESH_BINARY_INV)
element = cv2.getStructuringElement(cv2.MORPH_RECT, (22, 22))
img6 = cv2.dilate(grayfirst, element)
c_canny_img = cv2.Canny(img6, 10, 10)
contours, hierarchy = cv2.findContours(c_canny_img, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)
areas = []
for i, cnt in enumerate(contours):
x, y, w, h = cv2.boundingRect(cnt)
area = w * h
ars = [area, i]
areas.append(ars)
areas = sorted(areas, reverse=True)
maxares = areas[:1]
x, y, w, h = cv2.boundingRect(contours[maxares[0][1]])
print5 = print4[y:(y + h), x:(x + w)]
# 高小于宽
if print5.shape[0] < print5.shape[1]:
zh = int((print5.shape[1] - print5.shape[0]) / 2)
print5 = cv2.copyMakeBorder(print5, zh, zh, 0, 0, cv2.BORDER_CONSTANT, value=[255, 255, 255, 0])
else:
zh = int((print5.shape[0] - print5.shape[1]) / 2)
print5 = cv2.copyMakeBorder(print5, 0, 0, zh, zh, cv2.BORDER_CONSTANT, value=[255, 255, 255, 0])
resultprint = cv2.resize(print5, (150, 150))
cv2.imwrite(r'output\{}_result.png'.format(self.file_name), resultprint)
if __name__ == '__main__':
s = Seal(r"src\2.jpg")
s.extension_img()总结
效果很不错滴,感兴趣的朋友可以试试啦~
关注小编获取更多精彩内容!记得点击传送门哈
记得三连哦! 如需打包好的源码+素材免费分享滴!!传送门

边栏推荐
- LabVIEW或MAX下的VISA测试面板中串口无法工作
- Before we learn about high-performance computing, let's take a look at its history
- Leetcode 501: mode dans l'arbre de recherche binaire
- flutter 如何去掉listview顶部空白的问题
- LabVIEW 禁止其他可多核心处理的应用程序在所有核心上执行
- PHP实现iframe跨站替换文字/替换iframe网站文字的方法
- An adaptive size / full screen display method for iframe frames
- How to remove the blank at the top of listview
- Create millisecond time id in LabVIEW
- Classic sentences in Yi Shu's works
猜你喜欢

怎么生成自动参考文献(简单 有图)
![The shell script of pagoda plan task regularly deletes all files under a directory [record]](/img/dc/cfd449bf14c4545cd2e52bda4ab31e.png)
The shell script of pagoda plan task regularly deletes all files under a directory [record]

LabVIEW获取IMAQ Get Last Event坐标

Data and information resource sharing platform (VII)

High speed data stream disk for LabVIEW

Fiddler creates an autoresponder

BGP - route map extension (explanation + configuration)

SystemVerilog(十)-用户自定义类型

Simple impedance matching circuit and formula

im即时通讯源码带教程/uniapp即时通讯源码,附安装教程
随机推荐
长投学堂开户安全吗?靠谱吗?
Common settings for vs
启牛的证券账户是真的吗?安全的吗?
Perfect decoding purecodec 20220601
Build TAR model using beersales data set in TSA package
easyrecovery15操作简单方便的数据恢复工具
Serial port missing in Ni Max in LabVIEW
ILRuntime热更框架 安装以及断点调试
Two debugging techniques in embedded software development
Why many new websites are not included by search engines
Excel essential toolbox 17.0 Free Edition
1. open the R program, and use the apply function to calculate the sum of 1 to 12 in the sequence of every 3 numbers. That is, calculate 1+4+7+10=? 2+5+8+11=?, 3+6+9+12=?
宁愿“大小周”、每天只写 200 行代码、月薪 8k-17k 人群再涨
LabVIEW displays the time and date on the waveform chart or waveform chart
How to measure the refresh rate of oscilloscope
How to remove the blank at the top of listview
给线程池里面线程添加名称的4种方式
希尔排序
LabVIEW 禁止其他可多核心处理的应用程序在所有核心上执行
[untitled]
