c# - 在 C# 中查找大图像中的子图像

标签 c# image function location

我想编写一个获取 2 个位图(小图像和大图像)的函数。 它应该在大图像中搜索小图像的位置。然后它应该返回子图像所在的(X/Y)点。

我已经找到了一堆解决方案,但都是用 Java 编写的,例如这个:Find known sub image in larger image 所以我尝试用 C# 重新编写它们,但我总是失败,因为代码不是真正的 Java。那里不存在 Image.getPixel(x, y) 方法,我真的不知道阈值应该是多少。

最佳答案

给你。此函数返回在大图像中找到的所有位置,因为小图像也可以多次包含在大图像中。如果需要,您可以重写函数 unsafe 以获得更高的性能。

public static List<Point> GetSubPositions(Bitmap main, Bitmap sub) {
    List<Point> possiblepos = new List<Point>();

    int mainwidth = main.Width;
    int mainheight = main.Height;

    int subwidth = sub.Width;
    int subheight = sub.Height;

    int movewidth = mainwidth - subwidth;
    int moveheight = mainheight - subheight;

    BitmapData bmMainData = main.LockBits(new Rectangle(0, 0, mainwidth, mainheight), ImageLockMode.ReadWrite, PixelFormat.Format32bppArgb);
    BitmapData bmSubData = sub.LockBits(new Rectangle(0, 0, subwidth, subheight), ImageLockMode.ReadWrite, PixelFormat.Format32bppArgb);

    int bytesMain = Math.Abs(bmMainData.Stride) * mainheight;
    int strideMain = bmMainData.Stride;
    System.IntPtr Scan0Main = bmMainData.Scan0;
    byte[] dataMain = new byte[bytesMain];
    System.Runtime.InteropServices.Marshal.Copy(Scan0Main, dataMain, 0, bytesMain);

    int bytesSub = Math.Abs(bmSubData.Stride) * subheight;
    int strideSub = bmSubData.Stride;
    System.IntPtr Scan0Sub = bmSubData.Scan0;
    byte[] dataSub = new byte[bytesSub];
    System.Runtime.InteropServices.Marshal.Copy(Scan0Sub, dataSub, 0, bytesSub);

    for (int y = 0; y < moveheight; ++y) {
        for (int x = 0; x < movewidth; ++x) {
            MyColor curcolor = GetColor(x, y, strideMain, dataMain);

            foreach (var item in possiblepos.ToArray()) {
                int xsub = x - item.X;
                int ysub = y - item.Y;
                if (xsub >= subwidth || ysub >= subheight || xsub < 0)
                    continue;

                MyColor subcolor = GetColor(xsub, ysub, strideSub, dataSub);

                if (!curcolor.Equals(subcolor)) {
                    possiblepos.Remove(item);
                }
            }

            if (curcolor.Equals(GetColor(0, 0, strideSub, dataSub)))
                possiblepos.Add(new Point(x, y));
        }
    }

    System.Runtime.InteropServices.Marshal.Copy(dataSub, 0, Scan0Sub, bytesSub);
    sub.UnlockBits(bmSubData);

    System.Runtime.InteropServices.Marshal.Copy(dataMain, 0, Scan0Main, bytesMain);
    main.UnlockBits(bmMainData);

    return possiblepos;
}

private static MyColor GetColor(Point point, int stride, byte[] data) {
    return GetColor(point.X, point.Y, stride, data);
}

private static MyColor GetColor(int x, int y, int stride, byte[] data) {
    int pos = y * stride + x * 4;
    byte a = data[pos + 3];
    byte r = data[pos + 2];
    byte g = data[pos + 1];
    byte b = data[pos + 0];
    return MyColor.FromARGB(a, r, g, b);
}

struct MyColor {
    byte A;
    byte R;
    byte G;
    byte B;

    public static MyColor FromARGB(byte a, byte r, byte g, byte b) {
        MyColor mc = new MyColor();
        mc.A = a;
        mc.R = r;
        mc.G = g;
        mc.B = b;
        return mc;
    }

    public override bool Equals(object obj) {
        if (!(obj is MyColor))
            return false;
        MyColor color = (MyColor)obj;
        if(color.A == this.A && color.R == this.R && color.G == this.G && color.B == this.B)
            return true;
        return false;
    }
}

关于c# - 在 C# 中查找大图像中的子图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11343772/

相关文章:

c# - Azure 存储表中的 InsertOrUpdate 与 InsertOrMerge,有什么区别?

c# - 在 c# Windows 窗体应用程序中的文本框外单击鼠标时是否会触发事件处理程序?

java - 函数何时应该调用另一个函数以及何时使用方法依赖注入(inject)

function - `(Integer a) => a -> Bool` 和 ` Integer -> Bool` 之间的区别?

html - 垂直图像大于父 div

r - 为什么这个 geom_polygon 只包含整数而不包含小数?

c# - 将数据表转换为 excel 2007(.xlsx)

c# - 使用 DataContractSerializer 反序列化 XML

JavaScript/jQuery UI : Changing element img src from an array of images based on current img src

Python - OpenCV - 裁剪图像和隔离特定对象