파이썬으로 영상처리_크기 조정

k_minseokVv·2024년 1월 8일
0

OpenCV - Python

목록 보기
5/13

강의 출처 : https://www.youtube.com/watch?v=XK3eU9egll8&t=19409s

이미지의 크기 조정

  • 고정 크기로 조정
import cv2
img = cv2.imread('img.jpg')
dst = cv2.resize(img, (400,400)) #width height 사이즈로 고정

cv2.imshow('img',img)
cv2.imshow('dst',dst)
cv2.waitKey(0)
cv2.destroyAllWindows()
  • 비율로 크기 조정
import cv2
img = cv2.imread('img.jpg')
dst = cv2.resize(img, None, fx=0.5,fy=0.5) #x y를 0.5배씩 줄임

cv2.imshow('img',img)
cv2.imshow('dst',dst)
cv2.waitKey(0)
cv2.destroyAllWindows()

보간법

  • 이미지를 늘리거나 줄일 때 자연스럽게 하기 위해 사용
  1. cv2.INTER_AREA : 크기를 줄일 때 사용
  2. cv2.INTER_CUBIC : 크기 늘릴 때 사용(속도 느리지만 퀄리티가 좋음)
  3. cv2.INTER_LINEAR : 크기 늘릴 때 사용(기본 값)

보간법 적용해 축소

import cv2
img = cv2.imread('img.jpg')
dst = cv2.resize(img, None, fx=0.5,fy=0.5, interpolation=cv2.INTER_AREA) #x y를 0.5배씩 줄임

cv2.imshow('img',img)
cv2.imshow('dst',dst)
cv2.waitKey(0)
cv2.destroyAllWindows()

보간법 적용해 확대

import cv2
img = cv2.imread('img.jpg')
dst = cv2.resize(img, None, fx=2,fy=2, interpolation=cv2.INTER_CUBIC) #x y를 0.5배씩 줄임

cv2.imshow('img',img)
cv2.imshow('dst',dst)
cv2.waitKey(0)
cv2.destroyAllWindows()

동영상의 크기 조정

  • 고정 크기로 조정
import cv2
cap = cv2.VideoCapture('video.mp4')

while cap.isOpened():
    ret, frame = cap.read()
    if not ret:
        break
    
    frame_resized = cv2.resize(frame, (400,400))
    cv2.imshow('video',frame_resized)
    
    if cv2.waitKey(1) == ord('q'):
        break
        
cap.release()
cv2.destroyAllWindows()
  • 비율로 크기 조정
import cv2
cap = cv2.VideoCapture('video.mp4')

while cap.isOpened():
    ret, frame = cap.read()
    if not ret:
        break
    
    frame_resized = cv2.resize(frame, None, fx=0.5,fy=0.5,interpolation=cv2.INTER_AREA)
    cv2.imshow('video',frame_resized)
    
    if cv2.waitKey(1) == ord('q'):
        break
        
cap.release()
cv2.destroyAllWindows()
profile
C++, Python 활용 중

0개의 댓글