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

画像を回転して黒い枠を切り取る

Last updated at Posted at 2024-04-01

動機

AIの学習前にデータ拡張(水増し)する際、回転をさせると黒い枠が残るができれば消したい。
test.gif:width:100px

画像を回転して黒い枠を切り取る

rotate_and_crop.rb
import cv2
import matplotlib.pyplot as plt
import matplotlib.animation as animation
import math

img = cv2.imread('/content/drive/MyDrive/lena.png')
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
fig = plt.figure()
h, w, c = img.shape
imgs=[]

for i in range(0, 360, 10):
  rad = math.radians(i)
  scale = abs(math.sin(rad)) + abs(math.cos(rad))
  mat = cv2.getRotationMatrix2D((w / 2, h / 2), i, scale)
  affine_img = cv2.warpAffine(img, mat, (w, h))
  imgs.append([plt.imshow(affine_img)])

ani = animation.ArtistAnimation(fig, imgs, interval=360)
ani.save("rena.gif")

test (1).gif

1
1
4

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