Jooyoung Lee
기록 일지
Jooyoung Lee
  • 분류 전체보기 (27)
    • 임베디드 (9)
    • 딥러닝 (11)
      • ML (3)
      • Notions (4)
      • AI math (4)
      • 강의 리뷰 (0)
    • 논문 리뷰 (1)
    • 스터디 (1)
    • 컴퓨터 비전 (5)

블로그 메뉴

  • About me,
GitHub Contribution
Loading data ...
Jooyoung Lee

기록 일지

컴퓨터 비전

OpenCV 기초 - 영상 속성과 픽셀 값 처리

2022. 12. 19. 19:56

▶ OpenCV Python 관련 글은 황선규 박사님의 강의내용을 정리하였음.

Github 소스

영상의 속성과 픽셀 값 참조

  • OpenCV는 영상 데이터를 numpy.ndarray로 표현한다.
import cv2

img1 = cv2.imread('cat.bmp', cv2.IMREAD_GRAYSCALE)
img2 = cv2.imread('cat.bmp', cv2.IMREAD_COLOR)

numpy.ndarray

  • ndim : 차원 수, len(img.shape)과 같음.
  • shape : 각 차원의 크기 (h, w) Gray scale or (h, w, 3) Color  
                 → (h,w,n) : n 값이 2이면 graysclae , 3이면 color 라고 생각해도 문제 없다.
  • size : 전체 원소 개수
  • dtype : 원소의 데이터 타입. 영상 데이터는 unit8

OpenCV 영상 데이터 자료형과 Numpy 자료형

OpenCV 자료형 Numpy 자료형 구분
cv2.CV_8U numpy.uint8 8비트 부호없는 정수
cv2.CV_8S numpy.int8 8비트 부호있는 정수
cv2.CV_16U numpy.uint16 16비트 부호없는 정수
cv2.CV_16S numpy.int816 16비트 부호있는 정수
cv2.CV_32S numpy.int32 32비트 부호있는 정수
cv2.CV_32F numpy.float32 32비트 부동소수형
cv2.CV_64F numpy.float64 64비트 부동소수형
cv2.CV_16F numpy.float16 16비트 부동소수형
  • 그레이스케일 영상 : cv2.CV_8UC1 → numpy.uint8, shape = (h, w)
  • 컬러 영상 : cv2.CV_8UC3 → numpy.uint8, shape = (h, w, 3)

영상의 속성 참조 예제 코드

img1 = cv2.imread('cat.bmp', cv2.IMREAD_GRAYSCALE)
img2 = cv2.imread('cat.bmp', cv2.IMREAD_COLOR)

print('type(img1):', type(img1)) # type(img1): <class 'numpy.ndarray'>
print('img1.shape:', img1.shape) # img1.shape: (480, 640)
print('img2.shape:', img2.shape) # img2.shape: (480, 640, 3)
print('img2.dtype:', img2.dtype) # img2.dtype: uint8

h, w = img2.shape[:2] # h: 480, w: 640
print('img2 size: {} x {}'.format(w, h))

if len(img1.shape) == 2:
	print('img1 is a grayscale image')
elif len(img1.shape) == 3:
	print('img1 is a truecolor image')
img1 = cv2.imread('cat.bmp', cv2.IMREAD_GRAYSCALE)
img2 = cv2.imread('cat.bmp', cv2.IMREAD_COLOR)

for y in range(h):
	for x in range(w):
		img1[y, x] = 255
		img2[y, x] = [0, 0, 255]   #for문은 절대 사용 금지
        
        
# img1[:,:] = 255
# img2[:,:] = (0, 0, 255)

 

'컴퓨터 비전' 카테고리의 다른 글

OpenCV :: Mat Class (1)  (0) 2023.01.10
OpenCV :: 기본 자료형 클래스  (0) 2023.01.09
영상의 밝기 조절 과 산술 연산  (0) 2023.01.05
히스토그램 (Histogram)  (2) 2022.12.28
    '컴퓨터 비전' 카테고리의 다른 글
    • OpenCV :: Mat Class (1)
    • OpenCV :: 기본 자료형 클래스
    • 영상의 밝기 조절 과 산술 연산
    • 히스토그램 (Histogram)
    Jooyoung Lee
    Jooyoung Lee
    Embedded software engineer

    티스토리툴바