How to detect an image border programmatically?

前端 未结 5 1438
春和景丽
春和景丽 2021-02-11 07:47

I\'m searching for a program which detects the border of a image, for example I have a square and the program detects the X/Y-Coords

Example:

alt text http://img

相关标签:
5条回答
  • 2021-02-11 07:56

    You can use any image processing library such as Opencv. which is in c++ or python. You should look for edge detection functions such as Canny edge detection. Of course this would require some diving into image processing. The example image you gave should be straight forward to detect, how noisy/varied are the images going to be?

    0 讨论(0)
  • 2021-02-11 08:06

    A shape recognition algorithm might help you out, providing it has a solid border of some kind, and the background colour is a solid one.

    0 讨论(0)
  • 2021-02-11 08:13

    From the sounds of it, you just want a blob extraction algorithm. After that, the lowest/highest values for x/y will give you the coordinates of the corners.

    0 讨论(0)
  • 2021-02-11 08:15

    This is a very simple edge detector. It is suitable for binary images. It just calculates the differences between horizontal and vertical pixels like image.pos[1,1] = image.pos[1,1] - image.pos[1,2] and the same for vertical differences. Bear in mind that you also need to normalize it in the range of values 0..255.

    But! if you just need a program, use Adobe Photoshop.

    Code written in C#.

    public void SimpleEdgeDetection()
    {
        BitmapData data = Util.SetImageToProcess(image);
        if (image.PixelFormat != PixelFormat.Format8bppIndexed)
            return;
    
        unsafe
        {
            byte* ptr1 = (byte *)data.Scan0;
            byte* ptr2;
            int offset = data.Stride - data.Width;
            int height = data.Height - 1;
            int px;
    
            for (int y = 0; y < height; y++)
            {
                ptr2 = (byte*)ptr1 + data.Stride;
                for (int x = 0; x < data.Width; x++, ptr1++, ptr2++)
                {
                    px = Math.Abs(ptr1[0] - ptr1[1]) + Math.Abs(ptr1[0] - ptr2[0]);
                    if (px > Util.MaxGrayLevel) px = Util.MaxGrayLevel;
                    ptr1[0] = (byte)px;
                }
                ptr1 += offset;
            }
        }
        image.UnlockBits(data);
    }
    

    Method from Util Class

    static public BitmapData SetImageToProcess(Bitmap image)
    {
        if (image != null)
            return image.LockBits(
                new Rectangle(0, 0, image.Width, image.Height),
                ImageLockMode.ReadWrite,
                image.PixelFormat);
    
        return null;
    }
    

    If you need more explanation or algorithm just ask with more information without being so general.

    0 讨论(0)
  • 2021-02-11 08:15

    It depends what you want to do with the border, if you are looking at getting just the values of the edges of the region, use an algorithm called the Connected Components Region. You must know the value of the region prior to using the algorithm. This will navigate around the border and collect the outside region. If you are trying to detect just the outside lines get the gradient of the image and it will reveal where the lines are. To do this convolve the image with an edge detection filter such as Prewitt, Sobel, etc.

    0 讨论(0)
提交回复
热议问题