如何以编程方式检测图像的边界? [英] How to detect an image border programmatically?

查看:175
本文介绍了如何以编程方式检测图像的边界?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在寻找其检测图像的边界的程序,
比如我有一个广场和程序检测到X / Y-COORDS

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

例如:

推荐答案

这是一个非常简单的边缘检测。它适用于二值图像。它只是计算像image.pos水平和垂直像素之间的差异[1,1] = image.pos [1,1] - image.pos [1,2],并且在同一垂直差异。记住,你还需要正常化它值0..255的范围内。

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.

但是!如果你只需要一个程序,使用Adobe公司的Photoshop。

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

code C#编写的。

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);
}

从的Util类方法

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.

这篇关于如何以编程方式检测图像的边界?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