Numpy 배열은 여러 중요한 속성을 가지고 있습니다. 이미지 처리에서 이 속성들을 제대로 이해하는 것이 매우 중요합니다.
# 샘플 배열 생성
arr = np.array([[[1, 2], [3, 4]],
[[5, 6], [7, 8]]])
print(" 배열 정보:")
print(f" • ndim (차원 수): {arr.ndim}")
print(f" • shape (각 차원 크기): {arr.shape}")
print(f" • size (총 원소 개수): {arr.size}")
print(f" • dtype (데이터 타입): {arr.dtype}")
print(f" • itemsize (원소 크기): {arr.itemsize} bytes")
print(f" • nbytes (총 메모리): {arr.nbytes} bytes")

주요 속성들에 대해 나오게 됩니다.
이번 시간엔 배열의속성들이 어떤것이 있는지 배우도록 하겠습니다.
arr_1d = np.array([1, 2, 3])
arr_2d = np.array([[1, 2], [3, 4]])
arr_3d = np.array([[[1, 2], [3, 4]], [[5, 6], [7, 8]]])
print(f"1D array ndim: {arr_1d.ndim}") # 1
print(f"2D array ndim: {arr_2d.ndim}") # 2
print(f"3D array ndim: {arr_3d.ndim}") # 3
NumPy에서 ndim은 선언한 배열의 차원을 출력합니다.
앞장에서 흑백 이미지의 ndim은 2로 나오고 컬러 이미지의 경우엔 3으로 나오겠죠.
Shape는 튜플(tuple)로 표현되며, 각 숫자는 해당 차원의 크기를 의미합니다.
튜플은 리스트와 거의 같습니다. 내부 자료를 변경할수 없다는것을 제외하면요.
# 이미지 배열의 shape 이해
grayscale = np.random.randint(0, 256, (480, 640), dtype=np.uint8)
print(f"Grayscale image shape: {grayscale.shape}") # (480, 640)
# 의미: 높이 480픽셀, 너비 640픽셀
color = np.random.randint(0, 256, (480, 640, 3), dtype=np.uint8)
print(f"Color image shape: {color.shape}") # (480, 640, 3)
# 의미: 높이 480픽셀, 너비 640픽셀, RGB 3채널
batch = np.random.randint(0, 256, (10, 480, 640, 3), dtype=np.uint8)
print(f"Batch of images shape: {batch.shape}") # (10, 480, 640, 3)
# 의미: 10장의 이미지, 각각 480×640 크기의 RGB 이미지
Shape 읽는 법:
(3,): 원소가 3개인 1D 배열(2, 3): 2행 3열 (세로 2, 가로 3)(2, 3, 4): 2개의 (3×4) 행렬이 쌓인 구조