# How to detect edges and rectangles

I try to detect rectangles in images. The background of the images is one color (most of the time). I tried two methods to get an binary image (1 = background, 0 = edges), to do an Hough Transformation later on...

1. Sobel or Canny Filter

2. Smooth image A, Create difference image A - gauss, Create binary image with threshold (Create Histogram, highest bin should be background...)

The result is a binary image with edges. I don't really now which method works better for a variety of different images. Any ideas?

-

## migrated from stackoverflow.comSep 7 '12 at 14:38

This question came from our site for professional and enthusiast programmers.

What do you mean by "works better" ? Canny is very popular for this kind of thing but it depends on what you're trying to do once you have the edges. What are you trying to achieve, exactly ? – Paul R Sep 7 '12 at 12:47
Please don't down vote new users for their very first question on the community! – bot Sep 7 '12 at 12:48
This thread might be useful- dsp.stackexchange.com/questions/2975/… – Jim Clay Sep 7 '12 at 16:19
Edge detectors explained: dsp.stackexchange.com/q/74/1273 – penelope Sep 11 '12 at 15:27
"The result is a binary image with edges. I don't really now which method works better for a variety of different images. Any ideas?" Maybe you need some image test lib to find the answer or take some pictures in the environments that you maybe count. If there exists a best algorithms in this field, why should we learn so much others?I believe any algorithms has its advantage sometimes, in probability sense. – user4628 May 22 '13 at 2:35

I once wrote an application for rectangle detection. It used Sobel edge detection and line Hough transform.

Instead of looking for single peaks in Hough image (lines), the program searched 4 peaks with distance of 90 degrees between them.

For each column in Hough image (corresponding to some angle), three other columns were searched for local maxima. When satifactory peak was found in each of the four columns, the rectangle have been detected.

The program constructed the rectangle and made additional checks for color consistency within and outside the rectangle to discriminate false positives. The program was for detecting paper placement in scanned sheets of papers.

-

You might find that the Laplacian of Gaussian edge detector is a better choice. It should give you closed contours more often than the Canny edge detector. I believe that is what you want since your next step is to apply the Hough transform.

-

Might be its helpfull for you but its too late as i visit this site today

        Bitmap bmp=new Bitmap(pictureBox1.Image);
int x1=0, x2=0, y1=0, y2=0;
for (int i = 1; i < bmp.Height;i++ )
{
for (int j = 1; j < bmp.Width;j++ )
{
if( bmp.GetPixel(j,i).R<7  &&  bmp.GetPixel(j-1,i).R>240  && bmp.GetPixel(j,i-1).R>240 ){

for (int k = j; k < bmp.Width - 1;k++ )
{

if ((bmp.GetPixel(k, i).R < 7) && (bmp.GetPixel(k+1, i).R > 240) && (k-j>30)) {
int count1 = 0;

for (int g = j; g < k;g++ ){
if(bmp.GetPixel(g,i).R<7){
count1++;
}
}//get total width

if(count1==k-j){
x1 = j;
y1 = i;
x2 = k;
}
}
}
for (int a = i; a < bmp.Height - 1;a++ )
{
if ((bmp.GetPixel(j, a).R < 7) && (bmp.GetPixel(j, a+1).R > 240) && (a- i > 30)) {

int count2 = 0;

for (int x = i; x < a;x++ )
{
if(bmp.GetPixel(j,x).R<7){
count2++;
}
}

if (count2 == (a - i))
{

y2 = a;
}
else {
Console.WriteLine("check");
}
}

}

if ((bmp.GetPixel(x2, y2).R < 7) && (bmp.GetPixel(x2 + 1, y2).R > 240) && (bmp.GetPixel(x2, y2+1).R > 240))
{

bool r1 = false;
bool r2 = false;
int count3 = 0;
for (int y = y1; y < y2;y++ )
{
if(bmp.GetPixel(x2,y).R<7){
count3++;
}
}

if (count3== y2 - y1) {
r1 = true;
}
if(r1==true){
int count4=0;
for (int x = x1; x < x2;x++ )
{
if(bmp.GetPixel(x,y1).R<7){
count4++;
}
}

if(count4==x2-x1){
r2 = true;
Console.WriteLine("values :  X1 " + x1 + "   y1 :" + y1 + "   width : " + (x2 - x1) + "  height :  " + (y2 - y1));
Pen pen = new Pen(Color.Red, 2);
pictureBox1.CreateGraphics().DrawRectangle(pen, x1, y1, x2 - x1, y2 - y1);
}
}
}

}

}// initial point loop

}// first if

-