How to measure volume of objects with irregular shapes

Discussion related to "under the hood" OpenMV topics.
augur4u
Posts: 17
Joined: Tue Oct 23, 2018 4:38 am

How to measure volume of objects with irregular shapes

Postby augur4u » Sat Jan 05, 2019 4:18 am

Hi guys,
I wanna use vertical and horizontal cameras to measure the volumes of objects with irregular shapes on a moving conveyor belt.
Now I can get the pixels and area of vertical and horizontal blobs of the same object with openmv’s image.find_blobs().
But it is not accurate to just use the pixels and area of vertical and horizontal blobs of the same object with irregular shape to compute x*y*z of the blob for the object’s volume.
Could you give me some suggestions?
Looking forward to your answers.
Thank you very much!
augur4u
Posts: 17
Joined: Tue Oct 23, 2018 4:38 am

Re: How to measure volume of objects with irregular shapes

Postby augur4u » Sat Jan 05, 2019 9:56 am

Can I get coordinates of every pixels in blob found with find_blobs()?
If getting the coordinates, I can use calculus’s method to compute the volume accurately.
User avatar
kwagyeman
Posts: 2606
Joined: Sun May 24, 2015 2:10 pm

Re: How to measure volume of objects with irregular shapes

Postby kwagyeman » Sat Jan 05, 2019 12:21 pm

Hmm, I see what you want to do. Um, so, the number of pixels in each blob is exposed. Can you use this value to guess? Otherwise, you need to edit the C firmware to do what you want:

The loop that does the math accumulation is right here: https://github.com/openmv/openmv/blob/m ... lob.c#L250. If you study the find_blobs() code you can add new fields to the blob objects to keep track of the pixels.

Note... I'm going to extend the find_blobs() code this weekend for another customer. I can probably add a new feature for you too if it's some mathematical value I can just add as a field. If possible, study the code and get back to me ASAP on whatever new calculations you need and I'll add then.

That said, it has to be something that's very fast to compute in that inner loop. However, once the blob is finished being mapped there's a math section at the end: https://github.com/openmv/openmv/blob/m ... lob.c#L333 Where the output values are formed.
Nyamekye,
augur4u
Posts: 17
Joined: Tue Oct 23, 2018 4:38 am

Re: How to measure volume of objects with irregular shapes

Postby augur4u » Sat Jan 05, 2019 1:27 pm

Hi,
Thank you so much for your quick help!
Could you add and expose a array pixels[w][h] in the blob class to describe which pixels is inside of the blob and which ones are out of the blob but inside the rectangle bounding the blob:
If pixels[j]=1, the pixel is inside the blob;
If pixels[j]=0, the pixel is out of the blob but inside the rectangle bounding the blob.
If saving memory, we can also use bits to describe which pixels are in the blob.
Looking forward to your extending the find_blobs() code!
Thank you again!
Augur
augur4u
Posts: 17
Joined: Tue Oct 23, 2018 4:38 am

Re: How to measure volume of objects with irregular shapes

Postby augur4u » Sat Jan 05, 2019 1:37 pm

Hi,
Thank you so much for your quick suggestion and help!
Could you add and expose a array pixels[w][h] in the blob class to describe which pixels is inside of the blob and which ones are out of the blob but inside the rectangle bounding the blob:
If pixels [j]=1, the pixel is inside the blob;
If pixels [j]=0, the pixel is out of the blob but inside the rectangle bounding the blob.
If saving memory, we can also use bits to describe which pixels are in the blob.
Looking forward to your extending the find_blobs() code!
Thank you again!
Augur
augur4u
Posts: 17
Joined: Tue Oct 23, 2018 4:38 am

Re: How to measure volume of objects with irregular shapes

Postby augur4u » Sat Jan 05, 2019 1:39 pm

ACD6A785-F61C-4A1E-A131-592C3818E400.png
User avatar
kwagyeman
Posts: 2606
Joined: Sun May 24, 2015 2:10 pm

Re: How to measure volume of objects with irregular shapes

Postby kwagyeman » Sat Jan 05, 2019 1:41 pm

Hi, I can't expose that because of how much RAM it would take. I can only expose a mathematical derivative of the pixels inside of the blob. Please try to work out what mathematical operation you need done and then I can implement it in C code.

Please keep in mind that memory on the microcontrollers is not as large as a PC. A blob can be the size of an image and we support VGA image sizes for find_blobs() so the RAM for that would be huge. Also, iterating over that array in python would be terribly slow.
Nyamekye,
augur4u
Posts: 17
Joined: Tue Oct 23, 2018 4:38 am

Re: How to measure volume of objects with irregular shapes

Postby augur4u » Sat Jan 05, 2019 2:03 pm

Hi,
Yes, the memory is very limited in openmv.
But I wanna know the number of every column in blob.
Could you have better solutions?
Thank you.
augur4u
Posts: 17
Joined: Tue Oct 23, 2018 4:38 am

Re: How to measure volume of objects with irregular shapes

Postby augur4u » Sat Jan 05, 2019 2:06 pm

I wanna know the number of pixels in every column of the blob.
User avatar
kwagyeman
Posts: 2606
Joined: Sun May 24, 2015 2:10 pm

Re: How to measure volume of objects with irregular shapes

