原文 : https://kishordgupta.wordpress.com/2010/12/24/detect-object-from-image-based-on-object-color-by-c/html
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算法
爲了檢測一個基於顏色的對象,有一個簡單的算法。你必須選擇一個過濾方法。通常步驟c#
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 areapp
首先要選擇一個過濾方法,有許多爲c#提供的過濾方法。我更喜歡aforge過濾器,爲了這個目的他們沒有過濾器ide
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 .wordpress
我最喜歡的是euclideancolor過濾,它很簡單。對於其餘的過濾,你能夠在這裏瞭解更多。你必須下載Aforge的dll來在你的代碼中應用這些post
So see EuclideanColorFiltering work first seethis
因此先看看euclideancolor過濾工做idea
we are going to apply a color filter it is very simple code to use euclideanfilteringspa
咱們將應用一個顏色過濾器它是很是簡單的代碼使用歐幾里得過濾
// 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/