소스 뷰어
조회수 :   3
import cv2
import matplotlib.pyplot as plt

# 이미지를 회색조(그레이스케일)로 읽기
img_gray = cv2.imread('example.jpg', cv2.IMREAD_GRAYSCALE)

# 관심 영역 설정 (예: x, y 좌표와 폭, 높이)
x, y, w, h = 1100, 850, 400, 400  # x, y 위치와 폭, 높이
roi_gray = img_gray[ y : y + h, x : x + w ]  # 관심 영역 추출

# ROI 영상값 출력
print( "roi_gray =", roi_gray, sep="\n" )
print()

# 원본 회색조 이미지에서 관심 영역에 사각형 그리기 (시각화를 위해)
img_gray_with_rect = cv2.cvtColor(img_gray, cv2.COLOR_GRAY2BGR)  
# 사각형을 그리기 위해 BGR로 변환
cv2.rectangle(img_gray_with_rect, (x, y), (x + w, y + h), (255, 0, 0), 8)  # 사각형

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

# 1. 원본 회색조 이미지 출력 (관심 영역 표시 포함)
plt.subplot(1, 2, 1)
plt.imshow(img_gray_with_rect, cmap='gray')
plt.title('Original Grayscale Image with ROI')
plt.axis('off')

# 2. 관심 영역만 잘라서 출력
plt.subplot(1, 2, 2)
plt.imshow(roi_gray, cmap='gray')
plt.title('Region of Interest (ROI) in Grayscale')
plt.axis('off')

# 그래프 보여주기
plt.tight_layout()
plt.show()
roi_gray =
[[170 170 171 ...  62  33  84]
 [171 170 171 ...  92  36  57]
 [176 173 171 ... 102  18  72]
 ...
 [151 162 158 ...  59  43  39]
 [165 167 165 ...  64  52  34]
 [173 170 163 ...  78  67  40]]

No description has been provided for this image