-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGetBackGround.cs
More file actions
228 lines (206 loc) · 8.54 KB
/
Copy pathGetBackGround.cs
File metadata and controls
228 lines (206 loc) · 8.54 KB
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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Drawing.Imaging;
namespace AutoPickingSys
{
class GetBackGround
{
public void BGPoint(Bitmap b, int X, int Y, float R, out Point positionPoint,out Color BackGroundColor)
{
Point _positionPoint=new Point (0,0);
Color _backGroundColor;
//Bitmap b = (Bitmap)bitmap.Clone();
int width = b.Width;
int height = b.Height;
int rr, gg, bb;
int Rbg, Gbg, Bbg;
Rbg = Gbg = Bbg = 0;
int[] Rc = new int[256];
int[] Gc = new int[256];
int[] Bc = new int[256];
for (int i = 0; i < 255; i++)
{
Rc[i] = 0;
Gc[i] = 0;
Bc[i] = 0;
}
if (b != null)
{
bool getBG = false;
Bitmap dstImage = new Bitmap(width, height);// (Bitmap)b.Clone();//
dstImage = (Bitmap)b.Clone();
BitmapData srcData = b.LockBits(new Rectangle(0, 0, width, height),
ImageLockMode.ReadOnly, PixelFormat.Format32bppArgb);
unsafe
{
byte* src = (byte*)srcData.Scan0;
byte* nsrc;
int stride = srcData.Stride;
int offset = stride - width * 4;
nsrc = src;//保存左上角当前指针
for (int i = 0; i < width; i++)//
{
//纵向定位当前点指针
for (int j = 0; j < height; j++)//从上往下搜索
{
//dnum = 0;
src = nsrc + i * 4 + j * stride;
if ((i - X) * (i - X) + (j - Y) * (j - Y) < R * R)//如果x^2+y^2<r^2
{
bb = src[0];
gg = src[1];
rr = src[2];
gg = gg / 3;
rr = rr / 3;
bb = bb / 3;
Rc[rr]++;
Gc[gg]++;
Bc[bb]++;
}
}
}
for (int i = 0; i < 255; i++)
{
if (Rc[i] > Rc[Rbg])
Rbg = i;
if (Gc[i] > Gc[Gbg])
Gbg = i;
if (Bc[i] > Bc[Bbg])
Bbg = i;
}
Rbg = Rbg * 3;
Gbg = Gbg * 3;
Bbg = Bbg * 3;
_backGroundColor = Color.FromArgb(Rbg, Gbg, Bbg);
for (int i = 0; i < width; i++)//
{
//纵向定位当前点指针
for (int j = 0; j < height; j++)//从上往下搜索
{
//dnum = 0;
src = nsrc + i * 4 + j * stride;
if ((i - X) * (i - X) + (j - Y) * (j - Y) < R * R)//如果x^2+y^2<r^2
{
src = nsrc + i * 4 + j * stride;
bb = src[0];
gg = src[1];
rr = src[2];
if ((Math.Abs(rr - Rbg) < 3) & (Math.Abs(gg - Gbg) < 3) & (Math.Abs(bb - Bbg) < 3))
{
_positionPoint = new Point(i, j);
getBG = true;
break;
}
}
}
if (getBG)
break;
}
}
}
positionPoint = _positionPoint;
_backGroundColor = Color.FromArgb(Rbg, Gbg, Bbg);
BackGroundColor = _backGroundColor;
}
public void BGPointRectangle(Bitmap bitmap, int X, int Y, int W, int H, out Point positionPoint, out Color BackGroundColor)
{
Point _positionPoint = new Point(0, 0);
Color _backGroundColor;
Bitmap b = (Bitmap)bitmap.Clone();
int width = b.Width;
int height = b.Height;
int rr, gg, bb;
int Rbg, Gbg, Bbg;
Rbg = Gbg = Bbg = 0;//初始化;
int[] Rc = new int[256];//定义Rc的容量为256;
int[] Gc = new int[256];
int[] Bc = new int[256];
for (int i = 0; i < 256; i++)//全部置0;
{
Rc[i] = 0;
Gc[i] = 0;
Bc[i] = 0;
}
if (b != null)
{
bool getBG = false;//设定getBG初始布尔值为false;
Bitmap dstImage = new Bitmap(width, height);// (Bitmap)b.Clone();//
dstImage = (Bitmap)b.Clone();
BitmapData srcData = b.LockBits(new Rectangle(0, 0, width, height),
ImageLockMode.ReadOnly, PixelFormat.Format32bppArgb);
unsafe
{
byte* src = (byte*)srcData.Scan0;//源图像左上角当前指针;
byte* nsrc;
int stride = srcData.Stride;//扫描宽度;
int offset = stride - width * 4;
nsrc = src;//保存左上角当前指针
for (int i = 0; i < width; i++)//
{
//纵向定位当前点指针
for (int j = 0; j < height; j++)//从上往下搜索
{
//dnum = 0;
src = nsrc + i * 4 + j * stride;
if ((i - X > 0) & (i - X < W) & (j - Y > 0) & (j - Y < H))
{
bb = src[0];
gg = src[1];
rr = src[2];
gg = gg / 3;
rr = rr / 3;
bb = bb / 3;
Rc[rr]++;
Gc[gg]++;
Bc[bb]++;
}
}
}
for (int i = 0; i < 256; i++)
{
if (Rc[i] > Rc[Rbg])//一一比较,得到最大值;
Rbg = i;
if (Gc[i] > Gc[Gbg])
Gbg = i;
if (Bc[i] > Bc[Bbg])
Bbg = i;
}
Rbg = Rbg * 3;
Gbg = Gbg * 3;
Bbg = Bbg * 3;
_backGroundColor = Color.FromArgb(Rbg, Gbg, Bbg);//得到背景色的RGB值;
for (int i = 0; i < width; i++)//
{
//纵向定位当前点指针
for (int j = 0; j < height; j++)//从上往下搜索
{
//dnum = 0;
src = nsrc + i * 4 + j * stride;
if ((i - X > 0) & (i - X < W) & (j - Y > 0) & (j - Y < H))
{
src = nsrc + i * 4 + j * stride;
bb = src[0];
gg = src[1];
rr = src[2];
if ((Math.Abs(rr - Rbg) < 3) & (Math.Abs(gg - Gbg) < 3) & (Math.Abs(bb - Bbg) < 3))//相差3个灰度级;
{
_positionPoint = new Point(i, j);
getBG = true;
break;
}
}
}
if (getBG)
break;
}
}
}
positionPoint = _positionPoint;
_backGroundColor = Color.FromArgb(Rbg, Gbg, Bbg);
BackGroundColor = _backGroundColor;
}
}
}