[참고]

 

1. 원, 직선 그리기

 

[코드]

import cv2
import numpy as np 

img = np.zeros((500,500), np.uint8)
cv2.circle(img, (200,200), 50, 255, 3)
cv2.line(img, (100, 400), (400, 350), 255, 3)

cv2.imshow('img', img)
cv2.waitKey()
cv2.destroyAllWindows()

[결과]

원,선 그리기

2. 직선 좌표 구하기

import cv2
import numpy as np 

img = np.zeros((500,500), np.uint8)

cv2.line(img, (100, 400), (400, 350), 255, 3)

lines = cv2.HoughLinesP(img, 1, np.pi/180, 100, 100, 10)[0]

dbg_img = np.zeros((img.shape[0], img.shape[1], 3), np.uint8)
for x1, y1, x2, y2 in lines:
    print('Detected line: ({} {}) ({} {})'.format(x1,y1,x2,y2))
    cv2.line(dbg_img, (x1,y1), (x2,y2), (0,255,0),2)

cv2.imshow('img', img)
cv2.waitKey()
cv2.destroyAllWindows()

[결과]

 

 Detected line: (100 402) (401 349)

 

3. 원 중심, 반지름(raidus) 구하기

 

[코드]

import cv2
import numpy as np 

img = np.zeros((500,500), np.uint8)
cv2.circle(img, (200,200), 50, 255, 3)

circles = cv2.HoughCircles(img, cv2.HOUGH_GRADIENT, 1, 15, param1 = 200, param2 = 30)[0]

dbg_img = np.zeros((img.shape[0], img.shape[1], 3), np.uint8)

for c in circles:
    print('Detected circle: center = ({} {}), radius = {}'.format(c[0],c[1],c[2]))
    cv2.circle(dbg_img, (c[0], c[1]), c[2], (0,255,0), 2)

cv2.imshow('img', img)
cv2.waitKey()
cv2.destroyAllWindows()

[결과]

 

Detected circle: center = (199.5 199.5), radius = 47.29999923706055

 

 

4. 이미지로부터 원의 중심점 찾기

 

[코드]

import cv2
import numpy as np 

img = cv2.imread('do.jpg')
img = cv2.medianBlur(img, 5)
img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

circles = cv2.HoughCircles(img, cv2.HOUGH_GRADIENT, 1, 15, param1 = 200, param2 = 30)[0]

dbg_img = np.zeros((img.shape[0], img.shape[1], 3), np.uint8)

for c in circles:
    print('Detected circle: center = ({} {}), radius = {}'.format(c[0],c[1],c[2]))
    cv2.circle(dbg_img, (c[0], c[1]), c[2], (0,255,0), 2)

cv2.imshow('img', img)
cv2.imshow('dbg_img', dbg_img)
cv2.waitKey()
cv2.destroyAllWindows()

 

[결과]

Detected circle: center = (319.5 319.5), radius = 57.400001525878906
Detected circle: center = (212.5 55.5), radius = 49.599998474121094

 

 

+ Recent posts