前些日子看千图网的传图识色有点好奇就查找了一些资料,在此分析下:
首先,创建一个值类型:
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
新闻热点
疑难解答