Goal: My code below first creates a mask of the red lines. Then, starting at the center green dot, the code iterates one space at a time to the right until it finds a white pixel on the mask (at a fixed height). This pixel is signified by the green dot on the right. This process is then repeated with the left side to find the first left white pixel. I have also set up a counter to track the amount of even vs. odd numbered pixels the program finds during each search.
Problem: Although this code seems to be working, my problem is that when iterating to find the right white pixel, most of these pixels are always even. It seems to me that the distribution should be 50-50 for the percentage of even and odd pixels here. Similarly, when iterating to find the left white pixel, most of these pixels are always odd.
A common output from this program: Right Even: 795, Right Odd: 67, Left Even: 46, Left Odd: 808
import cv2
import numpy as np
import time
import cv2
time.sleep(.1)
def callback(x):
pass
ilowH = 75
ihighH = 100
ilowS = 120
ihighS = 255
ilowV = 0
ihighV = 255
rightEven = 0
rightOdd = 0
leftEven = 0
leftOdd = 0
width = 1280
height = 720
cap = cv2.VideoCapture(0)
cap.set(3, width)
cap.set(4, height)
def dot(point, image):
cv2.circle(image, point, 5, (0,255,0), -2)
while(True):
# grab the frame
ret, frame = cap.read()
#Create mask
frame = 255 - frame
hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)
lower_hsv = np.array([ilowH, ilowS, ilowV])
higher_hsv = np.array([ihighH, ihighS, ihighV])
mask = cv2.inRange(hsv, lower_hsv, higher_hsv)
frame = 255 - frame
center = int(width/2)
dot((center,int(height/2)), frame)
currRightPoint = center
currLeftPoint = center
rightPointFound = False
leftPointFound = False
while(rightPointFound == False):
currRightPoint = currRightPoint + 1
if (mask[int(height / 2)][currRightPoint] == 255):
rightPointFound = True
dot((currRightPoint, int(height/2)), frame)
if (currRightPoint % 2 == 0):
rightEven = rightEven + 1
else:
rightOdd = rightOdd + 1
if (currRightPoint == width - 1):
rightPointFound = True
while(leftPointFound == False):
currLeftPoint = currLeftPoint - 1
if (mask[int(height / 2)][currLeftPoint] == 255):
leftPointFound = True
dot((currLeftPoint, int(height/2)), frame)
if (currLeftPoint % 2 == 0):
leftEven = leftEven + 1
else:
leftOdd = leftOdd + 1
if (currLeftPoint == 0):
leftPointFound = True
print('Center: ' + str(center) + ', Right: ' + str(currRightPoint) + ', Left: ' + str(currLeftPoint))
# show thresholded image
cv2.imshow('mask', mask)
cv2.imshow('frame', frame)
k = cv2.waitKey(1) & 0xFF # large wait time to remove freezing
if k == 113 or k == 27:
break
print('Right Even: ' + str(rightEven) + ', Right Odd: ' + str(rightOdd))
print('Left Even: ' + str(leftEven) + ', Left Odd: ' + str(leftOdd))
cv2.destroyAllWindows()
cap.release()
For this application, the exact pixel count between the left and right edges is very important, so I need the exact values. Since these values aren't 50% even and 50% odd, I believe I am not finding the most exact points.
Solution Attempt 1) I have tried using different webcams in the case that the camera is up-scaling from a resolution lower than 720p, but the exact same results have occured.
Solution Attempt 2) I have also tried iterating starting from the left side of the screen to the left edge of the left line. Since I was iterating to the right, most of these pixel values turned up even.
Any insight into the problem would be appreciated!

