Counting Pixels In A Blob

This is a discussion on Counting Pixels In A Blob within the C Programming forums, part of the General Programming Boards category; The purpose of the program is to take a position and count how many "pixels" are in the "blob". If ...

  1. #1
    Registered User
    Join Date
    Nov 2009
    Posts
    4

    Counting Pixels In A Blob

    The purpose of the program is to take a position and count how many "pixels" are in the "blob". If there is no "pixel" aka the value is 0, the blob number is 0. If there is a pixel, the blob_check function recursively checks the surrounding cells for "pixels" aka values of 1. My program compiles but doesn't accurately count the number of pixels in the blob. If I ask it to count the number of pixels in the blob for (0,0) it gives a 4 instead of a 5. Please help!



    Code:
    #include <stdlib.h>
    #include <stdio.h>
    
    #define N 5
    
    int blob_check(int pic[N][N], int x, int y)
    {
    	if (pic[x][y] == 0)
    		return 0;
    	else
    	{
    		pic[x][y] = 0;
    		
    		int sum = 1;
    		
    		//check 
    		if (x>0)
    		{
    			sum += blob_check(pic, x-1, y);
    			if (y<N-1)
    				sum += blob_check(pic, x-1, y+1);
    		}
    		else if (y<N-1)
    		{
    			sum += blob_check(pic, x, y+1);
    			if (x<N-1)
    				sum += blob_check(pic, x+1, y+1);
    		}
    		else if (x<N-1)
    		{
    			sum += blob_check(pic, x+1, y);
    			if (y>0)
    				sum += blob_check(pic, x+1, y-1);
    		}		
    		else if (y>0)
    		{
    			sum += blob_check(pic, x, y-1);
    			if (x>0)
    				sum += blob_check(pic, x-1, y-1);
    		}
    
    		return sum;
    	}
    }
    
    int main()
    {
    	int x,y,i,j;
    	int row=0;
    	char line[80];
    	int table[N][N] = { {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}};
    				
    	printf("\t0\t1\t2\t3\t4\t");
    	printf("\n   -----------------------------------------");			
    	for(i=0;i<5;i++)
    	{
    		printf("\n   |\n%d  |\t", i);
    		for(j=0;j<5;j++)
    		{
    			printf("%d\t", table[i][j]);
    		}
    	}
    								
    								
    	printf("\n\nEnter x-y coordinates of cell  => ");
    	scanf("%d %d", &y, &x);								
    	
    	printf("Pixel quantity in blob: %i\n", blob_check(table, x, y));
    	
    	system("pause");
    	return 0;
    }

  2. #2
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Katy, Texas
    Posts
    2,309
    A simple printf on entry to blob_check would tell you the order or elements being checked. Are you only supposed to be checking adjacent cells?
    Last edited by Dino; 11-20-2009 at 01:42 PM. Reason: typo
    Mac and Windows cross platform programmer. Ruby lover.

    Quote of the Day
    12/20: Mario F.:I never was, am not, and never will be, one to shut up in the face of something I think is fundamentally wrong.

    Amen brother!

  3. #3
    Registered User
    Join Date
    Nov 2009
    Posts
    4
    We have to check the 8 cells surrounding the given one, vertically, horizontally and diagonally.

  4. #4
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Katy, Texas
    Posts
    2,309
    Then you are checking too many.
    Mac and Windows cross platform programmer. Ruby lover.

    Quote of the Day
    12/20: Mario F.:I never was, am not, and never will be, one to shut up in the face of something I think is fundamentally wrong.

    Amen brother!

  5. #5
    Registered User
    Join Date
    Nov 2009
    Posts
    4
    what do you mean?

  6. #6
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Katy, Texas
    Posts
    2,309
    Add a printf statement to your blob_check function, printing out the coordinates being checked, and you'll see what I mean.
    Mac and Windows cross platform programmer. Ruby lover.

    Quote of the Day
    12/20: Mario F.:I never was, am not, and never will be, one to shut up in the face of something I think is fundamentally wrong.

    Amen brother!

  7. #7
    Registered User
    Join Date
    Nov 2009
    Posts
    4
    ahhh i see... but why would it be checking those? if it returns a 0, shouldn't it end the recursion?

  8. #8
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Katy, Texas
    Posts
    2,309
    It's doing exactly what you coded it to do. Check your logic - work it out on paper with the output of your printf and see why. Then, fix it once you understand what is happening.
    Mac and Windows cross platform programmer. Ruby lover.

    Quote of the Day
    12/20: Mario F.:I never was, am not, and never will be, one to shut up in the face of something I think is fundamentally wrong.

    Amen brother!

  9. #9
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,307
    This is essentially a variant on the flood fill algorithm.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Trying to write individual pixels to the screen
    By IanC in forum C++ Programming
    Replies: 3
    Last Post: 04-14-2004, 12:49 PM
  2. bitmap pixels???????????
    By SuperNewbie in forum Windows Programming
    Replies: 2
    Last Post: 03-23-2004, 12:53 AM
  3. creating image from pixels stored in file
    By Kristian25 in forum Windows Programming
    Replies: 3
    Last Post: 01-21-2003, 01:08 PM
  4. Algo needed for 'Fit to page'
    By Unregged in forum Windows Programming
    Replies: 6
    Last Post: 10-03-2002, 07:09 AM
  5. What Else can I do besides Plot Pixels?
    By Xterria in forum C++ Programming
    Replies: 13
    Last Post: 10-02-2001, 07:11 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21