Skip to content

Instantly share code, notes, and snippets.

@ritwikraha
Last active June 22, 2019 19:28
Show Gist options
  • Save ritwikraha/20cd39e0e613ca14ac5797057cbddc36 to your computer and use it in GitHub Desktop.
Save ritwikraha/20cd39e0e613ca14ac5797057cbddc36 to your computer and use it in GitHub Desktop.
code snippet to create polygon from camera point
import cv2
import numpy as np
clickPt = []
def clickDetect(event, x, y, flags, param):
global clickPt
if event == cv2.EVENT_LBUTTONDOWN:
clickPt = [(x, y)]
elif event == cv2.EVENT_LBUTTONUP:
# cv2.circle(image, clickPt[0], 5, (0,0,255), -1)
clickPt.append((x, y))
x=clickPt[0][0]
y=clickPt[0][1]
x1=clickPt[1][0]
y1=clickPt[1][1]
r=np.sqrt((x-x1)**2-(y-y1)**2)
cv2.rectangle(img, (x+r, y), (x1-r, y1+r), (255, 255, 00), 2)
cv2.imshow("image", image)
print(clickPt) # this prints the two edge points of the line drawn
coeffs = np.polyfit(clickPt[0], clickPt[1], 1)
print(coeffs) # prints the polynomial (1 degree here) coefficients
print(f"y = \b{np.poly1d(coeffs)}") # prints the equation
image = cv2.imread('floorplan.jpeg')
cv2.namedWindow("image")
cv2.setMouseCallback("image", clickDetect)
while True:
cv2.imshow("image", image)
key = cv2.waitKey(1) & 0xFF
if key == ord("q"):
break
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment