Thread: Median of a 2d matrix

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User
    Join Date
    Jul 2014
    Posts
    8

    Median of a 2d matrix

    Hey,

    I m doing a image processing project. I need to find the median in a 2d matrix.

    What is the another way, instead of saving elements in 1d array, sort and then find median.

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    What is the another way, instead of saving elements in 1d array, sort and then find median.
    Why not just sort the 2d array, then find the median?


    Jim

  3. #3
    Registered User
    Join Date
    Jul 2014
    Posts
    8
    I think complexity can be reduced in that way..

  4. #4
    Registered User
    Join Date
    Mar 2012
    Location
    the c - side
    Posts
    373
    The only optimisation Wikipedia mentions is the use of a selection sort, but only until the median value is found.

  5. #5
    Registered User
    Join Date
    Mar 2011
    Posts
    596
    Since this is for an image processing project,
    Are you referring to a median filter?

    Median filter - Wikipedia, the free encyclopedia

    -

  6. #6
    Registered User
    Join Date
    Jul 2014
    Posts
    8
    Yes, I am referring median filter

  7. #7
    Registered User
    Join Date
    Jul 2014
    Posts
    8
    hey I am getting below error in my code.I am really fail to overcome this.
    median22: malloc.c:2451: sYSMALLOc: Assertion `(old_top == (((mbinptr) (((char *) &((av)->bins[((1) - 1) * 2])) - __builtin_offsetof (struct malloc_chunk, fd)))) && old_size == 0) || ((unsigned long) (old_size) >= (unsigned long)((((__builtin_offsetof (struct malloc_chunk, fd_nextsize))+((2 * (sizeof(size_t))) - 1)) & ~((2 * (sizeof(size_t))) - 1))) && ((old_top)->size & 0x1) && ((unsigned long)old_end & pagemask) == 0)' failed.
    Aborted (core dumped)


    My code is:

    #include <stdio.h>
    #include <stdlib.h>
    #define SIZE 50

    typedef struct PGM
    {
    char name[100];
    int width;
    int height;
    int max_value;
    int **matrix;
    } PGM;

    int ** create_matrix(int height, int width)
    {

    int **matrix = malloc(height*sizeof(int*)) ;

    if (matrix)
    {
    int i;
    for (i = 0; i < height; i++)
    {
    matrix[i] =malloc(width * sizeof (int)) ;
    }
    }
    return matrix;
    }


    //void display(PGM *p) {
    // printf("displaying p matrix");
    // printf("name %s\n", p->name);
    // printf("height %d width %d max_value %d\n", p->height, p->width, p->max_value);
    // int i,j;
    // for (i = 0; i < p->height; i++) {
    // for (j = 0; j < p->width; j++) {
    // printf("%2d ", p->matrix[i][j]);
    // }
    // printf("\n");
    // }
    // }


    void skip_comments(FILE* fp, char* line, int size)
    {
    do{
    fgets(line, size, fp);
    if (line[0] != '#')
    {
    return ;
    }
    } while(line!= NULL);
    }

    PGM* f2p(char *file_name) {

    FILE * fp;
    char c;
    char name[100];
    char line[SIZE];
    int height, width, max_value;

    fp = fopen(file_name, "r");

    if (fp == NULL)
    return NULL;

    PGM *p = malloc(sizeof(PGM));


    skip_comments(fp, line, SIZE);
    sscanf(line, "%s", p->name);

    skip_comments(fp, line, SIZE);
    sscanf(line, "%d %d", &width, &height);

    skip_comments(fp, line, SIZE);
    sscanf(line, "%d", &max_value);

    int **matrix = malloc(height*sizeof(int*)) ;
    if (matrix){
    int i,j;
    for (i = 0; i < height; i++) {

    matrix[i] = malloc(width * sizeof (int)) ;
    for (j = 0; j < width; j++) {

    c = fscanf(fp, "%d", &matrix[i][j]);
    }
    }

    p->matrix = matrix;
    p->height = height;
    p->width = width;
    p->max_value = max_value;
    return p;
    }

    fclose(fp);
    return NULL;
    }

    void p2f(PGM* p, char *file_name) {
    FILE* fp = fopen(file_name, "w");
    fprintf(fp, "%s\n", p->name);
    fprintf(fp, "%d %d\n%d\n", p->width, p->height, p->max_value);
    int i,j;
    for (i = 0; i < p->height; i++)
    {
    for (j = 0; j < p->width; j++)
    {
    fprintf(fp, "%d ", p->matrix[i][j]);
    }
    fprintf(fp,"\n");
    }
    fclose(fp);
    }

    int sort(int *a,int filt)
    {
    int i,j,t;
    for (i = 1 ; i <= filt; i++)
    {
    for (j = 1 ; j <= filt ; j++)
    {
    if (a[j] <= a[j+1])
    {
    t = a[j];
    a[j] = a[j+1];
    a[j+1] = t;
    }
    else continue ;
    }
    }
    return a[(filt+1)/2];
    }


    void apply_median_filter(int** source, int** dest, int height, int width,int k,int filt) {
    int i,j,m,l,median;
    for (i = k; i < height; i++){
    for (j = k; j < width ; j++){
    int c=0;
    int *temp=(int*)malloc(filt*filt*sizeof(int));
    for(m=0;m<filt;m++){

    for(l=0;l<filt;l++)
    temp[c++]=source[i-k+m][j-k+l];
    }

    median=sort(temp,filt*filt);
    dest[i-k][j-k]=median;
    }
    }

    }



    void apply_padding(PGM* p,int n)
    {
    int k;
    k=n/2;
    int new_height = p->height + k;
    int new_width = p->width + k;


    int **matrix = create_matrix(new_height, new_width);


    if (matrix){
    int i,j;


    for (i = 0; i < new_height; i++){
    for (j = 0; j < new_width; j++){

    if((i<k) || (j<k) || i>(p->height) || j>(p->width))

    matrix[i][j] = 0;
    }
    }


    for (i = k; i < new_height-k; i++){
    for (j = k; j < new_width-k; j++) {
    matrix[i][j] = p->matrix[i-k][j-k];
    }
    }

    apply_median_filter(matrix, p->matrix, p->height, p->width,k,n);


    }
    }
    int main()
    {
    char file_name[50];
    int n;
    printf("Enter the file name you want to open\n");
    scanf("%s",file_name);
    PGM *p = f2p(file_name);

    printf("Enter any odd number for the mask-size which you want to apply\n");
    scanf("%d",&n);

    apply_padding(p,n);
    //display(p);

    char file_name1[] = "output.pgm";
    p2f(p, file_name1);
    }

  8. #8
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Argh! My eyes are bleeding!

    Please post your code in [code][/code] tags and make sure it is properly formatted and indented. Use the "preview post" button (you may need to use the advanced editor UI) to ensure your post looks nice before you submit it.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 05-19-2014, 07:32 PM
  2. Median Help!
    By tidbit in forum C++ Programming
    Replies: 4
    Last Post: 05-04-2010, 09:55 PM
  3. Median
    By Neo1 in forum C++ Programming
    Replies: 4
    Last Post: 07-02-2007, 04:00 PM
  4. Median
    By Suchy in forum C++ Programming
    Replies: 1
    Last Post: 10-21-2006, 02:07 AM
  5. median
    By frank in forum C++ Programming
    Replies: 4
    Last Post: 10-28-2001, 04:32 PM

Tags for this Thread