1. Not finding help here? Sign up for a free 30min tutor trial with Chegg Tutors
    Dismiss Notice
Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Image Median Filtering (Selection Algorithm)

  1. Nov 15, 2011 #1
    1. The problem statement, all variables and given/known data

    I started working on a C++ image median filter for fun... I'm supposed to read a 30x30 16-bit grayscale binary image and apply selection algorithm. 3x3 kernel.

    Please help output is messed up and i don't know where to start fixing it.

    2. Relevant equations

    using stdio.h

    For every pixel aside from edges,
    - 3x3 neighbourhood is assigned pixel numbers 1-9 by relative position.
    - Scanning left to right then next row.
    - If there is a smaller number than current number then number is replaced.
    - Repeat for rest of neighbourhood.
    - ((9-1)/2 + 1)th value is median
    - continue for next pixel

    3. The attempt at a solution

    - headers, definitions left out

    void main()
    {
    unsigned short matrix[30][30];
    unsigned short filtered[30][30];

    int kernel[9];
    int ordered[9];
    int y, x;
    int minvalue;
    int i, j;
    int count, jcount;
    int medcount;
    int median, upper, lower;

    FILE *original;
    FILE *output;

    if ((original = fopen ("F:\\test\\ArrayQ.bin", "rb")) == NULL)
    {
    printf("Error opening binary file");
    }

    if (fread(matrix, sizeof(unsigned short), 900, original) != 900)
    {
    printf("Error reading binary file");
    }

    fclose(original);

    for (y=1;y<29;y++)
    {
    for (x=1;x<29;x++)
    {
    kernel[1]=matrix[y-1][x-1];
    kernel[2]=matrix[y-1][x];
    kernel[3]=matrix[y-1][x+1];
    kernel[4]=matrix[y][x-1];
    kernel[5]=matrix[y][x];
    kernel[6]=matrix[y][x+1];
    kernel[7]=matrix[y+1][x-1];
    kernel[8]=matrix[y+1][x];
    kernel[9]=matrix[y+1][x+1];

    for (count=0;count<SIZE;count++)
    {
    i=kernel[count];
    minvalue=i;
    jcount=count+1;

    while(jcount<SIZE)
    {
    j=kernel[jcount]
    if (j < minvalue)
    {
    minvalue=j;
    }
    jcount++;
    }

    ordered[count]=minvalue;
    }

    if (SIZE%2==1)
    {
    medcount=((SIZE-1)/2)+1;
    median=ordered[medcount];
    }
    else
    {
    lower=SIZE/2;
    upper=(SIZE/2)+1;
    median=(ordered[lower]+ordered[upper])/2;
    }

    printf("%d\t", median);
    }
    }

    if ((output = fopen ("F:\\test\\avefilter.bin", "wb")) == NULL)
    {
    printf("\nError creating binary file!\n\n");
    }
    if (fwrite (filtered, sizeof (unsigned short), 900, output) !=900)
    {
    printf("\nError writing binary file!\n\n");
    }

    fclose(output);

    }
     
  2. jcsd
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook

Can you offer guidance or do you also need help?



Similar Discussions: Image Median Filtering (Selection Algorithm)
  1. Piezoelectric filter (Replies: 0)

  2. Greedy Algorithm (Replies: 0)

Loading...