Intel® Integrated Performance Primitives
Deliberate problems developing high-performance vision, signal, security, and storage applications.
Announcements
FPGA community forums and blogs have moved to the Altera Community. Existing Intel Community members can sign in with their current credentials.

How to compute the Median value of an Image

Kadir_Kirtac
Beginner
2,259 Views
Hello, how can I compute the median value of my image?
0 Kudos
9 Replies
alexey_stetsenko
Beginner
2,260 Views

Hi Kadir,

find a histogram and take the central value of it (median = histogram[histogram.length /2])

sorry:

find a histogram, sort histogram index by value and take level with mean histogram index, example in c#:

*****
IppiSize roi_size = new IppiSize(Width, Height);
byte tmp_max = 0;
byte tmp_min = 0;
ippiMinMax_8u_C1R((byte*)data, Stride, roi_size, &tmp_min, &tmp_max);
min = tmp_min;
max = tmp_max;
int[] hist = new int[tmp_max - tmp_min + 1];
int[] levels = new int[hist.Length + 1];
fixed (int* phist = &hist[0], plevels = &levels[0])
{
ippiHistogramEven_8u_C1R((byte*)data, Stride, roi_size, phist, plevels, levels.Length, tmp_min, tmp_max + 1);
}

int[] tmp = Array.Sort(levels);
int median = tmp[tmp.Length / 2];


Array.Sort(levels, hist);
int median = levels[levels.Length / 2];

0 Kudos
Kadir_Kirtac
Beginner
2,260 Views
wouldn't the following code work for median value calculation ?

Ipp32f *pSrcSort = ippsMalloc_32f(len);
ippsCopy_32f(pSrc, pSrcSort, len);
ippsAbs_32f_I(pSrcSort, len);
ippsSortAscend_32f_I(pSrcSort);
Ipp32f medVal = pSrcSort[len/2];
0 Kudos
alexey_stetsenko
Beginner
2,260 Views
it will be working fine, but you need to allocate more memory
0 Kudos
alexey_stetsenko
Beginner
2,260 Views

and another problem may be happened, if image has the aligned allocation in the memory, then it will be wrong result...

for small images like width * height < 256 your code ispreferable

0 Kudos
alexey_stetsenko
Beginner
2,260 Views

the last two line of code is not correct, as i thought

Array.Sort(levels, hist);
int median = levels[levels.Length / 2];

It should be found the first index of integral amplitude which has intersection with Width*Height/2 value:

int a = 0;
int center = Width * Height / 2;
for(int i = 0; i < hist.Length; i++)
{
a += hist;
if (a >= center)
{
median = level;
break;
}
}

0 Kudos
Kadir_Kirtac
Beginner
2,260 Views
what you mean by aligned allocation is probably, if the image width is not a number which is multiple of 8, than IPP adds zero values to align the image width to the nearest multiple of 8. Than I will get a wrong result in median calculation by considering also these zero values at the end of the aligned lines, since I can't specify the ROI in ipps functions, am I right ?
0 Kudos
alexey_stetsenko
Beginner
2,260 Views

Ipp32f *pSrcSort = ippsMalloc_32f(len);
ippsCopy_32f(pSrc, pSrcSort, len);
ippsAbs_32f_I(pSrcSort, len);
ippsSortAscend_32f_I(pSrcSort);
Ipp32f medVal = pSrcSort[len/2];

yes, that is what i did mean, the IPP use a 32-bytes boundary, but if you change the function ippsCopy_<> to ippiCopy_<> it will be OK. Is the pixel of image the float value? Why you use the ippsAbs_32f_I(pSrcSort, len)?

0 Kudos
Kadir_Kirtac
Beginner
2,260 Views

Ipp32f *pSrcSort = ippsMalloc_32f(len);
ippsCopy_32f(pSrc, pSrcSort, len);
ippsAbs_32f_I(pSrcSort, len);
ippsSortAscend_32f_I(pSrcSort);
Ipp32f medVal = pSrcSort[len/2];

yes, that is what i did mean, the IPP use a 32-bytes boundary, but if you change the function ippsCopy_<> to ippiCopy_<> it will be OK. Is the pixel of image the float value? Why you use the ippsAbs_32f_I(pSrcSort, len)?

Yes, image pixel values are of type float; to make the negative signed values positive, I use the ippsAbs function;
so ippiCopy may help to ignore the aligned zero values, I will try, thank you.

0 Kudos
alexey_stetsenko
Beginner
2,260 Views

to make the negative signed values positive, I use the ippsAbs function ..

I don't know the background of the task, but in this case the result can be wrong. If you need to get a real median value, you shouldn't use the ippsAbs function.

0 Kudos
Reply