Try projective transformation of images using OpenCV with Python

Introduction

Object extraction in image by pattern matching using OpenCV with Python

When I looked into OpenCV when creating the above article, it seems that I can really do various things, so I decided to try what I came up with for the time being.

So, the procedure of "projection conversion" that corrects the image of a business card taken with a camera as if it was taken from the front is summarized below.

Operating environment

item Contents
Machine MacBook Air (13-inch, Early 2015)
Processor 2.2 GHz Intel Core i7
memory 8 GB 1600 MHz DDR3
Python 3.6.0 :: Anaconda 4.3.1 (x86_64)
Jupyter Notebook 4.2.1
OpenCV 3.3.0-rc

Environment construction procedure

Please refer to the following URL for the usual front miso.

-Procedure to quickly create a deep learning environment on Mac with TensorFlow and OpenCV

-Procedure to quickly create a machine learning environment on Ubuntu 16.04

Photo used

Use the back side of your business card taken with the iPhone camera. (IMG_4778.JPG)

IMG_4778.jpg

Library read

python


import cv2
import numpy as np

from IPython.display import display, Image

def display_cv_image(image, format='.png'):
    decoded_bytes = cv2.imencode(format, image)[1].tobytes()
    display(Image(data=decoded_bytes))

Image reading

python


img = cv2.imread("IMG_4778.JPG")
display_cv_image(img)

Color binarization and contour extraction

python


#Grayscale
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

#Binarization
ret,th1 = cv2.threshold(gray,200,255,cv2.THRESH_BINARY)
display_cv_image(th1)

The result of binarization is as follows.

Unknown-1.png

python


#Contour extraction
image, contours, hierarchy = cv2.findContours(th1, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)

#Sort only those with a large area
areas = []
for cnt in contours:
    area = cv2.contourArea(cnt)
    if area > 10000:
        epsilon = 0.1*cv2.arcLength(cnt,True)
        approx = cv2.approxPolyDP(cnt,epsilon,True)
        areas.append(approx)

cv2.drawContours(img,areas,-1,(0,255,0),3)
display_cv_image(img)

The outline was extracted correctly and I was able to surround it with a red frame.

Unknown-2.png

Projection transformation

Projection transformation is performed for each point in the frame according to the corresponding coordinates.

python


img = cv2.imread("IMG_4778.JPG")

dst = []

pts1 = np.float32(areas[0])
pts2 = np.float32([[600,300],[600,0],[0,0],[0,300]])

M = cv2.getPerspectiveTransform(pts1,pts2)
dst = cv2.warpPerspective(img,M,(600,300))

display_cv_image(dst)

The converted result is as follows.

Unknown.png

did it!

bonus

I tried to ORC the recognized business card characters using a library called tesseract-ocr.

tesseract-oct

Add the following code after the above source code.

python


import pyocr
from PIL import Image
tools = pyocr.get_available_tools()
tool = tools[0]
print(tool.image_to_string(Image.fromarray(dst), lang="jpn"))

Result is...

[Accusative case]
ヽ/Dimension Tweer Opening Technology Appetizer
Coastal news ‡
Type 3 Denki Chief Brancher
Otoshiki Sagi 4 Condyle Temari Temari Handling Clothes
Karate first stage

 

Nisuta

... there is room for improvement (sweat)

Recommended Posts

Try projective transformation of images using OpenCV with Python
[Python] Using OpenCV with Python (Image transformation)
[Python] Using OpenCV with Python (Basic)
Using OpenCV with Python @Mac
I tried "morphology conversion" of images with Python + OpenCV
[Python] Using OpenCV with Python (Image Filtering)
[Python] Using OpenCV with Python (Edge Detection)
Try face recognition with python + OpenCV
Load gif images with Python + OpenCV
Basic study of OpenCV with Python
Try mathematical formulas using Σ with python
Capturing images with Pupil, python and OpenCV
Try using Python with Google Cloud Functions
[python, openCV] base64 Face recognition with images
Aligning scanned images of animated video paper using OpenCV and Python
[Python] Read images with OpenCV (for beginners)
Object extraction in images by pattern matching using OpenCV with Python
[Python] Try to recognize characters from images with OpenCV and pyocr
Get and estimate the shape of the head using Dlib and OpenCV with python
Try using the collections module (ChainMap) of python3
Anonymous upload of images using Imgur API (using Python)
Wavelet transform of images with PyWavelets and OpenCV
Try to detect fish with python + OpenCV2.4 (unfinished)
Try scraping with Python.
Try using Tweepy [Python2.7]
[OpenCV / Python] I tried image analysis of cells with OpenCV
[Windows] [Python] Camera calibration of fisheye lens with OpenCV
Bordering images with python Part 1
Try using PythonTex with Texpad.
"Apple processing" with OpenCV3 + Python3
[S3] CRUD with S3 using Python [Python]
[Python] Try using Tkinter's canvas
Try edge detection with OpenCV
Using Quaternion with Python ~ numpy-quaternion ~
Image editing with python OpenCV
Try using matplotlib with PyCharm
Camera capture with Python + OpenCV
Try Python output with Haxe 3.2
Try using Kubernetes Client -Python-
Try using OpenCV on Windows
python: Basics of using scikit-learn ①
Face detection with Python + OpenCV
Try running Python with Try Jupyter
Faster loading of Python images
Try face recognition with Python
Try OpenCV with Google Colaboratory
Try using folium with anaconda
Send using Python with Gmail
Try scraping the data of COVID-19 in Tokyo with Python
I tried "gamma correction" of the image with Python + OpenCV
Getting Started with python3 # 3 Try Advanced Computations Using Import Statements
Cloud Functions to resize images using OpenCV with Cloud Storage triggers
The story of displaying images with OpenCV or PIL (only)
Try to automate the operation of network devices with Python
Estimate the attitude of AR markers with Python + OpenCV + drone
Try encryption / decryption using OpenSSL key with Python3 pow function
[Python] Easy reading of serial number image files with OpenCV
Complement python with emacs using company-jedi
Number recognition in images with Python
Environment construction of python and opencv
Harmonic mean with Python Harmonic mean (using SciPy)