首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > C# >

c# 关于找图有关问题 -有关问题代码 求修改 求解 F5 已烂

2013-09-17 
c# 关于找图问题 -------问题代码求修改 求解 F5 已烂public Point GetImageContains(Bitmap p_SourceBitm

c# 关于找图问题 -------问题代码 求修改 求解 F5 已烂

public Point GetImageContains(Bitmap p_SourceBitmap, Bitmap p_PartBitmap, int p_Float,int pointnums)
        {
            Point P_res = new Point(-1, -1);

            int Height_S = p_SourceBitmap.Height;
            int Width_S = p_SourceBitmap.Width;

            int Height_P = p_PartBitmap.Height;
            int Width_P = p_PartBitmap.Width;

            //原图转换成 byte[]
            BitmapData _SourceData = p_SourceBitmap.LockBits(new Rectangle(0, 0, p_SourceBitmap.Width, p_SourceBitmap.Height), ImageLockMode.ReadWrite, PixelFormat.Format32bppArgb);
            byte[] DataSource = new byte[_SourceData.Stride * p_SourceBitmap.Height];
            Marshal.Copy(_SourceData.Scan0, DataSource, 0, DataSource.Length); 
            p_SourceBitmap.UnlockBits(_SourceData);
            //子图转换成 byte[]
            BitmapData _PartData = p_PartBitmap.LockBits(new Rectangle(0, 0, p_PartBitmap.Width, p_PartBitmap.Height), ImageLockMode.ReadWrite, PixelFormat.Format32bppArgb);
            byte[] DataPart = new byte[_PartData.Stride * p_PartBitmap.Height];
            Marshal.Copy(_PartData.Scan0, DataPart, 0, DataPart.Length);
            p_PartBitmap.UnlockBits(_PartData);

            int index=0;           
           
            for (int b = 0; b < Height_S - Height_P; b++)
                for (int a = 0; a < Width_S - Width_P; a++)


                {
                    //5点  分别为左上  右上  中间   左下   右下
                    index = (b * Width_S + a) * 4;
                    bool b1 = ScanColor(DataSource[index], DataSource[index + 1], DataSource[index + 2], DataPart[0], DataPart[1], DataPart[2], p_Float);
                    if (!b1) continue;

                    Point tempp = new Point(a, b);

                    int p2 = (Width_P - 1) * 4;
                    b1 = ScanColor(DataSource[index + p2], DataSource[index + 1 + p2], DataSource[index + 2 + p2], DataPart[p2], DataPart[p2 + 1], DataPart[p2 + 2], p_Float);
                    if (!b1) continue;

                    tempp = new Point(a, b + Width_P);

                    p2 = ((Height_P / 2) * Width_P + (Width_P / 2)) * 4;
                    int p3 = ((b + (Height_P / 2)) * Width_S + (a + (Width_P / 2))) * 4;
                    b1 = ScanColor(DataSource[p3], DataSource[p3 + 1], DataSource[p3 + 2], DataPart[p2], DataPart[p2 + 1], DataPart[p2 + 2], p_Float);
                    if (!b1) continue;



                    p2 = ((Height_P - 1) * Width_P) * 4;
                    p3 = ((b + (Height_P - 1)) * Width_S + a) * 4;
                    b1 = ScanColor(DataSource[p3], DataSource[p3 + 1], DataSource[p3 + 2], DataPart[p2], DataPart[p2 + 1], DataPart[p2 + 2], p_Float);
                    if (!b1) continue;

                    p2 = (Height_P * Width_P - 1) * 4;
                    p3 = ((b + Height_P) * Width_S + (a + Width_P) - 1) * 4;
                    b1 = ScanColor(DataSource[p3], DataSource[p3 + 1], DataSource[p3 + 2], DataPart[p2], DataPart[p2 + 1], DataPart[p2 + 2], p_Float);
                    if (!b1) continue;
                    else
                    {
                        //5点匹配 则匹配其余的点
                        if (ImgDb(index, DataSource, DataPart, p_Float, pointnums))
                        {
                            P_res = new Point(a, b);
                            break;


                        }
                        else
                            P_res = new Point(-1, -1);
                    }
                }
            return P_res;
        }

private bool ImgDb(int index, byte[] DataImage, byte[] DataPart, int p_Float, int pointnums)
        {
            int tempint = pointnums;
            for (int i = index; i < (DataPart.Length + index); i+=4)
            {
                bool b3 = ScanColor(DataImage[index], DataImage[index + 1], DataImage[index + 2], DataPart[i - index], DataPart[i - index + 1], DataPart[i - index + 2], p_Float);

                if (!b3 && tempint > 0)
                {
                    tempint--;
                }
                else if (!b3)
                    return false;
            }
            return true;
        }

private static bool ScanColor(int r1,int g1,int b1,int r2,int g2,int b2, int p_Float)
        {


            return (r1 <= (r2 + p_Float) && (r1 >= r2 - p_Float)) && ((g1 <= g2 + p_Float) && g1 >= (g2 - p_Float)) && (b1 <= (b2 + p_Float) && (b1 >= b2 - p_Float));
        }




以上代码有 写错的地方  貌似是  ImgDb 方法  求解 c# bitmap 找图
[解决办法]
顶。。。。。。
[解决办法]
引用:

如果你的原图是100*100的 你要找的图片是99*99的
即是说这两张图片是一样的
只是宽和高有一个像素的变化
这两张图片你在对比的时候 
你的就行不通了 
不知道你的错误点数是怎么判断的
有一个相似度?

热点排行