PX4-Flow icon indicating copy to clipboard operation
PX4-Flow copied to clipboard

Improve flow quality

Open lericson opened this issue 8 years ago • 52 comments

We noticed quite heavy flickering in the image output from the camera sensor. We tested an array of approaches to mitigate the issue.

Primarily we perform whitening of the camera input. This improves the flow quality and stability immensely for us.

lericson avatar Jun 15 '17 08:06 lericson

And can you take a picture of the hardware? If the supply is not properly stabilized you might suffer from a bad HW build.

LorenzMeier avatar Jun 15 '17 08:06 LorenzMeier

Hold on, apparently can't send images on github.

lericson avatar Jun 15 '17 09:06 lericson

https://www.dropbox.com/sc/8jlcyy7gj9tknvy/AAANLvfISKlj71DTSpeL4wZCa

  • Ludvig

On 15 Jun 2017, at 10:52, Lorenz Meier [email protected] wrote:

And can you take a picture of the hardware? If the supply is not properly stabilized you might suffer from a bad HW build.

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub, or mute the thread.

lericson avatar Jun 15 '17 10:06 lericson

Pushed the suggested changes!

lericson avatar Jun 15 '17 10:06 lericson

Great job on getting that NUC onto a 250 quad! :O

mhkabir avatar Jun 15 '17 10:06 mhkabir

~FYI the CI failure seems to be a bug, refresh it and it should work -- it checked out old code.~ Sorry my fault, forgot to commit a second change. Force pushing.

lericson avatar Jun 15 '17 11:06 lericson

By the way, the difference in signal quality from the flow sensors is enormous for us with this whitening turned on. Might be hardware, of course.

lericson avatar Jun 16 '17 15:06 lericson

@jgoppert Could you cross-test it?

LorenzMeier avatar Jun 18 '17 10:06 LorenzMeier

You're right that AGC should probably not be on, empirically though it seems to be better. I'll look into it again tomorrow.

  • Ludvig

On 17 Jun 2017, at 20:00, James Goppert [email protected] wrote:

@jgoppert commented on this pull request.

