trying to make median mask work :(

Discussion in 'Mac Programming' started by OneDreamCloser, Jan 24, 2010.

  1. OneDreamCloser macrumors member

    Jun 16, 2008
    hello all,

    here is an implementation of the median mask along with some parameters:
    /* necessary definitions */
    #define	M 100
    char array1[M] ;
    char array2[M] ;
    /* initialization */
    for( i=0; i<M; i++ )	array1[i] = i ;
    /* call median */
    median(array1, array2, M, M) ;
    /* median filter implementation */
    void median(char* imageIn,    
    			 char* imageOut,   
    			 unsigned width,  
    			 unsigned height) {
    	const int matrix_size = 9;
    	char  values [matrix_size];
    	int i;
    	memset(imageOut, 0, width);
    	// scan the picture starting at second row
    	for (i = width; i < width * (height - 1); i++) {
    		// copy first pixels into matrix
    		memcpy ((char*)&values,   &imageIn[i-width-1], 3);
    		memcpy ((char*)&values+3, &imageIn[i-1],       3);
    		memcpy ((char*)&values+6, &imageIn[i+width-1], 3);
    		// insertion sort with the matrix
    			int x, min;
    			char tmp;
    			for (x=0; x<9; x++) {
    				tmp = values[x];
    				min = x;
    				while ( (min>0) && (values[min-1]>tmp) ) {
    					values[min] = values [min-1];
    				values[min] = tmp;
    		// end of insertion sort with the matrix
    		// store median value in pixel considered
    		imageOut[i] = values[4];
    		memset(imageOut+i, 0, width);
    but i get a segmentation fault when the "memcpy" is called
    ( the implementation of median is for sure correct, the problem is in the way i use it ),
    can someone please help me overcome this ?

    thank you
  2. jpyc7 macrumors 6502

    Mar 8, 2009
    Denver, CO
    memcpy ((char*)&values,   &imageIn[i-width-1], 3);
    For the above statement, I think you have two choices:
    1. Remove the ampersand in the memcpy destination (values).
    2. Put in the array location reference, e.g. &(values[0])

    I think the fact that you have a cast to char pointer in front meant that the compiler did not give you a warning. So you might want to remove the cast also.
  3. OneDreamCloser thread starter macrumors member

    Jun 16, 2008
    first of all, thank you jpyc7 for the interest

    i tried both ways, but none of them worked,
    basically i am worrying about the index of imageIn

Share This Page