Advertisement
Guest User

Untitled

a guest
May 4th, 2017
542
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.90 KB | None | 0 0
  1. import cv2
  2. import numpy as np
  3. import math
  4.  
  5. def findtraingle(tri):
  6. a=tri[0][0]
  7. b=tri[1][0]
  8. c=tri[2][0]
  9. line_ab=math.sqrt(((a[0]-b[0])**2)+((a[1]-b[1])**2))
  10. line_bc=math.sqrt(((c[0]-b[0])**2)+((c[1]-b[1])**2))
  11. line_ca=math.sqrt(((a[0]-c[0])**2)+((a[1]-c[1])**2))
  12. maxline=max(line_ab,line_bc,line_ca)
  13. if maxline==line_ab:
  14. ml1=a
  15. ml2=b
  16. halfmaxline=(a+b)/2
  17. peakpoint=c
  18. elif maxline==line_bc:
  19. ml1=b
  20. ml2=c
  21. halfmaxline=(c+b)/2
  22. peakpoint=a
  23. elif maxline==line_ca:
  24. ml1=c
  25. ml2=a
  26. halfmaxline=(c+a)/2
  27. peakpoint=b
  28. x1=ml1[0]
  29. y1=ml1[1]
  30. x2=ml2[0]
  31. y2=ml2[1]
  32. slope=(halfmaxline[0]-peakpoint[0])/(halfmaxline[1]-peakpoint[1])
  33. alpha=math.atan(slope)
  34. alpha=math.degrees(alpha)
  35. if peakpoint[1] > halfmaxline[1]:
  36. if peakpoint[0] < halfmaxline[0]:
  37. alpha=180+alpha
  38. elif peakpoint[0] > halfmaxline[0]:
  39. alpha=-(180-alpha)
  40.  
  41. print("peakpoint[0]:{} halflinemax[0]:{}".format(peakpoint[0],halfmaxline[0]))
  42. return int(alpha),x1,y1,x2,y2,halfmaxline
  43.  
  44.  
  45. def threshbycolor(blur,lower,upper,name):
  46. if(name=="red"):
  47. thresh=cv2.inRange(blur,lower,upper)
  48.  
  49. thresh=cv2.inRange(blur,lower,upper)
  50.  
  51. image, contours, hierarchy = cv2.findContours(thresh,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)
  52.  
  53.  
  54. cnt = cv2.cvtColor(image, cv2.COLOR_GRAY2BGR)
  55.  
  56. cv2.drawContours(cnt, contours, -1, (0, 255, 0), 2)
  57.  
  58. cv2.imshow(str(name), cnt)
  59. cv2.imwrite(str(name)+".jpg",cnt)
  60. point=0
  61. for i in contours:
  62.  
  63. M = cv2.moments(i)
  64. rect = cv2.minAreaRect(i)
  65. w,h=rect[1]
  66. angle = int(rect[2])
  67. theta=abs(angle)
  68. if abs(theta) > 180:
  69. theta = abs(theta)-180
  70. if w>h:
  71. theta=90+abs(theta)
  72. elif h>w:
  73. theta=theta
  74.  
  75.  
  76. approx = cv2.approxPolyDP(i,0.05*cv2.arcLength(i,True),True) ####epsilo$
  77. if len(approx)==3:
  78. shape=("Triangle")
  79. point=1
  80. (cen,tri) = cv2.minEnclosingTriangle(i)
  81.  
  82. if cv2.contourArea(i)>1000:
  83. try:
  84. alpha,x1,y1,x2,y2,halfmaxline=findtraingle(tri)
  85. cv2.circle(copyframe,(tri[0][0][0],tri[0][0][1]), 5, (0,0,255), -1)
  86. cv2.circle(copyframe,(tri[1][0][0],tri[1][0][1]), 5, (0,255,255), -1)
  87. cv2.circle(copyframe,(tri[2][0][0],tri[2][0][1]), 5, (0,255,0), -1)
  88. cv2.drawContours(copyframe,[i],0,255,2)
  89. cv2.line(copyframe,(x1,y1),(x2,y2),(255,255,255),3)
  90. cv2.circle(copyframe,(halfmaxline[0],halfmaxline[1]), 5, (0,0,0), -1)
  91. except:
  92. pass
  93. elif len(approx)==4:
  94. shape=("Rectangle")
  95. point=1
  96. if cv2.contourArea(i)>1000:
  97. cv2.drawContours(copyframe,[i],0,255,2)
  98.  
  99. M=cv2.moments(i)
  100. if (M['m00'] != 0):
  101.  
  102. cx = int(M['m10'] / M['m00'])
  103.  
  104. cy = int(M['m01'] / M['m00'])
  105.  
  106. if point == 1 and cv2.contourArea(i)>1000:
  107. point == 0
  108. try:
  109. if shape=="Rectangle":
  110. cv2.putText(copyframe, shape+str(len(approx))+" "+str(cx)+","+str(cy)+name+','+str(theta), (cx - 30, cy - 20),cv2.FONT_HERSHEY_SIMPLEX, 0.5, (255, 255, 255), 1)
  111. elif shape=="Triangle":
  112. cv2.putText(copyframe, shape+str(len(approx))+" "+str(cx)+","+str(cy)+name+','+str(alpha), (cx - 30, cy - 20),cv2.FONT_HERSHEY_SIMPLEX, 0.5, (255, 255, 255), 1)
  113. except:
  114. pass
  115.  
  116. cap = cv2.VideoCapture(1)
  117.  
  118.  
  119. lower_red = np.array([131,92,141]) #red
  120. upper_red = np.array([179,255,255])
  121.  
  122. lower_green = np.array([50,38,169]) #green
  123. upper_green = np.array([82,255,255])
  124.  
  125. lower_yellow = np.array([18,32,214]) #yellow
  126. upper_yellow = np.array([31,255,255])
  127.  
  128. lower_blue = np.array([91,93,142]) #blue
  129. upper_blue = np.array([105,255,255])
  130.  
  131. lower_orange = np.array([3,67,213]) #orange
  132. upper_orange = np.array([19,255,255])
  133.  
  134. lower_violet = np.array([110,70,171]) #violet
  135. upper_violet = np.array([121,255,255])
  136.  
  137. while(True):
  138. ret, frame = cap.read()
  139. copyframe=frame
  140. gray = cv2.cvtColor(copyframe, cv2.COLOR_BGR2HSV)
  141. blurred=cv2.GaussianBlur(gray,(5,5),0)
  142. threshbycolor(blurred,lower_red,upper_red,"red")
  143. threshbycolor(blurred,lower_yellow,upper_yellow,"yellow")
  144. threshbycolor(blurred,lower_green,upper_green,"green")
  145. threshbycolor(blurred,lower_blue,upper_blue,"blue")
  146. threshbycolor(blurred,lower_orange,upper_orange,"orange")
  147.  
  148. cv2.imshow("out",copyframe)
  149. cv2.imwrite("out.jpg",copyframe)
  150. if cv2.waitKey(1) & 0xFF == ord('q'):
  151. break
  152.  
  153. cap.release()
  154. cv2.destroyAllWindows()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement