Options

# Is there a way to use statistical analysis in Image-Pro Plus

edited June 2013

(Originally posted by tommus on 11/30/2006)

I would like to calculate the median of an array. One way is to calculate the median myself by generating the array, sort the array values and determine the median. I have done this by using the bubble sort algorithm to sort the array and determine the median. Unfortunately, this sorting algorithm was very slow in IPP. I know there are faster algorithms but as I am not a professional programmer, I have a hard time implementing these (if possible at all in VBA scripting).
Exporting the array values to eg excel and calculate the median there is not an option since I need this median to set thresholds later on in the script. Does anyone now if there is a fast way to calculate the median inside IPP?

• Options
edited June 2013

(Originally posted by YuriG on 11/30/2006)

There is a DLL from Array-Pro, which can be used in IPP for statistical analysis. Download the file:
ftp://ftp.mediacy.com/pub/forumBMPs/IpMath32.zip

Extract IpcMath.bas to the BAS folder and ipmath32.dll to the main folder of Image-Pro. Then you can call all functions listed in IpcMath.bas from IPP macros (stats,sort,...), description of the functions and parameters are also there.
Here is the example how to get median value from an array of single values:

```
Sub GetMedianTest()
Dim inArray(1000) As Single,i%
'fill array with random numbers
For i=0 To 1000
inArray(i)=Rnd(1)
Next i
Dim Stats(15) As Single
'get statistics
IpMathGetStats(IMC_FLOAT,1000,inArray(0),Stats(0),0,2)
Debug.Print "Mean value = " & Stats(0)
Debug.Print "Median value = " & Stats(11)
End Sub
```

• Options

(Originally posted by Tommus on 12/4/2006)

Thanks a lot Yuri. Your solution works great. I get almost instant median values of my arrays.

• Options
edited June 2013

(Originally posted by Rik on 12/27/2006)

Can the IPMath32 plugin also be used to calculate the median of an image?
I have to following code to transfer image intensity (per pixel) to a two dimensional array:

Dim iInfo As IPDOCINFO
ret=IpDocGet(GETDOCINFO, DOCSEL_ACTIVE, iInfo)
ReDim ImBuf(1 To iInfo.Width,1 To iInfo.Height) As Integer
ret=IpDocGetArea(DOCSEL_ACTIVE,iInfo.Extent,ImBuf(1,1),0)

Next the IPMath32 code to calculate the median of the image:

Dim Stats(15) As Single

IpMathGetStats(iInfo.iClass,(iInfo.Width * iInfo.Height),ImBuf(1,1),Stats(0),0,2)

Debug.Print "Mean value = " & Stats(0)
Debug.Print "Median value = " & Stats(11)

The second part of the code generates an (10062) Overflow error message.

• Options

(Originally posted by YuriG on 12/27/2006)

Rik,

The problem in your case is caused by overflow multiplying 2 integers iInfo.Width * iInfo.Height. You have to extend the first to Long. The following code works:

IpMathGetStats(IMC_GRAY16,(CLng(iInfo.Width) * iInfo.Height),ImBuf(1,1),Stats(0),0,2)

Note, that I also used IMC_GRAY16 as data type, because the data type is Integer (not image type).