일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
Tags
- imread
- mariadb
- Lotto
- RNN
- pycharm
- mean
- CNN
- E-P1
- Button
- Python
- ipad
- Series
- pip
- Splunk
- GitHub
- DFS
- javascript
- 알고리즘
- GT-S80
- SPL
- pandas
- keras
- LSTM
- install
- 삼성소프트웨어멤버십
- SciPy
- synology
- dataframe
- Numpy
- index
Archives
- Today
- Total
잠토의 잠망경
[Opencv] connectedComponents & connectedComponentsWithStats 본문
connectedComponents & connectedComponentsWithStats
0 0 0 0 0 0
1 0 0 0 1 0
1 0 0 0 1 0
1 0 0 0 1 0
1 1 0 0 0 0
위와 같이 어떤 image에 저와 같은 배열을 갖는 이미지가 있었다.
우리가 cv2.connectedComponents(image) 를 하면 연결된 components의 개수를 반환하고 Labeling을 해준다.
0 0 0 0 0 0
1 0 0 0 2 0
1 0 0 0 2 0
1 0 0 0 2 0
1 1 0 0 0 0
그럼 우리는 이것들을 분리해서 처리할 수 있다.
또한 cv2.connectedComponentsWithStats() 는 중심 좌표 및 범위를 알려주기 때문에
대상(component)를 특정화 시킬수 있다.
import cv2
import numpy as np
img = cv2.imread('imagePath') # color image load
gray = cv2.cvtColor(img, cv2.Color_bgr2gray) # gray 변환
ret, labels, stats, centriods = cv2.connectedComponentsWithStats(gray)
img1 = np.zeros(img.shape, dtype=img.dtype)
for i in range(1, ret):
r = np.random.randint(256)
g = np.random.randint(256)
b = np.random.randint(256)
img1[labels==i] = [b, g, r]
x, y, width, height = stats[i]
cv2.rectangle(img1, (x, y), (x+width, y+height), (0,0,255), 2)
cx, cy = centriods[i]
cv2.circle(img1, (int(cx), int(cy)), 5, (255,0,0), -1)
cv2.imshow('components', img1)
cv2.waitkey()
cv2.destroyAllwindows()
이렇게 하면 중점과 범위를 표기할 수 있다.
Comments