久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

獲取圖像中每種顏色的使用百分比

Get the percentage usage of every colour in an image(獲取圖像中每種顏色的使用百分比)
本文介紹了獲取圖像中每種顏色的使用百分比的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧!

問題描述

我有這個工作,但它在 jpeg 圖像上太慢了,還需要一些改變.

I have this one working but it is so damn slow on jpeg images and also needs some changing.

我需要知道圖像中的各個顏色(RGB 的容差為 +/- 1)以及該顏色在圖像中的百分比.

I need to know the individual colours in an image (with a tolerance of +/- 1 for RGB) and the % of the image that is that colour.

所以如果圖像是黑白的,它會說類似白色:74%黑色:26%

so if an image was black and white it would say something like White : 74% Black : 26%

下面的代碼像我說的那樣工作,但我還需要添加一個容差系統(tǒng),我不知道該怎么做.

The code below works like I said but I need to add a tolerance system as well and I have no idea on how I would do that.

private Dictionary<string, string> getPixelData(Bitmap image)
{
    Dictionary<string, string> pixelData = new Dictionary<string, string>();
    //int col, row;
    //int r, g, b;
    Color pixel;

    double offset = 0.000001;
    int hmm = (image.Height * image.Width);
    double current = 0;
    offset = 100 / double.Parse(hmm.ToString());// 0.01;// 100 / (image.Height * image.Width) * 10000;

    try
    {
        for (int i = 0; i < image.Height; i++)
        {
            for (int j = 0; j < image.Width; j++)
            {
                current = current + offset;
                pixel = image.GetPixel(i, j);                        
                pixelData.Add(i + "," + j, (pixel.R.ToString() + " " + pixel.G.ToString() + " " + pixel.B.ToString()));
                pBarprocess.Value = int.Parse(Math.Floor(current).ToString());
                pBarprocess.Update();
                Application.DoEvents();
            }
        }
    }
    catch (Exception ex)
    {
        MessageBox.Show("Unable to parse image " + ex);
    }

    return pixelData;
}

其他功能

private void btnProcess_Click(object sender, EventArgs e)
{
    pBarprocess.Value = 0;
    pBarprocess.Enabled = false;
    Bitmap foo = Bitmap.FromFile(@txtFileName.Text) as Bitmap;
    Dictionary<string, string> pixelData = new Dictionary<string, string>();

    lblProcess.Text = "Processing pixel map";
    pixelData = getPixelData(foo);

    lblProcess.Text = "Calculating Density";
    lblProcess.Update();

    var distinctList = pixelData.Values.Distinct().ToList();

    Console.WriteLine("DL = " + distinctList.Count);
    double offset = 100 / double.Parse(distinctList.Count.ToString());
    double current = 0;

    foreach (var value in distinctList)
    {
        IEnumerable<string> query = pixelData.Values.Where(fruit => fruit == value);
        double perc = (double.Parse(query.Count().ToString()) / double.Parse(pixelData.Count.ToString())) * 100;
        Console.WriteLine(value + " = " + query.Count() + "(" + perc + "%)");
        txtAnalysis.Text = "Colour " + value + " : " + query.Count() + " (" + perc.ToString() + "%)
" + txtAnalysis.Text;
        txtAnalysis.Update();
        pBarprocess.Value = int.Parse(Math.Floor(current).ToString());
        pBarprocess.Update();
        Application.DoEvents();
    }

    lblProcess.Text = "Finished.";
    pBarprocess.Value = 0;
    pBarprocess.Enabled = false;
}

推薦答案

GetPixel 并不是訪問圖像數(shù)據(jù)的真正快捷方式.使用 LockBits 方法.

GetPixel is not really a fast way to access image data. Use the LockBits method.

好吧,您正在用字符串做很多事情.以這種方式構(gòu)建 pixelData Dictionary 非常無用,為什么不立即處理不同的顏色?顏色是一個不可變的結(jié)構(gòu)體,所以它已經(jīng)是我們字典的一個很好的鍵了.

Well you're doing a lot of things with strings. Building the pixelData Dictionary that way is pretty useless, why don't you process the distinct colors right away? Color is an immutable struct, so that's a good key for our dictionary already.

Dictionary<Color, int> frequency = new Dictionary<Color, int>();
for (int i = 0; i < image.Height; i++) {
  for (int j = 0; j < image.Width; j++) {
    pixel = image.GetPixel(i, j);
    if (frequency.ContainsKey(pixel)) frequency[pixel]++;
    else frequency.Add(pixel, 1);
  }
}

// and finally
int totalPixels = image.Width * image.Height;
foreach (var kvp in frequency) {
  Console.WriteLine("Color (R={0},G={1},B={2}): {3}", kvp.Key.R, kvp.Key.G, kvp.Key.B, kvp.Value / (double)totalPixels);
}

這應(yīng)該可以,除非您想使其更快并使用 LockBits 而不是 GetPixel.

And that should do it, except when you want to make it even faster and use LockBits instead of GetPixel.

其他一些觀察:

int hmm = (image.Height * image.Width);
double offset = 100 / double.Parse(hmm.ToString());

您使用了一種非常奇怪且緩慢的從 int 轉(zhuǎn)換為 double 的方法.你可以只寫 double offset = 100/(double)hmm; 并且它是一樣的(你也可以寫 100.0 而不是 100,編譯器會為你創(chuàng)建一個 double 所以你不需要投嗯).

You're using a very strange and slow way of casting from int to double. You can just write double offset = 100 / (double)hmm; and it's the same (you could also write 100.0 and not 100 and the compiler will create a double for you so you don't need to cast hmm).

這讓我笑了:

IEnumerable<string> query = pixelData.Values.Where(fruit => fruit == value);

為什么是水果!?好像你從某個地方復(fù)制了這個.

Why fruit!? Seems like you copied this from somewhere.

這篇關(guān)于獲取圖像中每種顏色的使用百分比的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

【網(wǎng)站聲明】本站部分內(nèi)容來源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請聯(lián)系我們刪除處理,感謝您的支持!

相關(guān)文檔推薦

Right-click on a Listbox in a Silverlight 4 app(右鍵單擊 Silverlight 4 應(yīng)用程序中的列表框)
WPF c# webbrowser scrolls over top menu(WPF c# webbrowser 在頂部菜單上滾動)
C# Console app - How do I make an interactive menu?(C# 控制臺應(yīng)用程序 - 如何制作交互式菜單?)
How to avoid duplicate form creation in .NET Windows Forms?(如何避免在 .NET Windows Forms 中創(chuàng)建重復(fù)的表單?)
UI Automation Control Desktop Application and Click on Menu Strip(UI自動化控制桌面應(yīng)用程序并單擊菜單條)
Removing thin border around the menuitems(刪除菜單項周圍的細(xì)邊框)
主站蜘蛛池模板: 日韩一区二区av | 国产日韩av在线 | 日韩欧美三级 | 91亚洲国产成人精品性色 | 在线观看亚洲一区 | 手机成人在线视频 | 我要看一级黄色片 | 日韩精品一区二 | 免费观看全黄做爰视频 | 欧美一级在线观看 | 亚洲永久精品视频 | 国产精品成人一区 | 一区二区三区免费在线观看 | 另类ts人妖一区二区三区 | 日韩av在线看 | 成年人视频在线播放 | 中文字幕免费高清 | 日韩福利视频 | 国产激情一区二区三区 | 人人爱人人插 | 日韩精品免费 | 国产一区二区网站 | 久久久久久久成人 | 免费看一级黄色片 | 亚洲综合激情网 | 性欧美69| 91福利网 | 亚洲一区在线播放 | 97caoporn| 免费av播放 | 亚洲色欲色欲www在线观看 | 日韩免费在线观看视频 | 秘密爱大尺度做爰呻吟 | 综合色在线 | 精品福利在线 | 欧美日一区二区三区 | 午夜精品久久久久久久 | 亚洲一区成人 | 黄色1级片 | 久久久久久网 | 中文字幕在线观看网站 |