python에서 컴퓨터 비전 처리를 위한 라이브러리 중 가장 유명한 라이브러리인 OpenCV(Open Source Computer Vision)입니다.
(SimpleCV도 있긴 하지만.. )
Home
OpenCV provides a real-time optimized Computer Vision library, tools, and hardware. It also supports model execution for Machine Learning (ML) and Artificial Intelligence (AI).
opencv.org
주요 기능으로, 이미지 처리, 비디오 처리, 비전 인식(객체 탐지, OCR, 패턴 인식 등), 기계학습, 이미지 변환 등이 있습니다.
0) 설치
설치는 pip를 이용해서 설치할 수 있습니다.
pip install --upgrade opencv-python
# 또는 외부확장모듈 포함
pip install --upgrade opencv-contrib-python
(2024.05.12. 기준으로 v4.9.0.80이 릴리즈 중입니다)
(참고, 설치 도중 No such file or directory: '~\\envs\\tf390\\lib\\site-packages\\numpy-1.23.5.dist-info\\METADATA' 등
오류 발생 시 해당 폴더(numpy-1.23.5.dist-info)를 삭제한 후 재설치를 해봅시다)
1) 추가.. imutils
또한 OpenCV의 이미지 처리 기능을 좀더 편리하고 넓게 활용할 수 있도록 도와주는 패키지인 imutils 입니다.
마찬가지로 설치는 pip를 이용해서 설치할 수 있습니다.
pip install imutils
2-1) 이용 - 이미지 이용
이미지 이용
import cv2 as cv
# 이미지 읽기 - 1
img = cv.imread('image.jpg')
# 이미지 읽기 - 2 (파일링크에 한글 있을 때)
#import numpy as np
#s_file = 'image.jpg'
#img_ary = np.fromfile(s_file, np.uint8)
#img = cv.imdecode(img_ary, cv.IMREAD_COLOR)
# 이미지 변환(회색)
img_gray = cv.cvtColor(img, cv.COLOR_BGR2GRAY)
# 이미지 표시
cv.imshow('Image Title - Image', img_gray); cv.waitKey(0)
# RGB
img_b, img_g, img_r = cv.split(img)
blank = np.zeros(img.shape[:2], dtype='unit8')
#cv.imshow('Image B', img_b)
#cv.imshow('Image G', img_g)
#cv.imshow('Image R', img_r)
merged_img = cv.merge([img_b, img_g, img_r])
img_b2 = cv.merge([img_b, blank, blank])
img_g2 = cv.merge([blank, img_g, blank])
img_r2 = cv.merge([blank, blank, img_r])
#cv.imshow('Image B', img_b2)
#cv.imshow('Image G', img_g2)
#cv.imshow('Image R', img_r2)
mask_img = cv.circle(blank, (img.shape[1]//2, img.shape[0]//2), 150, 255, -1)
masked_img = cv.bitwise_and(img, img, mask = mask_img)
#cv.imshow('Masked Image', masked_img)
# 이미지 저장
#cv.imwrite('image_gray.jpg', img_gray)
cv.waitKey(0)
cv.destroyAllWindows()
2-2) 이용 - 영상(웹캠) 이용
웹캠 영상 이용
import cv2 as cv
WEBCAM_NO = 0
webcam = cv.VideoCapture(WEBCAM_NO)
while True:
ret, frame = webcam.read()
if ret == False:
continue
cv.imshow('WEBCAM Video', frame)
key = cv.waitKey(1)
if key == 27: # ESC key
break
2-3) 이용 - 이미지 처리
1) 해상도 향상 - Super Resolution
OpenCV에서는 화질손실을 줄이며 이미지를 확대할 있도록 Super Resolution을 제공하고 있습니다.
① EDSR 모델 다운로드 - https://github.com/Saafke/EDSR_Tensorflow/tree/master/models (ex) x2 = 2배..
② ESPCN 모델 다운로드 - https://github.com/fannymonori/TF-ESPCN/tree/master/export
③ FSRCNN 모델 다운로드 - https://github.com/Saafke/FSRCNN_Tensorflow/tree/master/models
④ LapSRN 모델 다운로드 - https://github.com/fannymonori/TF-LapSRN/tree/master/export
opencv-contrib 모듈 설치 후, 모델을 다운받아 py 와 함께 복사해둡니다.
(참고.. 처리 속도 ESPCN > FSRCNN > LapSRN > EDSR, 성능 EDSR > LapSRN, ESPCN, FSRCNN)
import cv2 as cv
# 이미지 읽기 - 1
img = cv.imread('image.jpg')
# 모델 준비
sr = cv.dnn_superres.DnnSuperResImpl_create()
sr.readModel('LapSRN_x4.pb'); sr.setModel('lapsrn', 4) # (모델명, 배율)
# 모델 이용 이미지 업스케일링
img1 = sr.upsample(img)
# 비교.. OpenCV 자체 보간법 = Bicubic
img2 = cv.resize(img, dsize = None, fx = 4, fy = 4) # 4배..
참고)
https://docs.opencv.org/4.9.0/
https://docs.opencv.org/4.9.0/d6/d00/tutorial_py_root.html
https://docs.opencv.org/4.9.0/d3/df2/tutorial_py_basic_ops.html
https://docs.opencv.org/4.10.0/d2/d96/tutorial_py_table_of_contents_imgproc.html
https://learnopencv.com/super-resolution-in-opencv/
'개발' 카테고리의 다른 글
[Framework]Front-End Framework들 (0) | 2024.07.04 |
---|---|
[tool]MS Visual Studio Code (1) | 2024.06.15 |
[python-오류]ERROR: Could not install packages due to an OSError: [Errno 2] No such file or directory ... (0) | 2024.06.10 |
[python]파이썬 기반 웹프레임워크 - Django, Flask (0) | 2024.05.25 |
[python]DuckDB와 데이터 처리 (0) | 2024.05.07 |