Commit Graph

205 Commits

Author SHA1 Message Date
iabdalkader
15d6b96bed Fix py_image_get_buffer to support writing BAYER. 2017-03-12 19:25:48 +02:00
iabdalkader
4502256e19 Fix py_image assertion message. 2017-03-12 19:19:00 +02:00
iabdalkader
040a3cb0e4 Use separable gaussian kernels. 2017-03-09 22:41:37 +02:00
Kwabena W. Agyeman
1b3f444351 Add callbacks back in for emulating pixy. 2017-02-15 13:18:11 -05:00
Kwabena W. Agyeman
cd07c0d5fa AprilTags Fixes
* Made all memory allocations during the exhaustive time safe.
* Added 3D pose output from the AprilTags code.
2017-02-14 18:36:52 -05:00
Kwabena W. Agyeman
443ba3bff8 AprilTags Support
Works super good.
2017-02-09 17:37:18 -05:00
Kwabena W. Agyeman
02af722fe8 Fixed fb_alloc() with mark for recently re-worked code.
I also tested the firmware for about an hour to make sure there was no
stack leak.

Note that I prefer for fb_free() to still be called versus
fb_free_till_mark() doing that for you in the code.

For functions without this fix they will just free the entire fb_alloc
stack when an exception happens. For functions with this fix they will
only free up to and including the mark. Since there are no places in the
firmware where you could start building a second fb_alloc stack when one
is already in place this point is moot currently. But, if we do
something like that in the future the problem will have already been
solved.

Any new code or re-worked code should use the mark function.
2017-01-18 18:39:08 -05:00
Ibrahim Abd Elkader
f7e04a450b Merge pull request #177 from kwagyeman/update_lens_corr
Speed up lens_corr.
2017-01-16 04:43:56 +02:00
Kwabena W. Agyeman
d6b49adefa Speed up lens_corr.
Speed up the algorithm by fixing the abs() issue. Do not use that
function in any of your code. It by itself cut the speed of the code
in half. I don't know what's in that function but I'm guessing it does
ABS of a float using ints or something.

I made the zoom parameter functional now too so you can use lens_corr to
zoom in on the image. Argument parsing is handled too. Finally, I
updated the only script where this is used.

Note that I'm able to get more than 10 FPS at 160x120 on the M4 and 15
FPS at 160x120 on the M7. Previous this was at about 5 FPS and 7.5 FPS
respectively.
2017-01-15 21:32:55 -05:00
Kwabena W. Agyeman
ed3f226478 Tested stats with examples and fixed bugs
The automatic grayscale and rgb565 color tracking scripts are very cool.
2017-01-15 14:59:42 -05:00
iabdalkader
bba8e5a9e0 Remove match_descriptor type arg.
* Not really needed can check MP object types.
2017-01-14 01:15:24 +02:00
iabdalkader
cdef215a6e Make match_keypoints more robust.
* Check keypoints sizes.
* Handle zero matches case.
2017-01-12 04:54:40 +02:00
iabdalkader
cfdb4bdc90 Cast sin/cos to ints in draw_keypoints. 2017-01-12 04:52:50 +02:00
iabdalkader
5467993b38 Free FB memory if compress or compressed fail. 2017-01-12 04:51:21 +02:00
Kwabena W. Agyeman
9efd7474ae Fix compress and compressed. 2017-01-11 21:21:04 -05:00
Kwabena W. Agyeman
9f37c83def Minor Speed Improvements to Blob, QRCode, and Stats 2017-01-11 17:09:46 -05:00
Kwabena W. Agyeman
cf4cb787f5 Removed new_image_t, replaced with current image_t. 2017-01-10 18:26:49 -05:00
iabdalkader
58ef87cd85 ORB: Change default threshold to 85 and min match to 5. 2017-01-10 04:11:41 +02:00
iabdalkader
d42b2987a7 Fix lookup types for find_keypoints. 2017-01-09 01:30:08 +02:00
iabdalkader
02334ebea3 Init theta. 2017-01-09 01:11:27 +02:00
iabdalkader
1ab282a7eb ORB: estimate and return the angle of rotation. 2017-01-09 00:59:33 +02:00
iabdalkader
ff6b3d8367 Change default matching threshold to 70. 2017-01-09 00:40:37 +02:00
iabdalkader
c233131ca4 Change default max_keypoints to 100. 2017-01-09 00:29:22 +02:00
iabdalkader
6b7eb1a105 Add keypoints detector arg. 2017-01-08 18:40:28 +02:00
iabdalkader
05ea5115b5 Add constants for corner detectors. 2017-01-08 18:33:10 +02:00
iabdalkader
96e4f770c0 Use scale_factor instead of scale in find_features
* Updated all scripts.
2017-01-08 18:23:25 +02:00
iabdalkader
861627d61b Set default keypoint size to 10% of the image size 2017-01-08 17:53:59 +02:00
iabdalkader
7fa9894bdc ORB: Add max_keypoints and scale_factor args 2017-01-08 17:35:47 +02:00
Kwabena W. Agyeman
0111084125 Updated stats code
We now have a method to get an the normalized histogram of an image
patch. The histogram is returned as an object with methods too. You can
then get the stats off of the histogram or just get the CDF of it. The
CDF is particularly useful for automatically chaning the the color
tracking bounds.
2017-01-07 20:11:30 -05:00
iabdalkader
f47b96360a Fix keypoints size. 2017-01-07 18:42:52 +02:00
iabdalkader
6000684cb3 Fix keypoint drawing code. 2017-01-07 02:35:18 +02:00
iabdalkader
945a83c789 Add keypoints filtering function.
* This function filters keypoints far from the centroid, it's very useful for finding an accurate bounding box for an object.
  If a bounding box for the object is not needed, the centroid can be used instead since it's not affected too much by outliers.
