Advertisement
Guest User

Untitled

a guest
May 23rd, 2015
226
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 14.00 KB | None | 0 0
  1. # import numpy as np
  2. # import cv2
  3.  
  4. # capture = cv2.VideoCapture(0)
  5. # #capture.open()
  6.  
  7. # while(True):
  8. # ret, frame = capture.read()
  9.  
  10. # gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
  11.  
  12. # cv2.imshow('frame', gray)
  13.  
  14. # if cv2.waitKey() & 0xFF == ord('q'):
  15. # break
  16.  
  17. # capture.release()
  18. # cv2.destroyAllWindows()
  19.  
  20. # /////////////////////////////////////////////////////////////////
  21.  
  22. # 4 Cameras, trippy...
  23.  
  24. # import numpy as np
  25. # import cv2
  26.  
  27. # cap = cv2.VideoCapture(0)
  28.  
  29. # print cap
  30. # print(cap.get(3))
  31. # print(cap.get(4))
  32. # # if cv2.isOpened
  33. # print("Hello World")
  34.  
  35. # while(True):
  36. # # Capture frame-by-frame
  37. # ret, frame = cap.read()
  38.  
  39. # # Our operations on the frame come here
  40. # gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
  41.  
  42. # # Uninvert camera image (flip image over Y-Axis)
  43. # flipped_gray = cv2.flip(gray,1)
  44.  
  45. # gray_x = cv2.flip(gray,0)
  46. # flipped_gray_x = cv2.flip(flipped_gray,0)
  47.  
  48. # # Display the resulting frame
  49. # cv2.imshow('frame1',flipped_gray)
  50. # cv2.imshow('frame2',gray)
  51.  
  52. # cv2.imshow('frame3',flipped_gray_x)
  53. # cv2.imshow('frame4',gray_x)
  54.  
  55. # if cv2.waitKey(1) & 0xFF == ord('q'):
  56. # break
  57.  
  58. # # When everything done, release the capture
  59. # cap.release()
  60. # cv2.destroyAllWindows()
  61.  
  62. # /////////////////////////////////////////////////////////////////
  63.  
  64. # import cv2
  65.  
  66. # cv2.namedWindow("preview")
  67. # vc = cv2.VideoCapture(0)
  68.  
  69. # if vc.isOpened(): # try to get the first frame
  70. # rval, frame = vc.read()
  71. # else:
  72. # rval = False
  73.  
  74. # while rval:
  75. # cv2.imshow("preview", frame)
  76. # rval, frame = vc.read()
  77. # key = cv2.waitKey(20000)
  78. # if key == 27: # exit on ESC
  79. # break
  80. # cv2.destroyWindow("preview")
  81.  
  82. # /////////////////////////////////////////////////////////////////
  83. # # Stick Figure
  84. # import numpy as np
  85. # import cv2
  86.  
  87. # # Load a color image in grayscale
  88. # img = cv2.imread('C:\Users\Mark\Desktop\doge.jpg',4)
  89.  
  90.  
  91. # # img = np.zeros((512,512,4), np.uint8)
  92. # # The 4 comes from the number of channels
  93. # # Can be 1 - Grayscale
  94. # # 3 - Color (RGB)
  95. # # 4 - Color with Alpha (RGBAlpha)
  96.  
  97. # # Draw a diagonal blue line with thickness of 5 px
  98. # cv2.line(img,(40, 425),(120,325),(255,0,0),5) #left leg
  99.  
  100. # cv2.line(img,(200, 425),(120,325),(255,0,0),5) #right leg
  101.  
  102. # cv2.line(img,(120, 150),(120,325),(255,0,0),5) #torso
  103.  
  104. # cv2.line(img,(50, 200),(190,200),(255,0,0),5) #arms
  105.  
  106. # cv2.circle(img,(120,100), 50, (255,0,0), 5) #head
  107. # # (x Center Coord, y Center Coord), radius, (Red Color, Green Color, Blue Color), Thickness of Circle
  108.  
  109. # cv2.imshow('image',img)
  110. # cv2.waitKey(0)
  111. # cv2.destroyAllWindows()
  112.  
  113. # # //////////////////////////////////////////////////////////////////
  114.  
  115. # import numpy as np
  116. # import cv2
  117.  
  118. # cap = cv2.VideoCapture(0)
  119.  
  120. # while(True):
  121. # # Capture frame-by-frame
  122. # ret, frame = cap.read()
  123.  
  124. # # Our operations on the frame come here
  125. # gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
  126.  
  127. # # Display the resulting frame
  128.  
  129. # # cv2.line(frame,(40, 425),(120,325),(50,50,50),50) #left leg
  130.  
  131. # # cv2.line(frame,(200, 425),(120,325),(255,0,0),5) #right leg
  132.  
  133. # # cv2.line(frame,(120, 150),(120,325),(255,0,0),5) #torso
  134.  
  135. # # cv2.line(frame,(50, 200),(190,200),(255,0,0),5) #arms
  136.  
  137. # cv2.circle(frame,(120,100), 50, (255,0,0), 5) #head
  138. # # (x Center Coord, y Center Coord), radius, (Red Color, Green Color, Blue Color), Thickness of Circle
  139.  
  140. # cv2.imshow('frame',frame)
  141.  
  142. # if cv2.waitKey(1) & 0xFF == ord('q'):
  143. # break
  144.  
  145. # # When everything done, release the capture
  146. # cap.release()
  147. # cv2.destroyAllWindows()
  148.  
  149. # # //////////////////////////////////////////////////////////////////
  150.  
  151. # import cv2
  152. # import numpy as np
  153.  
  154. # img = cv2.imread('matchshapes.jpg',0)
  155. # img = cv2.medianBlur(img,5)
  156. # cimg = cv2.cvtColor(img,cv2.COLOR_GRAY2BGR)
  157.  
  158. # circles = cv2.HoughCircles(img,cv2.CV_HOUGH_GRADIENT,1,20,
  159. # param1=50,param2=30,minRadius=0,maxRadius=0)
  160.  
  161. # circles = np.uint16(np.around(circles))
  162. # for i in circles[0,:]:
  163. # # draw the outer circle
  164. # cv2.circle(cimg,(i[0],i[1]),i[2],(0,255,0),2)
  165. # # draw the center of the circle
  166. # cv2.circle(cimg,(i[0],i[1]),2,(0,0,255),3)
  167.  
  168. # cv2.imshow('detected circles',cimg)
  169. # cv2.waitKey(0)
  170. # cv2.destroyAllWindows()
  171.  
  172. # //////////////////////////////////////////////////////////////////
  173.  
  174. # Draw line between two furthest points (on the x axis), distance on y axis is ignored
  175.  
  176. # import cv2
  177. # import numpy as np
  178.  
  179. # cap = cv2.VideoCapture(0)
  180.  
  181. # while(True):
  182. # # Load from feed, frame by frame
  183. # _, raw = cap.read()
  184.  
  185. # # Uninvert camera image (flip image over Y-Axis)
  186. # raw = cv2.flip(raw,1)
  187.  
  188. # # Convert BGR to HSV
  189. # hsv = cv2.cvtColor(raw, cv2.COLOR_BGR2HSV)
  190.  
  191. # # Extract Color from hsv image
  192. # # RED: Lower = (160,50,50), Upper = (179,255,255)
  193. # # BLUE: Lower = (110,100,100), Upper = (130,255,255) - Works great with dark blues, doesn't detect jeans
  194. # lower_bound = (110,100,100)
  195. # upper_bound = (130,255,255)
  196. # mask = cv2.inRange(hsv, lower_bound, upper_bound)
  197.  
  198. # # Eliminate noise
  199. # # Erode:
  200. # # kernel notes:
  201. # # (Erode away from top and bottom, Erode away from sides)
  202. # kernel = np.ones((3,3),np.uint8)
  203. # mask_eroded = cv2.erode(mask, kernel, iterations = 1)
  204.  
  205. # # Dilate
  206. # mask_dilated = cv2.dilate(mask_eroded, kernel, iterations = 1)
  207.  
  208. # # Find Contours
  209. # ret, thresh = cv2.threshold(mask_dilated, 127,255,0)
  210. # contours, hierarchy = cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
  211.  
  212. # # Prevent intersections of contour lines and manually drawn lines
  213.  
  214. # # print(contours)
  215. # # print ('\n')
  216. # if(len(contours) > 0):
  217. # cnt = contours[0]
  218. # rightmost = tuple(cnt[cnt[:,:,0].argmax()][0])
  219. # leftmost = tuple(cnt[cnt[:,:,0].argmin()][0])
  220. # maxright = rightmost
  221. # maxleft = leftmost
  222. # for i in range(len(contours)):
  223. # cnt = contours[i]
  224. # rightmost = tuple(cnt[cnt[:,:,0].argmax()][0])
  225. # leftmost = tuple(cnt[cnt[:,:,0].argmin()][0])
  226. # # print(rightmost[0])
  227. # print(rightmost[1])
  228. # if(rightmost[0] > maxright[0]):
  229. # maxright = rightmost
  230. # if(leftmost[0] < maxleft[0]):
  231. # maxleft = leftmost
  232.  
  233. # cv2.line(raw, maxleft, maxright, (0,0,255), 5)
  234. # else:
  235. # print("No contours found")
  236.  
  237. # # Find & print center coordinates
  238.  
  239. # # M = cv2.moments(cnt)
  240. # # centroid_x = int(M['m10']/M['m00'])
  241. # # centroid_y = int(M['m01']/M['m00'])
  242. # # print("X: ")
  243. # # print(centroid_x)
  244. # # print("Y: ")
  245. # # print(centroid_y)
  246.  
  247. # # Draw circle at center coordinates
  248. # # cv2.circle(raw,(centroid_x,centroid_y), 50, (255,0,0), -1)
  249.  
  250. # # Draw Contours
  251.  
  252. # cv2.drawContours(raw,contours,-1,(0,255,0),3)
  253.  
  254. # # Display the frame(s)
  255. # cv2.imshow('raw', raw)
  256. # # cv2.imshow('hsv', hsv)
  257. # # # cv2.imshow('mask', mask)
  258. # # cv2.imshow('eroded mask', mask_eroded)
  259. # # cv2.imshow('eroded and dilated mask', mask_dilated)
  260.  
  261. # # Wait for ESC key to exit
  262. # k = cv2.waitKey(5) & 0xFF
  263. # if(k == 27):
  264. # break
  265. # cv2.destroyAllWindows
  266.  
  267. # //////////////////////////////////////////////////////////////////
  268.  
  269. import numpy as np
  270. import cv2
  271. import math as m
  272.  
  273. cap = cv2.VideoCapture(0)
  274.  
  275. flag, frame = cap.read()
  276.  
  277. width = np.size(frame, 1)
  278. height = np.size(frame, 0)
  279.  
  280. # returns degrees
  281. def radsTOdegrees(rads):
  282. return int(rads*180/m.pi)
  283.  
  284. # returns rads
  285. def degreesTOrads(degs):
  286. return (degs*m.pi/180)
  287.  
  288. # def distance(A, B):
  289. # return m.sqrt((A[0] - B[0])^2 + (A[1] - B[1])^2)
  290.  
  291. def XgivenAll(y, slope, yIntercept):
  292. if slope == 0:
  293. return 0
  294. return (y - yIntercept)/slope
  295.  
  296. speed = 8
  297.  
  298. x = 400
  299. y = 40
  300. r = 10
  301.  
  302. p = m.pi
  303.  
  304. angle = 16
  305.  
  306. while (True):
  307. stillInside = False
  308. flag, frame = cap.read()
  309. frame = cv2.flip(frame,1)
  310. flag2 = False
  311. cv2.circle(frame,(x, y), r,(0,255,0),5) #ball
  312.  
  313. if cv2.waitKey(1) & 0xFF == ord('q'):
  314. break
  315. if 0xFF == ord('w'):
  316. speed = speed + 1
  317.  
  318. hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)
  319.  
  320. # Extract Color from hsv image
  321. # RED: Lower = (160,50,50), Upper = (179,255,255)
  322. # BLUE: Lower = (110,100,100), Upper = (130,255,255) - Works great with dark blues, doesn't detect jeans
  323. lower_bound = (70,210,140)
  324. upper_bound = (110,2550,170)
  325.  
  326. # # coat
  327. # lower_bound = (110,100,140)
  328. # upper_bound = (130,130,170)
  329. mask = cv2.inRange(hsv, lower_bound, upper_bound)
  330.  
  331. # Eliminate noise
  332. # Erode # kernel parameters:
  333. # (Erode away from top and bottom, Erode away from sides)
  334. kernel = np.ones((3,3),np.uint8)
  335. mask_eroded = cv2.erode(mask, kernel, iterations = 1)
  336.  
  337. # Dilate
  338. mask_dilated = cv2.dilate(mask_eroded, kernel, iterations = 1)
  339.  
  340. # Find Contours
  341. ret, thresh = cv2.threshold(mask_dilated, 127,255,0)
  342. contours, hierarchy = cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
  343.  
  344. # Prevent intersections of contour lines and manually drawn lines
  345.  
  346. # print(contours)
  347. # print ('\n')
  348. slope = 0
  349. yIntercept = 0
  350. theta = 0
  351. flag = False
  352. if(len(contours) > 0):
  353. flag = True
  354. cnt = contours[0]
  355. rightmost = tuple(cnt[cnt[:,:,0].argmax()][0])
  356. leftmost = tuple(cnt[cnt[:,:,0].argmin()][0])
  357. maxright = rightmost
  358. maxleft = leftmost
  359. for i in range(len(contours)):
  360. cnt = contours[i]
  361. rightmost = tuple(cnt[cnt[:,:,0].argmax()][0])
  362. leftmost = tuple(cnt[cnt[:,:,0].argmin()][0])
  363. # print(rightmost[0])
  364. # print(rightmost[1])
  365. if(rightmost[0] > maxright[0]):
  366. maxright = rightmost
  367. if(leftmost[0] < maxleft[0]):
  368. maxleft = leftmost
  369. ## draw red line between leftmost and rightmost vertices of contour lines.
  370. cv2.line(frame, maxleft, maxright, (0,0,255), 5)
  371. xdiff = maxright[0] - maxleft[0]
  372. ydiff = maxright[1] - maxleft[1]
  373. theta = -radsTOdegrees(m.atan2(ydiff, xdiff))
  374. # print("x")
  375. # print(xdiff)
  376. # print()
  377. # print("y")
  378. # print(ydiff)
  379. slope = float(float(ydiff)/xdiff)
  380. # print(slope)
  381. # print("X of Max Right")
  382. # print (maxright[0])
  383. # print("Y Max Right")
  384. # print (maxright[1])
  385. yIntercept = -maxright[1] - slope*maxright[0]
  386. # print("MaxrightY-Slope*MaxRightX")
  387. # test = -maxleft[1] - slope*maxleft[0]
  388. # print("using right")
  389. # print(yIntercept)
  390. # print("using left")
  391. # print(test)
  392. # print("Contours found")
  393. # print(theta)
  394.  
  395. # print("Slope")
  396. # print(slope)
  397. # print("Y Intercept")
  398. # print(yIntercept)
  399.  
  400. else:
  401. "strasdf"
  402. # x is the coordinate of the green circle
  403. if(flag == True and x >= maxleft[0] and x <= maxright[0]):
  404. flag2 = True
  405. lineYcoord = (x - maxleft[0])*slope + maxleft[1]
  406. # print("lineYcoord")
  407. # print(lineYcoord)
  408. # print("y")
  409. # print(y)
  410.  
  411. if(lineYcoord - y <= 15 and lineYcoord - y >= -15):
  412. if(flag2 == True):
  413. if(stillInside == False):
  414. M1 = slope
  415. M2 = m.tan(degreesTOrads(angle))
  416. A1 = m.atan(slope)
  417. A3 = m.atan((M1 - M2) / (1 + M1 * M2)) + A1
  418. newAngle = radsTOdegrees(A3)
  419. angle = newAngle
  420. while(angle > 360):
  421. angle = angle - 360
  422. while(angle < 0):
  423. angle = angle + 360
  424. if(angle == 90 or angle == 180 or angle == 270 or angle == 360):
  425. angle = angle - 1
  426. if(angle == 0):
  427. angle = angle + 1
  428.  
  429. if(angle > 0 and angle < 90):
  430. if(x < width and y > 0): #in bounds
  431. x = x + int(speed*m.cos(degreesTOrads(angle)))
  432. y = y - int(speed*m.sin(degreesTOrads(angle)))
  433. elif(x >= width): #off right
  434. angle = angle + 2*(90 - angle)
  435. x = x - int(speed*m.cos(degreesTOrads(angle)))
  436. y = y - int(speed*m.sin(degreesTOrads(angle)))
  437. elif(y <= 0): #off top
  438. angle = 360 - angle
  439. x = x + int(speed*m.cos(degreesTOrads(angle)))
  440. y = y + int(speed*m.sin(degreesTOrads(angle)))
  441.  
  442. elif(angle > 90 and angle < 180):
  443. if(x > 0 and y > 0): #in bounds
  444. x = x + int(speed*m.cos(degreesTOrads(angle)))
  445. y = y - int(speed*m.sin(degreesTOrads(angle)))
  446. elif(x <= 0): #off left
  447. angle = angle - 2*(angle-90)
  448. x = x + int(speed*m.cos(degreesTOrads(angle)))
  449. y = y - int(speed*m.sin(degreesTOrads(angle)))
  450. elif(y <= 0): #off top
  451. angle = (180-angle) + 180
  452. x = x - int(speed*m.cos(degreesTOrads(angle)))
  453. y = y + int(speed*m.sin(degreesTOrads(angle)))
  454.  
  455. elif(angle > 180 and angle < 270):
  456. if(x > 0 and y < height): #in bounds
  457. x = x + int(speed*m.cos(degreesTOrads(angle)))
  458. y = y - int(speed*m.sin(degreesTOrads(angle)))
  459.  
  460. elif(x <= 0): #off left
  461. angle = angle + (270 - angle)*2
  462. x = x + int(speed*m.cos(degreesTOrads(angle)))
  463. y = y + int(speed*m.sin(degreesTOrads(angle)))
  464. elif(y >= height): #off bottom
  465. angle = angle - (angle-180)*2
  466. x = x - int(speed*m.cos(degreesTOrads(angle)))
  467. y = y + int(speed*m.sin(degreesTOrads(angle)))
  468.  
  469. elif(angle > 270 and angle < 360):
  470. if(x < width and y < height): #in bounds
  471. x = x + int(speed*m.cos(degreesTOrads(angle)))
  472. y = y - int(speed*m.sin(degreesTOrads(angle)))
  473. elif(x >= width): #off right
  474. angle = angle - (angle - 270)*2
  475. x = x - int(speed*m.cos(degreesTOrads(angle)))
  476. y = y + int(speed*m.sin(degreesTOrads(angle)))
  477. elif(y >= height): #off bottom
  478. angle = 360 - angle
  479. x = x + int(speed*m.cos(degreesTOrads(angle)))
  480. y = y - int(speed*m.sin(degreesTOrads(angle)))
  481.  
  482.  
  483. cv2.drawContours(frame,contours,-1,(0,255,0),3)
  484.  
  485. cv2.imshow('frame',frame)
  486. ### cv2.imshow('hsv', hsv)
  487. ### cv2.imshow('mask', mask_eroded)
  488. ### cv2.imshow('mask_eroded', mask_eroded)
  489. ### cv2.imshow('mask_dilated', mask_dilated)
  490.  
  491. if(flag2 == True):
  492. if(lineYcoord - y <= 20 and lineYcoord - y >= -20):
  493. stillInside = True
  494. else:
  495. stillInside = False
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement