소스 뷰어
import cv2
import matplotlib.pyplot as plt
# 이미지 불러오기
img = cv2.imread('example.jpg')
print( "img.shape =", img.shape )
# 특정 위치의 화소 접근 (컬러 이미지)
pixel = img[100, 200] # (100, 200) 위치의 BGR 값
blue = img[100, 200, 0] # Blue 채널 값
green = img[100, 200, 1] # Green 채널 값
red = img[100, 200, 2] # Red 채널 값
print( "pixel = img[100, 200] ", pixel )
print( "blue = img[100, 200, 0]", blue )
print( "green = img[100, 200, 1]", green )
print( "red = img[100, 200, 2]", red )
# 특정 화소 값 변경 (예: (100, 200) 위치를 파란색으로 변경)
img[100, 200] = [255, 0, 0]
# 이미지의 특정 영역을 변경
img[ 50 : 250 , 100 : 400 ] = [0, 255, 0]
img[ 251 : 500 , 100 : 400 ] = [255, 0, 0]
img[ 501 : 750 , 100 : 400 ] = [0, 0, 255]
# OpenCV는 이미지를 BGR로 읽기 때문에 RGB로 변환
# img_rgb = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
# NumPy를 사용하여 BGR을 RGB로 변경
img_rgb = img[:,:,::-1] # 채널 순서를 역으로 뒤집음 (BGR -> RGB)
# 이미지 출력
plt.imshow( img_rgb )
plt.title('Color image pixel access')
plt.axis('off') # 축 제거
plt.show()