LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Averaging values in 2-D Array in a circular area

Solved!
Go to solution

Thank you everyone for your help and time.  Your solutions helped me create a program that I needed to average a 2-d array area as a function of the radius of the circle.  Here is the code for anyone that is interested.

0 Kudos
Message 31 of 38
(3,053 Views)

Sorry, but your code is very (very!) inefficient and orders of magnitude slower than it could be. To create the mask, all you need is sweep the square area and set the elements to zero or 1, depending on the distance from the center. Touch each potential element exactly once!

Even if you want to graph the temperature as a function of radius, you don't need to generate these huge data structures. Do everything "in place" in the first loop. For example keep two 1D arrays if size 2000, one for the count and one for the sum, and then accumulate histograms directly in one step, followed by division at the end. 

 

 

You instead sweep in a circular motion with a fixed iteration count. In the first iteration (r=0), you are owerwriting the same element 2000 times, in the next iteration (r=1) things don't improve much and in the last iteration you might generate holes if the radius is too big. You are doing orders of magntude more operations than needed!

  

Don't use these gigantic diagram constants. Use "Initialize array" instead to make the code more readable. There is no penalty, because the code gets folded into a constant at compile time anyway.

 

Don't place controls and indicators inside the loops. The values are constant for the duration of the loop, so there is no need to poll the indicator 2000 times in a row for the same value.

 

Why don't you go back and study my example again, it's much better. 🙂

 

Also please add some real data to the VI so we can run it without guessing.

0 Kudos
Message 32 of 38
(3,046 Views)
altenbach, I wish I could look at your code.  I'm working with an older version of labview(8.2) and I'm not allowed to install anything on the computers I work with.  I still don't quite understand what you mean by doing things "in place" to increase the code's efficiency.  Are you suggesting I not make a mask for each radius?  If so how else do check the array for each radius, since now I don't want the full circle but just the outer edge?  I understand that using a constant to break down the angles in the circle could catch up with me if the radius is large enough and is also highly inefficient at smaller radii; but I did it for simplicity since I knew the dimensions of my array 240x320 and it would work for my specific application.  I welcome all your suggestions on how to better my program and thanks for you continued help.
0 Kudos
Message 33 of 38
(3,013 Views)

Here's the LabVIEW 8.2 version of my earlier code.

 

What do you mean by "outer edge"?

0 Kudos
Message 34 of 38
(2,995 Views)

When I said outer edge I meant the edge of the circle at a certain radius.  I am trying to break down the circle into discrete radii and average the values at each radii.  I thought using a mask would be the best way to accomplish this since I couldn't think of a way to use a case structure.  I figured since I'm only appoximating circles that mathmatically using a case structure would be difficult(maybe use a greater than and a less than check to see if the points lie between radii?).  I am interested in your suggestions since I am running this code multiple times for a sequence of arrays so any improvement will definately be noticed.

 

I cleaned up my code a little, took away an extra for loop that was unnessesary.  I had an idea to make the loop that breaks up the angles of the circle to be a function of the circles radius.  But I am mediocre at math at best so I guess I'll just experiment with some exponential functions.

0 Kudos
Message 35 of 38
(2,976 Views)

You are still running way too many iterations than warrented by the number of pixels in your image.

 

All you need to do is iterate over each pixel in the image, measure the distance to the center, and then make a histogram of average vs. r. Instead of averaging only one group as in my previous example, keep two 1D arrays, one to keep the sum(r) and one to keep the count(r), with the r mapped into the array index. At the end, divide the two 1D arrays to get the average as a function of distance.

0 Kudos
Message 36 of 38
(2,965 Views)

OK, here's a quick draft of what I had in mind (LV 8.2). Modify as needed.

 

(Move the center with the mouse to adjust).

 

 

Message Edited by altenbach on 08-11-2009 02:34 PM
Download All
Message 37 of 38
(2,949 Views)
altenbach, your the man.  Took me awhile to figure out your code but it's very clever.  Thanks for the help.
0 Kudos
Message 38 of 38
(2,927 Views)