neilofbodom
Member
Hi folks,
I have an assignment to do in C for a programming class. Basically, the idea is to convert an image to grayscale and apply a median filter to the grayscale image.
Here's my code:
#include "imageio.h"
#include "imageio.c"
int main(void)
{
int i, j, grey, temp, median;
int size = 3;
IMAGE image = loadImage("test.bmp");
int window[size];
RGB pixel = image.pixels[j];
for(i = 0; i < image.height; i++)
for(j = 0; j < image.width; j++)
{
// Get current pixel value
// RGB is defined in imageio.h
// Reduce intensity by 50%
pixel.R = pixel.R * 0.5;
pixel.G = pixel.G * 0.5;
pixel.B = pixel.B * 0.5;
// Convert to grayscale
grey = (0.299 * pixel.R + 0.587 * pixel.G + 0.144 * pixel.B);
pixel.R = grey;
pixel.G = grey;
pixel.B = grey;
// Place modified pixel back into its place
image.pixels[j] = pixel;
window = grey;
}
for (i = 0; i < (size*size)-1; i++)
for(j = 0; j < (size*size)-i-1; j++)
{
// sorting in ascending order, using bubble sort
if (window > window[i+1])
{
temp = window;
window = window[i+1];
window[i+1] = temp;
} else continue;
}
// finding the median
median = window[(((size*size)-1)/2)];
pixel.R = median;
pixel.G = median;
pixel.B = median;
image.pixels[j] = pixel;
saveImage("mod_test_image.bmp", image);
}
I have no idea what is wrong with my code. When I run the program, "main.exe has stopped working" comes up. I'm assuming it's an infinite loop, but honestly I can't see the problem.
The idea behind how I tackled the problem is this:
An odd-sized window (lecturer told us to ignore cases for even-size) is moved across the image and the pixels are arranged in ascending order. The median of the window is then found and replaced back into the original image.
Could you help me please?
If you need the header files let me know.
Cheers!
I have an assignment to do in C for a programming class. Basically, the idea is to convert an image to grayscale and apply a median filter to the grayscale image.
Here's my code:
#include "imageio.h"
#include "imageio.c"
int main(void)
{
int i, j, grey, temp, median;
int size = 3;
IMAGE image = loadImage("test.bmp");
int window[size];
RGB pixel = image.pixels[j];
for(i = 0; i < image.height; i++)
for(j = 0; j < image.width; j++)
{
// Get current pixel value
// RGB is defined in imageio.h
// Reduce intensity by 50%
pixel.R = pixel.R * 0.5;
pixel.G = pixel.G * 0.5;
pixel.B = pixel.B * 0.5;
// Convert to grayscale
grey = (0.299 * pixel.R + 0.587 * pixel.G + 0.144 * pixel.B);
pixel.R = grey;
pixel.G = grey;
pixel.B = grey;
// Place modified pixel back into its place
image.pixels[j] = pixel;
window = grey;
}
for (i = 0; i < (size*size)-1; i++)
for(j = 0; j < (size*size)-i-1; j++)
{
// sorting in ascending order, using bubble sort
if (window > window[i+1])
{
temp = window;
window = window[i+1];
window[i+1] = temp;
} else continue;
}
// finding the median
median = window[(((size*size)-1)/2)];
pixel.R = median;
pixel.G = median;
pixel.B = median;
image.pixels[j] = pixel;
saveImage("mod_test_image.bmp", image);
}
I have no idea what is wrong with my code. When I run the program, "main.exe has stopped working" comes up. I'm assuming it's an infinite loop, but honestly I can't see the problem.
The idea behind how I tackled the problem is this:
An odd-sized window (lecturer told us to ignore cases for even-size) is moved across the image and the pixels are arranged in ascending order. The median of the window is then found and replaced back into the original image.
Could you help me please?
If you need the header files let me know.
Cheers!