We’ve had a task today where we had to find all pixels which were in connection with each other in a picture. The picture is simply green = 1, no color = 0, and can be represented with an array:
int grid[5][5] = {
{1, 1, 0, 0, 0},
{0, 1, 1, 0, 0},
{0, 0, 1, 0, 1},
{1, 0, 0, 0, 1},
{0, 1, 0, 1, 1},
};
We sat for 3 hours in my group and tried to solve this (first lecture about recursion), but we simply cannot get it to work. The issue seems to be that the functions somehow accepts being outside the grid. Any who, here’s the code:
#include <stdio.h>
#include <string.h>
//Prototype
int blob_count(int y, int x, int gridCopy[][5], int sum);
int blob_count(int y, int x, int gridCopy[][5], int sum){
//Local vars
int posX, posY;
printf("\n\nX: %d - Y: %d\nposX: %d - posY: %d\nSum: %d", x, y, posX, posY, sum);
printf("\n\n{%d - %d - %d - %d - %d}\n{%d - %d - %d - %d - %d}\n{%d - %d - %d - %d - %d}\n{%d - %d - %d - %d - %d}\n{%d - %d - %d - %d - %d}\n",
gridCopy[0][0],
gridCopy[0][1],
gridCopy[0][2],
gridCopy[0][3],
gridCopy[0][4],
gridCopy[1][0],
gridCopy[1][1],
gridCopy[1][2],
gridCopy[1][3],
gridCopy[1][4],
gridCopy[2][0],
gridCopy[2][1],
gridCopy[2][2],
gridCopy[2][3],
gridCopy[2][4],
gridCopy[3][0],
gridCopy[3][1],
gridCopy[3][2],
gridCopy[3][3],
gridCopy[3][4],
gridCopy[4][0],
gridCopy[4][1],
gridCopy[4][2],
gridCopy[4][3],
gridCopy[4][4]);
//Find the position 1 behind and 1 above the starting point, and start the loop there
for(posX = -1;posX <=1; posX++){
for(posY = -1; posY <= 1; posY++){
if((y + posY) >= 0 && (x + posX) >= 0){
if((y + posY) <= 5 && (x + posX) <= 5){
if(gridCopy[posY+y][posX+x] == 1){
//Set the starting point to 0 (so it wont get calculated again)
gridCopy[posY+y][posX+x] = 0;
y = posY+y;
x = posX+x;
sum++;
blob_count(y, x, gridCopy, sum);
}
}
}
}
}
return sum;
}
int main (void)
{
int grid[5][5] = {
{1, 1, 0, 0, 0},
{0, 1, 1, 0, 0},
{0, 0, 1, 0, 1},
{1, 0, 0, 0, 1},
{0, 1, 0, 1, 1},
};
//Create a manipulateable grid
int gridCopy[5][5];
memcpy(gridCopy, grid, 5*5*sizeof(int));
//Start Positions
int start_x = 0;
int start_y = 3;
//If the starting point selected, is not 0, initiate the function:
if(grid[start_y][start_x] != 0){
printf("\nSum: %d", blob_count(start_y ,start_x, gridCopy, 0));
}
}
Edit:
I have updated the code above, and now I have a new problem, it gets the right sum (ammount of pixels in conjunction of each other), however when I print out the sum (in the main();) it says 1, why doesn’t it carry over from the recursive function to the main function?
You look at all the neighbours of the current pixel:
And you check that the coordinates are positive:
But you don’t check that the coordinates don’t overflow:
And access the undefined data outside the grid instead:
It probably still won’t work though, but since this is homework I’ll just give you some tips: you’re ignoring the return value of the recursive call (don’t do that) and you don’t initialize
sum(turn on the compiler warnings to get notified of that one). You also don’t copy the complete grid:memcpy(gridCopy, grid, 5*5);copies 25 bytes, not 25 ints. Usememcpy(gridCopy, grid, 5 * 5 * sizeof(int));instead.