-
Notifications
You must be signed in to change notification settings - Fork 340
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Improve flow quality #90
base: master
Are you sure you want to change the base?
Conversation
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Awesome, thanks! Would you mind doing the optimizations I suggested and re-test?
src/modules/flow/dcmi.c
Outdated
double mean = sum / image_size; | ||
double rss = 0.0; | ||
for (uint16_t pixel = 0; pixel < image_size; pixel++) | ||
rss += pow(source[pixel] - mean, 2); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This executes a double instruction and pow instead of square. Please replace with: (source[pixel] - mean) * (source[pixel] - mean)
. This will be orders of magnitude faster.
src/modules/flow/dcmi.c
Outdated
double rss = 0.0; | ||
for (uint16_t pixel = 0; pixel < image_size; pixel++) | ||
rss += pow(source[pixel] - mean, 2); | ||
double stddev = sqrt(rss/(image_size - 1)); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please use sqrtf()
to keep this in float and use float variables.
src/modules/flow/dcmi.c
Outdated
} | ||
|
||
void whitened_image(uint8_t *source, uint8_t *dest, uint16_t image_size) { | ||
double sum = 0.0; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
double is very expensive on embedded hardware and you don't need this here (unless you need a resolution better than about 0.0000001). Please replace with float.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good point, will do.
And can you take a picture of the hardware? If the supply is not properly stabilized you might suffer from a bad HW build. |
Hold on, apparently can't send images on github. |
… On 15 Jun 2017, at 10:52, Lorenz Meier ***@***.***> 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.
|
Pushed the suggested changes! |
Great job on getting that NUC onto a 250 quad! :O |
|
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. |
src/modules/flow/dcmi.c
Outdated
(*current_image)[pixel] = source[pixel]; | ||
} | ||
|
||
void whitened_image(uint8_t *source, uint8_t *dest, uint16_t image_size) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So I'm wondering how this compares to the auto gain and brightness controls within the camera 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 whitening methods coded in the camera firmware, maybe you want to turn auto gain etc. off.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@jgoppert Could you cross-test it? |
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 ***@***.***> 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.
|
The noise is about the same with and without AGC, so I removed it as per your recommendation @jgoppert -- one less moving part. |
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. |
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. |
Seems to work well in sunlight as well actually. How low light are low light conditions? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hi!
It greatly improved flow quality on low-textured floor.
However the flow quality drops to almost 0 when the camera sees bright areas that are created by sunlight through a window.
@@ -108,10 +108,10 @@ volatile uint32_t boot_time10_us = 0; | |||
#define TIMER_LPOS 8 | |||
#define MS_TIMER_COUNT 100 /* steps in 10 microseconds ticks */ | |||
#define LED_TIMER_COUNT 500 /* steps in milliseconds ticks */ | |||
#define SONAR_TIMER_COUNT 100 /* steps in milliseconds ticks */ | |||
#define SONAR_TIMER_COUNT 9999/* steps in milliseconds ticks */ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Any particular reason you modified this? This gave very slow sonar update when I tested.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Whops, not intended to be included. I did this since we don't use this data at all, as the sonar is actually really bad.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you revert?
Great feedback, @jlecoeur, thanks. Indeed it does have issues with high-variance areas; in particular multi-modalities such as the one you presented. |
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? |
Indeed @lericson , see the histograms: |
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 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. The performance cost goes increasing: Feel free to cherry-pick the 3 last commits if you like it. |
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 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. |
Too bad nothing is happening here! A demonstration of a hexacopter flying indoors using the code in this PR: https://youtu.be/mgxxTmgxnQ0 |
@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 Yes. I'll have a look at it as soon as possible. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I Still have to actually test it...
@@ -108,10 +108,10 @@ volatile uint32_t boot_time10_us = 0; | |||
#define TIMER_LPOS 8 | |||
#define MS_TIMER_COUNT 100 /* steps in 10 microseconds ticks */ | |||
#define LED_TIMER_COUNT 500 /* steps in milliseconds ticks */ | |||
#define SONAR_TIMER_COUNT 100 /* steps in milliseconds ticks */ | |||
#define SONAR_TIMER_COUNT 9999/* steps in milliseconds ticks */ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you revert?
@@ -134,7 +136,7 @@ void mt9v034_context_configuration(void) | |||
desired_brightness = 58; // VALID RANGE: 8-64 | |||
resolution_ctrl = 0x0202;//10 bit linear | |||
hdr_enabled = 0x0000; // off | |||
aec_agc_enabled = 0x0303; // on | |||
aec_agc_enabled = 0x0101; // aec on, agc off |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I guess agc can stay enabled.?
resolution_ctrl = 0x0202;//10bit linear | ||
hdr_enabled = 0x0000; // off | ||
aec_agc_enabled = 0x0303; // on | ||
aec_agc_enabled = 0x0101; // aec on, agc off |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same here. See above
@lericson I have bench-tested this PR and I'm not very convinced. When pointing to my relatively dark carpet the output looks like this: I got better results using #86 @jgoppert FYI |
@ChristophTobler These are strange images, can you test with master and send an image ? A picture from smartphone camera may help too. Your carpet seems to have very bright spots on dark background. So I wonder if you have the same problem as in the images I sent above. |
@jlecoeur The grid in the second image is a test pattern. See the pertinent PR for a discussion on them. @ChristophTobler Strange, I'm curious how your camera quality is on average if PR #86 works better than this does -- I heard some hardware batches are worse than others? The device is completely useless to us without the changes suggested in this pull request. It looks like your carpet is dark enough and in a dark enough environment that all pixels are the same value, it would explain why the quality is 100% and it would explain the resulting image as the variance would be near or actually at zero. It doesn't seem worthwhile to set a minimum, because if the variance actually is that low, tracking flow will be impossible. |
@lericson ok that makes a lot more sense now :) I am still curious about that first image @ChristophTobler what does it say with AGC enabled? I remember I reverted that change when testing on this branch |
@jlecoeur Here's the image from master (before the merge): Here's how it looks with this PR + agc |
I think that looks really odd. It just looks like noise, no? We have a fairly well-lit testing area; is yours really dark? |
Also I don't really understand the last image, the camera output looks fine, doesn't it? |
Not really. Maybe a little bit darker in real life than on the image above that was taken by my phone.
That's the odd thing, yes. The picture looks good but it doesn't detect any optical flow... Have a look at the Analyze plot in the lower left of the image. Everything is zero (which means no flow/low quality). |
Well, you're not plotting the quality at all, so I can't speak for that, but having a zero flow when the camera is still would seem to make sense. (The gyro plots are also zero, leading me to believe you had the camera at perfect rest.) |
You would still see some gyro noise. It wouldn't be absolutely zero :) |
That's not how it works... Even when perfectly still, PX4Flow outputs some noise on both pixel flow and especially gyro (both have noise). When everything is perfectly zero, it means that flow quality is not good enough and it won't integrate values -> 0 flow quality. You can test it yourself by e.g. covering the lens |
Agreed that there should be gyro noise, but during development of this whitening, we typically had zero flow when the camera was completely still. |
Pixel flow is zero if you have very little image noise, true. But with this PR the image is flickering heavily. Especially on this carpet. |
@ChristophTobler I did not see flickering with this PR |
So, it seems like @jlecoeur and I both are unlucky enough to suffer from flickering WITHOUT this pull request. When I use this whitening method, the flickering goes away; in fact, that is exactly why I went for whitening. |
@lericson in fact flickering never stroke me as being a problem, with or without this PR. |
@ChristophTobler No, I compared with master before #86 was merged |
Sounds like a good plan, @jlecoeur. I will try to find the time for it; I would need to test it on the hardware as well. |
I should note that I'm a bit skeptical about using what was in #86 with this. I've tried it once before, and the results are available a bit further up in the comment history here -- I will try again, though. |
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.