LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Create & extract circle out of a 2D-Array

Solved!
Go to solution

Hello,

 

I am trying for 2 days to get this right, but I cannot solve this problem.

 

Circle Out of 2D-Array

 

I have an 2D-Array containing random integers. I want to create a circle and extract and add all values that are inside that circle.

 

Inputs:

- 2D-Array = 2D-Array with random integers 

- Center of Circle = coordinate in array that specifies the center of the circle

- Radius = is the radius of the circle in values of array (in example picture this is 3)   

 

Output:

- Integer = sum of all values inside the circle

 

The radius of the circle, the size of the 2D-array, the values of the 2D-Array and the Center of the Circle are variable.

 

 

Can you guys give me suggestions how to solve this problem? :mansurprised: 

Kudos will be given! 

--------------------------------
The Enrichment Center is required to remind you that you will be baked, and then there will be cake.

0 Kudos
Message 1 of 15
(14,362 Views)

There might be a more efficient method but I would use the circle formula to determine if an index is inside of the circle

if (Xi-Xc)^2 +(Yi-Yc)^2 >r2 then add to a shift register.  You can easily narrow down this by extracting the subset rectangle that contains the circle (center +/- Radius in each direction),  this would increase the speed limiting it to the order of the number of pixels of the square r x r.

Depending on what you are doing, if you are summing this alow and the circle does not move, make a mask, an array of same size as the circle where value of 1 is in the circle, value of 0 is out of the circle and miltiply the 2 arrays, then sum.  This is how i do this in an image space and it is very fast.

 

Paul Falkenstein
Coleman Technologies Inc.
CLA, CPI, AIA-Vision
Labview 4.0- 2013, RT, Vision, FPGA
Message 2 of 15
(14,344 Views)

I like Paul idea but it doesn't give the expected result (as for the yellow circle displayed some values like 0 would not be included). Maybe by using a larger radius like (Xi-Xc)^2 +(Yi-Yc)^2 < [r^2 + (r+1)^2]/2. A value between r and r+1.

The main issue is probably to define what you labelled a circle by an equation.

Ben

Message 3 of 15
(14,318 Views)

The soluce of Paul seems to be ok Smiley Wink :

 

CircleSum.jpg

Message 4 of 15
(14,288 Views)

No need to built the array, that's just a waste of memory. All you need is a scalar shift register initialized with zero and then replace the "built array" with an "add".

 

Personally, I would probably create a circle in a small array (=size of the circle) and then generate a 2D array of indices to get from the big 2D array with with the offset shifted to the new center. There is no need to loop and test through a huge array just to get a few matching elements. There are many other ways to do this. Another possibility would be to get the square subset first. Whatever... 😉

0 Kudos
Message 5 of 15
(14,283 Views)

Definitly using the subset would speed up the algorithm if the circle is a small sublet of the overall area.  Glad it works, I thought I had forgotten my math.  If you wanted to be very slick you could even do some antialliasing for sub pixel accuracy (if the difference between the radius and edge pixel is less than 1 then use the fraction of the value to add to the sum but this depends on the nature of the data)

Paul Falkenstein
Coleman Technologies Inc.
CLA, CPI, AIA-Vision
Labview 4.0- 2013, RT, Vision, FPGA
0 Kudos
Message 6 of 15
(14,277 Views)
Sure for the array Smiley Wink, just before posted my message, I moved it as this simple array of the value but firstly, it was the array of cluster including coordinates of element so as lazily, I didn't make the effort to change it as scalar....
0 Kudos
Message 7 of 15
(14,275 Views)

The correct algorithm also really depends on what you actually need to do.

 

For example if you would need to repeat that algorithm for every possible midpoint element in the 2D array, a 2D convolution with a normalized circle of the correct size would be the way to go. 🙂

 

(e.g. similar to this)

0 Kudos
Message 8 of 15
(14,264 Views)
I did this for fun. This sounded like a fun little project. I am sure that it is more complicated than it needs to be and everyone will point that out but here goes anyway.
Tim
GHSP
Message 9 of 15
(14,238 Views)

Try this one (LV2009).

 

Ben

Message 10 of 15
(14,223 Views)