Skip to content

Instantly share code, notes, and snippets.

@ksasao
Created June 28, 2025 03:50
Show Gist options
  • Save ksasao/34f7768e9dc47d1c18ada514956549b2 to your computer and use it in GitHub Desktop.
Save ksasao/34f7768e9dc47d1c18ada514956549b2 to your computer and use it in GitHub Desktop.
import cv2
import numpy as np
import sys
video_path = 'your_movie_file.mp4'
cap = cv2.VideoCapture(video_path)
if not cap.isOpened():
print("動画を開けませんでした")
sys.exit()
# 動画情報取得
total_frames = int(cap.get(cv2.CAP_PROP_FRAME_COUNT))
height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
center_x = width // 2
slice_width = 6 # 取り出すピクセル数(中央から左右slice_width/2ピクセル)
# 出力画像を初期化(高さ x (総フレーム数×スライス幅) x 色)
result_image = np.zeros((height, total_frames * slice_width, 3), dtype=np.uint8)
frame_idx = 0
while frame_idx < total_frames:
ret, frame = cap.read()
if not ret:
break
# 中央付近の slice_width ピクセルを抽出
start_x = center_x - slice_width // 2
end_x = start_x + slice_width
vertical_slice = frame[:, start_x:end_x, :]
# スライスを対応位置に代入
result_image[:, frame_idx * slice_width : (frame_idx + 1) * slice_width, :] = vertical_slice
frame_idx += 1
progress = (frame_idx / total_frames) * 100
print(f"\r処理中: {progress:.2f}% ({frame_idx}/{total_frames})", end='')
cap.release()
# 出力画像保存
cv2.imwrite('line_scan_result.jpg', result_image)
print("\n完了!")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment