Skip to content

Instantly share code, notes, and snippets.

@aminsaedi
Created September 8, 2017 21:49
Show Gist options
  • Save aminsaedi/1a3a64f0f6263a4358afe71efc561c56 to your computer and use it in GitHub Desktop.
Save aminsaedi/1a3a64f0f6263a4358afe71efc561c56 to your computer and use it in GitHub Desktop.
angle in opencv
<?xml version="1.0" encoding="UTF-8"?>
<module type="PYTHON_MODULE" version="4">
<component name="NewModuleRootManager">
<content url="file://$MODULE_DIR$" />
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
<component name="TestRunnerService">
<option name="PROJECT_TEST_RUNNER" value="Unittests" />
</component>
</module>
<component name="InspectionProjectProfileManager">
<settings>
<option name="useProjectProfile" value="false" />
<option name="USE_PROJECT_PROFILE" value="false" />
<version value="1.0" />
</settings>
</component>
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectRootManager" version="2" project-jdk-name="Python 2.7.12 virtualenv at ~/.virtualenvs/cv" project-jdk-type="Python SDK" />
</project>
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectModuleManager">
<modules>
<module fileurl="file://$PROJECT_DIR$/.idea/angle.iml" filepath="$PROJECT_DIR$/.idea/angle.iml" />
</modules>
</component>
</project>
import cv2
import numpy as np
import math
a=1
class ImageCallback:
def __init__(self):
self.image = None
def update(self, image):
self.image = image
def is_clicked(self):
return len(self.hues) > 0
def mouse_callback(self, event, x, y, m, n):
if self.image is not None:
if event == cv2.EVENT_LBUTTONDOWN:
print x, y
def calculate_angle(point_a, point_b):
dis_y = point_a[1] - point_b[1]
dis_x = point_a[0] - point_b[0]
angle = math.atan2(dis_y, dis_x)
angle = math.degrees(angle)
# lets reverse axis because (0,0) from image
# is not equal to real (0,0) in math
angle *= -1
if angle < 0:
angle += 360
return int(angle)
def callback(event, x, y, flags, param):
param[0] = x
param[1] = y
xypos = ImageCallback()
width = 1000
height = 1000
radius = 10
cap = cv2.VideoCapture(0)
target = [width / 2, height / 2]
center = (100, 100)
original = np.zeros((height, width, 3), np.uint8)
cv2.namedWindow("image", cv2.WINDOW_NORMAL)
cv2.circle(original, center, radius, (200, 100, 100), 2)
while True:
image = original.copy()
_, image = cap.read()
print "click on center of object"
cv2.setMouseCallback("image", xypos.mouse_callback)
xypos.update(image)
for i in range(90000):
print "geting pos"
cv2.imshow("image", image)
cv2.setMouseCallback("image", callback, param=target)
cv2.setMouseCallback("image", callback, param=target)
cv2.circle(image, tuple(target), radius, (50, 100, 0), 10)
cv2.line(image, tuple(target), center, (0, 0, 200), 2)
print calculate_angle(target, center)
#xypos.update(image)
cv2.imshow("image", image)
key = cv2.waitKey(1) & 0xFF
if key == 27:
break
cv2.destroyAllWindows()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment