Skip to content

Instantly share code, notes, and snippets.

@heliy
Created May 6, 2015 02:12
Show Gist options
  • Save heliy/9e8d026ee279f9412d3d to your computer and use it in GitHub Desktop.
Save heliy/9e8d026ee279f9412d3d to your computer and use it in GitHub Desktop.
Exercises --------- -# Try to create the logo of OpenCV using drawing functions available in OpenCV.
import numpy as np
import cv2 #3.0.0-dev
import math
r1 = 70
r2 = 30
ang = 60
d = 170
h = int(d/2*math.sqrt(3))
dot_red = (256,128)
dot_green = (dot_red[0]-d/2, dot_red[1]+h)
dot_blue = (dot_red[0]+d/2, dot_red[1]+h)
# tan = float(dot_red[0]-dot_green[0])/(dot_green[1]-dot_red[0])
# ang = math.atan(tan)/math.pi*180
red = (0, 0, 255)
green = (0, 255, 0)
blue = (255, 0, 0)
black = (0, 0, 0)
full = -1
img = np.zeros((512, 512, 3), np.uint8)
cv2.circle(img, dot_red, r1, red, full)
cv2.circle(img, dot_green, r1, green, full)
cv2.circle(img, dot_blue, r1, blue, full)
cv2.circle(img, dot_red, r2, black, full)
cv2.circle(img, dot_green, r2, black, full)
cv2.circle(img, dot_blue, r2, black, full)
cv2.ellipse(img, dot_red, (r1, r1), ang, 0, ang, black, full)
cv2.ellipse(img, dot_green, (r1, r1), 360-ang, 0, ang, black, full)
cv2.ellipse(img, dot_blue, (r1, r1), 360-2*ang, ang, 0, black, full)
cv2.imwrite("opencv_logo.png", img)
@masanatu
Copy link

Hello! I am new in github and do not know how to use it proficiently. So if there is any formatting or procedural mistake in my post please notice me. Anyway, i was following the tutorial series from OpenCV and tried to do this exercise. Below is my code where i roughly estimated the (x,y) coordinates in the logo, so not expected to be correct:

import numpy as np
import cv2
img = np.ones((256,256,3),np.float32)
font = cv2.FONT_HERSHEY_SIMPLEX
cv2.putText(img,'OpenCV',(32,240), font, 1.65, (0,0,0), 5, cv2.LINE_AA)
cv2.circle(img,(127,44),45,(0,0,255),-1)
cv2.circle(img,(127,44),15,(255,255,255),-1)
cv2.circle(img,(74,134),45,(0,255,0),-1)
cv2.circle(img,(74,134),15,(255,255,255),-1)
pts = np.array([[127,44],[74,134],[179,134]],np.int32)
cv2.fillPoly(img,[pts],(255,255,255))
cv2.circle(img,(179,134),45,(255,0,0),-1)
cv2.circle(img,(179,134),15,(255,255,255),-1)
pts2 = np.array([[179,134],[153,89],[205,89]])
cv2.fillPoly(img, [pts2],(255,255,255))
cv2.imshow('image',img)
cv2.imwrite('logotry.jpg',img)
cv2.waitKey(0)
cv2.destroyAllWindows()

When i run the code the image box seems to be fine however, the saved file in the directory is somewhat weird. I could not see the reason for that. I will appreciate if you can figure it out.

@hungIT1986
Copy link

Hello Heliy!
i try to write your code opencvlogo.py. but i get mistake like this: TypeError: integer argument expected, got float
Please tell me what it is! Thanks a lot

@glen1995
Copy link

thats because the function only accepts interger arguement you just have to modify those 14 and 15 lines as
dot_green = (round(dot_red[0]-d/2), round(dot_red[1]+h))
dot_blue = (round(dot_red[0]+d/2), round(dot_red[1]+h))
Round function rounds the value into integer and the this code runs

@siddarthjha
Copy link

Hello Heliy!
i try to write your code opencvlogo.py. but i get mistake like this: TypeError: integer argument expected, got float
Please tell me what it is! Thanks a lot

dot_green = (dot_red[0]-d//2, dot_red[1]+h)
dot_blue = (dot_red[0]+d//2, dot_red[1]+h)

Modify it with this it will execute

@xjohnxjohn
Copy link

@siddarthjha 'Cause it need integer, then you can modify from this code:
dot_green = (int(dot_red[0]-d/2),int(dot_red[1]+h))
dot_blue = (int(dot_red[0]+d/2), int(dot_red[1]+h))

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment