절단되는 좌표점(접점) 계산
import cv2
import numpy as np
img = np.zeros(shape=(512,512,3), dtype=np.uint8) + 255
x1, x2 = 100, 400
y1, y2 = 100, 400
cv2.rectangle(img, (x1, y1), (x2, y2), (0, 0, 255))
pt1 = 120, 50
pt2 = 300, 500
cv2.line(img, pt1, pt2, (255,0,0), 2)
imgRect = (x1, y1, x2-x1, y2-y1)
retval, rpt1, rpt2 = cv2.clipLine(imgRect, pt1, pt2)
if retval:
cv2.circle(img, rpt1, radius=5, color=(0, 255, 0), thickness=-1)
cv2.circle(img, rpt2, radius=5, color=(0, 255, 0), thickness=-1)
cv2.imshow('img', img)
cv2.waitKey()
cv2.destroyAllWindows()
사각형의 꼭지점 좌표 구하기
import cv2
import numpy as np
img = np.zeros(shape=(512,512,3), dtype=np.uint8) + 255
x, y = 256, 256
size = 200
for angle in range(0, 90, 10):
rect = ((256, 256), (size, size), angle)
box = cv2.boxPoints(rect).astype(np.int32)
r = np.random.randint(256)
g = np.random.randint(256)
b = np.random.randint(256)
cv2.polylines(img, [box], True, (r, g, b), 2)
cv2.imshow('img', img)
cv2.waitKey()
cv2.destroyAllWindows()
타원 위 좌표 계산
- delta의 각도를 줄이면 줄일수록 원에 붙는 모양이 됨
import cv2
import numpy as np
img = np.zeros(shape=(512,512,3), dtype=np.uint8) + 255
ptCenter = img.shape[0]//2, img.shape[1]//2
size = 200, 100
cv2.ellipse(img, ptCenter, size, 0, 0, 360, (255, 0, 0))
pts1 = cv2.ellipse2Poly(ptCenter, size, 0, 0, 360, delta=45)
cv2.ellipse(img, ptCenter, size, 45, 0, 360, (255, 0, 0))
pts2 = cv2.ellipse2Poly(ptCenter, size, 45, 0, 360, delta=45)
cv2.polylines(img, [pts1, pts2], isClosed=True, color=(0, 0, 255))
cv2.imshow('img', img)
cv2.waitKey()
cv2.destroyAllWindows()