You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 

41 lines
1.4 KiB

# 実験課題1
# 3.1 画像サイズ変換・画像の回転
import cv2
# 画像の読み込み(任意のカラー画像を用意し、ファイル名を指定してください)
image_path = 'sample.jpg'
img = cv2.imread(image_path)
if img is None:
print("画像を読み込めませんでした。ファイルパスを確認してください。")
else:
# --- 画像サイズ変換 ---
# 元画像の高さと幅を取得
height, width = img.shape[:2]
# 縦横を1/2のサイズに計算
new_width = width // 2
new_height = height // 2
# 画像のサイズを変更
resized_img = cv2.resize(img, (new_width, new_height))
# --- 画像の回転 ---
# 回転の中心座標を計算(画像の中心)
center = (width // 2, height // 2)
# 45度回転するための変換行列を作成(スケールは1.0でそのまま)
rotation_matrix = cv2.getRotationMatrix2D(center, 45, 1.0)
# アフィン変換を適用して画像を回転
rotated_img = cv2.warpAffine(img, rotation_matrix, (width, height))
# --- 結果の表示 ---
cv2.imshow('Original Image', img)
cv2.imshow('Resized Image (1/2)', resized_img)
cv2.imshow('Rotated Image (45 degrees)', rotated_img)
# キー入力待ち(任意のキーを押すとウィンドウが閉じる)
cv2.waitKey(0)
cv2.destroyAllWindows()