Hi,

I am using Cimage library to do a Sobel algorithm. Here is the code

CImage * image1;
image1->Load(".\\tempSnapShot1.bmp");
			
CImage* returnImage = new	CImage();
				
Sobel(image1,returnImage);

The Sobel function is

void CI90ControllerDlg::Sobel(CImage* image,CImage *returnImage)
 {
	 

   returnImage->Create(image->GetWidth(),image->GetHeight(), image->GetBPP(),0);
   
   double GX[3][3],GY[3][3];
   int		sumX = 0;
   int		sumY = 0;
   int		SUM = 0;

   // Masks //////////////////////////////////////
   //X//
   GX[0][0] = -1; GX[0][1] = 0; GX[0][2] = 1;
   GX[1][0] = -2; GX[1][1] = 0; GX[1][2] = 2;
   GX[2][0] = -1; GX[2][1] = 0; GX[2][2] = 1;
   //Y//
   GY[0][0] =  1; GY[0][1] = 2; GY[0][2] = 1;
   GY[1][0] =  0; GY[1][1] = 0; GY[1][2] = 0;
   GY[2][0] = -1; GY[2][1] = -2; GY[2][2] = -1;
   //int r=image->GetHeight();

   for(int Ypix=0; Ypix < image->GetHeight(); Ypix++)  
   {
	for(int X=0; X < image->GetWidth(); X++)  
	{
	     sumX = 0;
	     sumY = 0;

	     if(Ypix==0 || Ypix == image->GetHeight()-1)
		SUM = 0;
	     else if(X==0 || X == image->GetWidth()-1)
		SUM = 0;
	     else
	     {
	         for(int I=-1; I<=1; I++)  
			 {
				for(int J=-1; J<=1; J++) 
					{

					int piX = J + X;
					int piY = I + Ypix;

					COLORREF pixVal = image->GetPixel(piX,piY);

					int R = GetRValue(pixVal);
					int G = GetGValue(pixVal);
					int B = GetBValue(pixVal);

					int NC = (R+G+B)/3;

					sumX = sumX + (NC) * GX[J+1][I+1];
					sumY = sumY + (NC) * GY[J+1][I+1];  

					}
	         }
                SUM = abs(sumX) + abs(sumY);
          }
         if(SUM>255) SUM=255;
         if(SUM<0) SUM=0;
         int newPixel = (255 - (unsigned char)(SUM));

         COLORREF newPixCol =  RGB(newPixel,newPixel,newPixel);
         returnImage->SetPixel(X,Ypix,newPixCol) ;
         }
   }
  
 }

The image is loaded ok, but in the Sobel function it cracks and gives me this error

Debug assertion failed Expression: (x> = 0) & & (x <m_nWidth)

I also saw that when it goes to the sobel function I have

for(int Ypix=0; Ypix < image->GetHeight(); Ypix++)  
   {

and when I put a breakpoint at this for Ypix is a big number -8741824 or something like that...and that is where is cracks.

Can anyone help me? i tried to find answers on the internet but nothing came up.
Thank you,

at what line in your code the the assertion happen? also is the value for Ypix what you see when the debugger gets to that line? if show it is showing you the value of Ypix before it is assigned to 0.

Please use code tags!

at what line in your code the the assertion happen? also is the value for Ypix what you see when the debugger gets to that line? if show it is showing you the value of Ypix before it is assigned to 0.

The assertion is happening in atlimage.h , line 1410. The value of Ypix is big when I get to that line in my program, the assertion debug comes after a while, because the program blocks when I get to that line. I assigned 0 to Ypix at the beginning, so it will not be any confusion.

what do you mean by after awhile? after awhile in the for loops or after awhile once the program leaves the for loops.

what do you mean by after awhile? after awhile in the for loops or after awhile once the program leaves the for loops.

After a while, once the program leaves the for loop that I said is blocking the program. It doesn't follow th for loop completely, it is blocking after Ypix =0.

what happens if you move the Ypix deleration to before the for loop like

int Ypix = 0;
for(Ypix; Ypix < image->GetHeight(); Ypix++)
{
//...

what happens if you move the Ypix deleration to before the for loop like

int Ypix = 0;
for(Ypix; Ypix < image->GetHeight(); Ypix++)
{
//...

Yeah, I tried that and nothing is changed.

Are you using a debugger? Can you add a few lines of your own code such as

if ( Ypix < 0 )
{
   puts("whoa!"); /* put breakpoint here */
}

Maybe even sprinkle more than one of these around the loops to try to narrow down the search?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.