๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ

AI/Computer Vision

[Computer Vision] ํ…์ŠคํŠธ ์‚ฝ์ž… ๋ฐ ์œ ์šฉํ•œ ๊ธฐ๋Šฅ

728x90
๋ฐ˜์‘ํ˜•
๐Ÿ‘€ ๋ณธ ์˜ˆ์ œ๋Š” Window10์˜ VSCode, Python3.11.0๋กœ ์ž‘์„ฑ๋˜์—ˆ์Šต๋‹ˆ๋‹ค.

 

ํ…์ŠคํŠธ๋ฅผ ์‚ฝ์ž…ํ•˜๋Š” ๋ฉ”์„œ๋“œ๋Š” "putText"์ด๋‹ค.

import cv2
im = cv2.imread("test.png")

text ="rabbit"
cv2.putText(
    img=im,
    text=text,
    org=(50,50),
    fontFace=cv2.FONT_HERSHEY_SIMPLEX,
    fontScale=1,
    color=(242,200,100),
    thickness=1,
    lineType=cv2.LINE_AA,
    bottomLeftOrigin=False
)
cv2.imshow("text",im)
cv2.waitKey(0)
cv2.destroyAllWindows()

 

๋งค๊ฐœ๋ณ€์ˆ˜๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค.

  • img : ์ด๋ฏธ์ง€
  • text : ์‚ฝ์ž…ํ•  ํ…์ŠคํŠธ
  • org : ๋ฌธ์ž์—ด์„ ์ถœ๋ ฅํ•  ์œ„์น˜์˜ ์ขŒ์ธก ํ•˜๋‹จ ์ขŒํ‘œ
  • fontFace : ํฐํŠธ 
  • fontScale : ํฐํŠธ ์Šค์ผ€์ผ
  • color : ์ปฌ๋Ÿฌ
  • thickness : ์„  ๊ตต๊ธฐ 
  • lineType : ์„  ํƒ€์ž…
  • bottomLeftOrigin : True์˜ ๊ฒฝ์šฐ ๊ธ€์ž๊ฐ€ ์œ„์•„๋ž˜๋กœ ๋ฐ˜์ „๋จ   

 

 

 

 

 

Trackbar๋ผ๋Š” ๊ธฐ๋Šฅ์„ ์‚ฌ์šฉํ•ด ๋™์ ์œผ๋กœ ์ด๋ฏธ์ง€์— ๋ณ€ํ™”๋ฅผ ์ค„ ์ˆ˜ ์žˆ๋‹ค.

import cv2
img = cv2.imread("test.png",cv2.IMREAD_GRAYSCALE)
img = cv2.resize(img,(512,512),interpolation=cv2.INTER_LANCZOS4)

def on_change(pos:int):
    _,binary = cv2.threshold(img,pos,255,cv2.THRESH_BINARY)
    cv2.imshow("thres",binary)
cv2.imshow("thres",img)
cv2.createTrackbar("Threshold Controler","thres",100,125,on_change)

cv2.waitKey(0)
cv2.destroyAllWindows()

 

createTrackbar์™€ on_change ํ•จ์ˆ˜๋ฅผ ๊ฐ™์ด ์‚ฌ์šฉํ•˜๋ฉด ๋™์ ์œผ๋กœ ์ด๋ฏธ์ง€๋ฅผ ์ˆ˜์ •ํ•˜๋ฉด์„œ ๊ฒฐ๊ณผ๋ฅผ ์‹ค์‹œ๊ฐ„์œผ๋กœ ๋ณผ ์ˆ˜ ์žˆ๋‹ค.

 

 

์œ ์šฉํ•œ ๊ธฐ๋Šฅ์ค‘ ํ•˜๋‚˜๋กœ ๋งˆ์Šคํฌ ์—ฐ์‚ฐ์ด ์žˆ๋‹ค.

Python์—์„œ๋Š” image(3 Channel), mask(2 Channel)๋ฅผ ์‚ฌ์šฉํ•ด์„œ ๋งˆ์Šคํ‚น ํ•  ์ˆ˜ ์žˆ๋‹ค.

bitwise ์—ฐ์‚ฐ์˜ ๋งค๊ฐœ๋ณ€์ˆ˜๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค.

  • src1 : ์ฒซ๋ฒˆ์งธ ์ด๋ฏธ์ง€
  • src2 : ๋‘๋ฒˆ์งธ ์ด๋ฏธ์ง€
  • dst : ๊ฒฐ๊ณผ๋ฅผ ์ €์žฅํ•˜๊ฑฐ๋‚˜ ์ถœ๋ ฅํ•  ์ด๋ฏธ์ง€, ๊ธฐ๋ณธ๊ฐ’ None
  • mask : ์„ ํƒ ์‚ฌํ•ญ์œผ๋กœ, ํ‘๋ฐฑ ์ด๋ฏธ์ง€์—ฌ์•ผ ํ•˜๋ฉฐ, ํฐ์ƒ‰ ์˜์—ญ๋งŒ ์—ฐ์‚ฐ์— ํฌํ•จ

 

bitwise_and : ์ผ๋ฐ˜์ ์œผ๋กœ ๋งˆ์Šคํ‚น์„ ํ•  ๋•Œ ์‚ฌ์šฉํ•จ

import cv2
import numpy as np

img = cv2.imread("test.png")
img = cv2.resize(img,(512,512),interpolation=cv2.INTER_LANCZOS4)

mask = np.zeros(img.shape[:2],dtype=np.uint8)
cv2.rectangle(mask,(100,100),(300,400),255,thickness=cv2.FILLED)
# ๋งˆ์Šคํ‚น
inv_mask = cv2.bitwise_and(img,img,mask=mask)
cv2.imshow("inv_mask",inv_mask)
cv2.waitKey(0)
cv2.destroyAllWindows()

 

bitwise_not

import cv2
import numpy as np

img = cv2.imread("test.png")
img = cv2.resize(img,(512,512),interpolation=cv2.INTER_LANCZOS4)

mask = np.zeros(img.shape[:2],dtype=np.uint8)
cv2.rectangle(mask,(100,100),(300,400),255,thickness=cv2.FILLED)
# ๋งˆ์Šคํ‚น
inv_mask = cv2.bitwise_not(img,img,mask=mask)
cv2.imshow("inv_mask",inv_mask)
cv2.waitKey(0)
cv2.destroyAllWindows()

bitwise_or

import cv2
import numpy as np

img = cv2.imread("test.png")
img = cv2.resize(img,(512,512),interpolation=cv2.INTER_LANCZOS4)

mask = np.zeros(img.shape[:2],dtype=np.uint8)
cv2.rectangle(mask,(100,100),(300,400),255,thickness=cv2.FILLED)
# ๋งˆ์Šคํ‚น
inv_mask = cv2.bitwise_or(img,img,mask=mask)
cv2.imshow("inv_mask",inv_mask)
cv2.waitKey(0)
cv2.destroyAllWindows()

 

bitwise_xor

import cv2
import numpy as np

img = cv2.imread("test.png")
img = cv2.resize(img,(512,512),interpolation=cv2.INTER_LANCZOS4)

mask = np.zeros(img.shape[:2],dtype=np.uint8)
cv2.rectangle(mask,(100,100),(300,400),255,thickness=cv2.FILLED)
# ๋งˆ์Šคํ‚น
inv_mask = cv2.bitwise_xor(img,img,mask=mask)
cv2.imshow("inv_mask",inv_mask)
cv2.waitKey(0)
cv2.destroyAllWindows()

 

728x90
๋ฐ˜์‘ํ˜•