Postby kwagyeman » Sat Jan 05, 2019 2:09 pm

Um, can you write the code you need to parase that pixel array you talked about and I'll see what I can put in the C code.

I understand you need two cameras and have to take a view into account. However, if you do rotation correction on each cameras image then that removes that variable. So, then you just have two flat images from either side of the object to combine.

Anyway, work on the problem. Give me some code for it and I'll see what's valuable to add to the camera C code.
Nyamekye,
User avatar
kwagyeman
Posts: 2606
Joined: Sun May 24, 2015 2:10 pm

Re: How to measure volume of objects with irregular shapes

Postby kwagyeman » Sat Jan 05, 2019 2:11 pm

Mmm, number of pixels in every column is a reduction.

That's kinda doable. It's still a lot of RAM however. Please work on the full algorithm and see if we can get more reductions.

I can do like a flag which would enable population of an array value otherwise which would be null if the flag isn't passed to the method.
Nyamekye,
augur4u
Posts: 17
Joined: Tue Oct 23, 2018 4:38 am

Re: How to measure volume of objects with irregular shapes

Postby augur4u » Sat Jan 05, 2019 2:53 pm

Hi,
Good idea! I rethink my solution again. Thank you!
By the way, Could you add a parameter named pixels_distribution in find_blobs()?
If the function is called with an array parameter myarray[w] like image.find_blobs(,pixels_distribution=myarray,), the function will compute the number of pixels in every column and give myarray.
Otherwise doesn’t compute the number.
Okay?
Thanks.
augur4u
Posts: 17
Joined: Tue Oct 23, 2018 4:38 am

Re: How to measure volume of objects with irregular shapes

Postby augur4u » Sat Jan 05, 2019 3:48 pm

Hi, your solution is perfect “I can do like a flag which would enable population of an array value otherwise which would be null if the flag isn't passed to the method.”
Yes, adding a flag parameter in find_blob() and a method named pixels_distribution() which returns one-dimensional array.
augur4u
Posts: 17
Joined: Tue Oct 23, 2018 4:38 am

Re: How to measure volume of objects with irregular shapes

Postby augur4u » Sat Jan 05, 2019 3:54 pm

47085EAA-0EAA-487F-8C84-D53FA0F77B98.png
EA58F821-0D80-4B43-AE44-2BCBCE114F2B.png
User avatar
kwagyeman
Posts: 2606
Joined: Sun May 24, 2015 2:10 pm

Re: How to measure volume of objects with irregular shapes

Postby kwagyeman » Sat Jan 05, 2019 6:03 pm

Okay. I'll add the binary image projections:

http://www.cse.usf.edu/~r1k/MachineVisi ... apter2.pdf

For both w and h. I can allocate an array that's static for the width and height of the image and the accumulate into both. Then, after the accumulation I can find the start and stop of non zero bins and then and bin the bins into a histogram where you select how many bins of output you want. Finer detail means more RAM and processing time in python.

...

I thought about adding the contour but the problem is that I have to store the contour points in the MP heap which isn't designed to deal with lots of small allocations and will greatly slow the algorithm down along with easily running out of RAM depending on if the image gets too complex.

I could allocate the contour on the frame buffer stack in a temporary heap but then it would still need to be transferred to the MicroPython heap and it would still require a lot of space.
Nyamekye,
User avatar
kwagyeman
Posts: 2606
Joined: Sun May 24, 2015 2:10 pm

Re: How to measure volume of objects with irregular shapes

Postby kwagyeman » Sun Jan 06, 2019 8:44 pm

Okay, it's done.

Just add x_hist_bins_max=<a number> and y_hist_bins_max=<a number> to find_blobs().

Then you can get the histogram list via the x_hist_bins() and y_hist_bins() properties. X and Y are completely separate. So, enable one, or the other, or both. The returned value above is a python list of ints where the ints are the number of pixels across the object. For X this would be the height per x pixel location, for y this the width per y pixel location.

Later on I will make the returned value a histogram object so you can perform more interesting things on it. Right now it's just a list. (Probably will work on this feature later when I get to doing histogram upgrades).

Note - this feature takes up a lot of heap RAM. On the M4 is definitely a no-go. On the M7 it's probably okay. The H7 it's not possible to break things.

The max bin numbers control how binning is done. If the value there is really large you get per pixel bins. If you make it smaller you get less than per pixel bins but less memory usage. Note that the number of bins returned will never be larger than the object width/height. So, the max is used a max limiter.

Please avoid using callbacks with this on... otherwise I have to keep multiple copies of the pixel lists in RAM. It can get heavy fast.
Attachments
firmware.zip
(916.17 KiB) Downloaded 9 times
Nyamekye,
augur4u
Posts: 17
Joined: Tue Oct 23, 2018 4:38 am

Re: How to measure volume of objects with irregular shapes

Postby augur4u » Wed Jan 09, 2019 11:07 am

Hi kwagyeman,
Wow,quick and good job!
I really appreciate your assistance!
These days I am very busy with exams.
This weekend I will have enough time to develop my project.
I will seek your instruction if I will encounter problems.

Thank you so much. :D

Return to “Technical Discussion”

Who is online

Users browsing this forum: No registered users and 6 guests