IT/AI
[이미지] Numpy로 이미지를 224x224 크기로 crop 하기 [Python]-cv2
joseph24
2023. 8. 29. 12:12
728x90
from matplotlib import pyplot as plt
import numpy as np
import random
import cv2
org_img = cv2.imread("./cat2.png")[:,:,::-1]
plt.imshow(org_img)
plt.title(f"original image")
plt.show()
def cropping(img):
height, width = img.shape[:2]
plt.figure(figsize=(10,5))
for i in range(10):
# 가로, 세로의 crop할 시작점
crop_s_h = random.randint(0, height-224)
crop_s_w = random.randint(0, width-224)
# 시작점부터 가로, 세로가 224만큼 되도록 crop
crop_img = img[crop_s_h:crop_s_h+224, crop_s_w:crop_s_w+224, :]
# 10개의 이미지를 2행 5열로 나열
plt.subplot(2,5,i+1)
plt.imshow(crop_img)
plt.title(f"cropped image_{i+1}")
plt.show()
cropping(org_img)
random.randint$()$
import random
n = random.randint(start,end)
>>>
n = start 이상 end 이하의 정수