Write letters in the card illustration with OpenCV python

OpenCV installation

sudo pip install opencv-python

Prepare an image

(Example) 1. Card illustration blueeyes2.jpg Source: [[All of Blue-Eyes White Dragon] The locus of the legendary dragon with the title of Yu-Gi-Oh!](Https://yu-gi-oh.xyz/ Blue-Eyes White Dragon / post- 49725 /)

blueeyes2.jpg

(Example) 2. Image including card illustration blueeyes1.jpg Source: Adere

blueeyes1.jpg

Write a string in the upper left of the image

Use OpenCV putTxt

When displaying in the upper left

sample.py


import PIL
from PIL import Image
from PIL import ExifTags
import cv2 as cv
import numpy as np

def write_text(image_path ,x ,y):
    str = "ATK:3000, DEF:2500"
    str_height = 25
    str_width = 340

    img = cv.imread(image_path)

    #Add a black background
    cv.rectangle(img, (x, y), (x+str_width, y-str_height), (0, 0, 0), cv.FILLED, cv.LINE_AA)

    #Add a line
    cv.rectangle(img, (x, y), (x+str_width, y-str_height), (0, 0, 0), 2, cv.LINE_AA)

    #Add letters
    cv.putText(img, str, (x, y),
               cv.FONT_HERSHEY_PLAIN, 2.0,
               (255, 255, 255), 2, cv.LINE_AA)

    cv.imwrite(image_path + '.new.jpg', img)

if __name__ == '__main__':
    write_text('blueeyes1.jpg', 10, 30)

Run

python sample.py

blueeyes1.jpg.new.jpg

When combining pattern matching

Combine with the code below

Find card illustrations from images using feature point matching

sample.py


import cv2
import numpy as np

fname_img1='blueeyes1.jpg'
fname_img2='blueeyes2.jpg'

img1 = cv2.imread(fname_img1)
img2 = cv2.imread(fname_img2)

def write_text(img ,x ,y):
    str = "ATK:3000, DEF:2500"
    str_height = 25
    str_width = 340

    #Add a black background
    cv2.rectangle(img, (x, y), (x+str_width, y-str_height), (0, 0, 0), cv2.FILLED, cv2.LINE_AA)

    #Add a line
    cv2.rectangle(img, (x, y), (x+str_width, y-str_height), (0, 0, 0), 2, cv2.LINE_AA)

    #Add letters
    cv2.putText(img, str, (x, y),
               cv2.FONT_HERSHEY_PLAIN, 2.0,
               (255, 255, 255), 2, cv2.LINE_AA)
    cv2.imwrite(fname_img1 + '.new.jpg', img)

def detect(img1, img2):
    gray1 = cv2.cvtColor(img1, cv2.COLOR_BGR2GRAY)
    gray2 = cv2.cvtColor(img2, cv2.COLOR_BGR2GRAY)
    akaze = cv2.AKAZE_create()
    kp1, des1 = akaze.detectAndCompute(gray1, None)
    kp2, des2 = akaze.detectAndCompute(gray2, None)
    
    bf = cv2.BFMatcher(cv2.NORM_HAMMING, crossCheck=True)
    matches = bf.match(des1, des2)
    matches = sorted(matches, key = lambda x:x.distance)
    img3 = cv2.drawMatches(img1, kp1, img2, kp2, matches[:10], None, flags=2)
    cv2.imwrite('out-match.png', img3)
    
    #homography
    good_match_rate = 0.15;
    good = matches[:int(len(matches) * good_match_rate)]

    min_match=10
    if len(good) > min_match:
        src_pts = np.float32([kp1[m.queryIdx].pt for m in good]).reshape(-1, 1, 2)
        dst_pts = np.float32([kp2[m.trainIdx].pt for m in good]).reshape(-1, 1, 2)
        # Find homography
        M, mask = cv2.findHomography(dst_pts, src_pts, cv2.RANSAC)
        matchesMask = mask.ravel().tolist()

        print(M)
        print(M[0][2])
        print(M[1][2])
        
        height = img2.shape[0]
        width = img2.shape[1]
        top_left = (int(M[0][2] +0.5), int(M[1][2] +0.5)); #tx,ty
        bottom_right = (top_left[0] + width, top_left[1] + height)
        
    write_text(img1 ,top_left[0] ,top_left[1])

detect(img1, img2)

Run

$ $ python sample.py
[[ 6.43984577e-01 -4.58173428e-02  1.61203033e+02]
 [ 2.85230902e-02  4.12312421e-01  1.50286636e+02]
 [ 4.54533377e-04 -5.75705548e-04  1.00000000e+00]]
161.2030333928222
150.28663624674783

out-match.png

The character string is output at the position of the card blueeyes1.jpg.new.jpg

For webcam images

yugio3.png

yugio3.png.new.jpg

It is relatively easy to see.

Task

--Make it possible to match with other cards --Get the character string from DB etc. --Display other than ATK / DEF

It may be used for Yu-Gi-Oh's remote duel.

reference

opencv.jp cv::putText I tried face recognition with OpenCV (Part2: Authentication) How to write text on an image Find card illustrations from images using feature point matching

Recommended Posts

Write letters in the card illustration with OpenCV python
Use OpenCV with Python 3 in Window
Draw an illustration with Python + OpenCV
Write documentation in Sphinx with Python Livereload
Write the test in a python docstring
Display Python 3 in the browser with MAMP
Playing card class in Python (with comparison)
I tried "smoothing" the image with Python + OpenCV
Load the network modeled with Rhinoceros in Python ③
[Automation] Extract the table in PDF with Python
I tried "binarizing" the image with Python + OpenCV
The easiest way to use OpenCV with python
The trick to write flatten concisely in python
Load the network modeled with Rhinoceros in Python ②
Load the network modeled with Rhinoceros in Python ①
Binarization with OpenCV / Python
Write Python in MySQL
Basic information Write the 2018 fall algorithm problem in Python
[Python] Get the numbers in the graph image with OCR
Crawl the URL contained in the twitter tweet with python
Get the result in dict format with Python psycopg2
Fill the background with a single color with OpenCV2 + Python
I want to write in Python! (3) Utilize the mock
Write a log-scale histogram on the x-axis in python
Python OpenCV tried to display the image in text.
[Note] How to write QR code and description in the same image with python
Write Pandoc filters in Python
Scraping with selenium in Python
"Apple processing" with OpenCV3 + Python3
Working with LibreOffice in Python
Download the file in Python
Scraping with chromedriver in python
Find the difference in Python
Write beta distribution in Python
Image editing with python OpenCV
Camera capture with Python + OpenCV
Debugging with pdb in Python
Write python in Rstudio (reticulate)
[Python] Using OpenCV with Python (Basic)
Scraping with Selenium in Python
Scraping with Tor in Python
Tweet with image in Python
Combined with permutations in Python
Make Opencv available in Python
Let's write python with cinema4d.
Face detection with Python + OpenCV
Write to csv with Python
Call the API with python3.
Using OpenCV with Python @Mac
Try scraping the data of COVID-19 in Tokyo with Python
I tried "gamma correction" of the image with Python + OpenCV
Calculate the square root of 2 in millions of digits with python
[Homology] Count the number of holes in data with Python
Estimate the attitude of AR markers with Python + OpenCV + drone
Recursively get the Excel list in a specific folder with python and write it to Excel.
Number recognition in images with Python
Shining life with Python and OpenCV
Write a binary search in Python
How is the progress? Let's get on with the boom ?? in Python
Extract the xz file with python
Getting the arXiv API in Python