8 Replies - 2391 Views - Last Post: 10 February 2013 - 06:28 PM Rate Topic: -----

#1 Yuriy M  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 43
  • Joined: 05-September 07

C - Help for a program that recursively counts pixels in photos

Posted 18 October 2012 - 06:50 PM

Hi guys.

I've been working on a recursion program that is designed to count pixels belonging to an object in a photograph. The program is designed to only count the pixels that make up the chosen object blob. My program involves the user entering the desired x and y coordinates for the picture cell. From there, the program will count each existing filled pixel cell that make up the object blob and afterwards, return the total number of filled cells. When a cell is counted, the value for the cell will be 0 in order to prevent multiple counts of the same cell. Pixel cells that make up the object blob are designated with the number "1".

Here is my code thus far:

#include <stdio.h>
int blob_check(int [][], int, int);                      /* Checks for blob in the grid */
int main(void)
{
/* Declare variables */
int picture[4][4],                                               /* The picture grid */
         x_count,                                                        /* Counts x coordinates */
         y_count,                                                        /* Counts y coordinates */
         x_coord,                                                        /* Specified x coordinate */
         y_coord,                                                        /* Specified y coordinate */
         total_cells;                                            /* The total number of cells that make up the picture blob */

printf("\n");
/* Populate picture grid with blobs */
for (x_count = 0; x_count < 4; x_count++)
         for (y_count = 0; y_count < 4; y_count++)
         {
         printf("Enter 1 for pixel or any other key for blank: ");
         scanf("%d", &picture[x_count][y_count]);
        
         if (picture[x_count][y_count] == 1)
         picture[x_count][y_count] = 1;
         else
         picture[x_count][y_count] = 0;
         }

/* Print picture grid */
for (x_count = 0; x_count < 4; x_count++)
{
         printf("\n");
         for (y_count = 0; y_count < 4; y_count++)
         printf("%d", picture[x_count][y_count]);
}
printf("\n");

/* Prompt for coordinates to help determine the number of pixel cells that make up the blob */
printf("\nEnter the x and y coordinates for the picture blob: ");
scanf("%d %d", &x_coord, &y_coord);

/* Count the cells within the coordinates that make up the picture blob */
total_cells = blob_check(picture, x_coord, y_coord);

/* Display the total */
printf("\n%d cell(s) make up the picture blob.\n", total_cells);

/* Print picture grid */
for (x_count = 0; x_count < 4; x_count++)
{
         printf("\n");
         for (y_count = 0; y_count < 4; y_count++)
         printf("%d", picture[y_count][x_count]);
}
printf("\n");

return 0;
}
int blob_check(int pic_grid[4][4], int x, int y)
{
/* Increment cell total for every successful instance of a blob */
if (x > 3 || x < 0 || y > 3 || y < 0)
         return 0;
else if (pic_grid[x][y] == 1)
{
         pic_grid[x][y] = 0;
         return 1 + blob_check(pic_grid, x, y);
}
else if (pic_grid[x - 1][y + 1] == 1)
{
         pic_grid[x][y] = pic_grid[x - 1][y + 1];
         pic_grid[x][y] = 0;
         return 1 + blob_check(pic_grid, x, y);
}
else if (pic_grid[x][y + 1] == 1)
{
         pic_grid[x][y] = pic_grid[x][y + 1];
         pic_grid[x][y] = 0;
         return 1 + blob_check(pic_grid, x, y);
}
else if (pic_grid[x + 1][y + 1] == 1)
{
         pic_grid[x][y] = pic_grid[x + 1][y + 1];
         pic_grid[x][y] = 0;
         return 1 + blob_check(pic_grid, x, y);
}
else if (pic_grid[x - 1][y] == 1)
{
         pic_grid[x][y] = pic_grid[x - 1][y];
         pic_grid[x][y] = 0;
         return 1 + blob_check(pic_grid, x, y);
}
else if (pic_grid[x + 1][y] == 1)
{
         pic_grid[x][y] = pic_grid[x + 1][y];
         pic_grid[x][y] = 0;
         return 1 + blob_check(pic_grid, x, y);
}
else if (pic_grid[x - 1][y - 1] == 1)
{
         pic_grid[x][y] = pic_grid[x - 1][y - 1];
         pic_grid[x][y] = 0;
         return 1 + blob_check(pic_grid, x, y);
}
else if (pic_grid[x][y - 1] == 1)
{
         pic_grid[x][y] = pic_grid[x][y - 1];
         pic_grid[x][y] = 0;
         return 1 + blob_check(pic_grid, x, y);
}
else if (pic_grid[x + 1][y - 1] == 1)
{
         pic_grid[x][y] = pic_grid[x + 1][y - 1];
         pic_grid[x][y] = 0;
         return 1 + blob_check(pic_grid, x, y);
}
else
         return 0;
}


For the time being, I'm using a 4x4 grid and the grid is manually populated by the user. However, the problem I'm having with this program is that it frequently crashes and the pixel counts have a tendency to be off the mark at certain times. What could be wrong? :sad3:

Is This A Good Question/Topic? 0
  • +

Replies To: C - Help for a program that recursively counts pixels in photos

#2 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6111
  • View blog
  • Posts: 23,672
  • Joined: 23-August 08

Re: C - Help for a program that recursively counts pixels in photos

Posted 19 October 2012 - 04:34 AM

else if (pic_grid[x - 1][y + 1] == 1)


If x is 0 and y is 2, what are the indexes of the array element being accessed?
Was This Post Helpful? 0
  • +
  • -

#3 Yuriy M  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 43
  • Joined: 05-September 07

Re: C - Help for a program that recursively counts pixels in photos

Posted 19 October 2012 - 07:14 AM

View PostJackOfAllTrades, on 19 October 2012 - 05:34 AM, said:

else if (pic_grid[x - 1][y + 1] == 1)


If x is 0 and y is 2, what are the indexes of the array element being accessed?

I expect the program to just return 0 if the indexes of the array element are -1 and 3.
Was This Post Helpful? 0
  • +
  • -

#4 jimblumberg  Icon User is offline

  • member icon


Reputation: 4293
  • View blog
  • Posts: 13,462
  • Joined: 25-December 09

Re: C - Help for a program that recursively counts pixels in photos

Posted 19 October 2012 - 07:24 AM

Well that won't likely happen, what will usually happen is a crash. Array indexes can never be less than zero or equal or larger to the size of your array.

Jim
Was This Post Helpful? 0
  • +
  • -

#5 Yuriy M  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 43
  • Joined: 05-September 07

Re: C - Help for a program that recursively counts pixels in photos

Posted 09 February 2013 - 04:06 PM

All right, after months of tinkering with this program, it looks like I finally have it working. Here is the completed version:
#include <stdio.h>

int blob_check(int [][], int, int);           /* Checks for picture blob size in the grid */

int main(void)
{
    /* Declare variables */
    int grid[5][5],                           /* The picture grid */
        x_count,                              /* Counts the rows in the grid */
        y_count,                              /* Counts the columns in the grid */
        x_inp,                                /* The X co-ordinate entered by the user */
        y_inp,                                /* The Y co-ordinate entered by the user */
        blob_size;                            /* The cell number size of the picture blob */
    
    printf("\nEnter 1 to fill cell. Pressing any other key will leave cell empty.\n");
        
    /* Fill grid */
    for (y_count = 0; y_count < 5; y_count++)
     for (x_count = 0; x_count < 5; x_count++)
     {
      printf("\nX {%d} Y {%d}: ", x_count, y_count);
      scanf("%d", &grid[x_count][y_count]);
      
      if (grid[x_count][y_count] == 1)
       grid[x_count][y_count] = 1;
      else
       grid[x_count][y_count] = 0;
     }
    
    /* Display grid */
    for (y_count = 4; y_count >= 0; y_count--)
    {
     if (y_count == 2)
      printf("\n\ty  %d  ", y_count);
     else
      printf("\n\t   %d  ", y_count);
     for (x_count = 0; x_count < 5; x_count++)
      printf("%d ", grid[y_count][x_count]);
    }
    
    printf("\n\n\t      ");
    for (x_count = 0; x_count < 5; x_count++)
     printf("%d ", x_count);
    printf("\n\n\t          x\n");
    
    /* Get desired co-ordinates */
    printf("\nEnter X and Y co-ordinates for picture blob: ");
    scanf("%d%d", &y_inp, &x_inp);
    
    /* Count the size of the picture blob */
    blob_size = blob_check(grid, x_inp, y_inp);
    
    /* Display the size number */
    printf("\nThe picture blob has a cell size of %d\n", blob_size);
    
    return 0;   
}

int blob_check(int pic_grid[5][5], int x, int y)
{
    int total_size;                           /* The cell number size of the picture blob */
    
    /* Count size of picture blob */
    if (pic_grid[x][y] == 0 || (x < 0 || x > 4) || (y < 0 || y > 4))
     total_size = 0;
    else
    {
     pic_grid[x][y] = 0;
     total_size = 1 + blob_check(pic_grid, x, y) + blob_check(pic_grid, x + 1, y + 1) + blob_check(pic_grid, x - 1, y - 1) + 
     blob_check(pic_grid, x + 1, y) + blob_check(pic_grid, x - 1, y) + blob_check(pic_grid, x, y + 1) + blob_check(pic_grid, x, y - 1) + 
     blob_check(pic_grid, x + 1, y - 1) + blob_check(pic_grid, x - 1, y + 1);
    }
    
    return total_size;   
}

Thanks for all your help. :)
Was This Post Helpful? 0
  • +
  • -

#6 Adak  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 331
  • View blog
  • Posts: 1,168
  • Joined: 01-April 11

Re: C - Help for a program that recursively counts pixels in photos

Posted 09 February 2013 - 08:18 PM

Great that you got it working!

As a matter of design however, I question using recursion. Iteration would be inherently more robust.

Nevertheless, good effort! ;)

This post has been edited by Adak: 09 February 2013 - 08:19 PM

Was This Post Helpful? 0
  • +
  • -

#7 Yuriy M  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 43
  • Joined: 05-September 07

Re: C - Help for a program that recursively counts pixels in photos

Posted 10 February 2013 - 03:51 PM

View PostAdak, on 09 February 2013 - 08:18 PM, said:

Great that you got it working!

As a matter of design however, I question using recursion. Iteration would be inherently more robust.

Nevertheless, good effort! ;)/>

To be perfectly honest, I never wanted to use recursion in the first place. However, the programming question provided expects me to use recursion.

If I had my way, I would never use recursion at all and stick with good old iteration just for the readability factor alone. However, some people in this world swear by recursion and will expect their teammates or sub-ordinates to respect it. It always helps to be prepared for all scenarios.
Was This Post Helpful? 0
  • +
  • -

#8 jjl  Icon User is offline

  • Engineer
  • member icon

Reputation: 1112
  • View blog
  • Posts: 4,619
  • Joined: 09-June 09

Re: C - Help for a program that recursively counts pixels in photos

Posted 10 February 2013 - 04:11 PM

Quote

If I had my way, I would never use recursion at all and stick with good old iteration just for the readability factor alone.

Just as some problems are naturally solved iterativly, some problems are naturally solved recursively. There is a time and place for every tool in a tool box, but not every tool is perfect for any job.
Was This Post Helpful? 0
  • +
  • -

#9 Yuriy M  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 43
  • Joined: 05-September 07

Re: C - Help for a program that recursively counts pixels in photos

Posted 10 February 2013 - 06:28 PM

But let us say that you have two tools in the toolbox that can provide the results that you need but do things in a different manner to get to those said results. Eventually, it creates a conflict of interest where you may end up choosing one tool over the other. If you're more comfortable using one tool that gets the results that you need over the other, then why use the other tool?
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1