CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 13 of 13
  1. #1
    Join Date
    Apr 2009
    Posts
    27

    Number of pixels at radius r

    I have written the following code to derive find out the average intensities at a given radius from the center of an array.

    void temp_match_img::makevec(){
    int temp;
    for(int i=-radius; i<=radius; i++){
    for(int j=-radius; j<=radius; j++){
    temp=(int)sqrt(float(i*i+j*j)); //distance of the point from the local center
    if(temp<=radius){
    rpvec[temp]+=tmimg[i+row][j+col]/temp; //The average intensity at //radius=temp
    }
    }
    }
    }


    Can anyone tell me what is approximately the number of pixels in a circle say "n" pixels in radius?
    My current approximation is that it is some constant into "temp" - which is the distance of any point from the center of the array.

    N.B. The array has odd dimension, e.g. 21X21 or 105X105

    Also please let me know how to publish codes. People on this forum publish codes in good looking format, and I do the same in the above ugly format.

  2. #2
    Join Date
    Feb 2005
    Posts
    2,160

    Re: Number of pixels at radius r

    Well, roughly, the number of pixels would be the circumference of the circle so: 2*PI*r.

    Oh, use [code][/code] tags around your code.

  3. #3
    Join Date
    Jan 2010
    Posts
    1,133

    Re: Number of pixels at radius r

    I got the impression that the OP wanted to know the number of pixels inside the circle area with the radius of r pixels.
    In that case, shouldn't it be, again roughly, PI*r^2?

  4. #4
    Join Date
    Oct 2006
    Location
    Sweden
    Posts
    3,654

    Re: Number of pixels at radius r

    Hm. I would say the number of pixels would be about PI*r2.
    Debugging is twice as hard as writing the code in the first place.
    Therefore, if you write the code as cleverly as possible, you are, by
    definition, not smart enough to debug it.
    - Brian W. Kernighan

    To enhance your chance's of getting an answer be sure to read
    http://www.codeguru.com/forum/announ...nouncementid=6
    and http://www.codeguru.com/forum/showthread.php?t=366302 before posting

    Refresh your memory on formatting tags here
    http://www.codeguru.com/forum/misc.php?do=bbcode

    Get your free MS compiler here
    https://visualstudio.microsoft.com/vs

  5. #5
    Join Date
    Apr 2009
    Posts
    27

    Re: Number of pixels at radius r

    Sorry guys, by "in circle" I meant in the set represented by the circle. Basically the circumference.

    The answer is not very close to 2*(pi)*r - neither is the answer for the number of pixels inside the circle close to (pi)*r^2.

    There's some good approximation for both - I somehow am not able to remember.

  6. #6
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: Number of pixels at radius r

    Quote Originally Posted by sgsawant View Post
    Sorry guys, by "in circle" I meant in the set represented by the circle. Basically the circumference.
    Well, it gets a bit tricky defining precisely what's on the circumference. I presume you mean pixels on the outer boundary of those which fall inside the circle. I would expect that to be close to 2*pi*r.

    The answer is not very close to 2*(pi)*r - neither is the answer for the number of pixels inside the circle close to (pi)*r^2.
    You'll have to back that up with specific numbers if you want to claim that mathematics which has existed for thousands of years no longer works.

    Try it out on a 5x5 grid (radius 2)-----the filled circle contains 13 elements, and pi*2*2 is about 12.5. Coincidentally, the estimate of the circumference is the same in this case; if you go with 4-connectedness, then you've got 12 elements, so it's still close. Going with 8-connectedness yields only 8 elements on the boundary, but since the distance between each is now sqrt(2) rather than 1, you can estimate that as 2*pi*r/sqrt(2) = 8.8.

    The approximations should get better as you increase the radius. You may not simply be able to divide by sqrt(2) once your circle gets big enough that you need a combination of 4-connected and 8-connected pixels to represent it, so just stick with 4-connectedness is my suggestion.

  7. #7
    Join Date
    Aug 2000
    Location
    New York, NY, USA
    Posts
    5,656

    Re: Number of pixels at radius r

    Quote Originally Posted by sgsawant View Post
    Sorry guys, by "in circle" I meant in the set represented by the circle. Basically the circumference.

    The answer is not very close to 2*(pi)*r - neither is the answer for the number of pixels inside the circle close to (pi)*r^2.

    There's some good approximation for both - I somehow am not able to remember.
    Those formulas are pretty good approximation
    You are truncating the return of sqrt() function, you need to round it:
    Code:
    temp=(int)(sqrt(float(i*i+j*j)) + .5); //distance of the point from the local center
    Here is my test program:
    Code:
    #define _USE_MATH_DEFINES
    #include <math.h>
    
    void foo(int radius, int& countArea, int& countCirc)
    {
    	int temp;
    	for(int i=-radius; i<=radius; i++)
    	{
    		for(int j=-radius; j<=radius; j++)
    		{
    			temp=(int)(sqrt(float(i*i+j*j)) + .5); //distance of the point from the local center
    			if(temp<=radius)
    				countArea++;
    
    			if(temp==radius)
    				countCirc++;
    		}
    	}
    }
    
    int main()
    {
    	int radius = 105;
    	int countArea = 0;
    	int countCirk = 0;
    	foo(radius, countArea, countCirk);
    	int area = M_PI * radius * radius;
    	int circ = 2 * M_PI * radius;
    	return 0;
    }
    For radius of 105 I got ~1% error, but it goes down for greater values of radius.
    Vlad - MS MVP [2007 - 2012] - www.FeinSoftware.com
    Convenience and productivity tools for Microsoft Visual Studio:
    FeinWindows - replacement windows manager for Visual Studio, and more...

  8. #8
    Join Date
    Apr 2009
    Posts
    27

    Re: Number of pixels at radius r

    Oh.. ok. I was a bit concerned about the lower values. I guess if I manually enter the lower values the higher values can be estimated with reasonable accuracy as you have shown.

  9. #9
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: Number of pixels at radius r

    Yeah, that's a perfectly reasonable approach to take.

  10. #10
    Join Date
    Apr 2000
    Location
    Belgium (Europe)
    Posts
    4,626

    Re: Number of pixels at radius r

    The answer depends entirely on the actual algorithm used to draw the circle. there's pixel approximation algorithms that tend to be 'thicker' and there's algorithms that never fill a full angle.
    Code:
       *      *
       **      *
    full     no full angle

  11. #11
    Join Date
    Feb 2015
    Posts
    2

    Re: Number of pixels at radius r

    Quote Originally Posted by hoxsiew View Post
    Well, roughly, the number of pixels would be the circumference of the circle so: 2*PI*r.
    ...
    Actually, the number of distinct pixels drawn by the midpoint circle algorithm is 4 round(sqrt(2) r), or equivalently, 4 floor(sqrt(2) r + 1/2), which is approximately 4 sqrt(2) r. This is about 10% smaller than 2 pi r.
    Last edited by M.Aramini; February 17th, 2015 at 09:19 PM. Reason: Changed "π" back to "pi" for better readability.

  12. #12
    Join Date
    Jul 2013
    Posts
    576

    Re: Number of pixels at radius r

    Quote Originally Posted by sgsawant View Post
    I have written the following code to derive find out the average intensities at a given radius from the center of an array.
    Another approach is to actually "draw" the circle using a circle drawing algorithm. See the example C# implementation here,

    http://en.wikipedia.org/wiki/Midpoint_circle_algorithm

    You could easily modify it to visit all "pixels" on as well as inside a circle of a certain radius and do what ever calculations you like on the "pixels" you encounter. And of course you always know how many "pixels" you have visited.

    This is even more efficient than your current algoritm since there's no expensive square-root calculation and you only visit exactly those "pixels" you're interested in.
    Last edited by razzle; February 17th, 2015 at 05:09 AM.

  13. #13
    Join Date
    Feb 2015
    Posts
    2

    Re: Number of pixels at radius r

    Quote Originally Posted by Lindley View Post
    ...
    You'll have to back that up with specific numbers if you want to claim that mathematics which has existed for thousands of years no longer works.
    ...
    The thousands-of-years-old mathematics for the length of the circumference of a circle is, of course, (still) correct.

    However, the number of pixels does not have to be (and, in fact, isn't) the same, or even very close to, the length of the circumference of a circle.
    Last edited by M.Aramini; February 17th, 2015 at 05:04 PM. Reason: Improved wording.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured