Python Opencv實戰(zhàn)之印章提取的實現(xiàn)
前言
這期分享的是使用opencv提取印章,很多時候我們需要電子版的章,所以今天就帶大家使用代碼提取出來!
Photoshop雖然強大,但是奈何小編不會使啊,昨天就有一個小伙伴問我能不能幫忙,這不?
PS雖然我不會,但是我會寫代碼呀!這可難不倒我!安排安排~
(特別提醒:所有愛好設計和喜歡做圖的小伙伴們,切記千萬不要幫著老板或者朋友PS偽造公章,刑法第280條特別指出,偽造證件印章,是可以追究刑事責任的,違法的事情不要做哦。)

源碼展示
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):
"""
統(tǒng)一圖片的大小
: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) # 處理圖像色調(diào)
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):
"""
邊緣檢測,截取并輸出結(jié)果
: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()
效果展示
原圖

效果圖

到此這篇關于Python Opencv實戰(zhàn)之印章提取的實現(xiàn)的文章就介紹到這了,更多相關Python Opencv印章提取內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
在PyCharm中打包Python項目并將其運行到服務器上的方法(推薦)
在PyCharm中打包Python項目并運行到服務器上,主要步驟包括:創(chuàng)建并設置項目、編寫項目代碼、打包項目、配置服務器環(huán)境、上傳可執(zhí)行文件到服務器以及運行項目,通過這些步驟,可以將Python項目打包并部署到服務器上2024-11-11
python人工智能tensorflow構(gòu)建循環(huán)神經(jīng)網(wǎng)絡RNN
這篇文章主要為大家介紹了python人工智能tensorflow構(gòu)建循環(huán)神經(jīng)網(wǎng)絡RNN,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2022-05-05
PyCharm專業(yè)最新版2019.1安裝步驟(含激活碼)
這篇文章主要介紹了PyCharm專業(yè)最新版2019.1安裝步驟(含激活碼),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2019-10-10
python一行代碼就能實現(xiàn)數(shù)據(jù)分析的pandas-profiling庫
這篇文章主要為大家介紹了python一行代碼就能實現(xiàn)數(shù)據(jù)分析的pandas-profiling庫,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2024-01-01

