asmrobot
2019-11-13 576b92fd82f568572bc4beb125fa0ba0191a602f
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
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;
                }
            }
        }
    }
}