* The filter finds the centroid of all the previously cross-matched keypoints then finds the mean, variance and standard deviation,
  it then filters keypoints with a distance higher than standard deviation from the centroid.
2017-01-05 05:35:57 +02:00
iabdalkader
e2d0c48401 Replace FREAK with ORB.
* New keypoints descriptor, much better implementation than FREAK.
2017-01-04 05:44:06 +02:00
Kwabena W. Agyeman
8dfba8b208 Fixed copy_to_fb for loading images. 2017-01-03 12:25:23 -05:00
Kwabena W. Agyeman
af15ec6eb3 New Blob Code
The new API is backwards compatible with the previous one except for
advanced features. The new blob code uses a flood fill algorithm that is
3x faster in filling out blobs that the previous code. On the M7 the
performance cap of 30 FPS is usually reached.

Additionally, blobs are objects with named attributes now so you don't
have to index access them anymore. However, index access is still
supported.
2016-12-27 19:10:24 -05:00
Kwabena W. Agyeman
6a8ce01e79 Add QRCode detection.
Use the qrcodes script and point your OpenMV Cam at google search
results for QR Codes.
2016-12-27 15:30:35 -05:00
iabdalkader
1645ab94b2 Allow image loading directly to FB.
* Allows bigger images to be loaded to FB.
2016-12-14 18:44:23 +02:00
iabdalkader
199a0d0f63 Make functions return arg image to string operations. 2016-10-17 21:33:47 +02:00
iabdalkader
fdf0b96270 Add clear image function.
* Sets image pixels to 0's.
2016-10-10 17:22:26 +02:00
iabdalkader
ef6e6303c7 Add HoG. 2016-10-10 17:22:04 +02:00
iabdalkader
14b6330b67 Add simple lens correction function. 2016-09-23 18:46:11 +02:00
iabdalkader
af24644f87 Remove match pointer from keypoint struct. 2016-09-23 18:44:23 +02:00
iabdalkader
6beb89c6e6 Fix image.gaussian return object. 2016-09-23 18:25:19 +02:00
iabdalkader
f55c9b48eb Add gaussian function. 2016-09-20 01:03:22 +02:00
iabdalkader
e81f228796 Add simple edge function. 2016-09-14 20:54:53 +02:00
iabdalkader
e104be63df Implement Canny edge detector. 2016-09-13 02:37:43 +02:00
iabdalkader
83451de050 Add Hough Transform. 2016-09-12 22:16:58 +02:00
iabdalkader
627d81f569 Add imlib mask_ellipse. 2016-09-08 03:12:14 +02:00
iabdalkader
9153325c06 Fix LBP for face recognition.
* Rollback to 7x7 windows.
* Use chi square for distance with weights optimized for facerec.
2016-09-08 01:29:17 +02:00
iabdalkader
8cdf53d3fb Return arg image in histeq. 2016-09-04 21:33:41 +02:00
iabdalkader
1cbcb7e434 Add M7 support 2016-08-12 22:43:50 +02:00
iabdalkader
0575314ab3 Make image pool work on arg image without changing FB. 2016-06-20 22:25:25 +02:00
Kwabena W. Agyeman
7af818c4d8 Finished up optical flow code.
* Added pooling functions to make getting small images easy. set_binning
works too... but, it zooms in way to much. pooling functions aout you to
shrink the image while not zooming in.

