PX4Flow : Sub Pixel Histogram Filter Coding Error - And Fix

To whom it might concern,

When looking through the px4flow code that I am studying for a project something jumped out at me.
At line 488:flow.c the iterator i is used to index subdirs for feeding the histogram filter, while it was filled at line 483:flow.c using meancount.
As i can increase more rapidly than meancount (continue statement at line 436:flow.c and various if statements) one cannot assume that the subdir[i] will read even a subpixel flow from the current frame let alone the one just calculated.
The code in question (line: 486-492:flow.c):
/* feed histogram filter*/
uint8_t hist_index_x = 2sumx + (winmax-winmin+1);
if (subdirs[i] == 0 || subdirs[i] == 1 || subdirs[i] == 7) hist_index_x += 1;
if (subdirs[i] == 3 || subdirs[i] == 4 || subdirs[i] == 5) hist_index_x += -1;
uint8_t hist_index_y = 2
sumy + (winmax-winmin+1);
if (subdirs[i] == 5 || subdirs[i] == 6 || subdirs[i] == 7) hist_index_y += -1;
if (subdirs[i] == 1 || subdirs[i] == 2 || subdirs[i] == 3) hist_index_y += 1;

	histx[hist_index_x]++;
	histy[hist_index_y]++;

I suggest to fix it with:
/* feed histogram filter*/
uint8_t hist_index_x = 2sumx + (winmax-winmin+1);
if (mindir == 0 || mindir == 1 || mindir == 7) hist_index_x += 1;
if (mindir == 3 || mindir == 4 || mindir == 5) hist_index_x += -1;
uint8_t hist_index_y = 2
sumy + (winmax-winmin+1);
if (mindir == 5 || mindir == 6 || mindir == 7) hist_index_y += -1;
if (mindir == 1 || mindir == 2 || mindir == 3) hist_index_y += 1;

	histx[hist_index_x]++;
	histy[hist_index_y]++;

This fix should speak for itself.

I hope I have this helps!
If you have any further enquiries, please do not hesitate to contact me.

Yours faithfully,

Mark Ramaker
Student Technical Computer Sciences - Inholland University of Applied Sciences Alkmaar