0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

OpenCVの描画機能でアニメーション

Last updated at Posted at 2024-01-27

OpenCVで惑星・衛星軌道風アニメーションを作ってみた。

OpenCVでカメラ画像をリアルタイムで表示する記事はよく見かけますが、高速で描画できればアニメーションが作れるはずなので、計算だけで作れるアニメーションを作ってみました。

ご自由に改造してお使いくださいませ。

cmov
#!/usr/bin/python3

import cv2
import time  as tm
import numpy as np

wname="circle0"
# キャンバスの大きさとキャンバスの生成
cvx = 600
cvy = 600
canv = np.zeros((cvy, cvx, 3), dtype=np.uint8)
# 背景色
bkcolor = (64,0,64)
# 前景色1(惑星色)
fg1color = (128,255,128)
# 前景色2(衛星色)
fg2color = (64,128,128)

# 1000 × PI
pi=6282
# 惑星軌道半径 pix
rad1 = 200
# 衛星軌道半径 pix
rad2 = 40
# 角度
the = 0
# 角度ステップ
stp = 5
# 描画間隔(s)
wt=0.01

#  左下を原点
def coord(x,y):
    rx = x
    ry = cvy - y;
    return (rx,ry)

#  衛星の位置
def satpos(n, x, y):
    global the
    sx = int(rad2*np.cos(n*the/1000))+x
    sy = int(rad2*np.sin(n*the/1000))+y
    return coord(sx,sy)

while True:
    cx,cy=(int(cvx/2),int(cvy/2))
    px = int(cx + rad1 * np.cos(the/1000))
    py = int(cy + rad1 * np.sin(the/1000))
    the += stp
    if the > pi:
        the %= pi
    canv[:cvy,:cvx,0:3] = bkcolor
    cv2.circle(canv,coord(px,py),10,fg1color,thickness=3)
    cv2.circle(canv,satpos(20,px,py),4,fg2color,thickness=3)
    #                      ^^---衛星の回転数
    cv2.imshow(wname,canv)
    if cv2.waitKey(int(wt*1000) & 0xff) == ord('q'):
        break;

cv2.destroyWindow(wname)

書き換え箇所解説動画

0
1
5

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
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?