250x250
Link
๋‚˜์˜ GitHub Contribution ๊ทธ๋ž˜ํ”„
Loading data ...
Notice
Recent Posts
Recent Comments
๊ด€๋ฆฌ ๋ฉ”๋‰ด

Data Science LAB

[Python] OpenCV ๊ธฐ์ดˆ 10 - ์ด๋ฏธ์ง€ ๋ณ€ํ˜•(์ด์ง„ํ™”) ๋ณธ๋ฌธ

๐Ÿ–ฅ๏ธ Computer Vision/Opencv

[Python] OpenCV ๊ธฐ์ดˆ 10 - ์ด๋ฏธ์ง€ ๋ณ€ํ˜•(์ด์ง„ํ™”)

ใ…… ใ…œ ใ…” ใ…‡ 2022. 8. 10. 12:35
728x90

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
Comments