OpenCV 기초

Jae Gyeong Lee·2024년 2월 14일

OpenCV(Open Source Computer Vision)

  • 실시간 이미지/영상 처리를 위한 라이브러리
pip install opencv-python
import cv2

# 이미지 파일 열기
image = cv2.imread('input_image.jpg')

# 이미지 출력을 위한 윈도우 창 호출
cv2.imshow('Test Image', image)
cv2.waitKey(0) # 사용자 입력(아무 키) 대기
cv2.destroyAllWindows() # 사용자 입력 시, 호출된 이미지 종료


# 이미지 다른 파일로 저장
cv2.imwrite('path\output_image.jpg', image)
# 파일경로, 파일명 영문으로 설정

# 사각형 그리기
point_1 = (x1, y1); point_2 = (x2, y2)  # 좌표 설정
color = (0, 255, 0)                     # 선의 색 설정 (B, G, R)
thickness = 2                           # 선의 두께 설정
cv2.rectangle(image, point_1, point_2, color, thickness)

# 이미지 파일 크기 확인
height, width = image.shape[:2]
  • 주의: file path에 한글이 포함되어 있을 경우, image를 제대로 못 읽어오기도 함.
profile
안녕하세요 반갑습니다. 공부한 내용들을 기록하고 있습니다.

0개의 댓글