mean value in a sphere

后端 未结 3 1211
面向向阳花
面向向阳花 2021-01-22 14:35

I\'m trying to calculate the mean value of the pixels inside a circle. In the future this needs to be extended to 3D, but for now a 2D sollution would already help me out.

3条回答
  •  渐次进展
    2021-01-22 15:21

    The question is missing a question, but I'll assume that it's not how to calculate whether pixels are fully inside or outside the circle. That's a relatively simple task. That is, a pixel is fully inside if the furtherest corner of the pixel to the centre is less than a radius away from the centre, and a pixel is fully outside if the closest corner of the pixel to the centre is more than a radius away from the centre.

    The question of what proportion of pixels on the circumference fall within the circumference is much trickier. There are two fundamental solutions:

    1. Exact and hard.
    2. Approximate and a bit easier.

    In both cases, note the horizontal and vertical symmetry means only the top right quadrant need be considered.

    Then, for (1), translate the circle centre to the origin (0, 0) and treat the circumference as the function y(x) = sqrt(r^2 - x^2). Then, the area of an overlapping pixel within the circle is the integral:

    integral(y(x) - y0, from x0 to x1, with respect to x)

    where y0 is the bottom coordinate of the pixel, x0 is the left coordinate and x1 is the right coordinate.

    This integral can be solved exactly with a trigonometric identity and a trigonometric substitution.

    For (2), just generate a set of random points within the pixel and count how many of them fall within the circumference. As the set gets larger, the proportion of points that fall within the circumference to the count of all point approaches the proportion of the pixel within the circumference.

提交回复
热议问题