views:

250

answers:

2

Hello everyone, I have written a code to detect edges using double thresholding,what i am doing here is if the higher threshold image has a pixel value of 255 then it is valid and if the lower thrsehold image has a connected white pixel to that pixel then it is valid edge. but in my program only the high threshold image is getting copied.The low threshold valid pixels are not getting drawn.moreover this code is working for larger images not smaller images i really cant understant y because i have nowhere hardcoded the values of the width and height.

here is my code

 void edge_linking(ImgGray img_low,ImgGray img_high,ImgGray *img_edge_link,int mu)
 {
     int x,y;

     CPoint p;
     vector<Point>edgelink;

     for(y=mu;y<=(*img_edge_link).Height()-mu;y++)
     {
         for(x=mu;x<=(*img_edge_link).Width()-mu;x++)
         {
             //if(x>0 && y>0 && x< (*img_edge_link).Width()-1 &&(*img_edge_link).Height()-1)
             //{
             if(img_high(x,y)==255)
             {
                    (*img_edge_link)(x,y)=255;
                    edgelink.push_back(Point(img_high(x,y)));

                    while(!edgelink.empty())
                    {

                       p=edgelink.back();
                       edgelink.pop_back();

                       if (p.x >0 && img_low(p.x-1,p.y)==255 && (*img_edge_link)(p.x-1,p.y)!=255 )
                       { 
                           (*img_edge_link)(p.x-1,p.y)=255;
                           edgelink.push_back(Point(p.x-1,p.y));
                       }
                       if (p.y>0 && img_low(p.x,p.y-1)==255 && (*img_edge_link)(p.x,p.y-1)!=255)
                       {
                           (*img_edge_link)(p.x,p.y-1)=255;
                           edgelink.push_back(Point(p.x,p.y-1));
                       }
                       if (p.x<(*img_edge_link).Width()-1 && img_low(p.x+1,p.y)==255 && (*img_edge_link)(p.x+1,p.y)!=255)
                       {
                           (*img_edge_link)(p.x+1,p.y)=255;
                           edgelink.push_back(Point(p.x+1,p.y));
                       }
                       if (p.y <(*img_edge_link).Height()-1 && img_low(p.x,p.y+1)==255 && (*img_edge_link)(p.x,p.y+1)!=255 )
                       {
                           (*img_edge_link)(p.x,p.y+1)=255;
                           edgelink.push_back(Point(p.x,p.y+1));
                       }
                       if (p.x>0 && p.y<(*img_edge_link).Height()-1 && img_low(p.x-1,p.y+1)==255 && (*img_edge_link)(p.x-1,p.y+1)!=255)
                       {
                    (*img_edge_link)(p.x-1,p.y+1)=255;
                    edgelink.push_back(Point(p.x-1,p.y+1));
                       }
                       if(p.x>0 && p.y>0 && img_low(p.x-1,p.y-1)==255 && (*img_edge_link)(p.x-1,p.y-1)!=255)
                       {
                    (*img_edge_link)(p.x-1,p.y-1)=255;
                    edgelink.push_back(Point(p.x-1,p.y-1));
                       }
                       if(p.y<(*img_edge_link).Height()-1 && p.x<(*img_edge_link).Width()-1 && img_low(p.x+1,p.y+1)==255 && (*img_edge_link)(p.x+1,p.y+1)!=255)
                       {
                    (*img_edge_link)(p.x+1,p.y+1)=255;
                    edgelink.push_back(Point(p.x+1,p.y+1));
                       }
                       if(p.y>0 && p.x<(*img_edge_link).Width()-1 && img_low(p.x+1,p.y-1)==255 && (*img_edge_link)(p.x+1,p.y-1)!=255)
                       {
                     (*img_edge_link)(p.x+1,p.y-1)=255;
                     edgelink.push_back(Point(p.x+1,p.y-1));
                       }

                   }
              }
          }
     }
 }
+1  A: 

This line is a bit odd:

edgelink.push_back(Point(img_high(x,y)));

'edgelink' is a vector of points, yet img_high(x, y) presumably returns a single integer so you're initialising the Point class with a single value, rather than two co-ordinates. The following:

edgelink.push_back(Point(x,y));

... would seem to make more sense.

Andy
also has some typo with *Point* and *CPoint*
Amro
+1  A: 

Couple of points:

First, you say it doesn't work with small images, could it be because of the mu argument (Im assuming its some kind of border?). Also is the output image initialized to zeros?

Second, I think you can improve you code by avoiding some unnecessary checks.
Start by computing the difference between the low threshold image and the high image (low=low-high : pixels in low that are not in high). This will give you the weak edges stored in low, and the strong edges stored in high.
Next you loop over the high image with pixels values of 255, and mark their neighborhood in low with values equal 255 to say 128.
Finally simply set the pixels in high to 255 where the corresponding pixels in low are 128 (weak edges adjacent to strong ones).

Apart from the errors mentioned by others, I dont see further problems...

Amro