* To make the pooling functions easy to use I created a version that
pools the image out of place and one that pools the image in place. The
inplace pooling function can work on the frame buffer (see edits to
sensor.c)

* I added the code to do hann windowing to the FFT lib. However, I
commented it out after it improved performance by basically zero.
Specialized windowing stuff will only come in handy for folks trying to
tune their algorithm... not in general for everything.

* I added subpixel resolution for the phase correlation code. You can
now track the image movement really precisely. Additionally, I fixed up
the displacement outputs to give expected results. I also added a QoR
output for the displacement code so that you can know when the results
are bad.

* Finally, an example script has been added to show off the features.
2016-06-18 23:05:44 -04:00
iabdalkader
6adb46e1fa Make copy_to_fb accept (x,y) offset 2016-06-17 00:31:56 +02:00
iabdalkader
e40c50f739 Add diamond search for fast template matching. 2016-06-15 22:59:17 +02:00
iabdalkader
d9b03641f7 Add copy to framebuffer function and example script. 2016-06-15 21:59:36 +02:00
iabdalkader
2767791aae Add loop step argument to template matching. 2016-06-14 00:14:22 +02:00
iabdalkader
a25e0c162e Make template matching use ROIs. 2016-06-14 00:07:54 +02:00
Kwabena W. Agyeman
45fdcc973e Add FFT Code and Phase Correlation Code
The heart of the 1D FFT works. I tested this on the PC. However, 2D FFTs
may have issues and the phase correlation algorithm does not generate
the expected results. That said, most of the work is done. Stuff just
needs to be deubgged.

The FFT lib is designed to handle up to 1024 point real FFTs and 512
complex FFTs. As for 2D FFTs, we can do up to 64x64 pixels. After which,
we don't have enough RAM to handle them because they use up about 128KB
each.

Things to do... the 2D FFT needs to be verified. So, we need to run an
image through it and then back again to verify that there are no
problems. Then we need to compare the 2D FFT output with another 2D FFT
algorithm on the PC...

Once the FFTs are known to be good we then need to make sure the phase
corelation algorithm outs the correct results. We need to test that with
multiple shifted images, etc.
2016-06-12 19:55:45 -04:00
Kwabena W. Agyeman
7a01a9bede Final Imlib Cleanup
Finished going through imlib.c.
-> Histeq uses fb_alloc now and has hook for RGB histeq when reserve YUV
LUT is added (coming soon in next PR).

Cleanuped py_helper.c/h
-> No functional changes. Just added some header info.

Finished going through py_image.c
* 1 - Finished general code cleanup and updating everything to using new
library functions. In particular, I updated the remaining find_*
functions with the new roi clipping code when they accept rois.
* 2 - Made blob stuff return a list when nothing is found so you don't
have to do an if on the returned value anymore.
* 3 - img subscr is more powerful now allowing image reading and
writing. I updated this because I had to use it to find a previous bug
with socket.send() for the WINC driver.
* 4 - Renamed find_eyes to find_eye. Because it just finds one eye.
* 5 - Other than that just general code cleanup to make functions look
consistent.

And yes, changes have been test. Face tracking, eye tracking, keypoints,
etc. all work still.

