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

AI/Computer Vision

[Computer Vision] ๊ทธ๋ฆฌ๊ธฐ ํ•จ์ˆ˜

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

 

"์ง์„ "์„ ๊ทธ๋ฆฌ๋Š” ํ•จ์ˆ˜๋Š” "line" ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ๋œ๋‹ค.

import cv2
import numpy as np

# ํฐ๋ฐฐ๊ฒฝ์˜ ๋นˆ ์ด๋ฏธ์ง€ ์ƒ์„ฑ(512 x 512)

white_background = np.ones((512,512),dtype=np.uint8) * 255

# ์ง์„ 
cv2.line(
    img=white_background,
    pt1=(0,0),
    pt2=(256,256),
    color=100,
    thickness=3,
    lineType=cv2.LINE_4,
    shift=0)
cv2.imshow("line",white_background)
cv2.waitKey(0)
cv2.destroyAllWindows()

 

 

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

  • img : ๊ทธ๋ฆด np.array
  • pt1 : ์‹œ์ž‘ ์ขŒํ‘œ
  • pt2 : ๋ ์ขŒํ‘œ
  • color : ์ƒ‰
  • thickness : ์„  ๊ตต๊ธฐ
  • lineType : ์„  ํƒ€์ž…(cv2.LINE_4, cv2.LINE_8, cv2.LINE_AA)
  • shift : ๊ทธ๋ฆฌ๊ธฐ ์ขŒํ‘œ๊ฐ’์˜ ์ถ•์†Œ ๋น„์œจ

 

"ํ™”์‚ดํ‘œ ์ง์„ "์€ "arrowedLine" ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ๋œ๋‹ค.

import cv2
import numpy as np

# ํฐ๋ฐฐ๊ฒฝ์˜ ๋นˆ ์ด๋ฏธ์ง€ ์ƒ์„ฑ(512 x 512)

white_background = np.ones((512,512),dtype=np.uint8) * 255

# ํ™”์‚ดํ‘œ
cv2.arrowedLine(
    img=white_background,
    pt1=(0,0),
    pt2=(256,256),
    color=100,
    thickness=3,
    line_type=cv2.LINE_AA,
    shift=0,
    tipLength=0.2
)
cv2.imshow("arrow",white_background)

cv2.waitKey(0)
cv2.destroyAllWindows()

 

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

  • img : ๊ทธ๋ฆด np.array
  • pt1 : ์‹œ์ž‘ ์ขŒํ‘œ
  • pt2 : ๋ ์ขŒํ‘œ
  • color : ์ƒ‰
  • thickness : ์„  ๊ตต๊ธฐ
  • lineType : ์„  ํƒ€์ž…(cv2.LINE_4, cv2.LINE_8, cv2.LINE_AA)
  • shift : ๊ทธ๋ฆฌ๊ธฐ ์ขŒํ‘œ๊ฐ’์˜ ์ถ•์†Œ ๋น„์œจ
  • tipLength : ํ™”์‚ดํ‘œ ๋ชจ์–‘์˜ ๊ธธ์ด

 

 

"๋งˆ์ปค"์€ "drawMarker" ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ๋œ๋‹ค.

import cv2
import numpy as np

# ํฐ๋ฐฐ๊ฒฝ์˜ ๋นˆ ์ด๋ฏธ์ง€ ์ƒ์„ฑ(512 x 512)

white_background = np.ones((512,512),dtype=np.uint8) * 255

# ๋งˆ์ปค
cv2.drawMarker(
    img=white_background,
    position=(50,50),
    color=0,
    markerType=cv2.MARKER_DIAMOND,
    markerSize=20,
    thickness=3,
    line_type=cv2.LINE_8
)
cv2.imshow("Marker",white_background)
cv2.waitKey(0)
cv2.destroyAllWindows()

 

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

  • img : ๊ทธ๋ฆด np.array
  • position : ๊ทธ๋ฆด ์ขŒํ‘œ
  • color : ์ƒ‰
  • markerType : ๋งˆ์ปค์˜ ์ข…๋ฅ˜(cv2.MARKER_DIAMONE, ...)
  • markerSize : ๋งˆ์ปค์˜ ํฌ๊ธฐ
  • thickness : ์„  ๊ตต๊ธฐ
  • lineType : ์„  ํƒ€์ž…(cv2.LINE_4, cv2.LINE_8, cv2.LINE_AA)

 

 

 

"์‚ฌ๊ฐํ˜•"์€ "rectangle"์„ ์‚ฌ์šฉํ•œ๋‹ค.

import cv2
import numpy as np

background = np.ones((512,512,3),dtype=np.uint8) * 255 # BGR ์ฑ„๋„

# ์‚ฌ๊ฐํ˜•
cv2.rectangle(
    img=background,
    pt1 = (50,50),
    pt2 = (250,250),
    color=(255,0,0),
    thickness=1,
    lineType=cv2.LINE_AA,
    shift=0
)
cv2.imshow("rectangle",background)

cv2.waitKey(0)
cv2.destroyAllWindows()

 

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

  • img : ๊ทธ๋ฆด np.array
  • pt1 : ์‹œ์ž‘ ์ขŒํ‘œ
  • pt2 : ๋ ์ขŒํ‘œ
  • color : ์ƒ‰
  • thickness : ์„  ๊ตต๊ธฐ
  • lineType : ์„  ํƒ€์ž…(cv2.LINE_4, cv2.LINE_8, cv2.LINE_AA)
  • shift : ๊ทธ๋ฆฌ๊ธฐ ์ขŒํ‘œ๊ฐ’์˜ ์ถ•์†Œ ๋น„์œจ

 

 

"์›"์€ "circle"์„ ์‚ฌ์šฉํ•œ๋‹ค.

import cv2
import numpy as np

background = np.ones((512,512,3),dtype=np.uint8) * 255 # RGB ์ฑ„๋„

cv2.circle(
    img=background,
    center=(256,256),
    radius=50,
    color=(0,255,0),
    thickness=3,
    lineType=cv2.LINE_8,
    shift=0
)
cv2.imshow("circle",background)
cv2.waitKey(0)
cv2.destroyAllWindows()

 

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

  • img : ๊ทธ๋ฆด np.array
  • center : ์ค‘์‹ฌ์  ์ขŒํ‘œ
  • radius : ๋ฐ˜์ง€๋ฆ„
  • color : ์ƒ‰
  • thickness : ์„  ๊ตต๊ธฐ
  • lineType : ์„  ํƒ€์ž…(cv2.LINE_4, cv2.LINE_8, cv2.LINE_AA)
  • shift : ๊ทธ๋ฆฌ๊ธฐ ์ขŒํ‘œ๊ฐ’์˜ ์ถ•์†Œ ๋น„์œจ

 

 

"ํƒ€์›"์€ "ellipse"๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค.

import cv2
import numpy as np

background = np.ones((512,512,3),dtype=np.uint8) * 255 # RGB ์ฑ„๋„

cv2.ellipse(
    img=background,
    center=(256,256),
    axes=(170,100),
    angle=0,
    startAngle=0,
    endAngle=360,
    color=(0,255,100),
    thickness=3,
    lineType=cv2.LINE_AA,
    shift=0
)
cv2.imshow("ellipse",background)

cv2.waitKey(0)
cv2.destroyAllWindows()

 

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

  • img : ๊ทธ๋ฆด np.array
  • center : ์ค‘์‹ฌ์  ์ขŒํ‘œ
  • axes : (x๋ฐ˜์ง€๋ฆ„, y๋ฐ˜์ง€๋ฆ„)
  • angle : ํƒ€์› ํšŒ์ „ ๊ฐ๋„(x์ถ• ๊ธฐ์ค€, ์‹œ๊ณ„ ๋ฐฉํ–ฅ)
  • startAngle : ํƒ€์› ํ˜ธ์˜ ์‹œ์ž‘ ๊ฐ๋„(x์ถ•๊ธฐ์ค€, ์‹œ๊ณ„๋ฐฉํ–ฅ)
  • endAngle : ํƒ€์› ํ˜ธ์˜ ๋ ๊ฐ๋„(y์ถ• ๊ธฐ์ค€, ์‹œ๊ณ„ ๋ฐฉํ–ฅ)
  • color : ์ƒ‰
  • thickness : ์„  ๊ตต๊ธฐ
  • lineType : ์„  ํƒ€์ž…(cv2.LINE_4, cv2.LINE_8, cv2.LINE_AA)
  • shift : ๊ทธ๋ฆฌ๊ธฐ ์ขŒํ‘œ๊ฐ’์˜ ์ถ•์†Œ ๋น„์œจ

 

 

"๋‹ค๊ฐํ˜•"์„ ๊ทธ๋ฆด๋•Œ๋Š” "polylines"๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค.

import cv2
import numpy as np

background = np.zeros((512,512,3),dtype=np.uint8) # RGB ์ฑ„๋„
points = np.array([[[10,10],[170,10],[200,230],[70,70],[50,150]]],dtype=np.int32)

cv2.polylines(
    img=background,
    pts=points,
    isClosed=True,
    color=(255,255,0),
    thickness=2,
    lineType=cv2.LINE_AA,
    shift=0
)
cv2.imshow("polylines",background)
cv2.waitKey(0)
cv2.destroyAllWindows()

 

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

  • img : ๊ทธ๋ฆด np.array
  • pts : ํด๋ฆฌ๊ณค ์ขŒํ‘œ ๋ฐฐ์—ด
  • isClosed : True ์‹œ ๋‹ซํžŒ ๋„ํ˜•
  • color : ์ƒ‰
  • thickness : ์„  ๊ตต๊ธฐ
  • lineType : ์„  ํƒ€์ž…(cv2.LINE_4, cv2.LINE_8, cv2.LINE_AA)
  • shift : ๊ทธ๋ฆฌ๊ธฐ ์ขŒํ‘œ๊ฐ’์˜ ์ถ•์†Œ ๋น„์œจ

 

"๋ณผ๋ก ๋‹ค๊ฐํ˜•"์„ ๊ทธ๋ฆด๋•Œ๋Š” "fillConvexPoly"๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค.

import cv2
import numpy as np

background = np.zeros((512,512,3),dtype=np.uint8) # RGB ์ฑ„๋„

points = np.array([[[10,10],[170,10],[200,230],[70,70],[50,150]]],dtype=np.int32)

# ๋ณผ๋ก ๋‹ค๊ฐํ˜•
cv2.fillConvexPoly(
    img=background,
    points=points,
    color=(102,100,24),
    lineType=cv2.LINE_8,
    shift=0
)
cv2.imshow("fill Poly",background)

cv2.waitKey(0)
cv2.destroyAllWindows()

 

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

  • img : ๊ทธ๋ฆด np.array
  • points : ํด๋ฆฌ๊ณค ์ขŒํ‘œ ๋ฐฐ์—ด
  • axes : (x๋ฐ˜์ง€๋ฆ„, y๋ฐ˜์ง€๋ฆ„)
  • color : ์ƒ‰
  • lineType : ์„  ํƒ€์ž…(cv2.LINE_4, cv2.LINE_8, cv2.LINE_AA)
  • shift : ๊ทธ๋ฆฌ๊ธฐ ์ขŒํ‘œ๊ฐ’์˜ ์ถ•์†Œ ๋น„์œจ

 

 

728x90
๋ฐ˜์‘ํ˜•