国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

.NET傳圖識色

2019-11-14 14:35:17
字體:
來源:轉載
供稿:網友
前些日子看千圖網的傳圖識色有點好奇就查找了一些資料,在此分析下:

首先,創建一個值類型:
 1 public struct MajorColor : IComparable<MajorColor> 2 { 3     internal int Color;//顏色值 4     internal int Amount;//顏色總數 5     public MajorColor(int Color, int Amount) 6     { 7         this.Color = Color; 8         this.Amount = Amount; 9     }10     public int CompareTo(MajorColor obj)11     {12         return this.Amount.CompareTo(obj.Amount);13     }14 }
創建一個值類型

接下來是主要算法:

 1 /// <summary> 2 /// 識別主色調 3 /// </summary> 4 /// <param name="Bmp">Bmp位圖</param> 5 /// <param name="PCAAmount">主色調數目</param> 6 /// <param name="Delta">閾值</param> 7 /// <returns></returns> 8 public unsafe static List<MajorColor> PRincipalColorAnalysis(Bitmap Bmp, int PCAAmount, int Delta = 24) 9 {10     List<MajorColor> MC = new List<MajorColor>();11 12     int X, Y, Width, Height, Stride, Index, TotalColorAmount = 0;13     int HalfDelta;14     byte* Pointer, Scan0;//定義指針所以需要unsafe關鍵字15     BitmapData BmpData = Bmp.LockBits(new Rectangle(0, 0, Bmp.Width, Bmp.Height), ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb);//將位圖鎖定到系統內存中16     Height = Bmp.Height;17     Width = Bmp.Width;18     Stride = BmpData.Stride;//Bitmap對象的跨距寬度(也稱掃描寬度)19     Scan0 = (byte*)BmpData.Scan0;//位圖中第一個像素數據的地址20 21     int[] Table = new int[256 * 256 * 256];22     int[] NonZero = new int[Width * Height];23     int[] Map = new int[256];24 25     if (Delta > 2)26     {27         HalfDelta = Delta / 2 - 1;28     }29     else30     {31         HalfDelta = 0;32     }33     for (Y = 0; Y < 256; Y++)34     {35         Map[Y] = ((Y + HalfDelta) / Delta) * Delta;36         if (Map[Y] > 255) Map[Y] = 255;37     }38     for (Y = 0; Y < Height; Y++)39     {40         Pointer = Scan0 + Stride * Y;41         for (X = 0; X < Width; X++)42         {43             Index = (Map[*Pointer] << 16) + (Map[*(Pointer + 1)] << 8) + Map[*(Pointer + 2)];44             if (Table[Index] == 0)//顏色未出現45             {46                 NonZero[TotalColorAmount] = Index;//記錄顏色47                 TotalColorAmount++;//總數加148             }49             Table[Index]++;//對應的顏色數目加150             Pointer += 3;//遍歷下個像素51         }52     }53     MajorColor[] Result = new MajorColor[TotalColorAmount];54     for (Y = 0; Y < TotalColorAmount; Y++)55     {56         Result[Y].Amount = Table[NonZero[Y]];57         Result[Y].Color = NonZero[Y];58     }59     Array.Sort(Result);//排序60     Array.Reverse(Result);//反轉61 62     for (Y = 0; Y < (Result.Length > PCAAmount ? PCAAmount : Result.Length); Y++)63     {64         MC.Add(new MajorColor(Result[Y].Color, Result[Y].Amount));65     }66     Bmp.UnlockBits(BmpData);//從系統內存中解鎖此位圖67     GC.Collect();//釋放內存68     return MC;69 }
算法

unsafe關鍵字編譯報錯解決方案:項目屬性-生成-勾選允許不安全代碼即可。

最后獲取到的List< MajorColor > 就是我們要獲取的主色調了。

接下來遍歷循環獲取所有色調

 1 if (MC != null) 2 { 3     for (int i = 0; i < MC.Count; i++) 4     { 5         IntToColor(MC[i].Color))); 6     } 7 } 8 //顏色值轉換成RGB 9 public static string IntToColor(int color)10 {11     int R = color & 255;12     int G = (color & 65280) / 256;13     int B = (color & 16711680) / 65536;14     return ColorTranslator.ToHtml(System.Drawing.Color.FromArgb(R, G, B));15 }
循環獲取色調

這塊小功能也算初步完成了,至于圖片怎么傳到后臺這個應該不用說明了。

友情提示:B/S可將圖片轉成Base64數字編碼。

源碼來源于 ->作者 : laviewpbt

 


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 隆回县| 杭锦旗| 吉木乃县| 酒泉市| 曲阜市| 延庆县| 临安市| 辉南县| 丽水市| 宕昌县| 峨边| 泸溪县| 长宁区| 成武县| 平南县| 富民县| 仁怀市| 永昌县| 潼南县| 兴文县| 本溪市| 汶川县| 宜城市| 平和县| 葫芦岛市| 胶州市| 长子县| 邛崃市| 海口市| 东兰县| 福泉市| 龙口市| 乌兰浩特市| 安西县| 彭州市| 海南省| 如皋市| 汉中市| 安岳县| 渭源县| 元谋县|