0

I am currently developing an OCR for a sudoku and i am trying to first get a clean black and white image. I first apply a grayscale then a median filter then an otsu algorithm.

My problem is that my results are better when i dont apply my median filter. Does anyone know why ?

starting image

with my median filter

without my median filter

here is the code for my median filter :

void median_filter(SDL_Surface *image) {
    int width = image->w;
    int height = image->h;
    for (int y = 1; y < height - 1; y++) {
       for (int x = 1; x < width - 1; x++) {
            Uint8 gray_values[9];
            int index = 0;
            for (int dy = -1; dy <= 1; dy++) {
                for (int dx = -1; dx <= 1; dx++) {
                    int pixel_offset = (y+dy) * image->pitch + (x+dx) * 4;
                    Uint8 r = *(Uint8 *)((Uint8 *)image->pixels + pixel_offset);
                    Uint8 g = *(Uint8 *)((Uint8 *)image->pixels + pixel_offset + 1);
                    Uint8 b = *(Uint8 *)((Uint8 *)image->pixels + pixel_offset + 2);
                    gray_values[index++] = (0.3 * r) + (0.59 * g) + (0.11 * b);
                }
            }
            qsort(gray_values, 9, sizeof(Uint8), cmpfunc);
            Uint8 gray = gray_values[4];
            int pixel_offset = y * image->pitch + x * 4;
            *(Uint8 *)((Uint8 *)image->pixels + pixel_offset) = gray;
            *(Uint8 *)((Uint8 *)image->pixels + pixel_offset + 1) = gray;
            *(Uint8 *)((Uint8 *)image->pixels + pixel_offset + 2) = gray;
        }
    }
}

1 Answers1

0

You are filtering with some neighbour values that were already filtered – the three pixels above and one on the left.

You need to create median values in a new image. This must also include the unfiltered pixels around the edges.

If you are applying multiple filters, then use one buffer as the source, and another as the destination, then swap the direction for the next filter application (by passsing two buffers to the filter functions).

Weather Vane
  • 33,872
  • 7
  • 36
  • 56