LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 04-13-2006, 08:57 AM   #1
tomazN
Member
 
Registered: May 2005
Location: Slovenia
Distribution: Suse 10.2 64bit
Posts: 127

Rep: Reputation: 16
C code for median filter on color images


Somebody has maybe somewhere color median filter code written in C to filter out a color picture with it. I have read image to image_pointer but i dont know the algorithm to do median filtering on it. I made it for gray pictures which is easy, but i am stuck for color pictures. If somebody maybe has this filter already written or seen it somewhere i would appreciate it. BTW. using some opencv or some other image processing plugin or whatever doesnt come into account I need to have the C code for it.

Thx.
 
Old 04-13-2006, 09:52 AM   #2
paulsm4
LQ Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
http://www.imagemagick.org
 
Old 04-13-2006, 09:56 AM   #3
tomazN
Member
 
Registered: May 2005
Location: Slovenia
Distribution: Suse 10.2 64bit
Posts: 127

Original Poster
Rep: Reputation: 16
Yah, i know this url, it doesnt help me. It needs libs from that software, i have to use pure C (have to reinvent the whole thing, dont ask why its a damn school project ...).

For exmpl. this is median for gray pictures:

Code:
#include <stdio.h>
#include "../ip.h"


/***************************************************************************
 * Func: median_filt                                                       *
 *                                                                         *
 * Desc: median filters an image using a block mask                        *
 *                                                                         *
 * Params: source - pointer to image in memory                             *
 *         cols - number of columns in image                               *
 *         rows - number of rows in image                                  *
 *         filename - name of output file                                  *
 *         side - width of block                                           *
  ***************************************************************************/
main()
{
}

void median_filt(image_ptr source, int cols, int rows, char *filename,
		 int side)
    {
    int x, y, i;             /* image loop variables */
    int index;               /* image index */
    int winx, winy;          /* index into the samples window */
    int xextra, yextra;      /* size of boundary */
    int conv_line;           /* size of output line */
    unsigned long destadd;   /* destination image address */
    unsigned long sourceadd, sourcebase; /* source addressing */
    char dest[1024];         /* destination image line */
    FILE *fp;                /* output file pointer */
    int *window;             /* window for block samples */
    unsigned char left[25];  /* storage for left edge pixel duplication */
    unsigned char right[25]; /* storage for right edge pixel duplication */
    int xpad, ypad;          /* size of padding */
    int last_line;           /* last line to process */
    int new_pixel;           /* newly processed pixel */
    int wsize;               /* number of elements in block */

    wsize = side * side;
    yextra = (side/2)*2;
    ypad = yextra/2;
    xextra = yextra;
    xpad = ypad;
    conv_line = cols - xextra;

    window = malloc(wsize * sizeof(int));
    if((fp=fopen(filename, "wb")) == NULL)
	{
	printf("Unable to open %s for output\n",filename);
	exit(1);
	}
    fprintf(fp, "P5\n%d %d\n255\n", cols, rows); /* print out header */
    last_line = rows - yextra;
    for(y=0; y<last_line; y++)
	{
	sourcebase=(unsigned long) cols * y;
	destadd=0;
	for(x=xpad; x<(cols - xpad); x++)
	   {
	   index=0;
	   for(winy=0; winy<side; winy++)
	       for(winx=0; winx<side; winx++)
		  {
		  sourceadd=sourcebase+winx+winy*cols;
		  window[index++] = (int) source[sourceadd];
		  }
	   new_pixel = median(window, wsize);
	   dest[destadd++]=(unsigned char) new_pixel;
	   sourcebase++;
	   } /* for x */
	 for(i=0; i<xpad; i++)
	   left[i]=dest[0];
	 for(i=0; i<xpad; i++)
	   right[i]=dest[conv_line-1];
	 if(y==0)
	     for(i=0; i<ypad; i++)
	     {
	     fwrite(left, 1, xpad, fp);
	     fwrite(dest, 1, conv_line, fp);
	     fwrite(right, 1, xpad, fp);
	     }
	 fwrite(left, 1, xpad, fp);
	 fwrite(dest, 1, conv_line, fp);
	 fwrite(right, 1, xpad, fp);
	 if(y==(last_line-1))
	     for(i=0; i<ypad; i++)
		 {
		 fwrite(left, 1, xpad, fp);
		 fwrite(dest, 1, conv_line, fp);
		 fwrite(right, 1, xpad, fp);
		 }
    } /* for y */
    free(window);
    }

/****************************************************************************
 * Func: median                                                             *
 *                                                                          *
 * Desc: finds median value of an image block                               *
 *                                                                          *
 * Param: window - 3x3 window of source image                               *
 *                                                                          *
 * Returns: the median value                                                *
 ****************************************************************************/

int median(int *window, int wsize)
    {
    quicksort(window, 0, wsize);
    return window[wsize/2];
    }

/***************************************************************************
 * Func: quicksort                                                        *
 *                                                                         *
 * Desc: sorts an array of integers using the quicksort algorithm         *
 *                                                                         *
 * Params: array - the array of integers                                  *
 *         left - leftmost entry in array                                 *
 *         right - rightmost entry in array                               *
 ***************************************************************************/

void quicksort(int *array, int left, int right)
    {
    int i, last;

    if(left >= right)
	return;
    swap(array, left, (left + right)/2);
    last = left;
    for( i = left + 1; i<= right; i++)
	if(array[i] < array[left])
	    swap(array, ++last, i);
    swap(array, left, last);
    quicksort(array, left, last-1);
    quicksort(array, last+1, right);
    }

/***************************************************************************
 * Func: swap                                                              *
 *                                                                         *
 * Desc: swaps two elements in an array                                    *
 *                                                                         *
 * Params: array - array of element (two of which will be swapped)         *
 *         i - first element in array to swap                              *
 *         j - second element in array to swap                             *
 ***************************************************************************/
void swap(int *array, int i, int j)
    {
    int temp;

    temp = array[i];
    array[i] = array[j];
    array[j] = temp;
    }
 
Old 04-13-2006, 12:32 PM   #4
slantoflight
Member
 
Registered: Aug 2005
Distribution: Smoothwall
Posts: 283
Blog Entries: 3

Rep: Reputation: 35
Its looks like you need to add checking for 16bit and 24 bit color images. You are aware of little endianess, right? Your swap array is only effective on 4bit images(16 color grayscale) because it only works with two bytes.


Code:
void swap(int *array, int i, int j)
    {
    int temp;

    temp = array[i];
    array[i] = array[j];
    array[j] = temp;
    }
It would be better to reverse the order all together,but I realise that in order for your program to work properly it has to operate in bit chunks. So maybe if you grab 24 bits(which is the maximux bit depth of any color image) at a time and adjust your algorithm. add 24 bit 16bit 4 bit command line options. That should do it.

Last edited by slantoflight; 04-13-2006 at 12:33 PM.
 
Old 04-13-2006, 12:39 PM   #5
tomazN
Member
 
Registered: May 2005
Location: Slovenia
Distribution: Suse 10.2 64bit
Posts: 127

Original Poster
Rep: Reputation: 16
The thing i have for grayscal images is ok, because i use pgm files. I need algorithm for median for color images (RGB) format ppm.
 
Old 04-13-2006, 12:47 PM   #6
tomazN
Member
 
Registered: May 2005
Location: Slovenia
Distribution: Suse 10.2 64bit
Posts: 127

Original Poster
Rep: Reputation: 16
I have already stored image in memory as image_ptr. I only need the algorithm what to do with pixels. I know how to write the image back aswell.
 
Old 04-13-2006, 12:53 PM   #7
slantoflight
Member
 
Registered: Aug 2005
Distribution: Smoothwall
Posts: 283
Blog Entries: 3

Rep: Reputation: 35
Quote:
Originally Posted by tomazN
I have already stored image in memory as image_ptr. I only need the algorithm what to do with pixels. I know how to write the image back aswell.
I think your main function for calculating median is ok. I mean calculating mean is the same everywhere right? you just have to fix your swap function.
 
Old 04-13-2006, 12:56 PM   #8
tomazN
Member
 
Registered: May 2005
Location: Slovenia
Distribution: Suse 10.2 64bit
Posts: 127

Original Poster
Rep: Reputation: 16
gray pixture is like this: valuse from 1-255 '23 34 45 234 12' each representing one pixel. RGB picture on the other hand is like this: 23 34 56 78 34 120 first three values reperesent one pixel with first number being R second G third B. Now you have to do meadian somehow based on that, so no i cant use gray median for this not even close I am stuck
 
Old 04-13-2006, 12:57 PM   #9
tomazN
Member
 
Registered: May 2005
Location: Slovenia
Distribution: Suse 10.2 64bit
Posts: 127

Original Poster
Rep: Reputation: 16
pgm - see http://netpbm.sourceforge.net/doc//pgm.html
ppm - see http://netpbm.sourceforge.net/doc//ppm.html

for some reference
 
Old 04-13-2006, 01:55 PM   #10
slantoflight
Member
 
Registered: Aug 2005
Distribution: Smoothwall
Posts: 283
Blog Entries: 3

Rep: Reputation: 35
they look nearly the same, except the last two values are swapped. But are they really both 32bit in length? That seems wasteful for a grayscale.

But ahh I see

Quote:
The PGM format is a lowest common denominator grayscale file format. It is designed to be extremely easy to learn and write programs for. (It's so simple that most people will simply reverse engineer it because it's easier than reading this specification).
Then the only it appears you need to worry about is bit 7 out of each 32 bit chunk.
 
Old 04-13-2006, 02:03 PM   #11
tomazN
Member
 
Registered: May 2005
Location: Slovenia
Distribution: Suse 10.2 64bit
Posts: 127

Original Poster
Rep: Reputation: 16
I cant make same procedure to get median value on color picture. Because you cant make median on three pixels. you must make median on a window of 3x3 for example on R and then on G and on B then u write back all those components to get next pixel with RGB, or else you would get just diffrent colors if not all black.

I just need the algorithm for this procedure, i can manage other myself, just dont know the logic part of the algorithm.
 
Old 11-26-2006, 12:16 PM   #12
anne_regina
LQ Newbie
 
Registered: Nov 2006
Posts: 1

Rep: Reputation: 0
Request for ip.h file

Quote:
Originally Posted by tomazN
Yah, i know this url, it doesnt help me. It needs libs from that software, i have to use pure C (have to reinvent the whole thing, dont ask why its a damn school project ...).

For exmpl. this is median for gray pictures:

Code:
#include <stdio.h>
#include "../ip.h"


/***************************************************************************
 * Func: median_filt                                                       *
 *                                                                         *
 * Desc: median filters an image using a block mask                        *
 *                                                                         *
 * Params: source - pointer to image in memory                             *
 *         cols - number of columns in image                               *
 *         rows - number of rows in image                                  *
 *         filename - name of output file                                  *
 *         side - width of block                                           *
  ***************************************************************************/
main()
{
}

void median_filt(image_ptr source, int cols, int rows, char *filename,
		 int side)
    {
    int x, y, i;             /* image loop variables */
    int index;               /* image index */
    int winx, winy;          /* index into the samples window */
    int xextra, yextra;      /* size of boundary */
    int conv_line;           /* size of output line */
    unsigned long destadd;   /* destination image address */
    unsigned long sourceadd, sourcebase; /* source addressing */
    char dest[1024];         /* destination image line */
    FILE *fp;                /* output file pointer */
    int *window;             /* window for block samples */
    unsigned char left[25];  /* storage for left edge pixel duplication */
    unsigned char right[25]; /* storage for right edge pixel duplication */
    int xpad, ypad;          /* size of padding */
    int last_line;           /* last line to process */
    int new_pixel;           /* newly processed pixel */
    int wsize;               /* number of elements in block */

    wsize = side * side;
    yextra = (side/2)*2;
    ypad = yextra/2;
    xextra = yextra;
    xpad = ypad;
    conv_line = cols - xextra;

    window = malloc(wsize * sizeof(int));
    if((fp=fopen(filename, "wb")) == NULL)
	{
	printf("Unable to open %s for output\n",filename);
	exit(1);
	}
    fprintf(fp, "P5\n%d %d\n255\n", cols, rows); /* print out header */
    last_line = rows - yextra;
    for(y=0; y<last_line; y++)
	{
	sourcebase=(unsigned long) cols * y;
	destadd=0;
	for(x=xpad; x<(cols - xpad); x++)
	   {
	   index=0;
	   for(winy=0; winy<side; winy++)
	       for(winx=0; winx<side; winx++)
		  {
		  sourceadd=sourcebase+winx+winy*cols;
		  window[index++] = (int) source[sourceadd];
		  }
	   new_pixel = median(window, wsize);
	   dest[destadd++]=(unsigned char) new_pixel;
	   sourcebase++;
	   } /* for x */
	 for(i=0; i<xpad; i++)
	   left[i]=dest[0];
	 for(i=0; i<xpad; i++)
	   right[i]=dest[conv_line-1];
	 if(y==0)
	     for(i=0; i<ypad; i++)
	     {
	     fwrite(left, 1, xpad, fp);
	     fwrite(dest, 1, conv_line, fp);
	     fwrite(right, 1, xpad, fp);
	     }
	 fwrite(left, 1, xpad, fp);
	 fwrite(dest, 1, conv_line, fp);
	 fwrite(right, 1, xpad, fp);
	 if(y==(last_line-1))
	     for(i=0; i<ypad; i++)
		 {
		 fwrite(left, 1, xpad, fp);
		 fwrite(dest, 1, conv_line, fp);
		 fwrite(right, 1, xpad, fp);
		 }
    } /* for y */
    free(window);
    }

/****************************************************************************
 * Func: median                                                             *
 *                                                                          *
 * Desc: finds median value of an image block                               *
 *                                                                          *
 * Param: window - 3x3 window of source image                               *
 *                                                                          *
 * Returns: the median value                                                *
 ****************************************************************************/

int median(int *window, int wsize)
    {
    quicksort(window, 0, wsize);
    return window[wsize/2];
    }

/***************************************************************************
 * Func: quicksort                                                        *
 *                                                                         *
 * Desc: sorts an array of integers using the quicksort algorithm         *
 *                                                                         *
 * Params: array - the array of integers                                  *
 *         left - leftmost entry in array                                 *
 *         right - rightmost entry in array                               *
 ***************************************************************************/

void quicksort(int *array, int left, int right)
    {
    int i, last;

    if(left >= right)
	return;
    swap(array, left, (left + right)/2);
    last = left;
    for( i = left + 1; i<= right; i++)
	if(array[i] < array[left])
	    swap(array, ++last, i);
    swap(array, left, last);
    quicksort(array, left, last-1);
    quicksort(array, last+1, right);
    }

/***************************************************************************
 * Func: swap                                                              *
 *                                                                         *
 * Desc: swaps two elements in an array                                    *
 *                                                                         *
 * Params: array - array of element (two of which will be swapped)         *
 *         i - first element in array to swap                              *
 *         j - second element in array to swap                             *
 ***************************************************************************/
void swap(int *array, int i, int j)
    {
    int temp;

    temp = array[i];
    array[i] = array[j];
    array[j] = temp;
    }
Hi, I would like to request for the file ip.h from the book "A simplified approach to image processing" please? I have the other snippets but without the ip.h, i can't run the code...I'm doing project on image processing using median filter.. really appreciate it if you could send to my email: anne_regina@yahoo.com ..Thanks!
 
Old 02-20-2012, 01:12 AM   #13
jayshri
Member
 
Registered: Feb 2012
Posts: 71

Rep: Reputation: Disabled
Smile image processing

hello people m also doing a project in image processing... i have already devloped the code that reads the image pixel by pixel. now i want to do median filtering.... i have to make a structured program.. i.e i hav to incude the median filter code in the code which is reading pixel value... can nebody help me with the coding of median filtering..
i need it in c.
thank u.
regards
 
Old 02-20-2012, 05:11 AM   #14
firstfire
Member
 
Registered: Mar 2006
Location: Ekaterinburg, Russia
Distribution: Debian, Ubuntu
Posts: 709

Rep: Reputation: 428Reputation: 428Reputation: 428Reputation: 428Reputation: 428
Quote:
Originally Posted by jayshri View Post
hello people m also doing a project in image processing... i have already devloped the code that reads the image pixel by pixel. now i want to do median filtering.... i have to make a structured program.. i.e i hav to incude the median filter code in the code which is reading pixel value... can nebody help me with the coding of median filtering..
i need it in c.
thank u.
regards
Hi.
Welcome to LQ!
Take a look at this Wikipedia article. You will find the pseudo code for median filtering there as well as a link to rosettacode.org with implementations in different languages (including C).

Hope this helps.
 
Old 02-20-2012, 05:31 AM   #15
jayshri
Member
 
Registered: Feb 2012
Posts: 71

Rep: Reputation: Disabled
image processing

thank u...
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Applying median filter to a picture ... tomazN Programming 7 04-03-2006 05:15 AM
Minolta MC 2300dl + foo2zjs driver = color images printed in black? J_Szucs Linux - Hardware 1 02-06-2006 02:30 AM
What are the color code ? cigarstub Linux - Networking 5 10-22-2005 10:51 AM
how to draw color images with xlib? BBB Programming 19 07-15-2005 02:04 PM
filter the code Xiangbuilder Programming 7 10-23-2003 07:28 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 12:54 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration