Image Median Filtering (Selection Algorithm)

Thank you!In summary, the conversation is about a person working on a C++ image median filter and facing issues with the output. The filter is supposed to read a 30x30 16-bit grayscale binary image and apply a selection algorithm using a 3x3 kernel. The person is using the stdio.h library and has provided a detailed description of the algorithm they are trying to implement. However, they have not mentioned what exactly is wrong with the output or provided their code for further assistance.
  • #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);

}
 
Physics news on Phys.org
  • #2


Hello! It looks like you have a good start on your C++ image median filter program. Can you provide more information on what specifically is going wrong with the output? Are the values incorrect or is the image distorted? It would also be helpful to see your code so we can better understand the issue and provide guidance on how to fix it.
 

What is image median filtering?

Image median filtering is a popular method used to reduce noise and smooth images. It involves replacing each pixel in an image with the median value of its neighboring pixels.

Why is median filtering used instead of mean filtering?

Median filtering is often preferred over mean filtering because it is more effective at removing noise, especially in images with sharp edges or details. Mean filtering can blur these edges, while median filtering preserves them.

How does the selection algorithm work in image median filtering?

The selection algorithm in image median filtering involves taking a window of neighboring pixels, sorting them, and then selecting the middle value as the new value for the pixel being filtered. This process is repeated for each pixel in the image.

What are the advantages and disadvantages of image median filtering?

The main advantage of image median filtering is its ability to effectively remove noise without blurring edges or details. However, it can also result in a loss of sharpness in the image and may not be as effective for smoothing larger areas of noise.

Are there any variations of image median filtering?

Yes, there are variations of image median filtering, such as adaptive median filtering, which adjusts the size of the window based on the level of noise in the image. There is also weighted median filtering, which assigns different weights to the neighboring pixels based on their distance from the pixel being filtered.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
5
Views
3K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
3K
  • Engineering and Comp Sci Homework Help
Replies
3
Views
4K
  • Engineering and Comp Sci Homework Help
Replies
0
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
4
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
5
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
0
Views
3K
  • Programming and Computer Science
Replies
19
Views
2K
Replies
6
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
6K
Back
Top