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?

New Topic/Question
Reply



MultiQuote





|