일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
Tags
- 군집화
- 빅데이터분석기사
- 독립표본
- 언더샘플링
- 텍스트분석
- Lambda
- DBSCAN
- 대응표본
- 빅데이터
- opencv
- 크롤링
- 파이썬
- numpy
- 오버샘플링
- 워드클라우드
- LDA
- 주성분분석
- ADsP
- 데이터분석
- ADP
- Python
- 데이터분석전문가
- PCA
- 데이터불균형
- dataframe
- t-test
- 데이터분석준전문가
- pandas
- iloc
- datascience
Archives
Data Science LAB
[Python] OpenCV 기초 14 - 이미지 검출 (2) (윤곽선) 본문
728x90
1. 윤곽선 검출 (Contour) : 경계선을 연결한 선
rat, otsu = cv.threshold([이미지], -1, 255, cv2.THRESH_BINARY | cv2.THRESH_OTSU)
cv2.findCountours(otsu, [윤곽선 찾기 모드], cv2.CHAIN_APPORX_NONE)
import cv2
img = cv2.imread('img.png')
target_img = img.copy()
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
rat, otsu = cv2.threshold(gray, -1, 255, cv2.THRESH_BINARY | cv2.THRESH_OTSU)
contours, hierachy = cv2.findContours(otsu, cv2.RETR_LIST, cv2.CHAIN_APPROX_NONE) # 윤곽선 검출
# 윤곽선 정보, 윤곽선 구조
# 이미지, 윤곽선을 찾는 모드(mode), 윤곽선을 찾을때 사용하는 근사치 방법(method) : cv2.CHAIN_APPROX_SIMPLE, cv2.CHAIN_APPROX_NONE
COLOR = (0,200,0) # 녹색
cv2.drawContours(target_img, contours, -1, COLOR, 2) #윤곽선 그리기
# 대상 이미지, 윤곽선 정보, 인데스(-1이면 전체), 색깔, 두께
cv2.imshow('img',img)
cv2.imshow('gray',gray)
cv2.imshow('otsu',otsu)
cv2.imshow('contour',target_img)
cv2.waitKey(0)
cv2.destroyAllWindows()
윤곽선 찾기 모드
1. cv2.RETR_EXTERNAL : 가장 외곽의 윤곽선만 찾음
2. cv2.RETR_LIST : 모든 윤곽선 찾음(계층 정보 없음)
3. cv2.RETR_TREE : 모든 윤곽선 찾으(계층 정보를 트리 구조로 생성)
2. 경계 사각형 찾기 (boundingRect())
import cv2
img = cv2.imread('jpg.png')
target_img = img.copy()
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
rat, otsu = cv2.threshold(gray, -1, 255, cv2.THRESH_BINARY | cv2.THRESH_OTSU)
contours, hierachy = cv2.findContours(otsu, cv2.RETR_LIST, cv2.CHAIN_APPROX_NONE) # 윤곽선 검출
COLOR = (0,200,0) # 녹색
for cnt in contours:
x,y , width, height = cv2.boundingRect(cnt)
cv2.rectangle(target_img, (x,y), (x+width, y+height), COLOR, 2) # 사각형 그림
cv2.imshow('img',img)
cv2.imshow('gray',gray)
cv2.imshow('otsu',otsu)
cv2.imshow('contour',target_img)
cv2.waitKey(0)
cv2.destroyAllWindows()
3. 사각형 면적 구하기 (contourArea())
import cv2
img = cv2.imread('card.png')
target_img = img.copy()
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
rat, otsu = cv2.threshold(gray, -1, 255, cv2.THRESH_BINARY | cv2.THRESH_OTSU)
contours, hierachy = cv2.findContours(otsu, cv2.RETR_LIST, cv2.CHAIN_APPROX_NONE) # 윤곽선 검출
COLOR = (0,200,0) # 녹색
for cnt in contours:
if cv2.contourArea(cnt) > 25000:
x,y , width, height = cv2.boundingRect(cnt)
cv2.rectangle(target_img, (x,y), (x+width, y+height), COLOR, 2) # 사각형 그림
cv2.imshow('img',img)
cv2.imshow('gray',gray)
cv2.imshow('otsu',otsu)
cv2.imshow('contour',target_img)
cv2.waitKey(0)
cv2.destroyAllWindows()
728x90
'🖥️ Computer Vision > Opencv' 카테고리의 다른 글
[Python] OpenCV 기초 13 - 이미지 검출 (0) | 2022.08.14 |
---|---|
[Python] OpenCV 기초 12 - 이미지 변환 (2) (열림, 닫힘) (0) | 2022.08.13 |
[Python] OpenCV 기초 11 - 이미지 변환(팽창 및 침식) (0) | 2022.08.11 |
[Python] OpenCV 기초 10 - 이미지 변형(이진화) (0) | 2022.08.10 |
[Python] OpenCV 기초 9 - 이미지 변형 (2) (원근) (0) | 2022.08.09 |
Comments