Future things todo before release:
1 - Change all LAB stuff to YUV.
2 - Add in reverse YUV->RGB LUT and update functions like Mode() to use
this so they don't generate messed up outputs, also histeq() too.
3 - Add any remaining sensor control functions like agc control.
2016-04-25 21:55:49 -04:00
Kwabena W. Agyeman
367e5fdd3d Improved JPEG capabilities.
* Added the ability to control the quality on JPEG functions... However,
due to our JPEG implementation this doesn't seem to help. 90% JPEG
quality images and regular images should be about equal. But, you can
see heavy degredation with 90% still. E.g. text is unreabable. Not
exactly sure why this is happening but it can be fixed later.

* Changed the compress() function to compressed(). Also, it now
compresses using FB_Alloc to prevent realloc issues when compressing.

* Added new compress() function. This function compresses an image in
place and if that image is the frame bufffer then it will update the
frame buffer bpp value to reflect the image was compressed. Users can use
this function to basically finalize the frame buffer and then pass the FB
to functions that need to send image bytes. The benefit of using this
function is that it should allow higher quality JPEGs and let everything
run at a faster speed while connected to the IDE.

I made this function to speed up WiFi. However, I encountered a bug with
the winc.send() method. It appears to zero the bytes it sends. I didn't
debug further except to verify that the image data became zero after
calling send.
2016-04-23 16:37:21 -04:00
Kwabena W. Agyeman
b534dc6ffc Imlib cleanup
*Changed subimg to copy.
*Made blend work the same way as all our other double image argument
functions.
*Changed bilt to replace (the name of bilt is way to esoteric). Replace
gives you the basic assignment op.
* Removed scale/sacled. I removed this code because we don't want to
encourage people to scale things and allocate additional images in
memory. I decided to keep copy() for completeness sakes... but, I don't
see anyone using it. (By completeness sakes I mean that we now have the
assignment op, copy op, etc. for an image object).
* Removed rainbow. This feature is built into the FIR module now.

Moving on, compress needs to be renamed to compressed and a new compress
function will need to be added.

The compress() function will compress the image (or frame buffer, etc)
and not return a new object. The compressed() function will return a new
object and not compress the original image.

The compress function will make it easier for users to compress images
once they are done working on them before sending the image some where.
I don't see compressed() being used much then after adding the
compress() function. Since the compress() function won't use up heap
space this makes it very good.
2016-04-19 21:42:56 -04:00
Kwabena W. Agyeman
16daeb83a2 Fixed blob code.
Removed micropython code from the image libary. Also, blobs are now 10
tuple values by default now. The multilist thing has been removed from
blobs and it will return just a list of blobs instead of a tree of
lists.

