Skip to content

Commit

Permalink
distort video
Browse files Browse the repository at this point in the history
  • Loading branch information
misakurai committed Aug 15, 2024
1 parent c5e0de7 commit f71f6c0
Show file tree
Hide file tree
Showing 3 changed files with 112 additions and 54 deletions.
28 changes: 10 additions & 18 deletions src/testImage1.py
Original file line number Diff line number Diff line change
@@ -1,29 +1,21 @@

import cv2
import numpy as np
# from matplotlib import pyplot as plt


path = 'public/sample-desktop-1.png'
# 画像のパスを指定
path = 'public/sample-desktop-1.jpg'
i = cv2.imread(path, 1)

# 変換前後の対応点を設定
p_original = np.float32([[0,0], [473,55], [14, 514], [467, 449]])
p_trans = np.float32([[0,0], [524,0], [0,478], [524,478]])
p_original = np.float32([[0, 0], [473, 55], [14, 514], [467, 449]])
p_trans = np.float32([[0, 0], [524, 0], [0, 478], [524, 478]])

# 変換マトリクスと射影変換
M = cv2.getPerspectiveTransform(p_original, p_trans)
i_trans = cv2.warpPerspective(i, M, (524, 478))

# 変換後の画像を表示
cv2.imshow('Transformed Image', i_trans)

# # ここからグラフ設定
# fig = plt.figure()
# ax1 = fig.add_subplot(111)

# # 画像をプロット
show = cv2.cvtColor(i_trans, cv2.COLOR_BGR2RGB)
# ax1.imshow(show)

# fig.tight_layout()
# plt.show()
# plt.close()
# 'q'キーが押されたら終了
if cv2.waitKey(0) & 0xFF == ord('q'):
cv2.destroyAllWindows()
54 changes: 54 additions & 0 deletions src/testImage2.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import cv2
import numpy as np


path = 'public/sample-desktop-1.jpg'
i = cv2.imread(path, 1)

# フレームのサイズを取得
height, width, channels = i.shape




A = [1312,1000]
B = [2837,1000]
C = [3925,2239]
D = [529,2239]


a = abs(C[0] - D[0])
b = abs(A[0] - B[0])
c = abs(A[1] - D[1])
print(f'a:{a}')
print(f'b:{b}')
print(f'c:{c}')

Y = a
print(f'Y:{Y}')


A_trans = [width/2,0]
B_trans = [width/2 + a,0]
C_trans = [width/2 + a,Y]
D_trans = [width/2,Y]
print(A_trans)


# 変換前後の対応点を設定
p_original = np.float32([A, B, C, D])



p_trans = np.float32([A_trans, B_trans, C_trans, D_trans])

# 変換マトリクスと射影変換
M = cv2.getPerspectiveTransform(p_original, p_trans)
i_trans = cv2.warpPerspective(i, M, (width*2, height*2))

# 変換後の画像を表示
cv2.imshow('Transformed Image', i_trans)

# 'q'キーが押されたら終了
if cv2.waitKey(0) & 0xFF == ord('q'):
cv2.destroyAllWindows()
84 changes: 48 additions & 36 deletions src/testVideo1.py
Original file line number Diff line number Diff line change
@@ -1,30 +1,40 @@

import cv2
import numpy as np
# from matplotlib import pyplot as plt
import math

# カメラの高さと目標の座標を設定
camera_height = 100
Y = 200
X = camera_height

path = 'public/sample-desktop-1.jpg'
i = cv2.imread(path, 1)
# カメラキャプチャのオブジェクトを作成
capture = cv2.VideoCapture(0)

# フレームのサイズを取得
height, width, channels = i.shape
# カメラからフレームを1枚取得
ret, frame = capture.read()

if not ret:
capture.release()
cv2.destroyAllWindows()
exit()

# フレームのサイズを取得
height, width, channels = frame.shape

# 変換前後の対応点を設定
a = width / 2
b = a * X / math.sqrt(X**2 + Y**2)

A = [1312,1000]
B = [2837,1000]
C = [3925,2239]
D = [529,2239]
A = [width/2, height - Y]
B = [width, height - Y]
C = [width, height]
D = [width/2, height]

# A_trans = [width/2, height - Y]
# B_trans = [width, height - Y]
# C_trans = [width, height]
# D_trans = [width/2, height]

a = abs(C[0] - D[0])
b = abs(A[0] - B[0])
c = abs(A[1] - D[1])
print(f'a:{a}')
print(f'b:{b}')
print(f'c:{c}')

Y = a
print(f'Y:{Y}')
Expand All @@ -37,27 +47,29 @@
print(A_trans)


# 変換前後の対応点を設定
# 変換前後の対応点
p_original = np.float32([A, B, C, D])
p_trans = np.float32([A_trans, B_trans, C_trans, D_trans])

while True:
# カメラからのフレーム取得
ret, frame = capture.read()
if not ret:
break

# 射影変換
M = cv2.getPerspectiveTransform(p_original, p_trans)
frame_trans = cv2.warpPerspective(frame, M, (width, height))

p_trans = np.float32([A_trans, B_trans, C_trans, D_trans])

# 変換マトリクスと射影変換
M = cv2.getPerspectiveTransform(p_original, p_trans)
i_trans = cv2.warpPerspective(i, M, (width*2, height*2))


# ここからグラフ設定
# fig = plt.figure()
# ax1 = fig.add_subplot(111)

# 画像をプロット
show = cv2.cvtColor(i_trans, cv2.COLOR_BGR2RGB)

cv2.imshow("tekitou",show)

# fig.tight_layout()
# plt.show()
# plt.close()
# A_trans、B_transの各点に円を描画する
cv2.circle(frame_trans, (int(A_trans[0]), int(A_trans[1])), 5, (255, 0, 0), -1)
cv2.circle(frame_trans, (int(B_trans[0]), int(B_trans[1])), 5, (255, 0, 0), -1)

# 表示
cv2.imshow('変換後の映像', frame_trans)

if cv2.waitKey(1) & 0xFF == ord('q'):
break

capture.release()
cv2.destroyAllWindows()

0 comments on commit f71f6c0

Please sign in to comment.