01-05-2010 08:31 AM
Hello,
I am trying for 2 days to get this right, but I cannot solve this problem.
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!
Solved! Go to Solution.
01-05-2010 08:45 AM
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.
01-05-2010 09:29 AM
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
01-05-2010 11:05 AM
The soluce of Paul seems to be ok :
01-05-2010 11:16 AM
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... 😉
01-05-2010 11:23 AM
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)
01-05-2010 11:25 AM
01-05-2010 11:38 AM
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. 🙂
01-05-2010 12:30 PM
01-05-2010 01:47 PM