zoukankan      html  css  js  c++  java
  • 基于对象颜色的对象检测(翻译)

    原文 : https://kishordgupta.wordpress.com/2010/12/24/detect-object-from-image-based-on-object-color-by-c/

    To detect a object based on its color , there is an easy algorithm for that .u have to choose a filtering method . Steps normally are

    为了检测一个基于颜色的对象,有一个简单的算法。你必须选择一个过滤方法。一般步骤

    • Take the image (获取对象)
    • Apply ur filtering (过滤)
    • Apply greyscalling
    • subtract background and get ur objects (去掉背景,获取你的对象)
    • find all the objects position (查找对象为准)
    • mark the objects

    First u have to choose a filtering method , there are many filtering method provided for c#. mainly i prefer aforge filters , for this purpose they have few filter they are

    首先要选择一个过滤方法,有许多为c#提供的过滤方法。我更喜欢aforge过滤器,为了这个目的他们没有过滤器

    • ColorFiltering 颜色过滤
    • ChannelFiltering
    • HSLFiltering
    • YCbCrFiltering
    • EuclideanColorFiltering  欧几里得过滤

    my favorite is EuclideanColorFiltering it is easy and simple. for other filtering u can know more about them here. U have to download Aforge dll for apply these in ur code .

    我最喜欢的是euclideancolor过滤,它很简单。对于其他的过滤,你可以在这里了解更多。你必须下载Aforge的dll来在你的代码中应用这些

    So see EuclideanColorFiltering work first see

    所以先看看euclideancolor过滤工作

    we are going to apply a color filter it is very simple code to use euclideanfiltering

    我们将应用一个颜色过滤器它是非常简单的代码使用欧几里得过滤

    // create filter
    EuclideanColorFiltering filter = new EuclideanColorFiltering( );
    // set center colol and radius
    filter.CenterColor = Color.FromArgb( 215, 30, 30 );
    filter.Radius = 100;
    // apply the filter
    filter.ApplyInPlace( image );
    

    now see the effect

    现在看到的效果

    well to understand how it work look closely at the code

    要理解它是如何工作的,仔细看看代码

    filter.CenterColor = Color.FromArgb( 215, 30, 30 );
    filter.Radius = 100;

    first line select the select color value. you all know color has a value 0 to 255.
    by filter.CenterColor = Color.FromArgb( 215, 30, 30 ); i specified my center color will be a red effected color because here value of red is 215, green and blue is 30. and filter.Radius = 100 means that all color value near than 100 in my specified color.
    now my filter filters pixels, which color is inside/outside of RGB sphere with specified center and radius – it keeps pixels with colors inside/outside of the specified sphere and fills the rest with specified color.

    第一行选择颜色值。你们都知道颜色的值是0到255。
    通过filter.CenterColor = Color.FromArgb( 215, 30, 30 );我指定我的中心颜色是红色影响的颜色因为红色的值是215,绿色和蓝色是30。和 filter.Radius = 100 表示在我指定的颜色中,所有颜色值都在100附近。
    现在我的滤镜过滤像素,它的颜色是在RGB的内部/外部的指定的中心和半径-它保持像素的颜色在指定的范围内/外面,并填充其余的指定颜色。

    Now for detect objects i use bitmap data and use lockbits method to understand clearly this method see here . then we make it greyscale algorithom hten unlock it.

    现在,为了检测对象,我使用位图数据,并使用lockbits方法来清楚地理解这里的方法。然后我们把它变成灰度算法hten来解锁它。

    BitmapData objectsData = image.LockBits(new Rectangle(0, 0, image.Width, image.Height),ImageLockMode.ReadOnly, image.PixelFormat);
    // grayscaling
    UnmanagedImage grayImage = grayscaleFilter.Apply(new UnmanagedImage(objectsData));
    // unlock image
    image.UnlockBits(objectsData);

    now the object we use blobcounter for that. it is a very strong class that aforge provided.

    现在我们使用blobcounter的对象。aforge提供的是一门非常强大的课程

     blobCounter.MinWidth = 5;
                blobCounter.MinHeight = 5;
                blobCounter.FilterBlobs = true;
                blobCounter.ProcessImage(grayImage);
                Rectangle[] rects = blobCounter.GetObjectRectangles();
                foreach(Rectangle recs in rects)
                if (rects.Length > 0)
                {
                    foreach (Rectangle objectRect in rects)
                    {
    
                        Graphics g = Graphics.FromImage(image);
    
                        using (Pen pen = new Pen(Color.FromArgb(160, 255, 160), 5))
                        {
                            g.DrawRectangle(pen, objectRect);
                        }
    
                        g.Dispose();
                    }
    
                }

    blobCounter.MinWidth and blobCounter.MinHeight define the smallest size of the object in pixel.
    and blobCounter.GetObjectRectangles() return all the objects rectangle position. and using graphics class i draw rectangle over the images.

    blobCounter.MinWidth 和 blobCounter.MinHeight 定义了像素中对象的最小大小。
    矩形和 blobCounter.GetObjectRectangles() 返回的所有对象的位置。使用图形类,我在图像上绘制矩形。

    now if u want to take only the biggest object there is a method for that

    现在,如果你想只取最大的对象,有一个方法

                blobCounter.MinWidth = 5;
                blobCounter.MinHeight = 5;
                blobCounter.FilterBlobs = true;
                blobCounter.ObjectsOrder = ObjectsOrder.Size;
                blobCounter.ProcessImage(grayImage);
                Rectangle[] rects = blobCounter.GetObjectRectangles();
                foreach(Rectangle recs in rects)
                if (rects.Length > 0)
                {
                       Rectangle objectRect = rects[0];
                        Graphics g = Graphics.FromImage(image);
                        using (Pen pen = new Pen(Color.FromArgb(160, 255, 160), 5))
                        {
                            g.DrawRectangle(pen, objectRect);
                        }
                        g.Dispose();
                }

    now image is like that

    图像是这样的

    but if u want to extract u can use following code

    但是如果您想要提取,可以使用以下代码

    Bitmap bmp = new Bitmap(rects[0].Width, rects[0].Height);
    Graphics g = Graphics.FromImage(bmp);
    g.DrawImage(c, 0, 0, rects[0], GraphicsUnit.Pixel);

    so u will get ur object like that

     你会得到这样的对象

    now if u want to draw the rectangles in main image use that image reference in graphics. u will find u result. Hope this can be helpful

    see update at this post

    现在,如果你想画出主图像中的矩形,在图形中使用图像引用。你会发现你的结果。希望这能有所帮助
    请参见本文的更新

    原文 : https://kishordgupta.wordpress.com/2010/12/24/detect-object-from-image-based-on-object-color-by-c/

  • 相关阅读:
    RayTracing练习
    聚类方法总结
    Mysql、SqlServer和Oracle 添加修改删除字段
    gridview增加thead 和tbody
    数据库设计的三大范式
    c#+sql事务
    gridview应用
    DOS命令大全 IIS命令大全 SQL命令大全……
    TFS
    C#.NET官方类库Json序列化,反序列化
  • 原文地址:https://www.cnblogs.com/hi-gdl/p/8143018.html
Copyright © 2011-2022 走看看