How to reduce the noise in image so it does not affect the values passed down from the server to the client
Question
Using a server that is connected to a camera that detects the location of a ball and a robot. When the client request for the coordinates of the ball and the robot, the value of the coordinates that are passed down are varying in +2/-2 due to the noise of the image. Is there anyway to solve it in the sense that i want an absolute value because i would be calling a method based on the values changed and if the values keep on varying each time it will cause a bug in the program when i run it
client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
client_socket.connect(('59.191.193.42',5555))
def updateBallx(valueList):
# updates red ball x-axis position
ballx = int(valueList[8])
return ballx
def updateBally(valueList):
# updates red ball y-axis position
bally = int(valueList[9])
return bally
def updateRobotx(valueList):
# updates robot x-axis position
robotx = int(valueList[12])
return robotx
def updateRoboty(valueList):
# updates robot x-axis position
roboty = int(valueList[13])
return roboty
def updateRobota(valueList):
# updates robot angle position
robota = int(valueList[14])
return robota
def activate():
new_x = 413 #updateBallx(valueList)
print new_x
new_y = 351 #updateBally(valueList)
print new_y
old_x = 309 #updateRobotx(valueList)
print old_x
old_y = 261 #updateRoboty(valueList)
print old_y
angle = 360 #updateRobota(valueList)
print angle
turn_to(brick,new_x, new_y, old_x, old_y, angle)
move_to(brick,new_x, new_y, old_x, old_y)
screenw = 0
screenh = 0
old_valueList = []
while 1:
client_socket.send("loc\n")
data = client_socket.recv(8192)
valueList = data.split()
if (not(valueList[-1] == "eom" and valueList[0] == "start")):
#print "continuing.."
continue
if(screenw != int(valueList[2])):
screenw = int(valueList[2])
screenh = int(valueList[3])
if valueList != old_valueList:
activate(valueList)
old_valueList = valueList[:]
Solution
So to rephrase your question: you have a dynamical system, for which you only have noisy observations, and you would like to infer the true state of the system from noisy observations before using the state information for further processing? Am I understanding you correctly?
If I do, then what you want is some sort of temporal filtering. You can do this either on the server side or on your client.
The simplest would be to run moving average across multiple consecutive frames (or some short time window if you are not sampling equally spaced frames). However, this only works if the time window over which you do averaging is much shorter comparing to the dynamics of the system (ex. speed of motion of robots and ball). If not, you gonna blur some of the actual motion, which is probably a bad idea.
The more sophisticated thing you can try is a Kalman filter. You can find plenty of tutorial if you google. Scipy has library for it http://www.scipy.org/Cookbook/KalmanFiltering
The even more sophisticated one is a particle filter. Which I don't really recommend using given your simple 2D problem, because it just has way too many parameters to tweak.