2
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 15

サッカーボールの位置を上から見てみーる(Python3.9、OpenCV)

Last updated at Posted at 2022-12-14

はじめに

サッカーボールの位置を上から見てみよう

開発環境

  • Windows 10
  • Python 3.9

実行

main.py
import cv2
import numpy as np
import math

ratio = 1.0
filename = "soccer.jpeg" 
 
p1 = np.array([1280, 1157])
p2 = np.array([4392, 1157])
p3 = np.array([602, 1896])
p4 = np.array([5070, 1896])
p5 = np.array([[[2047,1633]]], dtype='float32')
print(p5)
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)
print(M)

result = cv2.warpPerspective(image, M,(width2, height2))
p6 = cv2.perspectiveTransform(p5, M)
print(p6)
cv2.putText(result,text='gachi',org=(200, 500),fontFace=cv2.FONT_HERSHEY_SIMPLEX,fontScale=10.0,color=(0, 0, 255),thickness=10,lineType=cv2.LINE_8)
cv2.circle(result,(int(p6[0][0][0]), int(p6[0][0][1])),10,(255,0,0),-1,cv2.LINE_8)
# cv2.rectangle(result,(0,0),(o_width,o_height),(0,255,0),10,cv2.LINE_8)
cv2.imwrite("soccer_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("soccer_result.jpg", result)
origin result warped
soccer.jpeg soccer_result.jpg soccer_warped.jpg

競技場の斜めから見たボールの位置は(2047, 1633)、上から見ると(939.91205, 723.5516)の位置にある

参考文献

2
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
2
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?