1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

がちもとさんAdvent Calendar 2022

Day 14

看板にテキスト描くやーつ(Python3.9、OpenCV)

Last updated at Posted at 2022-12-13

はじめに

看板を射影変換して、平面にして、そこにテキスト描いて、戻す

開発環境

  • Windows 10 PC
  • Python 3.9

実行

main.py
import cv2
import numpy as np
import math

ratio = 1.1
filename = "AdobeStock_344789310.jpeg" 
 
p1 = np.array([1777, 532])
p2 = np.array([3497, 1179])
p3 = np.array([1763, 1916])
p4 = np.array([3539, 2447])

image = cv2.imread(filename)
height, width = image.shape[:2]

width2 = np.linalg.norm(p2 - p1)
width2 = math.floor(width2 * ratio)

height2 = np.linalg.norm(p3 - p1)
height2 = math.floor(height2)
 
src = np.float32([p1, p2, p3, p4])
dst = np.float32([[0, 0],[width2, 0],[0, height2],[width2, height2]])
M = cv2.getPerspectiveTransform(src, dst)
 
result = cv2.warpPerspective(image, M,(width2, height2))
cv2.putText(result,text='gachi',org=(500, 500),fontFace=cv2.FONT_HERSHEY_SIMPLEX,fontScale=10.0,color=(0, 255, 0),thickness=10,lineType=cv2.LINE_4)
# cv2.rectangle(result,(0,0),(o_width,o_height),(0,255,0),10,cv2.LINE_8)
cv2.imwrite("warped.jpg", result)

dst = np.float32([[0, 0],[width2, 0],[0, height2],[width2, height2]])
src = np.float32([p1, p2, p3, p4])
M = cv2.getPerspectiveTransform(dst, src)

image2 = cv2.warpPerspective(result, M,(width, height))
pts = np.array((p1,p2,p4,p3))
cv2.fillPoly(image, [pts], (0,0,0), lineType=cv2.LINE_8)
result = image + image2

cv2.imwrite("result.jpg", result)
origin result warped
AdobeStock_344789310.jpeg result.jpg warped.jpg
1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?