- #1
Deathfish
- 86
- 0
Homework Statement
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.
Homework 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
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);
}