从较大的图像中查找较小的图像(请提供帮助) [英] Finding an smaller image from a larger image (help please)

查看:86
本文介绍了从较大的图像中查找较小的图像(请提供帮助)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,有人可以帮我吗,如果它们位于我的屏幕的50%以上并且可以完美地工作并找到完美的位置,我似乎无法弄清楚为什么这段代码找不到我想要的图像如果图片在屏幕的左侧50%. :/

Hello can anyone help me please, i cannot seem to figure out why this code can''t find the image i''m looking for if they are located on more than 50% of my screen and works perfectly and locate perfect if the image is on the left 50% of my screen. :/

private int[,] isImageThere(Bitmap fullImage, Bitmap smallImage)
{
    Color[,] fullPixels = ImageToArray(fullImage);
    Color[,] smallPixels = ImageToArray(smallImage);
    int R = 0;
    int G = 0;
    int B = 0;
    try
    {
        // Iterate through every row
        for (int fullColumn = 0; fullColumn < fullImage.Width; fullColumn++)
        {
            //1200 error
            if (fullColumn >= fullImage.Width)
                break;
            // Iterate through every pixel in the row
            for (int fullRow = 0; fullRow < fullImage.Height; fullRow++)
            {
                if (fullRow >= fullImage.Height)
                    break;
                // If the current pixel on the full image is the
                // same as the top left pixel on the small image...
                if (/*(fullPixels[fullRow, fullColumn] == smallPixels[0, 0])*/
                    (Convert.ToInt16(fullPixels[fullColumn, fullRow].R) + Convert.ToInt16(fullPixels[fullColumn, fullRow].G) + Convert.ToInt16(fullPixels[fullColumn, fullRow].B) - (Convert.ToInt16(smallPixels[0, 0].R) + Convert.ToInt16(smallPixels[0, 0].G) + Convert.ToInt16(smallPixels[0, 0].B))) >= -2 &&
                    (Convert.ToInt16(fullPixels[fullColumn, fullRow].R) + Convert.ToInt16(fullPixels[fullColumn, fullRow].G) + Convert.ToInt16(fullPixels[fullColumn, fullRow].B) - (Convert.ToInt16(smallPixels[0, 0].R) + Convert.ToInt16(smallPixels[0, 0].G) + Convert.ToInt16(smallPixels[0, 0].B))) <= 2
                    )
                {
                    // So far, we haven't found a mismatched pixel
                    bool badPixel = false;
                    try
                    {
                        // Iterate through every row in the small image
                        for (int smallRow = 0; smallRow < smallImage.Width; smallRow++)
                        {
                            // Iterate through every pixel in the row
                            for (int smallColumn = 0; smallColumn < smallImage.Height; smallColumn++)
                            {
                                // If the pixel on the full image isn't the same as on the small image...
                                R = (fullPixels[fullColumn + smallColumn, fullRow + smallRow].R - smallPixels[smallColumn, smallRow].R);
                                G = (fullPixels[fullColumn + smallColumn, fullRow + smallRow].G - smallPixels[smallColumn, smallRow].G);
                                B = (fullPixels[fullColumn + smallColumn, fullRow + smallRow].B - smallPixels[smallColumn, smallRow].B);
                                if (R < 0)
                                    R = R * -1;
                                if (G < 0)
                                    G = G * -1;
                                if (B < 0)
                                    B = B * -1;
                                //if (fullPixels[fullColumn + smallColumn, fullRow + smallRow] != smallPixels[smallColumn, smallRow])
                                if (
                                    (R >= 2) &&
                                    (G >= 2) &&
                                    (B >= 2)
                                   )
                                {
                                    // There is a mismatched pixel so set bad pixel to true
                                    badPixel = true;
                                    break;
                                }
                                else
                                    badPixel = false;
                                R = 0;
                                G = 0;
                                B = 0;
                            }
                            // If we found a bad pixel, break out of outer loop
                            if (badPixel == true)
                            {
                                break;
                            }
                        }
                    }
                    catch { }
                    // If we passed through all pixels in small image, return location
                    if (badPixel == false)
                    {
                        int[,] res = new int[,] { { fullColumn, fullRow } };
                        return res;
                    }
                }
            }
        }
    }
    catch { }
    // If we looped until here, not match was found
    return null;
}

推荐答案

下面的两行将引发IndexOutOfException.
The two line below will raise a IndexOutOfException.
//1200 error
 if (fullColumn >= fullImage.Width)
     break;





if (fullRow >= fullImage.Height)
                    break;



应该是这样;



It should be like this;

if (fullColumn >= fullImage.Width-smallImage.Width)
                break;



您的算法非常慢.每个图像都有一个关键点(也就是拐角点),即最高的变色比点.因此,您可以使用相同的算法提取角点星座图,而不是匹配星座图.



your algorithm is very very slow. Every Image has some key point(also corner point), that is, the highest color change ratio point. So you can use same algorithm extract the corner-point-constellation than match the constellation.


您可以使用此方法调整图像大小
You can use this method for re size image
public Bitmap ReSizeBitmap(Bitmap btp, int width, int height)
        {
            int originalW = btp.Width;
            int originalH = btp.Height;

            Bitmap bmp = new Bitmap(width, height);
            //create a new graphic from the Bitmap
            Graphics graphic = Graphics.FromImage((Image)bmp);
            graphic.InterpolationMode = InterpolationMode.HighQualityBicubic;
            //draw the newly resized image
            graphic.DrawImage(btp as Image, 0, 0, width, height);
            graphic.Dispose();
            //return the image
            return bmp;
        }


这篇关于从较大的图像中查找较小的图像(请提供帮助)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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