14
$\begingroup$

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?

$\endgroup$
5
  • 1
    $\begingroup$ 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 ? $\endgroup$
    – Paul R
    Sep 7, 2012 at 12:47
  • 4
    $\begingroup$ Please don't down vote new users for their very first question on the community! $\endgroup$
    – bot
    Sep 7, 2012 at 12:48
  • 1
    $\begingroup$ This thread might be useful- dsp.stackexchange.com/questions/2975/… $\endgroup$
    – Jim Clay
    Sep 7, 2012 at 16:19
  • $\begingroup$ Edge detectors explained: dsp.stackexchange.com/q/74/1273 $\endgroup$
    – penelope
    Sep 11, 2012 at 15:27
  • $\begingroup$ "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. $\endgroup$
    – user4628
    May 22, 2013 at 2:35

4 Answers 4

10
$\begingroup$

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.

$\endgroup$
5
$\begingroup$

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.

$\endgroup$
2
$\begingroup$

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
$\endgroup$
1
  • 2
    $\begingroup$ Welcome to dsp.stackexchange :) Any answer, even a late one, is very welcome, but it would be nice if you provided some context with your answer. Answers which provide explanations and sources are preferred -- could you edit your answer, write a few sentences of what the code does and how it would help with the problem asked, and maybe cite the source if it's not you? If would make your answer much better. Also, edit your identation please -- I've tried, but I got lost after going through a third of your code. $\endgroup$
    – penelope
    Nov 23, 2012 at 12:54
0
$\begingroup$

If your image is relatively clean, you have obvious rectangles without a lot of breaks the alternative to a Hough transform is to create contours and reduce them until they form a 4 sided contour = your rectangle.

There are opencv samples to do this

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.