소스 뷰어
import cv2
import matplotlib.pyplot as plt

# 이미지 읽기
all_img = cv2.imread( 'example.jpg' )

print( "img.shape =", all_img.shape )
# 이미지의 높이와 넓이
height, width = all_img.shape[ : 2 ] 

print( "height =", height )
print( "width =", width )

# 중앙 좌표 계산
center_x, center_y = width // 2, height // 2

print( "center_y =", center_y )
print( "center_x =", center_x )

# ROI 크기 결정 (예: 이미지 크기의 50%)
roi_width, roi_height = width // 2, height // 2

print( "roi_height =", roi_height )
print( "roi_width =", roi_width )

# ROI 시작 좌표 계산
x = center_x - roi_width // 2
y = center_y - roi_height // 2

print( "y =", y )
print( "x =", x )
print()

# ROI 설정
roi_img = all_img[ y : y + roi_height , x : x + roi_width ]

# 원본 이미지에서 ROI에 사각형 그리기 (시각화를 위해)
img_with_rect = all_img.copy()

cv2.rectangle(img_with_rect, (x, y), (x + roi_width, y + roi_height), (0, 0, 255), 16)  # 사각형

# OpenCV 이미지 BGR을 RGB로 변환 (Matplotlib 호환을 위해)
img_rgb = cv2.cvtColor(img_with_rect, cv2.COLOR_BGR2RGB)
roi_img_rgb = cv2.cvtColor(roi_img, cv2.COLOR_BGR2RGB)

# 이미지들을 비교하여 출력
plt.figure(figsize=(10, 5))

# 1. 원본 이미지 출력 (관심 영역 표시 포함)
plt.subplot(1, 2, 1)
plt.title('Original Image with Center ROI')
plt.imshow(img_rgb)
plt.axis('off')

# 2. 관심 영역만 잘라서 출력
plt.subplot(1, 2, 2)
plt.title('Center Region of Interest (ROI)')
plt.imshow(roi_img_rgb)
plt.axis('off')

# 그래프 보여주기
plt.tight_layout()
plt.show()
img.shape = (2848, 4272, 3)
height = 2848
width = 4272
center_y = 1424
center_x = 2136
roi_height = 1424
roi_width = 2136
y = 712
x = 1068

No description has been provided for this image