查找图像的矩形区域作物 [英] Find the rectangle area of image to Crop

查看:211
本文介绍了查找图像的矩形区域作物的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经与图像的(352×240)的结果
尺寸

I've image with the size of (352 x 240)

和我要裁剪的白色区域(选定区域,如下所示),结果

And I want to crop the white area (the selected area as show below).

我用这个代码来裁剪图片,

I use this code to crop image ,

private static Image cropImage(Image img, Rectangle cropArea)
{
   Bitmap bmpImage = new Bitmap(img);
   Bitmap bmpCrop = bmpImage.Clone(cropArea,
   bmpImage.PixelFormat);
   return (Image)(bmpCrop);
}



但我不能找到我需要确切的矩形区域!

我如何才能找到长方形选定区域的价值?结果
我只是想裁剪这个区域!谢谢!

But I can't find the exact rectangle area for my need !
How can I find Rectangle value of selected area ?
I just want to crop this area !! Thanks !

推荐答案

如果你想自己就肯定能做到。但如何做到这一点的细节最好将很大程度上取决于你对源图像究竟有多大。

If you want to do it yourself you certainly can. But the details of how to do it best will depend a lot on exactly how much you know about the source image.

下面是需要考虑的几个问题,并决定:

Here are a few questions and decisions to consider:


  • 你知道一些有关的图片

  • 你知道一些关于图像的位置,的尺寸将它始终处于底部的吧?

  • 请你知道一些关于偷工减料的大小?

  • 你知道的颜色?

  • 您宁愿减少在里面失去一些像素或?包括在外面的一些像素或一条中间道路

  • do you know something about the dimension of the image
  • do you know something about the position of the image, will it always be in the bottom right?
  • do you know something about the sizes of the cut corners?
  • do you know the colors?
  • would you rather cut losing some pixels on the inside or including some pixels on the outside or a middle way?

看着你的榜样,你可以例如尝试这种策略:

Looking at your example you could for example try this strategy:


  • 首先,水平穿越图像在某些距离,说在10或20片。

  • 在每一个横向行你观察和收集所有地方像素改变颜色

  • 则比较所产生的名单,看看是否有一些中间(!)的同意

  • 这可以让你的左右边界

  • 它也可以让你一个领域,你将可以做垂直扫描

  • 则重复垂直扫描,以获得顶部和底部坐标。

  • 垂直扫描不必要搜索的矩形,它们可以从的结果的中间工作水平扫描

  • First you traverse the image horizontally at some distances, say in slices of 10 or 20.
  • On each transversal of a line you observe and collect all the points where the pixels change color
  • then you compare the resulting lists to see if there are some in the middle (!) that agree
  • this gets you the left and right boundaries
  • it also gets you an area where you will can do the vertical scans
  • then you repeat for vertical scans to get the top and bottom coordinates.
  • the vertical scans don't have to search for the rectangle, they can work from the middle of the results of the horizontal scans

对于这个工作,你必须知道一点关于你所期望的颜色变化:会不会有平滑参与?如果是什么级别的变化你需要比较的 GetPixel 颜色结果..?

For this to work you must know a little about the color changes you expect: Will there be smoothing involved? If so what level of change do you need to compare the result of the GetPixel colors..?

另外,尺寸应既可以大致了解,否则你可能需要重复整个过程用细网格。

Also the dimensions should either be known approximately or else you may need to repeat the whole process with a finer mesh..

修改是从信息您的评论可以做出一些假设(*),并使用这段代码,以确定目标矩形。假设用于:

Using the info from your comment one can make a few assumptions (*) and use this piece of code to determine the target rectangle. The assumptions are used to :


  • 确定的扫描可以假定击中目标的主体2的扫描线的范围

  • 确定两个像素的内部和外部的颜色

我使用colordistance函数和积分的4列出并平均值几命中。变量模糊可与您的图像值2-6。

I use a colordistance function and 4 lists of Points and average over a few hits. The variable blur works with values 2-6 for your image.

List<Point> xPoints1 = new List<Point>();
List<Point> xPoints2 = new List<Point>();
List<Point> yPoints1 = new List<Point>();
List<Point> yPoints2 = new List<Point>();

Rectangle findRectangle()
{
    int xMax = pictureBox1.Image.Width; 
    int yMax = pictureBox1.Image.Height;

    // * we can asume that these lines hit the body
    int x0 = xMax * 3 / 4;
    int y0 = yMax * 3 / 4;

    using (Bitmap bmp = new Bitmap(pictureBox1.Image) )
    {
        // we can assume that these are the out- and inside colors
        Color col0 = bmp.GetPixel(9, 9);
        Color col1 = bmp.GetPixel(x0, y0);

        int blur = 4;
        int diff = colordistance(col0, col1) / blur;

        bool outside = true;
        // a few horizontal scans..
        for (int y = y0 - 20; y < y0 + 20; y += 4)
        {
          outside = true;
          for (int x = 0; x < xMax; x++)
          {
            Color c = bmp.GetPixel(x, y);

            if ( outside && colordistance(c, col0) > diff)
               { outside = !outside; xPoints1.Add(new Point(x, y)); }
            else if (!outside && colordistance(c, col1) > diff) 
               { outside = !outside; xPoints2.Add(new Point(x, y)); }
            }
        }              

        // a few vertical scans..
        for (int x = x0 - 20; x < x0 + 20; x += 4)
        {
           outside = true;
           for (int y = 0; y < yMax; y++)
           {
              Color c = bmp.GetPixel(x, y);
              if (outside && colordistance(c, col0) > diff) 
                  { outside = !outside; yPoints1.Add(new Point(x, y)); }
              else if (!outside && colordistance(c, col1) > diff) 
                  { outside = !outside; yPoints2.Add(new Point(x, y)); }
           }
        }

        int left   = (int)xPoints1.Average(p => p.X);
        int right  = (int)xPoints2.Average(p => p.X);
        int top    = (int)yPoints1.Average(p => p.Y);
        int bottom = (int)yPoints2.Average(p => p.Y);
        // if the target sits at the bottom we didn't find the edge
        if (bottom == 0) bottom = yMax;

        return = new Rectangle(left, top, right - left, bottom - top);

    }
}

int colordistance(Color c1, Color c2)
{
    return (int) Math.Sqrt((c1.R - c2.R) * (c1.R - c2.R) +
        (c1.G - c2.G) * (c1.G - c2.G) +
        (c1.B - c2.B) * (c1.B - c2.B));

}

注意:顶部和左边值击中目标的内部。底部(和可能的底部)击中外部。所以,你应该减少无论是前者还是后者由1个像素,根据您的需要..!

Note: The top and the left values hit the inside of the Target. The bottom (and possibly the bottom) hit the outside. So you should decrease either the former or the latter by 1 pixel, depending on your needs..!

修改第1版有之外变量设置在错误的地方。纠正。

Edit The 1st version had the outside variable set at the wrong spot. Corrected.

这篇关于查找图像的矩形区域作物的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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