OpenCV-Python (4) 영상의 생성, 복사, 부분 영상 추출

hjwon·2023년 9월 4일
0

OpenCV-Python

목록 보기
4/6

📌 지정한 크기로 새 영상 생성하기

numpy.empty(shape, dtype=float, ...) -> arr
numpy.zeros(shape, dtype=float, ...) -> arr
numpy.ones(shape, dtype=None, ...) -> arr
numpy.full(shape, fill_value, dtype=None, ...) -> arr
  • shape: 각 차원의 크기, (h, w) 또는 (h, w, 3)

  • dtype: 원소의 데이터 타입, 일반적이 영상이면 numpy.uint8 지정

  • arr: 생성된 영상 (numpy.ndarray)

  • 참고사항
    - numpy.empty() 함수는 임의의 값으로 초기화된 배열을 생성
    - numpy.zeros() 함수는 0으로 초기화된 배열을 생성
    - numpy.ones() 함수는 1로 초기화된 배열을 생성
    - numpy.full() 함수는 fill_value로 초기화된 배열을 생성


💻 영상의 생성 예제 코드

img1 = np.empty((480, 640), dtype=np.uint8)   # grayscale image
img2 = np.zeros((480, 640, 3), dtype=np.uint8)   # color image
img3 = np.ones((480, 640), dtype=np.uint8) * 255   # white
img4 = np.full((480, 640, 3), (0, 255, 255), dtype=np.uint8)   # yellow

영상 생성 결과


💻 영상의 참조 및 복사 예제 코드

img1 = cv2.imread('HappyFish.jpg')

img2 = img1
img3 = img1.copy()

영상 참조 및 복사 결과1

img1 = cv2.imread('HappyFish.jpg')

img2 = img1
img3 = img1.copy()

img1.fill(255)

영상 참조 및 복사 결과2


💻 부분 영상 추출 예제 코드

img1 = cv2.imread('HappyFish.jpg')

img2 = img1[40:120, 30:150]   # numpy.ndarray의 슬라이싱
img3 = img1[40:120, 30:150].copy()

img2.fill(0)

부분 영상 추출 결과


profile
열공워니

0개의 댓글