# 実験課題1 # 画像サイズ変換・画像の回転 import cv2 # 画像の読み込み(任意のカラー画像を用意し、ファイル名を指定してください) image_path = 'cat.jfif' img = cv2.imread(image_path) 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()