Notice
Recent Posts
Recent Comments
Link
| 일 | 월 | 화 | 수 | 목 | 금 | 토 |
|---|---|---|---|---|---|---|
| 1 | 2 | |||||
| 3 | 4 | 5 | 6 | 7 | 8 | 9 |
| 10 | 11 | 12 | 13 | 14 | 15 | 16 |
| 17 | 18 | 19 | 20 | 21 | 22 | 23 |
| 24 | 25 | 26 | 27 | 28 | 29 | 30 |
| 31 |
Tags
- Spring
- mybatis
- table
- tomcat
- R
- xPlatform
- 공정능력
- SPC
- SQL
- es6
- 보조정렬
- Kotlin
- IntelliJ
- SSL
- JavaScript
- Python
- plugin
- Java
- GIT
- hadoop
- window
- mapreduce
- vaadin
- react
- MSSQL
- NPM
- Eclipse
- Sqoop
- Express
- Android
Archives
- Today
- Total
DBILITY
python opencv 기초 정리 본문
반응형
설치부터 한다.pip install opencv-python
C:\Dev64\workspace\python_execise>pip install opencv-python
Collecting opencv-python
Downloading opencv_python-4.13.0.92-cp37-abi3-win_amd64.whl.metadata (20 kB)
Requirement already satisfied: numpy>=2 in C:\Python\Python313\Lib\site-packages (from opencv-python) (2.4.0)
Downloading opencv_python-4.13.0.92-cp37-abi3-win_amd64.whl (40.2 MB)
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 40.2/40.2 MB 9.0 MB/s 0:00:04
Installing collected packages: opencv-python
Successfully installed opencv-python-4.13.0.92
C:\Dev64\workspace\python_execise>py -c "import cv2; print(cv2.__version__)"
4.13.0
이미지를 읽어 출력하고 키입력이 있으면 창을 닫는 코드다.
import cv2
img = cv2.imread("sample.png") #이미지 읽기
cv2.imshow("img",img) #이미지 출력
cv2.waitKey(0) #키보드입력 대기
cv2.imwrite("copy_sample.png",img) #이미지 저장
cv2.destroyAllWindows() # 모든창 종료
opencv의 색상은 RGB가 아닌 BGR순서
import cv2
img = cv2.imread("sample.png")
height, width, channel = img.shape; #이미지 크기 확인
print(f"image size : {width}x{height}, channel : {channel}")
b, g, r = cv2.split(img) #색상 채널 분리 (B,G,R)
cv2.imshow("Red Channel", r)
cv2.imshow("Green Channel", g)
cv2.imshow("Blue Channel", b)
cv2.waitKey(0)
cv2.destroyAllWindows()
이미지크기 조절
import cv2
img = cv2.imread("sample.png")
height, width, channel = img.shape;
resized = cv2.resize(img, (width * 2, height * 2))
cv2.imshow("resized", resized)
resized_ratio = cv2.resize(img, (0, 0), fx=0.5, fy=0.5)
cv2.imshow("resized_ratio", resized_ratio)
resized_fixed = cv2.resize(img, (300, 300))
cv2.imshow("resized_fixed", resized_fixed)
cv2.waitKey(0)
cv2.destroyAllWindows()
이미지 회전
import cv2
img = cv2.imread("sample.png")
cv2.imshow("img",img)
rotated = cv2.rotate(img, cv2.ROTATE_90_CLOCKWISE)
cv2.imshow("rotated", rotated)
cv2.waitKey(0)
cv2.destroyAllWindows()
이미지 자르기
import cv2
img = cv2.imread("sample.png")
cv2.imshow("img", img)
height, width, channel = img.shape;
# 좌표 : img[ y_start:y_end, x_start:x_end ]
cropped = img[0:int(height / 2), 0:int(width / 2)]
cv2.imshow("cropped", cropped)
cv2.waitKey(0)
cv2.destroyAllWindows()
그레이스케일 변환 ( 연산속도증가, 에지(윤곽선) 탐지 용이 , 조명변화민감도 저감 장점 )
import cv2
img = cv2.imread("sample.png")
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
cv2.imshow("gray", gray)
cv2.waitKey(0)
cv2.destroyAllWindows()
이진화(Thresholding) - 흑백(0,255)로 구분, 기준값보다 밝으면 흰색, 어두우면 검은색
import cv2
img = cv2.imread("sample.png")
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
_, thresholded = cv2.threshold(gray, 127, 255, cv2.THRESH_BINARY);
cv2.imshow("thresholded", thresholded)
cv2.waitKey(0)
cv2.destroyAllWindows()
적응형 이진화(Adaptive Thresholding) - 조명변화가 심한 환경, 픽셀주변 기준으로 동적 임계값 계산
source는 grayscale , blockSize는 적용영역크기로 홀수로 지정, 마지막값 C 계산된 경계값에서 차감할 값
import cv2
img = cv2.imread("sample.png")
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
adaptive = cv2.adaptiveThreshold(gray, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY, 11, 2)
cv2.imshow("adaptive", adaptive)
cv2.waitKey(0)
cv2.destroyAllWindows()
반응형
'python' 카테고리의 다른 글
| fastapi framework 기본 실습 (0) | 2026.05.13 |
|---|---|
| pymssql을 사용한 SQL Server 접속 테스트 (0) | 2025.09.24 |
| pip를 통한 package install 등.. (0) | 2025.09.23 |
| python matplot scatter chart , linear regression line exercise (0) | 2021.10.13 |
| python pandas dataframe (0) | 2021.10.12 |
Comments
