I tried to digitize the stamp stamped on paper using OpenCV

Motive There is a place to stamp your seal on an Excel sheet or word. If it is a little important document, I sometimes want to use the same seal certificate registered in the city hall, but before, I printed the paper at a convenience store once, stamped it, scanned it again and submitted it as pdf. was doing.

I was doing a lot of analog processing, but I thought that if I could image the seal and paste it on the sheet, I wouldn't have to go to a convenience store.

At first I took a picture of the seal and tried to make the background color transparent using PowerPoint or a free image editing tool, but I gave up because I could not convert everything depending on the paper quality.

So, after trial and error, I wrote a few lines of image processing with OpenCV and solved the problem, so I will leave it as a memorandum.

Dataset I used the seal stamp sample from Akusea. ieyasu.jpg

Method

  1. Binarization + image inversion
  2. Masking with original image and 1 image
  3. Transparent background image (α value 100%)

Development

import cv2
import sys
import numpy as np

if __name__ == '__main__':

	if len(sys.argv) != 2:
		sys.exit()

	window_name = "sample"
	img_path = sys.argv[1]

	mat = cv2.imread(img_path)
	gray = cv2.cvtColor(mat, cv2.COLOR_BGR2GRAY)
	cv2.imwrite("gray.png ", gray)

	#Binarization
	ret2, mono = cv2.threshold(gray, 0, 255, cv2.THRESH_OTSU + cv2.THRESH_BINARY_INV) 
	cv2.imwrite("mono.png ", mono)

	#Masking process
	dst = cv2.bitwise_and(mat,mat,mask=mono)
	cv2.imwrite("dst.png ", dst)

	#channel 3->Change to 4
	alpha = cv2.cvtColor(dst, cv2.COLOR_BGR2BGRA)

	#Only the black background is transparent
	for i,col in enumerate(dst):
		for j,row in enumerate(col):
			color = dst[i][j]
			if (color == np.array([0,0,0])).all():
				alpha[i][j][3] = 0

	cv2.imshow(window_name, alpha)
	cv2.imwrite("out.png ", alpha)
	cv2.waitKey(0)
	cv2.destroyWindow(window_name)

This time, the image data processed halfway is also output.

Result

Processing flow output
raw data ieyasu.jpg
Black and white gray.png
Binarization+Invert mono.png
masking dst.png
Background color transparent
(Processing Exit)
out.png

Future Background transparency can be easily created by using cv2.bitwise_and. If it seems that it will take time to use the tool, this one seems to be better.

Reference -Image processing with python + Opencv 5 (image combination and mask) -Alpha blending and masking of images with Python, OpenCV, NumPy -Akusea --Original data set used for seal

Recommended Posts

I tried to digitize the stamp stamped on paper using OpenCV
I tried using the image filter of OpenCV
I tried to approximate the sin function using chainer
I tried to become an Ann Man using OpenCV
I tried to identify the language using CNN + Melspectogram
I tried to notify the honeypot report on LINE
I tried to complement the knowledge graph using OpenKE
I tried to compress the image using machine learning
I tried to simulate ad optimization using the bandit algorithm.
I tried using "Syncthing" to synchronize files on multiple PCs
[TF] I tried to visualize the learning result using Tensorboard
I tried changing the python script from 2.7.11 to 3.6.0 on windows10
I tried to launch ipython cluster to the minimum on AWS
I tried to approximate the sin function using chainer (re-challenge)
I tried to output the access log to the server using Node.js
I tried using GrabCut of OpenCV
I tried to move the ball
I tried using the checkio API
I tried to estimate the interval.
I tried to get the index of the list using the enumerate function
I tried to process the image in "sketch style" with OpenCV
I tried using the COTOHA API (there is code on GitHub)
I tried to display the video playback time (OpenCV: Python version)
I tried to register a station on the IoT platform "Rimotte"
I tried to get started with Bitcoin Systre on the weekend
I tried to visualize BigQuery data using Jupyter Lab on GCP
I tried to process the image in "pencil style" with OpenCV
I tried using Azure Speech to Text.
I tried to summarize the umask command
I tried to recognize the wake word
I tried to classify text using TensorFlow
I tried to summarize the graphical modeling.
I tried to estimate the pi stochastically
I tried to touch the COTOHA API
[Python] I tried to make a simple program that works on the command line using argparse.
I tried using the BigQuery Storage API
I tried to predict Covid-19 using Darts
I tried to transform the face image using sparse_image_warp of TensorFlow Addons
[Python] I tried to visualize the night on the Galactic Railroad with WordCloud!
I tried to get the batting results of Hachinai using image processing
I tried using jpholidayp over proxy to execute cron only on weekdays
I tried to estimate the similarity of the question intent using gensim's Doc2Vec
I want to display an image on Jupyter Notebook using OpenCV (mac)
I tried to control multiple servo motors MG996R using the servo driver PCA9685.
Matching karaoke keys ~ I tried to put it on Laravel ~ <on the way>
I tried to summarize various sentences using the automatic summarization API "summpy"
I tried to extract and illustrate the stage of the story using COTOHA
I tried with the top 100 PyPI packages> I tried to graph the packages installed on Python
I tried the common story of using Deep Learning to predict the Nikkei 225
Using COTOHA, I tried to follow the emotional course of Run, Melos!
I tried to analyze the New Year's card by myself using python
I tried "smoothing" the image with Python + OpenCV
I tried web scraping to analyze the lyrics.
I tried using scrapy for the first time
I tried using Remote API on GAE / J
vprof --I tried using the profiler for Python
I tried "differentiating" the image with Python + OpenCV
I tried to optimize while drying the laundry
I tried object detection using Python and OpenCV
I tried to save the data with discord
I tried to detect motion quickly with OpenCV