250x250
Link
๋์ GitHub Contribution ๊ทธ๋ํ
Loading data ...
Notice
Recent Posts
Recent Comments
์ผ | ์ | ํ | ์ | ๋ชฉ | ๊ธ | ํ |
---|---|---|---|---|---|---|
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
- ๋น ๋ฐ์ดํฐ๋ถ์๊ธฐ์ฌ
- pandas
- datascience
- ADP
- Lambda
- dataframe
- t-test
- ๋น ๋ฐ์ดํฐ
- DBSCAN
- ์ฃผ์ฑ๋ถ๋ถ์
- ํ ์คํธ๋ถ์
- opencv
- ์ค๋ฒ์ํ๋ง
- ์๋ํด๋ผ์ฐ๋
- LDA
- ์ธ๋์ํ๋ง
- Python
- ๋ฐ์ดํฐ๋ถ๊ท ํ
- ๋ฐ์ดํฐ๋ถ์
- PCA
- ๋ฐ์ดํฐ๋ถ์์ ๋ฌธ๊ฐ
- ADsP
- ๊ตฐ์งํ
- ๋ฐ์ดํฐ๋ถ์์ค์ ๋ฌธ๊ฐ
- ๋ ๋ฆฝํ๋ณธ
- ๋์ํ๋ณธ
- ํฌ๋กค๋ง
- ํ์ด์ฌ
- numpy
- iloc
Archives
Data Science LAB
[Python] OpenCV ๊ธฐ์ด 10 - ์ด๋ฏธ์ง ๋ณํ(์ด์งํ) ๋ณธ๋ฌธ
๐ฅ๏ธ Computer Vision/Opencv
[Python] OpenCV ๊ธฐ์ด 10 - ์ด๋ฏธ์ง ๋ณํ(์ด์งํ)
ใ ใ ใ ใ 2022. 8. 10. 12:35728x90
1. Threshold
cv2.threshold([์ด๋ฏธ์ง], ์๊ณ๊ฐ, ๋ฐํ๊ฐ, cv2.THRESH_BINARY)
import cv2
img = cv2.imread('book.jpg',cv2.IMREAD_GRAYSCALE)
ret, binary = cv2.threshold(img, 127, 255, cv2.THRESH_BINARY) # 127๋ณด๋ค ํฌ๋ฉด ํฐ์, ์์ผ๋ฉด ๊ฒ์ ๋ฐํ
cv2.imshow('img',img)
cv2.imshow('binary',binary)
cv2.waitKey(0)
cv2.destroyAllWindows()
127์ ๊ธฐ์ค์ผ๋ก 127๋ณด๋ค ํฐ ๊ฐ์ด๋ฉด ๋ฐํ๊ฐ์ธ ํฐ์, ์์ ๊ฐ์ด๋ฉด ๊ฒ์ ์์ ๋ฐํํจ
2. Trackbar
cv2.nameWindow([Window name])
cv2.createTrackbar([bar name], [Window name], ์ด๊ธฐ๊ฐ, ์ต๋๊ฐ, ์ด๋ฒคํธ ์ฒ๋ฆฌ)
import cv2
def empty(pos):
#print(pos)
pass
img = cv2.imread('book.jpg', cv2.IMREAD_GRAYSCALE)
name = 'Trackbar'
cv2.namedWindow(name)
cv2.createTrackbar('threshold', name, 127, 255, empty) # bar ์ด๋ฆ, ์ฐฝ์ ์ด๋ฆ, ์ด๊ธฐ๊ฐ, ์ต๋๊ฐ, ์ด๋ฒคํธ ์ฒ๋ฆฌ
while True:
thresh = cv2.getTrackbarPos('threshold',name) # bar ์ด๋ฆ, ์ฐฝ์ ์ด๋ฆ
ret, binary = cv2.threshold(img, thresh, 255, cv2.THRESH_BINARY)
if not ret:
break
cv2.imshow(name, binary)
if cv2.waitKey(1) == ord('q'):
break
cv2.destroyAllWindows()
Trackbar๋ผ๋ ์ด๋ฆ์ ์ ์๋์ฐ ์ฐฝ์ threshold๋ฅผ ์กฐ์ ํ ์ ์๋ Trackbar๊ฐ ์์ฑ๋จ์ ์ ์ ์๋ค. ์ด๊ธฐ๊ฐ์ 127๋ก ์ค์ ๋์ด์๋ค.
์๊ณ๊ฐ(Threshold)๋ฅผ ์๊ฒ ํ ์๋ก ํฐ์์ผ๋ก ๋ณํ๋๋ ๋ถ๋ถ์ด ๋ง์์ง์ ํ์ธํ ์ ์๋ค.
3. Adaptive Threshold
cv2.adaptiveThreshold([์ด๋ฏธ์ง], ๋ฐํ๊ฐ, cv2.ADAPTIVE_MEAN_C, cv2.THRESH_BINARY, blocksize,c)
import cv2
def empty(pos):
#print(pos)
pass
img = cv2.imread('book.jpg', cv2.IMREAD_GRAYSCALE)
name = 'Trackbar'
cv2.namedWindow(name)
cv2.createTrackbar('block_size', name, 25, 100, empty) # block size : ํ์๋ง ๊ฐ๋ฅ, 1๋ณด๋ค๋ ํฐ ๊ฐ
cv2.createTrackbar('c', name, 3, 10, empty) # c: ์ผ๋ฐ์ ์ผ๋ก ์์์ ๊ฐ์ ์ฌ์ฉ
while True:
block_size = cv2.getTrackbarPos('block_size',name) # bar ์ด๋ฆ, ์ฐฝ์ ์ด๋ฆ
c = cv2.getTrackbarPos('c',name)
if block_size <= 1 : #1์ดํ๋ฉด 3์ผ๋ก ๋ณ๊ฒฝ
block_size = 3
if block_size % 2 == 0 : # ์ง์๋ฉด ํ์๋ก ๋ณ๊ฒฝ
block_size +=1
binary = cv2.adaptiveThreshold(img, 255, cv2.ADAPTIVE_THRESH_MEAN_C, cv2.THRESH_BINARY, block_size, c)
cv2.imshow(name, binary)
if cv2.waitKey(1) == ord('q'):
break
cv2.destroyAllWindows()
Adaptive Threshold๋ ์ด๋ฏธ์ง๋ฅผ ์์ ์์ญ์ผ๋ก ๋๋์ด ์๊ณ์น๋ฅผ ์ ์ฉํ๋ค.
block size์ c ๊ฐ์ ์ด์ฉํ์ฌ ์ด๋ฏธ์ง๋ฅผ ๋ณํํ ์ ์๋ค.
4. ์ค์ธ ์๊ณ ๋ฆฌ์ฆ
cv2.threshold([์ด๋ฏธ์ง], -1, cv2.THRESHOLD_BINARY | cv2.THRESH_OTSU)
import cv2
img = cv2.imread('book.jpg',cv2.IMREAD_GRAYSCALE)
ret, binary = cv2.threshold(img, 127, 255, cv2.THRESH_BINARY) # ๊ทธ๋ฅ ์๊ณ๊ฐ
ret, otsu = cv2.threshold(img, -1, 255, cv2.THRESH_BINARY | cv2.THRESH_OTSU) # ์ค์ธ ์๊ณ ๋ฆฌ์ฆ ์ด์ฉ(์ต์ ์ ์๊ณ๊ฐ ํ์)
print('otsu threshold',ret)
cv2.imshow('img',img)
cv2.imshow('binary',binary)
cv2.imshow('otsu',otsu)
cv2.waitKey(0)
cv2.destroyAllWindows()
์ค์ธ ์๊ณ ๋ฆฌ์ฆ์ ์ต์ ์ ์๊ณ๊ฐ์ ํ์ํ๋ค. ๋ชจ๋ ์ด๋ฏธ์ง์ ๋ํด ์ต์ ์ ์๊ณ๊ฐ์ ์ฐพ์ง๋ ๋ชปํ์ง๋ง Bimodal Image์ ์ฌ์ฉํ๊ธฐ ์ ํฉํ๋ค.
728x90
'๐ฅ๏ธ Computer Vision > Opencv' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[Python] OpenCV ๊ธฐ์ด 12 - ์ด๋ฏธ์ง ๋ณํ (2) (์ด๋ฆผ, ๋ซํ) (0) | 2022.08.13 |
---|---|
[Python] OpenCV ๊ธฐ์ด 11 - ์ด๋ฏธ์ง ๋ณํ(ํฝ์ฐฝ ๋ฐ ์นจ์) (0) | 2022.08.11 |
[Python] OpenCV ๊ธฐ์ด 9 - ์ด๋ฏธ์ง ๋ณํ (2) (์๊ทผ) (0) | 2022.08.09 |
[Python] OpenCV ๊ธฐ์ด 8 - ์ด๋ฏธ์ง ๋ณํ(ํ๋ฐฑ, ํ๋ฆผ) (0) | 2022.08.08 |
[Python] OpenCV ๊ธฐ์ด 7 - ์ด๋ฏธ์ง ๋์นญ ๋ฐ ํ์ (0) | 2022.08.07 |
Comments