Median filter
Median():
sorted = SORT(array)
length = LEN(array)
if isOdd(length)
return sorted[FLOOR(length / 2)]
if isEven(length)
return AVERAGE(sorted[CEIL(length / 2)], sorted[FLOOR(length / 2)])Implementation
void cv_apply_median_filter(Image* img, int size) {
/* ... */
for (int i = 0; i < img->height; ++i) {
for (int j = 0; j < img->width; ++j) {
for (int k = 0; k < size; ++k) {
for (int l = 0; l < size; ++l) {
/* ... */
if (row <= 0 && row > img->height && col <= 0 && col > img->width) continue;
for (int c = 0; c < ch; ++c) {
pixelArray[pixelCount++] = img->bytes[(row * img->width + col) * ch + c];
}
}
}
for (int c = 0; c < ch; ++c) {
unsigned char medianValue = calculate_median(pixelValues, pixelCount);
tempBytes[(i * img->width + j) * ch + c] = medianValue;
}
/* ... */
}
}
/* ... */
}Result


Problems
Last updated