In src/modules/flow/dcmi.c:

}
  • else if (dcmi_image_buffer_unused == 2)
  • {
  • for (uint16_t pixel = 0; pixel < image_size; pixel++)
    
  • 	(*current_image)[pixel] = (uint8_t)(dcmi_image_buffer_8bit_2[pixel]);
    
  • }
  • else
  • {
  • for (uint16_t pixel = 0; pixel < image_size; pixel++)
    
  • 	(*current_image)[pixel] = (uint8_t)(dcmi_image_buffer_8bit_3[pixel]);
    
  • for (uint16_t pixel = 0; pixel < image_size; pixel++)
  • (*current_image)[pixel] = source[pixel];
    

+} + +void whitened_image(uint8_t *source, uint8_t *dest, uint16_t image_size) { So I'm wondering how this compares to the auto gain and brightness controls within the firmware. Both pieces of software have the same goal, so you might just be eating more cpu here. You might want to check. But if your method is better than the default firmware, maybe you want to turn auto gain off.

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub, or mute the thread.

lericson avatar Jun 18 '17 10:06 lericson

The noise is about the same with and without AGC, so I removed it as per your recommendation @jgoppert -- one less moving part.

lericson avatar Jun 19 '17 08:06 lericson

Wait, wait. Did you test in sunlight and at very low indoor conditions? Because without AGC you might perform worse in one of the two. Please make sure to run a solid test.

LorenzMeier avatar Jun 19 '17 09:06 LorenzMeier

I primarily tested indoors. Disabling AGC shouldn't affect the signal quality however, since the aim of the AGC mechanism is the same as the whitening operation. I'll try it with sunlight in a second.

lericson avatar Jun 19 '17 11:06 lericson

Seems to work well in sunlight as well actually. How low light are low light conditions?

lericson avatar Jun 19 '17 11:06 lericson

Here is an example where the image quality drops (this is the floor of my office right now): scene

  • above dark area: quality = 255 dark
  • above bright area: quality = 255 bright
  • above border: quality = 0 border

EDIT: on master, the quality for the same images was respectively 130, 190 and 150

jlecoeur avatar Jun 19 '17 16:06 jlecoeur

Great feedback, @jlecoeur, thanks. Indeed it does have issues with high-variance areas; in particular multi-modalities such as the one you presented.

lericson avatar Jun 19 '17 16:06 lericson

Yeah, agc likely controls how many photons it takes to make 1 bit, so it will have a different effect. In software with gain you are stretching the raw data out. That is why I never looked into a post camera image whitening. Do you have any intuitive sense why this is helping more than AGC and desired brightness?

jgoppert avatar Jun 19 '17 16:06 jgoppert

Indeed @lericson , see the histograms: histograms However computing the histograms and detecting multi-modes might be too expensive, can we afford calling compute_flow two times (before and after whitening), and keep the best one?

jlecoeur avatar Jun 19 '17 16:06 jlecoeur

I think it won't be worth it, @jlecoeur -- what we could do though is some kind of naive outlier rejection. This would mean half the image becomes either completely black or completely white, which would in this scenario be more appropriate. Something like having two estimates, one for pixels over 127 (i.e. 50%) and one for those under. Let's try that.

lericson avatar Jun 19 '17 17:06 lericson

@jgoppert The gain mechanism in the camera firmware is just a multiplier AFAICT.

lericson avatar Jun 19 '17 17:06 lericson

For the point grey cameras it is photons to bits. So worth checking. I think you would still want your whitening though because the agc lags and would explain the flickering.

jgoppert avatar Jun 19 '17 17:06 jgoppert

@jgoppert Hm ok, so you're saying AGC + whitening would make sense then?

lericson avatar Jun 19 '17 17:06 lericson

@jlecoeur Tried binning, not very surprisingly it turns out to give rise to flickering as the mode changes between the bins. Three bins was pretty good though.

lericson avatar Jun 19 '17 17:06 lericson

Could probably make it flicker less by introducing some penalty for switching mode.

lericson avatar Jun 19 '17 17:06 lericson

@lericson Yes, I think AGC + whitening makes sense. Basically AGC is your first pass before data collection, you have to guess what the gain should be, then after you collect the image it computes the mean and variance and updates the gain for the next loop. Your whitening method would then operate on the output of that loop and fix transients.

jgoppert avatar Jun 19 '17 17:06 jgoppert

@lericson I fixed the problem with high variance areas on this branch https://github.com/PX4/Flow/compare/master...lis-epfl:master_KTH_image_whitening?expand=1

I added a mode WHITENING_AUTO where optical flow is computed on whitened images, then if the flow quality is lower than a threshold, flow is re-computed using non-whitened images. Now the quality does not fall to 0 like in my previous comment.

The performance cost goes increasing: IMAGE_WHITENING_DISABLED: no performance cost compared to master IMAGE_WHITENING_ALWAYS: fixed cost for whitening of current image IMAGE_WHITENING_AUTO: fixed cost for whitening of current image + variable cost for 2nd flow computation only when quality is poor.

Feel free to cherry-pick the 3 last commits if you like it.

jlecoeur avatar Jun 23 '17 16:06 jlecoeur

Great stuff, @jlecoeur! Did you try it in practice? Is the added complexity and performance penalty worth the savings compared to just whitening all the time? I've been experimenting with this in a mixed light indoor environment (i.e. semi-occluded sunlight) and I've not found any significant performance hit.

In fact one of our bigger problems right now isn't the flow estimation -- it seems to work more or less flawlessly -- but making the LPE module of the PX4 firmware integrate the flow data into its estimation of the pose in a meaningful way. Right now it seems it trusts the accelerometer too much or something, and the velocity estimate just flies off the handle. Got any experience with this?

lericson avatar Jun 23 '17 17:06 lericson

@lericson I tried whitening in a building with sharp shadows on the ground. And indeed it was an issue, the drone was rejecting position control only when flying above shadow patterns. I assume this was the same phenomenon I witnessed in my office, but I have not tested the IMAGE_WHITENING_AUTO fix in that same building. I will do it probably on Monday.

Regarding the performance penalty, hand-held test showed that the 2nd flow computation happens only when there s a sharp brightness change in view. So in most cases this is not more expensive than whitening only.

Regarding estimators, I had the same trouble and finally settled with ekf2. As I understood it is not ready for full missions in GPS denied environments yet. But I take off and land in stabilized mode, so I do not care. I increased a bit flow innovation gate to prevent it from rejecting flow when it is not perfect. I also had to lower vibrations to around 1g~2g on x and y axes.

jlecoeur avatar Jun 23 '17 19:06 jlecoeur

Too bad nothing is happening here! A demonstration of a hexacopter flying indoors using the code in this PR: https://youtu.be/mgxxTmgxnQ0

lericson avatar Aug 09 '17 21:08 lericson

@jlecoeur @lericson Sorry for being slow on you. I'm very much impressed by your execution speed and contributions.

@ChristophTobler Could you help to get this in once you're back? Thanks!

LorenzMeier avatar Aug 09 '17 21:08 LorenzMeier

@LorenzMeier Yes. I'll have a look at it as soon as possible.

ChristophTobler avatar Aug 14 '17 07:08 ChristophTobler