Filter functions still work too.
2016-04-13 23:16:51 -04:00
Kwabena W. Agyeman
9e5d379c18 Remove old code.
Pixels, centroid, and orientation are calculated in the blob code now.
As for threshold, it is no longer needed (plus, it required storing a
secondary image in RAM which isn't really something we can handle).
2016-04-09 19:32:14 -04:00
Kwabena W. Agyeman
d1ff36602a New blob code.
Blob tracking has now been updated to work without requiring prior
segmentation of the image. You can still run it on a segmented image,
but, that is not needed anymore.

Use the copy color feature of the OpenMV IDE to get a color in the
image. Once you have that you can then pass the color to find_blobs which
will output a tuple of lists of blobs for each color. By default, all
blobs less than 1/1000th of the image are filtered out, however, you can
add a custom filter function which gets the image and the blob about to
be added to the list and you can decide to filter it or not.

For marker tracking, we now have a function called find markers which
basically merges all the blobs found by find blobs into one list of
blobs. Each new blob will have a color code value which will tell you
what colors are part of that blob. We support tracking up to 30 unique
colors this way.
2016-04-09 11:43:35 -04:00
Kwabena W. Agyeman
401b7b72a8 Added mean, median, and mode filters.
Mean filter -> Fast and easy to use. This will likely be the only filter
that gets alot of action on the M4.

Median filter -> Works really well, but, slow. On grayscale at 160x120
you can get also 10 FPS with it for a 3x3 kernel. That said, it's still
slow. Also, the code only works for 3x3 and 5x5 kernels.

About the previous histogram filter... technically, that filter should be
better. However, it suffers from a startup cost. The operation of finding
the median point in the histogram costs too much to compute. This is
what causes it to be slow. On very large kernels it will be faster than
the sorting median alrogithm I put up... but, large kernels will be too
slow for anyone to use anyway. The paper Ibrahim linked to about it
showed it being used for like 7x7 kernels and up... so, I think the
researcher who thought of the idea was really thinking about the
algorithm for large kernels.

Mode filter -> Works great on grayscale. Not so much on color. I think it
needs to be run on the LAB color space instead of the RGB color space. I
say this because it causes pretty strong artifacts around edges. When we
get more flash we'll be able to have a reverse lookup table for LAB to
make the mode filter better. Until then...
2016-04-04 20:04:50 -04:00
Kwabena W. Agyeman
978b4c12df Add Midpoint filter.
Has a bias value that allows you to control if its really a midpoint,
min, max filter, or something inbetween. Run at 160x120 or lower. 320x240
is slow (seems to be the case for all convoltions at that res).
2016-03-25 21:26:20 -04:00
Kwabena W. Agyeman
0ded05e333 Add color functions.
Now you can convert from rgb to lab/grayscale and vice-versa. Without
this functionality it would be hard to deal with moving between RGB and
LAB.
2016-03-03 23:48:04 -05:00
Kwabena W. Agyeman
488253a8b0 Add statistics function.
You can now get the color stats for an area in the image. The stats
function returns the mean, median, mode, min, max, st_dev,
lower_quartile, and upper_quartile.

This function allows you to automate binary and threshold functions
based on what's in the iamge.
2016-03-01 21:43:52 -05:00
Kwabena W. Agyeman
6ed08213b5 Fix unintialized error.
If the rect was not intersecting these functions would have returned bad
values.
2016-02-29 21:31:59 -05:00
Kwabena W. Agyeman
98f4dde21f Add new morph function.
The morph function lets you convolve the image with a kernel. It's
decently fast right now. But, in the future we'll have to optimize it by
a lot (unrolling loops, using SIMD instructions, etc.).

Anyway, along with morph I added an edge detection test script showing
how you can use a high pass filter on an image to get all the edges in
it. This is not as good as canny edge dection... but, it's about the
same and fast enough.

We'll need a Hough Transform system in the future to make edge dection
useful. Not sure how that will be implemented... so, that's going to be
far away for now.
2016-02-29 18:04:45 -05:00
Kwabena W. Agyeman
fb8b357c24 Dilate and errode working.
The old code did not actually implement the errode anhd dilate kernels
correctly. However, it migh have been a little faster because it avoided
the boundary problem.

In the future we can optimize all the kernel code to have different loops
for doing the edges of image versus the center. But, for now, this is
good enough. QVGA color tracking with kernels will be slow, but, the
speed can be improved with QQVGA resolution. Using a 3x3 kernel is
plenty fast. Larger ones are slower.

I also added the ability for you to set the threshold for erode and
dialte. This lets you make the kenrel a little bit smarter so that it
won't errode or dilate a pixel unless the threshold is met. Meaning,
you'll be able to use erode to erode an image down to 1 pixel wide
lines.
2016-02-28 16:27:49 -05:00
iabdalkader
7e1a4169a7 Add match_descriptor function.
* A single function to match LBP/FREAK descriptors.
2016-02-28 03:39:13 +02:00
iabdalkader
154cb5be69 Change descriptor functions to accept file pointer. 2016-02-28 03:22:07 +02:00
iabdalkader
acafef5207 Revert HaarCascade.
* HaarCascade doesn't really fit into generic descriptor functions.
2016-02-28 02:37:35 +02:00
iabdalkader
67acf7188c Use one load/save function for all descriptors. 2016-02-28 01:38:24 +02:00
Kwabena W. Agyeman
4ed49082a1 Background Subtraction is working!!!
Woot, all the effort to make it so you can manipulate the image buffer
with an image off disk works!

Try out the motion_detection script.
2016-02-27 11:58:37 -05:00
Kwabena W. Agyeman
1121a6f7df Fixed save image.
It now figures out the file type from the file extension. If no file
extension is given it just saves the file as BMP if its not a JPEG image
or JPEG if it's a JPEG image. If you specify an extension and the file is
not of that type then it will give you an error.

The new test_save.py should run until you reach the JPEG image part
where it quits due to lack of JPEG support natively on OV7725 boards.
Maybe JPEG mode should be supoorted by just compressing pictures?
2016-02-26 22:00:04 -05:00
iabdalkader
2b27d1542f Add save LBP descriptor. 2016-02-26 23:12:15 +02:00
Kwabena W. Agyeman
ac3d1e4373 Add BMP/PNM/JPEG image file loading and saving.
There's not a lot of actual functionality changes from the last commit.
However, switching the basic wrapper library to just long_jump on
failure and moving all the state info to structs required changes to all
the base functions in the last commit. The rest of the changes are to
link in the new functionality and to get the code to compile (usbdbg.c
edits).

Next I'll work on a function which abstracts the problem of opening an
image up and executing a line by line function op on it. I already
worked the code out for that. But, it's not in this commit to keep
things streamlined.
2016-02-25 22:26:56 -05:00
iabdalkader
074cf9d5e4 Add pointer to matching keypoint to kp_t struct.
* This way we don't need to allocate the array of matching keypoints
and risk running out of memory due to fragmentation. So all or nothing.
2016-02-26 01:44:23 +02:00
iabdalkader
5ff5419bf1 Return empty list if no blobs were found.
* Fixes #81
2016-02-26 00:47:31 +02:00
Kwabena W. Agyeman
d8ac8f9408 Fix swap bug.
The code clearly did not swap mins and maxes.
2016-02-24 19:07:57 -05:00
iabdalkader
4d7db778fb Remove angle from keypoint.
* Not really used right now and saves 4 bytes per keypoint.
2016-02-23 20:43:33 +02:00
iabdalkader
3a747ef952 Use arrays in FAST/FREAK keypoints.
* Using arrays for FAST/FREAK keypoints, this saves some memory
allocated for intermediate data.
2016-02-23 18:19:49 +02:00
iabdalkader
7a22cb3e5c Extract keypoints in one step.
* Make find_keypoints extract FAST keypoints.
2016-02-23 00:55:54 +02:00
iabdalkader
93d3885a3c Set keypoints threshold range between 0 and 100. 2016-02-22 20:31:24 +02:00
iabdalkader
9d02e2a42a Make match_lbp accept roi and extract second LBP. 2016-02-22 19:41:16 +02:00
iabdalkader
473b8025bf Fix assertion in find_features. 2016-02-22 19:39:26 +02:00
iabdalkader
015181588a Fix assertion in find_features. 2016-02-22 19:35:11 +02:00
iabdalkader
ddcb170af0 Cleanup py_image.c
* Code cleanups in py_image.c
2016-02-22 19:17:14 +02:00
iabdalkader
124a31772c Rename helper functions. 2016-02-22 18:47:35 +02:00
iabdalkader
5860695f5d Move helper functions to new files. 2016-02-22 18:41:50 +02:00
iabdalkader
0f48c12a89 Replace all mp_map_lookup()s with helper functions. 2016-02-22 18:35:00 +02:00
iabdalkader
93dc73c7f2 Fix arg to get_rectangle_kw. 2016-02-22 18:00:20 +02:00
iabdalkader
4c63721efa Use default roi when calling save_image 2016-02-22 17:55:26 +02:00
iabdalkader
4e5b200bdf Make keypoints_match return the % of matching kpts.
* Change keypoints_match to return the percentage of matching keypoints.
2016-02-22 03:31:15 +02:00
iabdalkader
032a6e3e4d Make Haar detector work on ROIs. 2016-02-21 23:01:34 +02:00
iabdalkader
50e53c91bc Change Iris detector to work on a single region.
* This allows the detector to work on ROIs detected by the eye Haar
  cascade, which improves the accuracy and speed of the detection.
2016-02-21 20:38:01 +02:00
Kwabena W. Agyeman
c3cfb117d0 Add frame differencing functions.
The negate function gives you the ability to negate an image before
running difference on it. The difference function will subtract two images
from each other and return the abs() of the result.

I believe it would have been optimal to work on the RGB565 image in the
LAB color space. However, since we don't have an inverse LAB lut this is
not possible. If we could replace LAB with YUV then that would free up
space to have an inverse YUV table (YUV->RGB).
2016-02-20 20:29:19 -05:00