-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathForm1.cs
More file actions
2447 lines (2196 loc) · 95.4 KB
/
Form1.cs
File metadata and controls
2447 lines (2196 loc) · 95.4 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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Net;
using AForge.Imaging.Filters;
using AForge.Imaging;
using AForge.Math.Geometry;
using System.Drawing.Imaging;
using AForge;
using AForge.Math;
using System.Drawing.Drawing2D;
namespace AforgenetTest
{
public partial class Form1 : Form
{
PictureBox m_thisPIC;
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
try
{
string strPath = Application.StartupPath + "\\imgs";
if (!Directory.Exists(strPath))
{
Directory.CreateDirectory(strPath);
}
string strGuid = Guid.NewGuid().ToString();
string strSavePath = Path.Combine(strPath, strGuid + ".jpg");
DownloadFile(textBox1.Text, strSavePath);
Bitmap bit = new Bitmap(strSavePath);
this.pictureBox1.Image = bit;
m_thisPIC = this.pictureBox1;
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
public void DownloadFile(string strURLAddress, string strPath)
{
try
{
// 设置参数
HttpWebRequest request = WebRequest.Create(strURLAddress) as HttpWebRequest;
CookieContainer cooks = new CookieContainer();
request.CookieContainer = cooks;
//发送请求并获取相应回应数据
HttpWebResponse response = request.GetResponse() as HttpWebResponse;
//直到request.GetResponse()程序才开始向目标网页发送Post请求
Stream responseStream = response.GetResponseStream();
//创建本地文件写入流
Stream stream = new FileStream(strPath, FileMode.Create);
byte[] bArr = new byte[1024];
int size = responseStream.Read(bArr, 0, (int)bArr.Length);
while (size > 0)
{
stream.Write(bArr, 0, size);
stream.Flush();
size = responseStream.Read(bArr, 0, (int)bArr.Length);
}
stream.Close();
responseStream.Close();
}
catch (Exception ex)
{
}
}
private void button2_Click(object sender, EventArgs e)
{
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
Bitmap bit = new Bitmap(openFileDialog1.FileName);
this.pictureBox1.Width = bit.Width * 2;
this.pictureBox1.Height = bit.Height * 2;
this.pictureBox1.Image = bit;
m_thisPIC = this.pictureBox1;
}
}
private void button3_Click(object sender, EventArgs e)
{
lblHuiDuValue.Text = trackBar1.Value + "-" + trackBar2.Value + "-" + trackBar3.Value;
double dbl1 = (double)trackBar1.Value / 100;
double dbl2 = (double)trackBar2.Value / 100;
double dbl3 = (double)trackBar3.Value / 100;
Bitmap bit = (m_thisPIC.Image as Bitmap).Clone(new Rectangle(0, 0, m_thisPIC.Image.Width, m_thisPIC.Image.Height), System.Drawing.Imaging.PixelFormat.Format24bppRgb);
Grayscale filter = new Grayscale(dbl1, dbl2, dbl3);
// apply the filter
Bitmap grayImage = filter.Apply(bit);
pictureBox2.Image = grayImage;
m_thisPIC = pictureBox2;
}
private void trackBar1_Scroll(object sender, EventArgs e)
{
lblHuiDuValue.Text = trackBar1.Value + "-" + trackBar2.Value + "-" + trackBar3.Value;
double dbl1 = (double)trackBar1.Value / 100;
double dbl2 = (double)trackBar2.Value / 100;
double dbl3 = (double)trackBar3.Value / 100;
Bitmap bit = (m_thisPIC.Image as Bitmap).Clone(new Rectangle(0, 0, m_thisPIC.Image.Width, m_thisPIC.Image.Height), System.Drawing.Imaging.PixelFormat.Format24bppRgb);
Grayscale filter = new Grayscale(dbl1, dbl2, dbl3);
// apply the filter
Bitmap grayImage = filter.Apply(bit);
pictureBox2.Image = grayImage;
}
private void button4_Click(object sender, EventArgs e)
{
trackBar1.Value = 50;
trackBar2.Value = 50;
trackBar3.Value = 50;
trackBar1_Scroll(null, null);
}
private void trackBar4_Scroll(object sender, EventArgs e)
{
Bitmap bit = (m_thisPIC.Image as Bitmap).Clone(new Rectangle(0, 0, m_thisPIC.Image.Width, m_thisPIC.Image.Height), System.Drawing.Imaging.PixelFormat.Format8bppIndexed);
Threshold filter = new Threshold(trackBar4.Value);
// apply the filter
filter.ApplyInPlace(bit);
pictureBox3.Image = bit;
lblErZhiValue.Text = trackBar4.Value.ToString();
}
private void button5_Click(object sender, EventArgs e)
{
Bitmap bit = (m_thisPIC.Image as Bitmap).Clone(new Rectangle(0, 0, m_thisPIC.Image.Width, m_thisPIC.Image.Height), System.Drawing.Imaging.PixelFormat.Format8bppIndexed);
Threshold filter = new Threshold(trackBar4.Value);
// apply the filter
filter.ApplyInPlace(bit);
pictureBox3.Image = bit;
lblErZhiValue.Text = trackBar4.Value.ToString();
m_thisPIC = pictureBox3;
}
private void button6_Click(object sender, EventArgs e)
{
SimpleSkeletonization filter = new SimpleSkeletonization();
Bitmap bit = (m_thisPIC.Image as Bitmap).Clone(new Rectangle(0, 0, m_thisPIC.Image.Width, m_thisPIC.Image.Height), System.Drawing.Imaging.PixelFormat.Format8bppIndexed);
// apply the filter
filter.ApplyInPlace(bit);
pictureBox4.Image = bit;
}
private void button7_Click(object sender, EventArgs e)
{
Bitmap bit = (m_thisPIC.Image as Bitmap).Clone(new Rectangle(0, 0, m_thisPIC.Image.Width, m_thisPIC.Image.Height), System.Drawing.Imaging.PixelFormat.Format24bppRgb);
bit = XiHua.Xihua(bit, XiHua.array);
pictureBox4.Image = bit;
}
private void button8_Click(object sender, EventArgs e)
{
string[] strs = txtZaoDianSize.Text.Split(',');
Bitmap bit = (m_thisPIC.Image as Bitmap).Clone(new Rectangle(0, 0, m_thisPIC.Image.Width, m_thisPIC.Image.Height), System.Drawing.Imaging.PixelFormat.Format24bppRgb);
bit = new BlobsFiltering(int.Parse(strs[0]), int.Parse(strs[1]), bit.Width, bit.Height).Apply(bit);
pictureBox5.Image = bit;
m_thisPIC = pictureBox5;
}
private int GetMinX(List<IntPoint> hull)
{
int x = int.MaxValue;
foreach (IntPoint item in hull)
{
if (item.X < x)
x = item.X;
}
return x;
}
private void button9_Click(object sender, EventArgs e)
{
try
{
int intMaxWidth = int.MaxValue;
bool blnIsCheckWidth = cboHeBing.Checked;
if (blnIsCheckWidth)
{
intMaxWidth = int.Parse(txtMaxWidth.Text);
}
string[] minSizeStrs = txtMinSize.Text.Split(',');
Size minSize = new System.Drawing.Size(int.Parse(minSizeStrs[0]), int.Parse(minSizeStrs[1]));
Bitmap bit = (m_thisPIC.Image as Bitmap).Clone(new Rectangle(0, 0, m_thisPIC.Image.Width, m_thisPIC.Image.Height), System.Drawing.Imaging.PixelFormat.Format24bppRgb);
Bitmap imageCache = (m_thisPIC.Image as Bitmap).Clone(new Rectangle(0, 0, m_thisPIC.Image.Width, m_thisPIC.Image.Height), System.Drawing.Imaging.PixelFormat.Format24bppRgb);
// process image with blob counter
BlobCounter blobCounter = new BlobCounter();
blobCounter.ProcessImage(bit);
Blob[] blobs = blobCounter.GetObjectsInformation();
// create convex hull searching algorithm
GrahamConvexHull hullFinder = new GrahamConvexHull();
// lock image to draw on it
BitmapData data = bit.LockBits(
new Rectangle(0, 0, bit.Width, bit.Height),
ImageLockMode.ReadWrite, bit.PixelFormat);
Dictionary<Bitmap, int> lstBitNew = new Dictionary<Bitmap, int>();
Dictionary<List<IntPoint>, int> lstedgePoints = new Dictionary<List<IntPoint>, int>();
// process each blob
foreach (Blob blob in blobs)
{
List<IntPoint> leftPoints = new List<IntPoint>();
List<IntPoint> rightPoints = new List<IntPoint>();
List<IntPoint> edgePoints = new List<IntPoint>();
// get blob's edge points
blobCounter.GetBlobsLeftAndRightEdges(blob,
out leftPoints, out rightPoints);
edgePoints.AddRange(leftPoints);
edgePoints.AddRange(rightPoints);
// blob's convex hull
List<IntPoint> hull = hullFinder.FindHull(edgePoints);
lstedgePoints.Add(hull, GetMinX(hull));
}
List<List<IntPoint>> dicLstPoints = (from d in lstedgePoints
orderby d.Value ascending
select d.Key).ToList();
if (blnIsCheckWidth)
{
#region 检测连通域是否可合并
bool isBreak = false;
while (!isBreak)
{
isBreak = true;
int intKeyLength = dicLstPoints.Count;
for (int i = 0; i < intKeyLength; i++)
{
if (i != 0)
{
bool bln = CheckIsHeBing(dicLstPoints[i - 1], dicLstPoints[i], intMaxWidth);
if (bln)
{
dicLstPoints[i].AddRange(dicLstPoints[i - 1]);
dicLstPoints.RemoveAt(i - 1);
isBreak = false;
break;
}
}
if (i != intKeyLength - 1)
{
bool bln = CheckIsHeBing(dicLstPoints[i], dicLstPoints[i + 1], intMaxWidth);
if (bln)
{
dicLstPoints[i].AddRange(dicLstPoints[i + 1]);
dicLstPoints.RemoveAt(i + 1);
isBreak = false;
break;
}
}
}
}
#endregion
}
if (cboCount.Checked)
{
int intCharCount = int.Parse(txtCount.Text);
if (dicLstPoints.Count < intCharCount)
{
int intMax = 0;
int intMaxIndex = 0;
List<IntPoint> itemIndex = new List<IntPoint>();
for (int i = 0; i < dicLstPoints.Count; i++)
{
List<IntPoint> item = dicLstPoints[i];
item = (from p in item orderby p.X select p).ToList();
int _intWidth = item[item.Count - 1].X - item[0].X;
if (_intWidth > intMax)
{
intMax = _intWidth;
intMaxIndex = i;
itemIndex = item;
}
}
int intSprite = 2;
dicLstPoints.RemoveAt(intMaxIndex);
int intSpiteWidth = intMax / intSprite;
List<IntPoint> cache1 = new List<IntPoint>();
List<IntPoint> cache2 = new List<IntPoint>();
int intMaxy = 0;
int intMiny = 10000;
foreach (var item in itemIndex)
{
if (item.X <= itemIndex[0].X + intSpiteWidth + 1)
{
cache1.Add(item);
}
else
{
cache2.Add(item);
}
if (intMaxy < item.Y)
intMaxy = item.Y;
if (intMiny > item.Y)
intMiny = item.Y;
}
cache1.Add(new IntPoint() { X = itemIndex[0].X + intSpiteWidth, Y = intMaxy });
cache1.Add(new IntPoint() { X = itemIndex[0].X + intSpiteWidth, Y = intMiny });
cache2.Add(new IntPoint() { X = itemIndex[0].X + intSpiteWidth, Y = intMaxy });
cache2.Add(new IntPoint() { X = itemIndex[0].X + intSpiteWidth, Y = intMiny });
dicLstPoints.Add(cache1);
dicLstPoints.Add(cache2);
}
}
foreach (List<IntPoint> item in dicLstPoints)
{
List<IntPoint> hull = hullFinder.FindHull(item);
int intMinX = 0;
Bitmap bitNew = GetRectangleByListPoint(hull, imageCache, ref intMinX);
if (bitNew.Width < minSize.Width || bitNew.Height < minSize.Height)
continue;
lstBitNew.Add(bitNew, intMinX);
Drawing.Polygon(data, hull, Color.Red);
//List<System.Drawing.Point> lps = new List<System.Drawing.Point>();
//hull.ForEach(p => lps.Add(new System.Drawing.Point(p.X, p.Y)));
//GraphicsPath path = new GraphicsPath();
//path.AddLines(lps.ToArray());
//Bitmap newBit = null;
//BitmapCrop(imageCache, path, out newBit);
//newBit.Save("d:\\123.jpg");
}
bit.UnlockBits(data);
Dictionary<Bitmap, int> dic1Asc1 = (from d in lstBitNew
orderby d.Value ascending
select d).ToDictionary(k => k.Key, v => v.Value);
imageList1.Images.Clear();
foreach (var item in dic1Asc1)
{
Bitmap bitItem = item.Key;
bitItem = ToResizeAndCenter(bitItem);
if (this.checkBox1.Checked)
{
bitItem = GetMinWidthBitmap(bitItem);
}
imageList1.Images.Add(bitItem);//添加图片
}
this.listView1.BeginUpdate();
listView1.Items.Clear();
for (int i = 0; i < dic1Asc1.Count; i++)
{
// listView1.LargeImageList.Images.Add(list.Images.Keys[i], list.Images[i]);
ListViewItem lvi = new ListViewItem();
lvi.ImageIndex = i;
this.listView1.Items.Add(lvi);
}
this.listView1.EndUpdate();
pictureBox6.Image = bit;
this.panelModes.Controls.Clear();
string strName = this.comboBox1.SelectedItem.ToString();
string strPath = Path.Combine(Application.StartupPath, strName, "imgs");
for (int i = 0; i < dic1Asc1.Count; i++)
{
TextBox txt = new TextBox();
txt.Name = "txt_" + i;
txt.Width = 30;
txt.Location = new System.Drawing.Point((30 + 5) * i, 5);
this.panelModes.Controls.Add(txt);
}
string strKeys = string.Empty;
foreach (var item in this.imageList1.Images)
{
string strKey = GetTextByOneChar((Bitmap)item, strPath);
if (!string.IsNullOrEmpty(strKeys))
strKeys += ",";
strKeys += strKey;
}
lblKeys.Text = strKeys;
}
catch (Exception exc)
{
MessageBox.Show(exc.ToString(), "错误");
}
}
/// <summary>
/// 图片截图
/// </summary>
/// <param name="bitmap">原图</param>
/// <param name="path">裁剪路径</param>
/// <param name="outputBitmap">输出图</param>
/// <returns></returns>
public static Bitmap BitmapCrop(Bitmap bitmap, GraphicsPath path, out Bitmap outputBitmap)
{
RectangleF rect = path.GetBounds();
int left = (int)rect.Left;
int top = (int)rect.Top;
int width = (int)rect.Width;
int height = (int)rect.Height;
Bitmap image = (Bitmap)bitmap.Clone();
outputBitmap = new Bitmap(width, height);
for (int i = left; i < left + width; i++)
{
for (int j = top; j < top + height; j++)
{
//判断坐标是否在路径中
if (path.IsVisible(i, j))
{
//复制原图区域的像素到输出图片
outputBitmap.SetPixel(i - left, j - top, image.GetPixel(i, j));
//设置原图这部分区域为透明
image.SetPixel(i, j, Color.FromArgb(0, image.GetPixel(i, j)));
}
else
{
outputBitmap.SetPixel(i - left, j - top, Color.FromArgb(0, 255, 255, 255));
}
}
}
// bitmap.Dispose();
return image;
}
private bool CheckIsHeBing(List<IntPoint> lst1, List<IntPoint> lst2, int intMaxWidth)
{
int minx1 = int.MaxValue, minx2 = int.MaxValue;
int maxx1 = int.MinValue, maxx2 = int.MinValue;
foreach (IntPoint item in lst1)
{
if (item.X > maxx1)
maxx1 = item.X;
if (item.X < minx1)
minx1 = item.X;
}
foreach (IntPoint item in lst2)
{
if (item.X > maxx2)
maxx2 = item.X;
if (item.X < minx2)
minx2 = item.X;
}
int intCenter1 = minx1 + (maxx1 - minx1) / 2;
int intCenter2 = minx2 + (maxx2 - minx2) / 2;
if ((intCenter1 > minx2 && intCenter1 < maxx2) || (intCenter2 > minx1 && intCenter2 < maxx1))
{
int _intMin = minx1 < minx2 ? minx1 : minx2;
int _intMax = maxx1 > maxx2 ? maxx1 : maxx2;
if (_intMax - _intMin > intMaxWidth)
return false;
else
return true;
}
else
return false;
}
/// <summary>
/// 功能描述:识别单数字
/// 作 者:huangzh
/// 创建日期:2016-08-25 15:19:35
/// 任务编号:
/// </summary>
/// <param name="bit">bit</param>
/// <param name="lstNoChar">lstNoChar</param>
/// <returns>返回值</returns>
private string GetTextByOneChar(Bitmap bit, string strSourceImagPath, List<string> lstNoChar = null)
{
bit = bit.Clone(new Rectangle(0, 0, bit.Width, bit.Height), System.Drawing.Imaging.PixelFormat.Format24bppRgb);
var files = Directory.GetFiles(strSourceImagPath);
if (lstNoChar != null && lstNoChar.Count > 0)
{
var newFiles = from p in files
where !lstNoChar.Contains(Path.GetFileName(p).Split('_')[0])
select p;
files = newFiles.ToArray<string>();
}
var templateList = files.Select(i => { return new Bitmap(i); }).ToList();
var templateListFileName = files.Select(i => { return Path.GetFileName(i); }).ToList();
var result = new List<string>();
ExhaustiveTemplateMatching templateMatching = new ExhaustiveTemplateMatching(0.8f);
float max = 0;
int index = 0;
bit = ToResizeAndCenter(bit);
for (int j = 0; j < templateList.Count; j++)
{
Console.WriteLine(j);
var compare = templateMatching.ProcessImage(bit, templateList[j]);
if (compare.Length > 0 && compare[0].Similarity > max)
{
//记录下最相似的
max = compare[0].Similarity;
index = j;
}
}
if (templateListFileName.Count > 0)
return templateListFileName[index].Split('_')[0];
else
return "";
}
/// <summary>
/// 功能描述:图片放到中心
/// 作 者:huangzh
/// 创建日期:2016-08-25 15:16:42
/// 任务编号:
/// </summary>
/// <param name="bit">bit</param>
/// <param name="w">w</param>
/// <param name="h">h</param>
/// <returns>返回值</returns>
private Bitmap ToResizeAndCenter(Bitmap bit)
{
bit = bit.Clone(new Rectangle(0, 0, bit.Width, bit.Height), System.Drawing.Imaging.PixelFormat.Format24bppRgb);
bit = new Invert().Apply(bit);
int sw = bit.Width;
int sh = bit.Height;
int w, h;
if (sw < 25 && sh < 25)
{
w = 25;
h = 25;
}
else
{
w = sw + 5;
h = sh + 5;
}
Crop corpFilter = new Crop(new Rectangle(0, 0, w, h));
bit = corpFilter.Apply(bit);
//再反转回去
bit = new Invert().Apply(bit);
//计算中心位置
int centerX = (w - sw) / 2;
int centerY = (h - sh) / 2;
bit = new CanvasMove(new IntPoint(centerX, centerY), Color.White).Apply(bit);
return bit;
}
private void button10_Click(object sender, EventArgs e)
{
Bitmap image = (m_thisPIC.Image as Bitmap).Clone(new Rectangle(0, 0, m_thisPIC.Image.Width, m_thisPIC.Image.Height), System.Drawing.Imaging.PixelFormat.Format24bppRgb);
image = ColorFanZhuan(image);
pictureBox3.Image = image;
m_thisPIC = pictureBox3;
}
private Bitmap ColorFanZhuan(Bitmap image)
{
for (int i = 0; i < image.Width; i++)
{
for (int j = 0; j < image.Height; j++)
{
Color c = image.GetPixel(i, j);
if (c.Name != "ffffffff")
{
image.SetPixel(i, j, Color.White);
}
else
{
image.SetPixel(i, j, Color.Black);
}
}
}
return image;
}
private void pictureBox6_Click(object sender, EventArgs e)
{
m_thisPIC = sender as PictureBox;
}
private void button11_Click(object sender, EventArgs e)
{
FillHoles filter = new FillHoles();
filter.MaxHoleHeight = 2;
filter.MaxHoleWidth = 2;
filter.CoupledSizeFiltering = false;
Bitmap image = (m_thisPIC.Image as Bitmap).Clone(new Rectangle(0, 0, m_thisPIC.Image.Width, m_thisPIC.Image.Height), System.Drawing.Imaging.PixelFormat.Format24bppRgb);
image = (m_thisPIC.Image as Bitmap).Clone(new Rectangle(0, 0, m_thisPIC.Image.Width, m_thisPIC.Image.Height), System.Drawing.Imaging.PixelFormat.Format8bppIndexed);
// apply the filter
Bitmap result = filter.Apply(image);
pictureBox5.Image = result;
m_thisPIC = pictureBox5;
}
private void button12_Click(object sender, EventArgs e)
{
Bitmap image = (m_thisPIC.Image as Bitmap).Clone(new Rectangle(0, 0, m_thisPIC.Image.Width, m_thisPIC.Image.Height), System.Drawing.Imaging.PixelFormat.Format24bppRgb);
image = ClearMaoBian(image);
pictureBox7.Image = image;
m_thisPIC = pictureBox7;
}
/// <summary>
/// 功能描述:去毛边(检查4点)
/// 作 者:huangzh
/// 创建日期:2016-09-01 15:36:37
/// 任务编号:
/// </summary>
/// <param name="bitImg">bitImg</param>
/// <returns>返回值</returns>
private static Bitmap ClearMaoBian(Bitmap bitImg)
{
Bitmap bit = bitImg.Clone(new Rectangle(0, 0, bitImg.Width, bitImg.Height), System.Drawing.Imaging.PixelFormat.Format24bppRgb);
for (int i = 0; i < bit.Width; i++)
{
for (int j = 0; j < bit.Height; j++)
{
// Console.WriteLine(i + "," + j);
Color c = bit.GetPixel(i, j);
if (c.Name == "ffffffff")
{
//判定上下左右
int intCount = 0;
//上
if (j > 0)
{
Color c1 = bit.GetPixel(i, j - 1);
if (c1.Name != "ffffffff")
intCount++;
}
else
{
intCount++;
}
//下
if (j < bit.Height - 1)
{
Color c2 = bit.GetPixel(i, j + 1);
if (c2.Name != "ffffffff")
intCount++;
}
else
{
intCount++;
}
//左
if (i > 0)
{
Color c3 = bit.GetPixel(i - 1, j);
if (c3.Name != "ffffffff")
intCount++;
}
else
{
intCount++;
}
//右
if (i < bit.Width - 1)
{
Color c4 = bit.GetPixel(i + 1, j);
if (c4.Name != "ffffffff")
intCount++;
}
else
{
intCount++;
}
if (intCount >= 3)
{
bit.SetPixel(i, j, Color.Black);
}
}
}
}
return bit;
}
/// <summary>
/// 去毛边2(检测8点)
/// </summary>
/// <param name="bitImg"></param>
/// <returns></returns>
private static Bitmap ClearMaoBian2(Bitmap bitImg)
{
Bitmap bit = bitImg.Clone(new Rectangle(0, 0, bitImg.Width, bitImg.Height), System.Drawing.Imaging.PixelFormat.Format24bppRgb);
for (int i = 1; i < bit.Width - 1; i++)
{
for (int j = 1; j < bit.Height - 1; j++)
{
// Console.WriteLine(i + "," + j);
Color c = bit.GetPixel(i, j);
if (c.Name == "ffffffff")
{
//判定上下左右
int intCount = 0;
//上
Color c1 = bit.GetPixel(i, j - 1);
if (c1.Name != "ffffffff")
intCount++;
//右上
Color c5 = bit.GetPixel(i + 1, j - 1);
if (c5.Name != "ffffffff")
intCount++;
//右
Color c4 = bit.GetPixel(i + 1, j);
if (c4.Name != "ffffffff")
intCount++;
//右下
Color c6 = bit.GetPixel(i + 1, j + 1);
if (c6.Name != "ffffffff")
intCount++;
//下
Color c2 = bit.GetPixel(i, j + 1);
if (c2.Name != "ffffffff")
intCount++;
//左下
Color c7 = bit.GetPixel(i - 1, j + 1);
if (c7.Name != "ffffffff")
intCount++;
//左
Color c3 = bit.GetPixel(i - 1, j);
if (c3.Name != "ffffffff")
intCount++;
//左上
Color c8 = bit.GetPixel(i - 1, j - 1);
if (c8.Name != "ffffffff")
intCount++;
if (intCount >= 5)
{
bit.SetPixel(i, j, Color.Black);
}
}
}
}
return bit;
}
/// <summary>
/// 去毛边2(检测8点)
/// </summary>
/// <param name="bitImg"></param>
/// <returns></returns>
private static Bitmap ClearMaoBian3(Bitmap bitImg)
{
Bitmap bit = bitImg.Clone(new Rectangle(0, 0, bitImg.Width, bitImg.Height), System.Drawing.Imaging.PixelFormat.Format24bppRgb);
for (int i = 1; i < bit.Width - 1; i++)
{
for (int j = 1; j < bit.Height - 1; j++)
{
// Console.WriteLine(i + "," + j);
Color c = bit.GetPixel(i, j);
if (c.Name == "ffffffff")
{
//判定上下左右
int intCount = 0;
//上
Color c1 = bit.GetPixel(i, j - 1);
if (c1.Name != "ffffffff")
intCount++;
//右上
Color c5 = bit.GetPixel(i + 1, j - 1);
if (c5.Name != "ffffffff")
intCount++;
//右
Color c4 = bit.GetPixel(i + 1, j);
if (c4.Name != "ffffffff")
intCount++;
//右下
Color c6 = bit.GetPixel(i + 1, j + 1);
if (c6.Name != "ffffffff")
intCount++;
//下
Color c2 = bit.GetPixel(i, j + 1);
if (c2.Name != "ffffffff")
intCount++;
//左下
Color c7 = bit.GetPixel(i - 1, j + 1);
if (c7.Name != "ffffffff")
intCount++;
//左
Color c3 = bit.GetPixel(i - 1, j);
if (c3.Name != "ffffffff")
intCount++;
//左上
Color c8 = bit.GetPixel(i - 1, j - 1);
if (c8.Name != "ffffffff")
intCount++;
if (intCount > 6)
{
bit.SetPixel(i, j, Color.Black);
}
}
}
}
return bit;
}
private Bitmap GetRectangleByListPoint(List<IntPoint> hull, Bitmap bitImg, ref int _minX)
{
int minx = int.MaxValue;
int miny = int.MaxValue;
int maxx = int.MinValue;
int maxy = int.MinValue;
foreach (IntPoint item in hull)
{
if (item.X > maxx)
maxx = item.X;
if (item.X < minx)
minx = item.X;
if (item.Y > maxy)
maxy = item.Y;
if (item.Y < miny)
miny = item.Y;
}
Bitmap bit = new Bitmap(maxx - minx + 1, maxy - miny + 1);
int bitImgWidth = bitImg.Width;
int bitImgHeight = bitImg.Height;
for (int i = minx; i <= maxx; i++)
{
for (int j = miny; j <= maxy; j++)
{
Color c = bitImg.GetPixel(i, j);
if (c.R == 0)
c = Color.White;
else
c = Color.Black;
bit.SetPixel(i - minx, j - miny, c);
}
}
_minX = minx;
// bit.Save("d:\\1\\" + Guid.NewGuid() + ".jpg");
return bit;
}
/// <summary>
/// 功能描述:得到左右30度最窄的图片
/// 作 者:huangzh
/// 创建日期:2016-08-25 14:58:32
/// 任务编号:
/// </summary>
/// <param name="bitImg">bitImg</param>
/// <returns>返回值</returns>
private Bitmap GetMinWidthBitmap(Bitmap bitImg, float fltNum = 15)
{
Bitmap bitCache = bitImg;
for (float i = fltNum * -1; i < fltNum; i++)
{
if (i == 0)
continue;
Bitmap bit1 = bitImg.Clone(new Rectangle(0, 0, bitImg.Width, bitImg.Height), System.Drawing.Imaging.PixelFormat.Format24bppRgb);
Bitmap bit2 = RotateImage(bit1, i);
Bitmap bit3 = GetMinBitmap(bit2);
if (bit3.Width < bitCache.Width)
{
bitCache = bit3;
}
else
{
bit3.Dispose();
}
}
return bitCache;
}
/// <summary>
/// 功能描述:缩小图片
/// 作 者:huangzh
/// 创建日期:2016-08-25 14:55:46
/// 任务编号:
/// </summary>
/// <param name="bit">bit</param>
/// <returns>返回值</returns>
private Bitmap GetMinBitmap(Bitmap bit)
{
int x1 = 0, y1 = 0;
int x2 = 0, y2 = 0;
GetXY(bit, ref x1, ref y1, ref x2, ref y2);
Bitmap bit11 = bit.Clone(new Rectangle(x1, y1, x2 - x1 + 1, y2 - y1 + 1), System.Drawing.Imaging.PixelFormat.Format24bppRgb);
return bit11;
}
/// <summary>
/// 功能描述:活动最大最小xy
/// 作 者:huangzh
/// 创建日期:2016-08-25 14:55:56
/// 任务编号:
/// </summary>
/// <param name="bit">bit</param>
/// <param name="x1">x1</param>
/// <param name="y1">y1</param>
/// <param name="x2">x2</param>
/// <param name="y2">y2</param>
private void GetXY(
Bitmap bit,
ref int x1,
ref int y1,
ref int x2,
ref int y2)
{
bool bln = false;
#region 最小X
for (int i = 0; i < bit.Width; i++)
{
for (int j = 0; j < bit.Height; j++)
{
Color c = bit.GetPixel(i, j);
if (c.Name != "ffffffff")
{
x1 = i;
bln = true;
break;
}
}
if (bln)
{
break;
}
}
#endregion
#region 最大X
bln = false;
for (int i = bit.Width - 1; i >= 0; i--)
{
for (int j = 0; j < bit.Height; j++)
{
Color c = bit.GetPixel(i, j);
if (c.Name != "ffffffff")
{
x2 = i;
bln = true;
break;
}