Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # import numpy as np
- # import cv2
- # capture = cv2.VideoCapture(0)
- # #capture.open()
- # while(True):
- # ret, frame = capture.read()
- # gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
- # cv2.imshow('frame', gray)
- # if cv2.waitKey() & 0xFF == ord('q'):
- # break
- # capture.release()
- # cv2.destroyAllWindows()
- # /////////////////////////////////////////////////////////////////
- # 4 Cameras, trippy...
- # import numpy as np
- # import cv2
- # cap = cv2.VideoCapture(0)
- # print cap
- # print(cap.get(3))
- # print(cap.get(4))
- # # if cv2.isOpened
- # print("Hello World")
- # while(True):
- # # Capture frame-by-frame
- # ret, frame = cap.read()
- # # Our operations on the frame come here
- # gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
- # # Uninvert camera image (flip image over Y-Axis)
- # flipped_gray = cv2.flip(gray,1)
- # gray_x = cv2.flip(gray,0)
- # flipped_gray_x = cv2.flip(flipped_gray,0)
- # # Display the resulting frame
- # cv2.imshow('frame1',flipped_gray)
- # cv2.imshow('frame2',gray)
- # cv2.imshow('frame3',flipped_gray_x)
- # cv2.imshow('frame4',gray_x)
- # if cv2.waitKey(1) & 0xFF == ord('q'):
- # break
- # # When everything done, release the capture
- # cap.release()
- # cv2.destroyAllWindows()
- # /////////////////////////////////////////////////////////////////
- # import cv2
- # cv2.namedWindow("preview")
- # vc = cv2.VideoCapture(0)
- # if vc.isOpened(): # try to get the first frame
- # rval, frame = vc.read()
- # else:
- # rval = False
- # while rval:
- # cv2.imshow("preview", frame)
- # rval, frame = vc.read()
- # key = cv2.waitKey(20000)
- # if key == 27: # exit on ESC
- # break
- # cv2.destroyWindow("preview")
- # /////////////////////////////////////////////////////////////////
- # # Stick Figure
- # import numpy as np
- # import cv2
- # # Load a color image in grayscale
- # img = cv2.imread('C:\Users\Mark\Desktop\doge.jpg',4)
- # # img = np.zeros((512,512,4), np.uint8)
- # # The 4 comes from the number of channels
- # # Can be 1 - Grayscale
- # # 3 - Color (RGB)
- # # 4 - Color with Alpha (RGBAlpha)
- # # Draw a diagonal blue line with thickness of 5 px
- # cv2.line(img,(40, 425),(120,325),(255,0,0),5) #left leg
- # cv2.line(img,(200, 425),(120,325),(255,0,0),5) #right leg
- # cv2.line(img,(120, 150),(120,325),(255,0,0),5) #torso
- # cv2.line(img,(50, 200),(190,200),(255,0,0),5) #arms
- # cv2.circle(img,(120,100), 50, (255,0,0), 5) #head
- # # (x Center Coord, y Center Coord), radius, (Red Color, Green Color, Blue Color), Thickness of Circle
- # cv2.imshow('image',img)
- # cv2.waitKey(0)
- # cv2.destroyAllWindows()
- # # //////////////////////////////////////////////////////////////////
- # import numpy as np
- # import cv2
- # cap = cv2.VideoCapture(0)
- # while(True):
- # # Capture frame-by-frame
- # ret, frame = cap.read()
- # # Our operations on the frame come here
- # gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
- # # Display the resulting frame
- # # cv2.line(frame,(40, 425),(120,325),(50,50,50),50) #left leg
- # # cv2.line(frame,(200, 425),(120,325),(255,0,0),5) #right leg
- # # cv2.line(frame,(120, 150),(120,325),(255,0,0),5) #torso
- # # cv2.line(frame,(50, 200),(190,200),(255,0,0),5) #arms
- # cv2.circle(frame,(120,100), 50, (255,0,0), 5) #head
- # # (x Center Coord, y Center Coord), radius, (Red Color, Green Color, Blue Color), Thickness of Circle
- # cv2.imshow('frame',frame)
- # if cv2.waitKey(1) & 0xFF == ord('q'):
- # break
- # # When everything done, release the capture
- # cap.release()
- # cv2.destroyAllWindows()
- # # //////////////////////////////////////////////////////////////////
- # import cv2
- # import numpy as np
- # img = cv2.imread('matchshapes.jpg',0)
- # img = cv2.medianBlur(img,5)
- # cimg = cv2.cvtColor(img,cv2.COLOR_GRAY2BGR)
- # circles = cv2.HoughCircles(img,cv2.CV_HOUGH_GRADIENT,1,20,
- # param1=50,param2=30,minRadius=0,maxRadius=0)
- # circles = np.uint16(np.around(circles))
- # for i in circles[0,:]:
- # # draw the outer circle
- # cv2.circle(cimg,(i[0],i[1]),i[2],(0,255,0),2)
- # # draw the center of the circle
- # cv2.circle(cimg,(i[0],i[1]),2,(0,0,255),3)
- # cv2.imshow('detected circles',cimg)
- # cv2.waitKey(0)
- # cv2.destroyAllWindows()
- # //////////////////////////////////////////////////////////////////
- # Draw line between two furthest points (on the x axis), distance on y axis is ignored
- # import cv2
- # import numpy as np
- # cap = cv2.VideoCapture(0)
- # while(True):
- # # Load from feed, frame by frame
- # _, raw = cap.read()
- # # Uninvert camera image (flip image over Y-Axis)
- # raw = cv2.flip(raw,1)
- # # Convert BGR to HSV
- # hsv = cv2.cvtColor(raw, cv2.COLOR_BGR2HSV)
- # # Extract Color from hsv image
- # # RED: Lower = (160,50,50), Upper = (179,255,255)
- # # BLUE: Lower = (110,100,100), Upper = (130,255,255) - Works great with dark blues, doesn't detect jeans
- # lower_bound = (110,100,100)
- # upper_bound = (130,255,255)
- # mask = cv2.inRange(hsv, lower_bound, upper_bound)
- # # Eliminate noise
- # # Erode:
- # # kernel notes:
- # # (Erode away from top and bottom, Erode away from sides)
- # kernel = np.ones((3,3),np.uint8)
- # mask_eroded = cv2.erode(mask, kernel, iterations = 1)
- # # Dilate
- # mask_dilated = cv2.dilate(mask_eroded, kernel, iterations = 1)
- # # Find Contours
- # ret, thresh = cv2.threshold(mask_dilated, 127,255,0)
- # contours, hierarchy = cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
- # # Prevent intersections of contour lines and manually drawn lines
- # # print(contours)
- # # print ('\n')
- # if(len(contours) > 0):
- # cnt = contours[0]
- # rightmost = tuple(cnt[cnt[:,:,0].argmax()][0])
- # leftmost = tuple(cnt[cnt[:,:,0].argmin()][0])
- # maxright = rightmost
- # maxleft = leftmost
- # for i in range(len(contours)):
- # cnt = contours[i]
- # rightmost = tuple(cnt[cnt[:,:,0].argmax()][0])
- # leftmost = tuple(cnt[cnt[:,:,0].argmin()][0])
- # # print(rightmost[0])
- # print(rightmost[1])
- # if(rightmost[0] > maxright[0]):
- # maxright = rightmost
- # if(leftmost[0] < maxleft[0]):
- # maxleft = leftmost
- # cv2.line(raw, maxleft, maxright, (0,0,255), 5)
- # else:
- # print("No contours found")
- # # Find & print center coordinates
- # # M = cv2.moments(cnt)
- # # centroid_x = int(M['m10']/M['m00'])
- # # centroid_y = int(M['m01']/M['m00'])
- # # print("X: ")
- # # print(centroid_x)
- # # print("Y: ")
- # # print(centroid_y)
- # # Draw circle at center coordinates
- # # cv2.circle(raw,(centroid_x,centroid_y), 50, (255,0,0), -1)
- # # Draw Contours
- # cv2.drawContours(raw,contours,-1,(0,255,0),3)
- # # Display the frame(s)
- # cv2.imshow('raw', raw)
- # # cv2.imshow('hsv', hsv)
- # # # cv2.imshow('mask', mask)
- # # cv2.imshow('eroded mask', mask_eroded)
- # # cv2.imshow('eroded and dilated mask', mask_dilated)
- # # Wait for ESC key to exit
- # k = cv2.waitKey(5) & 0xFF
- # if(k == 27):
- # break
- # cv2.destroyAllWindows
- # //////////////////////////////////////////////////////////////////
- import numpy as np
- import cv2
- import math as m
- cap = cv2.VideoCapture(0)
- flag, frame = cap.read()
- width = np.size(frame, 1)
- height = np.size(frame, 0)
- # returns degrees
- def radsTOdegrees(rads):
- return int(rads*180/m.pi)
- # returns rads
- def degreesTOrads(degs):
- return (degs*m.pi/180)
- # def distance(A, B):
- # return m.sqrt((A[0] - B[0])^2 + (A[1] - B[1])^2)
- def XgivenAll(y, slope, yIntercept):
- if slope == 0:
- return 0
- return (y - yIntercept)/slope
- speed = 8
- x = 400
- y = 40
- r = 10
- p = m.pi
- angle = 16
- while (True):
- stillInside = False
- flag, frame = cap.read()
- frame = cv2.flip(frame,1)
- flag2 = False
- cv2.circle(frame,(x, y), r,(0,255,0),5) #ball
- if cv2.waitKey(1) & 0xFF == ord('q'):
- break
- if 0xFF == ord('w'):
- speed = speed + 1
- hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)
- # Extract Color from hsv image
- # RED: Lower = (160,50,50), Upper = (179,255,255)
- # BLUE: Lower = (110,100,100), Upper = (130,255,255) - Works great with dark blues, doesn't detect jeans
- lower_bound = (70,210,140)
- upper_bound = (110,2550,170)
- # # coat
- # lower_bound = (110,100,140)
- # upper_bound = (130,130,170)
- mask = cv2.inRange(hsv, lower_bound, upper_bound)
- # Eliminate noise
- # Erode # kernel parameters:
- # (Erode away from top and bottom, Erode away from sides)
- kernel = np.ones((3,3),np.uint8)
- mask_eroded = cv2.erode(mask, kernel, iterations = 1)
- # Dilate
- mask_dilated = cv2.dilate(mask_eroded, kernel, iterations = 1)
- # Find Contours
- ret, thresh = cv2.threshold(mask_dilated, 127,255,0)
- contours, hierarchy = cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
- # Prevent intersections of contour lines and manually drawn lines
- # print(contours)
- # print ('\n')
- slope = 0
- yIntercept = 0
- theta = 0
- flag = False
- if(len(contours) > 0):
- flag = True
- cnt = contours[0]
- rightmost = tuple(cnt[cnt[:,:,0].argmax()][0])
- leftmost = tuple(cnt[cnt[:,:,0].argmin()][0])
- maxright = rightmost
- maxleft = leftmost
- for i in range(len(contours)):
- cnt = contours[i]
- rightmost = tuple(cnt[cnt[:,:,0].argmax()][0])
- leftmost = tuple(cnt[cnt[:,:,0].argmin()][0])
- # print(rightmost[0])
- # print(rightmost[1])
- if(rightmost[0] > maxright[0]):
- maxright = rightmost
- if(leftmost[0] < maxleft[0]):
- maxleft = leftmost
- ## draw red line between leftmost and rightmost vertices of contour lines.
- cv2.line(frame, maxleft, maxright, (0,0,255), 5)
- xdiff = maxright[0] - maxleft[0]
- ydiff = maxright[1] - maxleft[1]
- theta = -radsTOdegrees(m.atan2(ydiff, xdiff))
- # print("x")
- # print(xdiff)
- # print()
- # print("y")
- # print(ydiff)
- slope = float(float(ydiff)/xdiff)
- # print(slope)
- # print("X of Max Right")
- # print (maxright[0])
- # print("Y Max Right")
- # print (maxright[1])
- yIntercept = -maxright[1] - slope*maxright[0]
- # print("MaxrightY-Slope*MaxRightX")
- # test = -maxleft[1] - slope*maxleft[0]
- # print("using right")
- # print(yIntercept)
- # print("using left")
- # print(test)
- # print("Contours found")
- # print(theta)
- # print("Slope")
- # print(slope)
- # print("Y Intercept")
- # print(yIntercept)
- else:
- "strasdf"
- # x is the coordinate of the green circle
- if(flag == True and x >= maxleft[0] and x <= maxright[0]):
- flag2 = True
- lineYcoord = (x - maxleft[0])*slope + maxleft[1]
- # print("lineYcoord")
- # print(lineYcoord)
- # print("y")
- # print(y)
- if(lineYcoord - y <= 15 and lineYcoord - y >= -15):
- if(flag2 == True):
- if(stillInside == False):
- M1 = slope
- M2 = m.tan(degreesTOrads(angle))
- A1 = m.atan(slope)
- A3 = m.atan((M1 - M2) / (1 + M1 * M2)) + A1
- newAngle = radsTOdegrees(A3)
- angle = newAngle
- while(angle > 360):
- angle = angle - 360
- while(angle < 0):
- angle = angle + 360
- if(angle == 90 or angle == 180 or angle == 270 or angle == 360):
- angle = angle - 1
- if(angle == 0):
- angle = angle + 1
- if(angle > 0 and angle < 90):
- if(x < width and y > 0): #in bounds
- x = x + int(speed*m.cos(degreesTOrads(angle)))
- y = y - int(speed*m.sin(degreesTOrads(angle)))
- elif(x >= width): #off right
- angle = angle + 2*(90 - angle)
- x = x - int(speed*m.cos(degreesTOrads(angle)))
- y = y - int(speed*m.sin(degreesTOrads(angle)))
- elif(y <= 0): #off top
- angle = 360 - angle
- x = x + int(speed*m.cos(degreesTOrads(angle)))
- y = y + int(speed*m.sin(degreesTOrads(angle)))
- elif(angle > 90 and angle < 180):
- if(x > 0 and y > 0): #in bounds
- x = x + int(speed*m.cos(degreesTOrads(angle)))
- y = y - int(speed*m.sin(degreesTOrads(angle)))
- elif(x <= 0): #off left
- angle = angle - 2*(angle-90)
- x = x + int(speed*m.cos(degreesTOrads(angle)))
- y = y - int(speed*m.sin(degreesTOrads(angle)))
- elif(y <= 0): #off top
- angle = (180-angle) + 180
- x = x - int(speed*m.cos(degreesTOrads(angle)))
- y = y + int(speed*m.sin(degreesTOrads(angle)))
- elif(angle > 180 and angle < 270):
- if(x > 0 and y < height): #in bounds
- x = x + int(speed*m.cos(degreesTOrads(angle)))
- y = y - int(speed*m.sin(degreesTOrads(angle)))
- elif(x <= 0): #off left
- angle = angle + (270 - angle)*2
- x = x + int(speed*m.cos(degreesTOrads(angle)))
- y = y + int(speed*m.sin(degreesTOrads(angle)))
- elif(y >= height): #off bottom
- angle = angle - (angle-180)*2
- x = x - int(speed*m.cos(degreesTOrads(angle)))
- y = y + int(speed*m.sin(degreesTOrads(angle)))
- elif(angle > 270 and angle < 360):
- if(x < width and y < height): #in bounds
- x = x + int(speed*m.cos(degreesTOrads(angle)))
- y = y - int(speed*m.sin(degreesTOrads(angle)))
- elif(x >= width): #off right
- angle = angle - (angle - 270)*2
- x = x - int(speed*m.cos(degreesTOrads(angle)))
- y = y + int(speed*m.sin(degreesTOrads(angle)))
- elif(y >= height): #off bottom
- angle = 360 - angle
- x = x + int(speed*m.cos(degreesTOrads(angle)))
- y = y - int(speed*m.sin(degreesTOrads(angle)))
- cv2.drawContours(frame,contours,-1,(0,255,0),3)
- cv2.imshow('frame',frame)
- ### cv2.imshow('hsv', hsv)
- ### cv2.imshow('mask', mask_eroded)
- ### cv2.imshow('mask_eroded', mask_eroded)
- ### cv2.imshow('mask_dilated', mask_dilated)
- if(flag2 == True):
- if(lineYcoord - y <= 20 and lineYcoord - y >= -20):
- stillInside = True
- else:
- stillInside = False
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement