Created
March 25, 2019 01:04
-
-
Save enriqueornelasjr/b1f494e74302849b05943b16d4a2aad7 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Import the OpenCV Library | |
import cv2 | |
# Import NumPy, a scientific library that allows for the arrays we will be needing | |
import numpy as np | |
# This variable will be the original image | |
frame = cv2.imread('grid.png') | |
# Blur the image with a 6x6 average pixel destination | |
frame = cv2.blur(frame,(6, 6)) | |
# Convert the image from BGR (Blue, Green, Red) to HSV (Hue, Saturation, Value) | |
hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV) | |
# Provide the lower HSV values for red and higher in an array | |
#lower_red = np.array([hMin, sMin, vMin]) | |
#upper_red = np.array([hMax, sMax, vMax]) | |
lower_red = np.array([0, 110, 0]) | |
upper_red = np.array([179, 255, 245]) | |
# Create a mask by finding all of the hsv values that fit the HSV range in the image | |
mask = cv2.inRange(hsv, lower_red, upper_red) | |
# Perform a bitwise_and operation. Basically, whatever pixel is not in the range provided above, give it a value of 0 and each that is a value of 1 | |
res = cv2.bitwise_and(frame, frame, mask=mask) | |
# Ngl, don't know much about this one, see this for more info: https://docs.opencv.org/3.2.0/d7/d4d/tutorial_py_thresholding.html | |
ret,thresh = cv2.threshold(mask,127,255,0) | |
# Here, we use findContours to give us coordinates of the limits of the color that we are looking for, red in HSV. | |
# Think of contours as the edges of the shapes that we are looking for | |
contours, hierarchy = cv2.findContours(thresh,cv2.RETR_TREE,cv2.CHAIN_APPROX_NONE) | |
# The contours the code returns is 3- the path, the crack, and a false positive in the bottom left of the screen | |
# I created this short workaround that if the countour is less than 5 points, it might be a false positive and to ignore it | |
for cnt in contours: | |
if len(cnt) < 5: | |
contours.remove(cnt) | |
# The draw contours draws points at the coordinates of the contours. | |
# The last negative 1 tells the code to fill in what is inside. Change it to a positive value to see its difference | |
cv2.drawContours(res,contours,-1,(0,255,0),-1) | |
# Show the frames that we have so far | |
cv2.imshow('frame', frame) | |
cv2.imshow('hsv', hsv) | |
cv2.imshow('mask', mask) | |
# Current coordinates of our pointer | |
x = 182 | |
y = 78 | |
# New coordinates. Once we set the original coordinates to original color, original coordinates become these | |
newX = x | |
newY = y | |
# Keep track of the old color before changing it to our pointer color | |
oldColor = (0, 0, 0) | |
# Keep track of current direction | |
currDir = '' | |
while(1): | |
# Get old colors | |
(oldR, oldG, oldB) = res[y, x] | |
oldColor = (oldR, oldG, oldB) | |
# Change the color to our pointer color | |
res[y, x] = (255, 0, 0) | |
# Display the current image | |
cv2.imshow('Result',res) | |
# Wait for current input | |
k = cv2.waitKey(-1) | |
# Press ESC to stop, A, S, W, and D to control the pointer | |
# or SPACE to analyze current point | |
if k==27: # Esc key to stop | |
break | |
elif k==-1: # normally -1 returned,so don't print it | |
continue | |
if k == 119: | |
newY = y-1 | |
elif k == 115: | |
newY = y+1 | |
elif k == 97: | |
newX = x-1 | |
elif k == 100: | |
newX = x+1 | |
elif k == 32: | |
traveledL = traveledR = traveledU = traveledD = maxVal = 0 | |
maxDir = '' | |
if currDir != 'R' and currDir != 'L': | |
copyX = x | |
copyY = y | |
tempColor = oldColor | |
while tempColor[0] != 0 or tempColor[1] != 0 or tempColor[2] != 0: | |
traveledL += 1 | |
copyX = copyX - 1 | |
tempColor = res[copyY, copyX] | |
print("Left, ",traveledL) | |
if traveledL > maxVal: | |
maxDir = 'L' | |
maxVal = traveledL | |
if currDir != 'R' and currDir != 'L': | |
copyX = x | |
copyY = y | |
tempColor = oldColor | |
while tempColor[0] != 0 or tempColor[1] != 0 or tempColor[2] != 0: | |
traveledR += 1 | |
copyX = copyX + 1 | |
tempColor = res[copyY, copyX] | |
print("Right, ",traveledR) | |
if traveledR > maxVal: | |
maxDir = 'R' | |
maxVal = traveledR | |
if currDir != 'U' and currDir != 'D': | |
copyX = x | |
copyY = y | |
tempColor = oldColor | |
while tempColor[0] != 0 or tempColor[1] != 0 or tempColor[2] != 0: | |
traveledU += 1 | |
copyY = copyY - 1 | |
tempColor = res[copyY, copyX] | |
print("Up, ",traveledU) | |
if traveledU > maxVal: | |
maxDir = 'U' | |
maxVal = traveledU | |
if currDir != 'U' and currDir != 'D': | |
copyX = x | |
copyY = y | |
tempColor = oldColor | |
while tempColor[0] != 0 or tempColor[1] != 0 or tempColor[2] != 0: | |
traveledD += 1 | |
copyY = copyY + 1 | |
tempColor = res[copyY, copyX] | |
print("Down, ",traveledD) | |
if traveledD > maxVal: | |
maxDir = 'D' | |
maxVal = traveledD | |
currDir = maxDir | |
print() | |
print("MaxVal: ", maxVal) | |
print("CurrentDir: ", currDir) | |
print() | |
if not(res[y, x][0] == oldR and res[y, x][1] == oldG and res[y, x][2] == oldB): | |
res[y, x] = (oldR, oldG, oldB) | |
x = newX | |
y = newY |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment