954,499 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

System.StackOverflowException problem

Hi all,

I'm trying to implement a blob coloring/region labeling algorithm to find and label all blobs/objects in a given image so that at the end of it, I can eliminate all but the largest object. I represented the image by a mask matrix (int binary[image size]) where zero co-ordinate value represents background and one represents foreground.

N/B: its not binary since the assigned labels will assume value 2 onwards...

I first tried using a recursive algorithm which bugged with a stackoverflow exception, then tried using the 'L' shaped blob coloring algorithm which gave problems whenever I scaled to a larger image and I'm now back to the recursive algorithm out of frustration. My idea is to give all non-zero pixels a value similar to their neighbors and different from other regions using 4-connectivity.

I browsed some answers on the internet but they are too general to be of any use to my situation, I really need a good Samaritan to point out exactly what and where is the problem in my code.

My problem is that I receive the exception "An unhandled exception of type 'System.StackOverflowException' occurred in projectConsole.exe" whenever I execute my algorithm on a LARGER image (size 1024*1024 in this case). The IDE debugger (MS Visual studio 2008) points to the beginning of the recursive block which is not helpful since I believe the problem may have been caused earlier. This exception occurs after several executions of the recursive code (in fact it works right, even in terms of result, with very small images e.g. of size 9*9). Below are the two functions:

void Process::getRoi2()
{
	int r=cdata->nRows, c=cdata->nCols, *binary=new int[r*c], label=2;
	for(int i=0;i<(r*c);++i){if(mask[i]<3/*cdata->data[i]!=1*/) binary[i]=0; else binary[i]=1; }

	
	for(int i=0;i<r;++i)for(int j=0;j<c;++j){
		if(binary[i*c+j]==1) growr(binary,i,j,label++);
	}
	 
}
void Process::growr(int *bin, int i, int j, int label)
{
	int r=cdata->nRows, c=cdata->nCols; 
	if(i>=r || i<0 || j>=c && j<0) return;
	if(bin[i*c+j]!=1) return;
	
	/*regcnt++;*/ bin[i*c+j]=label;
	if(i>=0	&& i<r && j-1>=0 && j-1<c)	//left neighbor (i,j-1)
		{	if(bin[i*c+j-1]==1) growr(bin,i,j-1,label);	}
	if(i>=0 && i<r && j+1>=0 && j+1<c)	//right neighbor (i,j+1)
		{	if(bin[i*c+j+1]==1) growr(bin,i,j+1,label);	}
	if(i-1>=0 && i-1<r && j>=0 && j<c)		//top neighbor (i-1,j)
		{	if(bin[(i-1)*c+j]==1) growr(bin,i-1,j,label);	}
	if(i+1>=0 && i<r && j>0 && j<c)		//bottom neighbor (i+1,j)
		{	if(bin[(i+1)*c+j]==1) growr(bin,i+1,j,label);	}
	
}
darelet
Newbie Poster
19 posts since May 2010
Reputation Points: 10
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: