当前位置:网站首页>[opencv practice] this seal "artifact" is awesome, saving time and improving efficiency. It is powerful ~ (complete source code attached)
[opencv practice] this seal "artifact" is awesome, saving time and improving efficiency. It is powerful ~ (complete source code attached)
2022-06-10 23:54:00 【Programmer pear】
Preface
author :“ Programmer pear ”
** The article brief introduction **: This article is mainly about opencv Small program for extracting seal text !
** The source code of the article is available for free : In order to thank everyone who pays attention to me, the project source code of each article is none
Compensation sharing drop
Welcome friends give the thumbs-up 、 Collection 、 Leaving a message.
Text

This issue is about using opencv Extract seal , Many times we need electronic version of the chapter , So let's use the code today
extracted !
Photoshop Although powerful , But how can Xiaobian not make , Yesterday, a little friend asked me if I could help , see ?PS though
But I won't , But I can write code ! It's hard not to beat me ! Arrange... Arrange ~
( Special reminder : All the friends who like designing and drawing , Remember never to help your boss or friends PS Fake public
Chapter , Criminal Law No 280 This article specifically states that , Forged certificate seal , It can be investigated for criminal responsibility , Don't do anything illegal .)
Tutorial delivery , The source code is in the blue font at the end of the text or above. Take it for free —— Come on !

1) Effect display
Original picture ——

design sketch ——

2) Source code display
import cv2
import numpy as np
class Seal:
def __init__(self, img_path):
"""
Initialize picture
:param img_path: Original image 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):
"""
Uniform picture size
:return: Return an unprocessed target image """
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) # Process image hue
low_range = np.array([130, 43, 46]) # Set the lower boundary
high_range = np.array([180, 255, 255]) # Set the upper boundary
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):
"""
edge detection , Intercept and output the results
:return:
"""
imgreal = self.img_enhance()
# Expand the picture to prevent interception
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)]
# Height is less than width
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()summary
The effect is very good , Interested friends can try it ~
Follow Xiaobian for more wonderful content ! Remember to click on the portal
Remember Sanlian ! If you need packaged source code + Free sharing of materials !! Portal

边栏推荐
- Basic introduction and core components of kubernetes
- Data and information resource sharing platform (VII)
- LabVIEW使用MathScript Node或MATLAB脚本时出现错误1046
- LeetCode+ 16 - 20
- LabVIEW获取Clamp函数找到的所有点的信息
- Perfect decoding purecodec 20220601
- Is it safe to open an account online in Shanghai?
- Excel essential toolbox 17.0 Free Edition
- 自制APP连接OneNET---实现数据监控和下发控制(MQTT)
- LeetCode 501 :二叉搜索树中的众数
猜你喜欢

黑马头条丨腾讯薪酬制度改革引争议;英特尔全国扩招女工程师;黑马100%就业真的吗......

PHP实现iframe跨站替换文字/替换iframe网站文字的方法

Fiddler simulates low-speed network environment

Solve access denied for user 'root' @ 'localhost' (using password: yes)

It is known that the transverse grain pressure resistance of a certain wood obeys n (x, D2). Now ten specimens are tested for transverse grain pressure resistance, and the data are as follows: (unit:

LabVIEW 禁止其他可多核心处理的应用程序在所有核心上执行

How to measure the refresh rate of oscilloscope

Basic introduction and core components of kubernetes

LabVIEW执行串行回送测试

Self made app connected to onenet --- realize data monitoring and distribution control (mqtt)
随机推荐
How to measure the refresh rate of oscilloscope
LeetCode 501 :二叉搜索樹中的眾數
LabVIEW prohibits other multi-core processing applications from executing on all cores
启牛的证券账户是真的吗?安全的吗?
The time (in minutes) required for a group of workers to cooperate to complete the assembly process of a part are as follows:
LabVIEW中NI MAX中缺少串口
Self made app connected to onenet --- realize data monitoring and distribution control (mqtt)
宝塔计划任务Shell脚本定时删除某各目录下所有文件【记录】
Is it safe for changtou school to open an account? Is it reliable?
【Opencv实战】这个印章“神器”够牛,节省了时间提高了效率,厉害~(附完整源码)
Prefer "big and small weeks", only write 200 lines of code every day, and the monthly salary of 8k-17k people will rise again
LabVIEW执行串行回送测试
LabVIEW获取Clamp函数找到的所有点的信息
Difference between oscilloscope and spectrum analyzer
VS 番茄助手添加头注释 以及使用方式
示波器和频谱分析仪的区别
VS的常用设置
Perfect decoding purecodec 20220601
IGBT与三代半导体SiC双脉冲测试方案
Mathematics and quality education
