Skip to content

Instantly share code, notes, and snippets.

@garybradski
Last active April 28, 2020 23:02
Show Gist options
  • Save garybradski/88ea1da4cdc1a1a8c6c2fedd8c0dfc95 to your computer and use it in GitHub Desktop.
Save garybradski/88ea1da4cdc1a1a8c6c2fedd8c0dfc95 to your computer and use it in GitHub Desktop.
list of [x,y] coordinates for a circle offset that can then be offset to a given point in an image and samples
import numpy as np
import cv2
import copy
from copy import deepcopy
def offset_pts_for_circle(radius,db=False):
'''
Create offset coordinates for a circle around a point. The order of the resulting points will be
counter-clockwise
:param radius: Radius of circle
:param db: Debug output True or False? (default is False)
:return: List of [x,y] offsets that will make a circle if added to a central point
'''
radius = int(np.abs(radius))
if radius < 2:
radius = 2
h = radius*2 + 10 #Just give it a padding
w = radius*2 + 10
img = np.zeros((h, w), np.uint8) #create scratch pad
cv2.circle(img, (radius + 5,radius + 5), radius, (255,255,255),-1)
contour, _ = cv2.findContours(img, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)
contour = np.vstack(contour).squeeze() #contour is a list of lists of points, cat them and squeeze out extra dimensions
if db:
peri = cv2.arcLength(contour,True)
print("len(contour)={}, len(peri) = {}".format(len(contour),peri))
cv2.drawContours(img, [contour], 0, (150, 150, 150), 1)
cnts = deepcopy(contour)
len_cnts = int(len(cnts)/4)
cnt = 0
for pt in cnts: # Draw a quarter circle
img[pt[1],pt[0]] = 100
if cnt >= len_cnts - 1:
break
cnt += 1
cv2.imshow('image',img)
cv2.waitKey()
contour -= [radius + 5,radius + 5]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment