using System;
|
using System.Collections.Generic;
|
using System.Linq;
|
using System.Text;
|
using System.Threading.Tasks;
|
using ZTImage;
|
using ZTImage.Collections;
|
|
namespace RichCreator.Utility.Structs
|
{
|
/// <summary>
|
/// 颜色数组
|
/// </summary>
|
public class ColorArray
|
{
|
public static ColorArray Empty = default(ColorArray);
|
|
public ColorArray(Int32 processMode,ZTPoint basePosition,ZTSize size,List<ColorArrayItem> colors)
|
{
|
this.ProcessMode = processMode;
|
this.BasePosition = basePosition;
|
this.Size = size;
|
this.Colors = colors;
|
}
|
|
/// <summary>
|
/// 原始位置
|
/// </summary>
|
public ZTPoint BasePosition;
|
|
/// <summary>
|
/// 处理模式
|
/// 0:原图
|
/// 1:灰度
|
/// 2:二值化
|
/// 3:hsv float
|
/// </summary>
|
public Int32 ProcessMode;
|
|
/// <summary>
|
/// 二值化的阀值
|
/// </summary>
|
public Int32 ThresholdValue;
|
|
/// <summary>
|
/// 大小
|
/// </summary>
|
public ZTSize Size;
|
|
/// <summary>
|
/// 颜色列表
|
/// </summary>
|
public List<ColorArrayItem> Colors;
|
|
/// <summary>
|
/// 在图像数据中比对
|
/// </summary>
|
/// <param name="datas"></param>
|
/// <param name="x"></param>
|
/// <param name="y"></param>
|
/// <returns></returns>
|
public bool Compare(byte[,,] datas, Int32 x, Int32 y)
|
{
|
Int32 ax = 0, ay = 0;
|
for (int i = 0; i < this.Colors.Count; i++)
|
{
|
ax = x + this.Colors[i].Offset.X;
|
ay = y + this.Colors[i].Offset.Y;
|
if (!this.Colors[i].Compare(datas[ay, ax, 0], datas[ay, ax, 1], datas[ay, ax, 2]))
|
{
|
return false;
|
}
|
}
|
|
return true;
|
}
|
|
/// <summary>
|
/// 从字符串生成二值化识别方式
|
/// </summary>
|
/// <param name="colorsString">形如:1,2,23,231,33$2,3,1,2,3$....每个组用$分隔,逗号分割的为:x,y,r,g,b</param>
|
/// <returns></returns>
|
public static ColorArray FromThresholdString(Int32 thresholdValue,string colorsString)
|
{
|
return FromColorString(2, thresholdValue, colorsString);
|
}
|
|
|
/// <summary>
|
/// 从字符串生成二值化识别方式
|
/// </summary>
|
/// <param name="colorsString">形如:1,2,23,231,33$2,3,1,2,3$....每个组用$分隔,逗号分割的为:x,y,r,g,b</param>
|
/// <returns></returns>
|
public static ColorArray FromColorString(Int32 processMode,Int32 thresholdValue, string colorsString)
|
{
|
MultiList<Int32, Int32, byte, byte, byte> arrayItem = SplitColorsString(colorsString);
|
Int32 x, y, right, bottom;
|
RelocationColorArrayPosition(arrayItem, out x, out y, out right, out bottom);
|
|
List<ColorArrayItem> items = new List<ColorArrayItem>();
|
for (int i = 0; i < arrayItem.Count; i++)
|
{
|
items.Add(new ZTColorArrayItem(new ZTPoint(arrayItem[i].Item1, arrayItem[i].Item2), new ZTColor(arrayItem[i].Item3, arrayItem[i].Item4, arrayItem[i].Item5)));
|
}
|
|
ColorArray array = new ColorArray(processMode, new ZTPoint(x, y), new ZTSize(right + 1, bottom + 1), items);
|
array.ThresholdValue = thresholdValue;
|
return array;
|
}
|
|
|
/// <summary>
|
/// 从字符串生成hsv方式的识别颜色数组
|
/// </summary>
|
/// <param name="hOffset"></param>
|
/// <param name="colorString"></param>
|
/// <returns></returns>
|
public static ColorArray FromHsvFloatString(float hOffset,float sOffset,float vOffset,string colorString)
|
{
|
hOffset = Math.Abs(hOffset);
|
sOffset = Math.Abs(sOffset);
|
vOffset = Math.Abs(vOffset);
|
|
MultiList<Int32, Int32, byte, byte, byte> arrayItem = SplitColorsString(colorString);
|
Int32 basePositionX, basePositionY, right, bottom;
|
RelocationColorArrayPosition(arrayItem, out basePositionX, out basePositionY, out right, out bottom);
|
|
List<ColorArrayItem> items = new List<ColorArrayItem>();
|
for (int i = 0; i < arrayItem.Count; i++)
|
{
|
items.Add(new ZTHsvFloatColorArrayItem(hOffset,sOffset,vOffset,new ZTPoint(arrayItem[i].Item1, arrayItem[i].Item2), new ZTColor(arrayItem[i].Item3, arrayItem[i].Item4, arrayItem[i].Item5)));
|
}
|
|
ColorArray array = new ColorArray(3, new ZTPoint(basePositionX, basePositionY), new ZTSize(right + 1, bottom + 1), items);
|
|
return array;
|
}
|
|
/// <summary>
|
/// 分隔原始颜色字符串
|
/// </summary>
|
/// <param name="colorArrayString">形如:1,2,23,231,33$2,3,1,2,3$....每个组用$分隔,逗号分割的为:x,y,r,g,b</param>
|
/// <returns></returns>
|
private static MultiList<Int32,Int32,byte,byte,byte> SplitColorsString(string colorArrayString)
|
{
|
MultiList<Int32, Int32, byte, byte, byte> list = new MultiList<int, int, byte, byte, byte>();
|
if (string.IsNullOrEmpty(colorArrayString))
|
{
|
return list;
|
}
|
|
string[] colors = colorArrayString.Split(new char[] { '$' },StringSplitOptions.RemoveEmptyEntries);
|
Int32 x,y;
|
Int32 r, g, b;
|
|
for (int i = 0; i < colors.Length; i++)
|
{
|
string[] items = colors[i].Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
|
x=TypeConverter.StringToInt(items[0], -1);
|
if (x < 0)
|
{
|
continue;
|
}
|
y = TypeConverter.StringToInt(items[1], -1);
|
if (y < 0)
|
{
|
continue;
|
}
|
|
r = TypeConverter.StringToInt(items[2], -1);
|
if (r < 0&&r>255)
|
{
|
continue;
|
}
|
|
g = TypeConverter.StringToInt(items[3], -1);
|
if (g < 0&&g>255)
|
{
|
continue;
|
}
|
|
|
b = TypeConverter.StringToInt(items[4], -1);
|
if (b < 0&&b>255)
|
{
|
continue;
|
}
|
list.Add(x,y,(byte)r,(byte)g,(byte)b);
|
}
|
|
return list;
|
|
}
|
|
/// <summary>
|
/// 格式化array的坐标
|
/// </summary>
|
/// <param name="colorArray"></param>
|
private static void RelocationColorArrayPosition(MultiList<Int32, Int32, byte, byte, byte> colorArray,out Int32 x,out Int32 y,out Int32 right,out Int32 bottom)
|
{
|
x=Int32.MaxValue;
|
y=Int32.MaxValue;
|
right = 0;
|
bottom = 0;
|
for (int i = 0; i < colorArray.Count; i++)
|
{
|
if (colorArray[i].Item1 < x)
|
{
|
x = colorArray[i].Item1;
|
}
|
|
if (colorArray[i].Item2 < y)
|
{
|
y=colorArray[i].Item2;
|
}
|
}
|
|
for (int i = 0; i < colorArray.Count; i++)
|
{
|
MultiObject<Int32,Int32,byte,byte,byte> item = colorArray[i];
|
colorArray[i] = new MultiObject<Int32, Int32, byte, byte, byte>(item.Item1 - x, item.Item2 - y, item.Item3, item.Item4, item.Item5);
|
if (colorArray[i].Item1 > right)
|
{
|
right = colorArray[i].Item1;
|
}
|
|
if (colorArray[i].Item2 > bottom)
|
{
|
bottom = colorArray[i].Item2;
|
}
|
}
|
}
|
}
|
}
|