国产片侵犯亲女视频播放_亚洲精品二区_在线免费国产视频_欧美精品一区二区三区在线_少妇久久久_在线观看av不卡

服務器之家:專注于服務器技術及軟件下載分享
分類導航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術|正則表達式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

服務器之家 - 編程語言 - C# - C#簡單數字圖像處理程序

C#簡單數字圖像處理程序

2022-03-01 14:28Lynn_whu C#

這篇文章主要為大家詳細介紹了C#簡單數字圖像處理程序,具有一定的參考價值,感興趣的小伙伴們可以參考一下

c#編寫的簡單數字圖像處理程序,數字圖像處理的平時成績和編程作業竟然占50%,那就把最近做的事寫個札記吧。

先放個最終做成提交的效果看看:

C#簡單數字圖像處理程序

1.直方圖均衡化

C#簡單數字圖像處理程序

2.算子銳化

C#簡單數字圖像處理程序

C#簡單數字圖像處理程序

C#簡單數字圖像處理程序

3.空域增強

C#簡單數字圖像處理程序

C#簡單數字圖像處理程序

C#簡單數字圖像處理程序

C#簡單數字圖像處理程序

一、要達到的目的和效果

  1.打開,保存圖片;

  2.獲取圖像灰度值,圖像坐標;

  3.進行線性變換,直方圖均衡化處理;

  4.直方圖變換增強,以及各種濾波處理;

  5.圖像銳化(kirsch,laplace,sobel等算子)。

二、編程環境及語言

c#-windowsform-vs2015

三、圖標

最近發現了一個完全免費的矢量圖標網站阿里媽媽iconfont,超級好用。

C#簡單數字圖像處理程序

當然也可以自己動手畫一個

四、創建窗體

  1.先建一個c#windows窗體應用程序,設置好保存路徑和項目名稱;

  2.打開工具箱,找到menuscript,加到窗體中,依次填寫菜單以及子菜單的名稱,菜單里將完成主要的圖像處理操作;

  3.因為要顯示處理前后的圖片,所以再添加兩個picturebox控件,可以設置停靠模式為stretchimage;再加兩個groupbox,每個groupbox里添加label和textbox控件,用來顯示圖像灰度值及坐標,這樣窗體基本搭建完成,還是挺簡單的。

五、主要代碼

?
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
using system;
using system.collections.generic;
using system.componentmodel;
using system.data;
using system.drawing;
using system.drawing.imaging;
using system.linq;
using system.text;
using system.windows.forms;
 
 
namespace text1
{
 public partial class imageenhancement : form
 {
 public imageenhancement()
 {
  initializecomponent();
 }
 bitmap bitmap;
 int iw, ih;
 //打開文件
 private void 打開toolstripmenuitem_click(object sender, eventargs e)
 {
  picturebox1.image = null;//先設置兩個picturebox為空
  picturebox2.image = null;
  //使用 openfiledialog類打開圖片
  openfiledialog open = new openfiledialog();
  open.filter = "圖像文件(*.bmp;*.jpg;*gif;*png;*.tif;*.wmf)|"
   + "*.bmp;*jpg;*gif;*png;*.tif;*.wmf";
  if (open.showdialog() == dialogresult.ok)
  {
  try
  {
   bitmap = (bitmap)image.fromfile(open.filename);
  }
  catch (exception exp) { messagebox.show(exp.message); }
  picturebox1.refresh();
  picturebox1.image = bitmap;
  label6.text = "原圖";
  iw = bitmap.width;
  ih = bitmap.height;
 
 
 
  }
 }
 //保存文件
 private void 保存toolstripmenuitem_click(object sender, eventargs e)
 {
  string str;
  savefiledialog savefiledialog1 = new savefiledialog();
  savefiledialog1.filter = "圖像文件(*.bmp)|*.bmp|all file(*.*)|*.*";
  savefiledialog1.showdialog();
  str = savefiledialog1.filename;
  picturebox2.image.save(str);
 
 
 }
 //退出
 private void 退出toolstripmenuitem_click(object sender, eventargs e)
 {
  this.close();
 }
 private void label5_click(object sender, eventargs e)
 {
 }
 //讀取灰度值及坐標
 private void picturebox1_mousedown(object sender, mouseeventargs e)
 {
  color pointrgb = bitmap.getpixel(e.x, e.y);
  textbox1.text = pointrgb.r.tostring();
  textbox2.text = pointrgb.g.tostring();
  textbox3.text = pointrgb.b.tostring();
  textbox4.text = e.x.tostring();
  textbox5.text = e.y.tostring();
  int a = int.parse(textbox1.text);
 }
 //線性變換部分
 private void linearpo_click(object sender, eventargs e)
 {
  if (bitmap != null)
  {
  linearpoform linearform = new linearpoform();
  if (linearform.showdialog() == dialogresult.ok)
  {
   rectangle rect = new rectangle(0, 0, bitmap.width, bitmap.height);
   system.drawing.imaging.bitmapdata bmpdata = bitmap.lockbits(rect,
   system.drawing.imaging.imagelockmode.readwrite,
   bitmap.pixelformat);
   intptr ptr = bmpdata.scan0;
   //int bytes = bitmap.width *;
  }
  }
 }
 private void textbox4_textchanged(object sender, eventargs e)
 {
 }
 private void label3_click(object sender, eventargs e)
 {
 }
 //對比度擴展
 private void 對比度擴展toolstripmenuitem_click(object sender, eventargs e)
 {
  if (bitmap != null)
  {
  strechdialog dialog = new strechdialog();
 
 
  if (dialog.showdialog() == dialogresult.ok)
  {
   this.text = " 圖像增強 對比度擴展 ";
   bitmap bm = new bitmap(picturebox1.image);
 
 
   int x1 = convert.toint32(dialog.getx01);
   int y1 = convert.toint32(dialog.gety01);
   int x2 = convert.toint32(dialog.getx02);
   int y2 = convert.toint32(dialog.gety02);
 
 
   //計算灰度映射表
   int[] pixmap = pixelsmap(x1, y1, x2, y2);
 
 
   //線性拉伸
   bm = stretch(bm, pixmap, iw, ih);
 
 
   picturebox2.refresh();
   picturebox2.image = bm;
   label7.text = "對比度擴展結果";
  }
  }
 }
 
 //計算灰度映射表
 public int[] pixelsmap(int x1, int y1, int x2, int y2)
 {
  int[] pmap = new int[256];  //映射表
  if (x1 > 0)
  {
  double k1 = y1 / x1;  //第1段斜率k1
  //按第1段斜率k1線性變換
  for (int i = 0; i <= x1; i++)
   pmap[i] = (int)(k1 * i);
  }
  double k2 = (y2 - y1) / (x2 - x1); //第2段斜率k2
 
  //按第2段斜率k2線性變換
  for (int i = x1 + 1; i <= x2; i++)
  if (x2 != x1)
   pmap[i] = y1 + (int)(k2 * (i - x1));
  else
   pmap[i] = y1;
 
  if (x2 < 255)
  {
  double k3 = (255 - y2) / (255 - x2);//第2段斜率k2
 
  //按第3段斜率k3線性變換
  for (int i = x2 + 1; i < 256; i++)
   pmap[i] = y2 + (int)(k3 * (i - x2));
  }
  return pmap;
 }
 
 //對比度擴展函數
 public bitmap stretch(bitmap bm, int[] map, int iw, int ih)
 {
  color c = new color();
  int r, g, b;
  for (int j = 0; j < ih; j++)
  {
  for (int i = 0; i < iw; i++)
  {
   c = bm.getpixel(i, j);
   r = map[c.r];
   g = map[c.g];
   b = map[c.b];
   if (r >= 255) r = 255;
   if (r < 0) r = 0;
   if (g >= 255) g = 255;
   if (g < 0) g = 0;
   if (b >= 255) b = 255;
   if (b < 0) b = 0;
   bm.setpixel(i, j, color.fromargb(r, g, b));
  }
  }
  return bm;
 }
 private void 直方圖均衡化toolstripmenuitem_click(object sender, eventargs e)
 {
  if (bitmap != null)
  {
  this.text = " 圖像增強 直方圖均衡化";
  bitmap bm = new bitmap(picturebox1.image);
  //獲取直方圖
  int[] hist = gethist(bm, iw, ih);
 
  //直方圖均勻化
  bm = histequal(bm, hist, iw, ih);
 
  picturebox2.refresh();
  picturebox2.image = bm;
  label7.text = "直方圖均衡化結果";
  flag = true;
  }
 }
 bool flag = false;   //直方圖均衡化標志
 
 //顯示直方圖
 private void 顯示直方圖toolstripmenuitem_click(object sender, eventargs e)
 {
  if (flag)
  {
  bitmap b1 = new bitmap(picturebox1.image);
  bitmap b2 = new bitmap(picturebox2.image);
 
  int[] hist1 = gethist(b1, iw, ih);
  int[] hist2 = gethist(b2, iw, ih);
  drawhist(hist1, hist2);
  }
 }
 
 //獲取直方圖
 public int[] gethist(bitmap bm, int iw, int ih)
 {
  int[] h = new int[256];
  for (int j = 0; j < ih; j++)
  {
  for (int i = 0; i < iw; i++)
  {
   int grey = (bm.getpixel(i, j)).r;
   h[grey]++;
  }
  }
  return h;
 }
 //直方圖均衡化
 public bitmap histequal(bitmap bm, int[] hist, int iw, int ih)
 {
  color c = new color();
  double p = (double)255 / (iw * ih);
  double[] sum = new double[256];
  int[] outg = new int[256];
  int r, g, b;
  sum[0] = hist[0];
  for (int i = 1; i < 256; i++)
  sum[i] = sum[i - 1] + hist[i];
 
 
 
  //灰度變換:i-->outg[i]
  for (int i = 0; i < 256; i++)
  outg[i] = (int)(p * sum[i]);
 
  for (int j = 0; j < ih; j++)
  {
  for (int i = 0; i < iw; i++)
  {
   r = (bm.getpixel(i, j)).r;
   g = (bm.getpixel(i, j)).g;
   b = (bm.getpixel(i, j)).b;
   c = color.fromargb(outg[r], outg[g], outg[b]);
   bm.setpixel(i, j, c);
  }
  }
  return bm;
 }
 
 public void drawhist(int[] h1, int[] h2)
 {
  //畫原圖直方圖------------------------------------------
  graphics g = picturebox1.creategraphics();
  pen pen1 = new pen(color.blue);
  g.clear(this.backcolor);
 
  //找出最大的數,進行標準化.
  int maxn = h1[0];
  for (int i = 1; i < 256; i++)
  if (maxn < h1[i])
   maxn = h1[i];
 
  for (int i = 0; i < 256; i++)
  h1[i] = h1[i] * 250 / maxn;
 
  g.fillrectangle(new solidbrush(color.white), 0, 0, 255, 255);
 
  pen1.color = color.red;
  for (int i = 0; i < 256; i++)
  g.drawline(pen1, i, 255, i, 255 - h1[i]);
 
  g.drawstring("" + maxn, this.font, new solidbrush(color.blue), 0, 0);
 
  label6.text = "原圖直方圖";
 
  //畫均衡化后直方圖------------------------------------------
  g = picturebox2.creategraphics();
  pen1 = new pen(color.blue);
  g.clear(this.backcolor);
 
  //找出最大的數,進行標準化.
  maxn = h2[0];
  for (int i = 1; i < 256; i++)
  if (maxn < h2[i])
   maxn = h2[i];
 
  for (int i = 0; i < 256; i++)
  h2[i] = h2[i] * 250 / maxn;
 
  g.fillrectangle(new solidbrush(color.white), 0, 0, 255, 255);
 
  pen1.color = color.red;
  for (int i = 0; i < 256; i++)
  g.drawline(pen1, i, 255, i, 255 - h2[i]);
 
  g.drawstring("" + maxn, this.font, new solidbrush(color.blue), 0, 0);
  label7.text = "均衡化后直方圖";
  flag = false;
 }
 
 private void 閾值濾波toolstripmenuitem_click(object sender, eventargs e)
 {
  if (bitmap != null)
  {
  this.text = "圖像增強 閾值濾波";
  bitmap bm = new bitmap(picturebox1.image);
  //閾值濾波
  bm = threshold(bm, iw, ih);
 
  picturebox2.refresh();
  picturebox2.image = bm;
  label7.text = "閾值濾波結果";
  }
 }
 
 
 //3×3閾值濾波
 public bitmap threshold(bitmap bm, int iw, int ih)
 {
  bitmap obm = new bitmap(picturebox1.image);
 
 
  int avr,  //灰度平均
  sum,  //灰度和
  num = 0, //計數器
  nt = 4, //計數器閾值
  t = 50; //閾值
  int pij, pkl, //(i,j),(i+k,j+l)處灰度值
  err;  //誤差
 
 
  for (int j = 1; j < ih - 1; j++)
  {
  for (int i = 1; i < iw - 1; i++)
  {
   //取3×3塊的9個象素, 求和
   sum = 0;
   for (int k = -1; k < 2; k++)
   {
   for (int l = -1; l < 2; l++)
   {
    if ((k != 0) || (l != 0))
    {
    pkl = (bm.getpixel(i + k, j + l)).r;
    pij = (bm.getpixel(i, j)).r;
    err = math.abs(pkl - pij);
    sum = sum + pkl;
    if (err > t) num++;
    }
   }
   }
   avr = (int)(sum / 8.0f);  //平均值
   if (num > nt)
   obm.setpixel(i, j, color.fromargb(avr, avr, avr));
  }
  }
  return obm;
 }
 
 private void 均值濾波toolstripmenuitem_click(object sender, eventargs e)
 {
  if (bitmap != null)
  {
  this.text = "數字圖像處理";
  bitmap bm = new bitmap(picturebox1.image);
  bm = average(bm, iw, ih);
  picturebox2.refresh();
  picturebox2.image = bm;
  label7.text = "均值濾波結果";
  }
 }
 //均值濾波
 public bitmap average(bitmap bm, int iw, int ih)
 {
  bitmap obm = new bitmap(picturebox1.image);
  for (int j = 1; j < ih - 1; j++)
  {
  for (int i = 1; i < iw - 1; i++)
  {
   int avr;
   int avr1;
   int avr2;
   int sum = 0;
   int sum1 = 0;
   int sum2 = 0;
   for (int k = -1; k <= 1; k++)
   {
   for (int l = -1; l <= 1; l++)
   {
    sum = sum + (bm.getpixel(i + k, j + 1).r);
    sum1 = sum1 + (bm.getpixel(i + k, j + 1).g);
    sum2 = sum2 + (bm.getpixel(i + k, j + 1).b);
   }
   }
   avr = (int)(sum / 9.0f);
   avr1 = (int)(sum1 / 9.0f);
   avr2 = (int)(sum2 / 9.0f);
   obm.setpixel(i, j, color.fromargb(avr, avr1, avr2));
  }
  }
  return obm;
 }
 
 private void 中值濾波toolstripmenuitem_click(object sender, eventargs e)
 {
  if (bitmap != null)
  {
  
   this.text = "圖像增強 中值濾波";
   bitmap bm = new bitmap(picturebox1.image);
   int num =3;
   //中值濾波
   bm = median(bm, iw, ih, num);
 
   picturebox2.refresh();
   picturebox2.image = bm;
   label2.location = new point(370, 280);
   if (num == 1) label7.text = "1x5窗口濾波結果";
   else if (num == 2) label7.text = "5x1窗口濾波結果";
   else if (num == 3) label7.text = "5x5窗口濾波結果";
  
  }
 }
 
 //中值濾波方法
 public bitmap median(bitmap bm, int iw, int ih, int n)
 {
  bitmap obm = new bitmap(picturebox1.image);
  for (int j = 2; j < ih - 2; j++)
  {
  int[] dt;
  int[] dt1;
  int[] dt2;
  for (int i = 2; i < iw - 2; i++)
  {
   int m = 0, r = 0, r1 = 0, r2 = 0, a = 0, b = 0;
   if (n == 3)
   {
   dt = new int[25];
   dt1 = new int[25];
   dt2 = new int[25];
   //取5×5塊的25個象素
   for (int k = -2; k < 3; k++)
   {
    for (int l = -2; l < 3; l++)
    {
    //取(i+k,j+l)處的象素,賦于數組dt
    dt[m] = (bm.getpixel(i + k, j + l)).r;
    dt1[a] = (bm.getpixel(i + k, j + l)).g;
    dt2[b] = (bm.getpixel(i + k, j + l)).b;
    m++;
    a++;
    b++;
    }
   }
   //冒泡排序,輸出中值
   r = median_sorter(dt, 25); //中值
   r1 = median_sorter(dt1, 25);
   r2 = median_sorter(dt2, 25);
   }
   else if (n == 1)
   {
   dt = new int[5];
 
   //取1×5窗口5個像素
   dt[0] = (bm.getpixel(i, j - 2)).r;
   dt[1] = (bm.getpixel(i, j - 1)).r;
   dt[2] = (bm.getpixel(i, j)).r;
   dt[3] = (bm.getpixel(i, j + 1)).r;
   dt[4] = (bm.getpixel(i, j + 2)).r;
   r = median_sorter(dt, 5); //中值
   dt1 = new int[5];
 
 
   //取1×5窗口5個像素
   dt1[0] = (bm.getpixel(i, j - 2)).g;
   dt1[1] = (bm.getpixel(i, j - 1)).g;
   dt1[2] = (bm.getpixel(i, j)).g;
   dt1[3] = (bm.getpixel(i, j + 1)).g;
   dt1[4] = (bm.getpixel(i, j + 2)).g;
   r1 = median_sorter(dt1, 5); //中值
   dt2 = new int[5];
 
 
   //取1×5窗口5個像素
   dt2[0] = (bm.getpixel(i, j - 2)).b;
   dt2[1] = (bm.getpixel(i, j - 1)).b;
   dt2[2] = (bm.getpixel(i, j)).b;
   dt2[3] = (bm.getpixel(i, j + 1)).b;
   dt2[4] = (bm.getpixel(i, j + 2)).b;
   r2 = median_sorter(dt2, 5); //中值   
   }
   else if (n == 2)
   {
   dt = new int[5];
 
 
   //取5×1窗口5個像素
   dt[0] = (bm.getpixel(i - 2, j)).r;
   dt[1] = (bm.getpixel(i - 1, j)).r;
   dt[2] = (bm.getpixel(i, j)).r;
   dt[3] = (bm.getpixel(i + 1, j)).r;
   dt[4] = (bm.getpixel(i + 2, j)).r;
   r = median_sorter(dt, 5); //中值 dt = new int[5];
 
 
   //取5×1窗口5個像素
   dt1 = new int[5];
   dt1[0] = (bm.getpixel(i - 2, j)).g;
   dt1[1] = (bm.getpixel(i - 1, j)).g;
   dt1[2] = (bm.getpixel(i, j)).g;
   dt1[3] = (bm.getpixel(i + 1, j)).g;
   dt1[4] = (bm.getpixel(i + 2, j)).g;
   r1 = median_sorter(dt1, 5); //中值
 
   //取5×1窗口5個像素
   dt2 = new int[5];
   dt2[0] = (bm.getpixel(i - 2, j)).b;
   dt2[1] = (bm.getpixel(i - 1, j)).b;
   dt2[2] = (bm.getpixel(i, j)).b;
   dt2[3] = (bm.getpixel(i + 1, j)).b;
   dt2[4] = (bm.getpixel(i + 2, j)).b;
   r2 = median_sorter(dt2, 5); //中值
 
   }
   obm.setpixel(i, j, color.fromargb(r, r1, r2));  //輸出  
  }
  }
  return obm;
 }
 //冒泡排序,輸出中值
 public int median_sorter(int[] dt, int m)
 {
  int tem;
  for (int k = m - 1; k >= 1; k--)
  for (int l = 1; l <= k; l++)
   if (dt[l - 1] > dt[l])
   {
   tem = dt[l];
   dt[l] = dt[l - 1];
   dt[l - 1] = tem;
   }
  return dt[(int)(m / 2)];
 }
 private void picturebox1_click(object sender, eventargs e)
 {
 }
 
 
 private void 圖像銳化toolstripmenuitem_click(object sender, eventargs e)
 {
  }
 
 
 /*
  * pix --待檢測圖像數組
  * iw, ih --待檢測圖像寬高
  * num --算子代號.1:kirsch算子;2:laplace算子;3:prewitt算子;5:sobel算子
 */
 public bitmap detect(bitmap bm, int iw, int ih, int num)
  {
 
  bitmap b1 = new bitmap(picturebox1.image);
 
  color c = new color();
  int i, j, r;
  int[,] inr = new int[iw, ih]; //紅色分量矩陣
  int[,] ing = new int[iw, ih]; //綠色分量矩陣
  int[,] inb = new int[iw, ih]; //藍色分量矩陣
  int[,] gray = new int[iw, ih];//灰度圖像矩陣
 
  //轉變為灰度圖像矩陣
 
  for (j = 0; j < ih; j++)
  {
  for (i = 0; i < iw; i++)
  {
   c = bm.getpixel(i, j);
   inr[i, j] = c.r;
   ing[i, j] = c.g;
   inb[i, j] = c.b;
   gray[i, j] = (int)((c.r + c.g + c.b) / 3.0);
  }
  }
  if (num == 1)//kirsch
  {
  int[,] kir0 = {{ 5, 5, 5},
    {-3, 0,-3},
    {-3,-3,-3}},//kir0
 
   kir1 = {{-3, 5, 5},
    {-3, 0, 5},
    {-3,-3,-3}},//kir1
 
   kir2 = {{-3,-3, 5},
    {-3, 0, 5},
    {-3,-3, 5}},//kir2
 
   kir3 = {{-3,-3,-3},
    {-3, 0, 5},
    {-3, 5, 5}},//kir3
 
   kir4 = {{-3,-3,-3},
    {-3, 0,-3},
    { 5, 5, 5}},//kir4
 
   kir5 = {{-3,-3,-3},
    { 5, 0,-3},
    { 5, 5,-3}},//kir5
 
   kir6 = {{ 5,-3,-3},
    { 5, 0,-3},
    { 5,-3,-3}},//kir6
 
   kir7 = {{ 5, 5,-3},
    { 5, 0,-3},
    {-3,-3,-3}};//kir7
  //邊緣檢測
 
  int[,] edge0 = new int[iw, ih];
 
  int[,] edge1 = new int[iw, ih];
 
  int[,] edge2 = new int[iw, ih];
 
  int[,] edge3 = new int[iw, ih];
 
  int[,] edge4 = new int[iw, ih];
 
  int[,] edge5 = new int[iw, ih];
 
  int[,] edge6 = new int[iw, ih];
 
  int[,] edge7 = new int[iw, ih];
 
  edge0 = edgeenhance(gray, kir0, iw, ih);
  edge1 = edgeenhance(gray, kir1, iw, ih);
  edge2 = edgeenhance(gray, kir2, iw, ih);
  edge3 = edgeenhance(gray, kir3, iw, ih);
  edge4 = edgeenhance(gray, kir4, iw, ih);
  edge5 = edgeenhance(gray, kir5, iw, ih);
  edge6 = edgeenhance(gray, kir6, iw, ih);
  edge7 = edgeenhance(gray, kir7, iw, ih);
 
  int[] tem = new int[8];
  int max;
  for (j = 0; j < ih; j++)
  {
   for (i = 0; i < iw; i++)
   {
   tem[0] = edge0[i, j];
   tem[1] = edge1[i, j];
   tem[2] = edge2[i, j];
   tem[3] = edge3[i, j];
   tem[4] = edge4[i, j];
   tem[5] = edge5[i, j];
   tem[6] = edge6[i, j];
   tem[7] = edge7[i, j];
   max = 0;
   for (int k = 0; k < 8; k++)
    if (tem[k] > max) max = tem[k];
   if (max > 255) max = 255;
   r = 255 - max;
   b1.setpixel(i, j, color.fromargb(r, r, r));
   }
  }
  }
  else if (num == 2)   //laplace
  {
  int[,] lap1 = {{ 1, 1, 1},
    { 1,-8, 1},
    { 1, 1, 1}};
 
  /*byte[][] lap2 = {{ 0, 1, 0},
     { 1,-4, 1},
     { 0, 1, 0}}; */
 
  //邊緣增強
  int[,] edge = edgeenhance(gray, lap1, iw, ih);
 
  for (j = 0; j < ih; j++)
  {
   for (i = 0; i < iw; i++)
   {
   r = edge[i, j];
   if (r > 255) r = 255;
 
   if (r < 0) r = 0;
   c = color.fromargb(r, r, r);
   b1.setpixel(i, j, c);
   }
  }
  }
  else if (num == 3)//prewitt
  {
  //prewitt算子d_x模板
  int[,] pre1 = {{ 1, 0,-1},
    { 1, 0,-1},
    { 1, 0,-1}};
 
  //prewitt算子d_y模板
  int[,] pre2 = {{ 1, 1, 1},
    { 0, 0, 0},
    {-1,-1,-1}};
  int[,] edge1 = edgeenhance(gray, pre1, iw, ih);
  
  int[,] edge2 = edgeenhance(gray, pre2, iw, ih);
  for (j = 0; j < ih; j++)
  {
   for (i = 0; i < iw; i++)
   {
   r = math.max(edge1[i, j], edge2[i, j]);
   
   if(r > 255) r = 255;
   c = color.fromargb(r, r, r);
   b1.setpixel(i, j, c);
   }
  }
  }
 
  else if (num == 5)    //sobel
  {
  int[,] sob1 = {{ 1, 0,-1},
    { 2, 0,-2},
    { 1, 0,-1}};
  int[,] sob2 = {{ 1, 2, 1},
    { 0, 0, 0},
    {-1,-2,-1}},
 
  int[,] edge1 = edgeenhance(gray, sob1, iw, ih);
  int[,] edge2 = edgeenhance(gray, sob2, iw, ih);
  for (j = 0; j < ih; j++)
  {
   for (i = 0; i < iw; i++)
   {
   r = math.max(edge1[i, j], edge2[i, j]);
   if(r > 255) r = 255;
   c = color.fromargb(r, r, r);
   b1.setpixel(i, j, c);
   }
  }
  }
  return b1;
 }
 private void kirsch算子銳化toolstripmenuitem_click(object sender, eventargs e)
 {
  if (bitmap != null)
  {
  // this.text = " 圖像 - 圖像銳化 - kirsch算子";
  bitmap bm = new bitmap(picturebox1.image);
  //1: kirsch銳化
  bm = detect(bm, iw, ih, 1)
  picturebox2.refresh();
  picturebox2.image = bm;
  label7.text = " kirsch算子 銳化結果";
  }
 }
 public int[,] edgeenhance(int[,] ing, int[,] tmp, int iw, int ih)
 {
  int[,] ed = new int[iw, ih];
  for (int j = 1; j < ih - 1; j++)
  {
  for (int i = 1; i < iw - 1; i++)
  {
   ed[i, j] = math.abs(tmp[0, 0] * ing[i - 1, j - 1]
    + tmp[0, 1] * ing[i - 1, j] + tmp[0, 2] * ing[i - 1, j + 1]
    + tmp[1, 0] * ing[i, j - 1] + tmp[1, 1] * ing[i, j]
    + tmp[1, 2] * ing[i, j + 1] + tmp[2, 0] * ing[i + 1, j - 1]
    + tmp[2, 1] * ing[i + 1, j] + tmp[2, 2] * ing[i + 1, j + 1]);
  }
  }
  return ed;
 }
 //laplace算子
 private void laplace算子銳化toolstripmenuitem_click(object sender, eventargs e)
 {
  if (bitmap != null)
  {
  bitmap bm = new bitmap(picturebox1.image);
 
  //2: laplace銳化
  bm = detect(bm, iw, ih, 2);
  picturebox2.refresh();
  picturebox2.image = bm;
  label7.text = "laplace算子 銳化結果";
  }
 }
 
 //prewitt算子
 private void prewitt算子銳化toolstripmenuitem_click(object sender, eventargs e)
 {
  if (bitmap != null)
  {
  
  bitmap bm = new bitmap(picturebox1.image);
  //3:prewitt銳化
  bm = detect(bm, iw, ih, 3);
  picturebox2.refresh();
  picturebox2.image = bm;
  label2.location = new point(390, 280);
  label7.text = " prewitt算子 銳化結果";
  }
 }
 
 
 //roberts算子
 private void roberts算子銳化toolstripmenuitem_click(object sender, eventargs e)
 {
  if (bitmap != null)
  {
  bitmap bm = new bitmap(picturebox1.image);
  //robert邊緣檢測
  bm = robert(bm, iw, ih);
  picturebox2.refresh();
  picturebox2.image = bm;
  label2.location = new point(390, 280);
  label7.text = "roberts算子 銳化結果";
  }
 }
 
 //roberts算法
 public bitmap robert(bitmap bm, int iw, int ih)
 {
  int r, r0, r1, r2, r3, g, g0, g1, g2, g3, b, b0, b1, b2, b3;
  bitmap obm = new bitmap(picturebox1.image);
  int[,] inr = new int[iw, ih];//紅色分量矩陣
  int[,] ing = new int[iw, ih];//綠色分量矩陣
  int[,] inb = new int[iw, ih];//藍色分量矩陣
  int[,] gray = new int[iw, ih];//灰度圖像矩陣 
 
  for (int j = 1; j < ih - 1; j++)
  {
  for (int i = 1; i < iw - 1; i++)
  {
   r0 = (bm.getpixel(i, j)).r;
   r1 = (bm.getpixel(i, j + 1)).r;
   r2 = (bm.getpixel(i + 1, j)).r;
   r3 = (bm.getpixel(i + 1, j + 1)).r;
   
   r = (int)math.sqrt((r0 - r3) * (r0 - r3) + (r1 - r2) * (r1 - r2));
 
   g0 = (bm.getpixel(i, j)).g;
   g1 = (bm.getpixel(i, j + 1)).g;
   g2 = (bm.getpixel(i + 1, j)).g;
   g3 = (bm.getpixel(i + 1, j + 1)).g;
   g = (int)math.sqrt((g0 - g3) * (g0 - g3) + (g1 - g2) * (g1 - g2));
 
   b0 = (bm.getpixel(i, j)).b;
   b1 = (bm.getpixel(i, j + 1)).b;
   b2 = (bm.getpixel(i + 1, j)).b;
   b3 = (bm.getpixel(i + 1, j + 1)).b;
   b = (int)math.sqrt((b0 - b3) * (b0 - b3)
   + (b1 - b2) * (b1 - b2));
 
   if (r < 0)
   r = 0;     //黑色,邊緣點
   if (r > 255)
   r = 255;
 
   obm.setpixel(i, j, color.fromargb(r, r, r));
  }
  }
  return obm;
 }
 //sobel算子
 private void sobel算子銳化toolstripmenuitem_click(object sender, eventargs e)
 {
  if (bitmap != null)
  {
  bitmap bm = new bitmap(picturebox1.image);
  //5: sobel銳化
  bm = detect(bm, 256, 256, 5);
 
  picturebox2.refresh();
  picturebox2.image = bm;
 
  label7.text = " sobel算子 銳化結果";
  }
 }
 
 private void 低通濾波toolstripmenuitem_click(object sender, eventargs e)
 {
  if (bitmap != null)
  {
  bitmap bm = new bitmap(picturebox1.image);
  int num ;
  for (num = 1; num < 4; num++)
  {
   //低通濾波
   bm = lowpass(bm, iw, ih, num);
 
   picturebox2.refresh();
   picturebox2.image = bm;
 
   if (num == 1) label7.text = "1*5模板低通濾波結果";
   else if (num == 2) label7.text = "5*1模板低通濾波結果";
   else if (num == 3) label7.text = "5*5模板低通濾波結果";
  }
  }
 
 
 }
 //3×3低通濾波方法
 public bitmap lowpass(bitmap bm, int iw, int ih, int n)
 {
  bitmap obm = new bitmap(picturebox1.image);
  int[,] h;
 
  //定義擴展輸入圖像矩陣
  int[,] ex_inpix = exinpix(bm, iw, ih);
 
  //低通濾波
  for (int j = 1; j < ih + 1; j++)
  {
  for (int i = 1; i < iw + 1; i++)
  {
   int r = 0, sum = 0;
 
   //低通模板
   h = low_matrix(n);
 
   //求3×3窗口9個像素加權和
   for (int k = -1; k < 2; k++)
   for (int l = -1; l < 2; l++)
    sum = sum + h[k + 1, l + 1] * ex_inpix[i + k, j + l];
 
   if (n == 1)
   r = (int)(sum / 9); //h1平均值
   else if (n == 2)
   r = (int)(sum / 10); //h2
   else if (n == 3)
   r = (int)(sum / 16); //h3
   obm.setpixel(i - 1, j - 1, color.fromargb(r, r, r)); //輸出  
  }
  }
  return obm;
 }
 //定義擴展輸入圖像矩陣
 public int[,] exinpix(bitmap bm, int iw, int ih)
 {
  int[,] ex_inpix = new int[iw + 2, ih + 2];
  //獲取非邊界灰度值
  for (int j = 0; j < ih; j++)
  for (int i = 0; i < iw; i++)
   ex_inpix[i + 1, j + 1] = (bm.getpixel(i, j)).r;
  //四角點處理
  ex_inpix[0, 0] = ex_inpix[1, 1];
  ex_inpix[0, ih + 1] = ex_inpix[1, ih];
  ex_inpix[iw + 1, 0] = ex_inpix[iw, 1];
  ex_inpix[iw + 1, ih + 1] = ex_inpix[iw, ih];
  //上下邊界處理
  for (int j = 1; j < ih + 1; j++)
  {
  ex_inpix[0, j] = ex_inpix[1, j]; //上邊界
  ex_inpix[iw + 1, j] = ex_inpix[iw, j];//下邊界
  }
 
//左右邊界處理
  for (int i = 1; i < iw + 1; i++)
  {
  ex_inpix[i, 0] = ex_inpix[i, 1]; //左邊界
  ex_inpix[i, ih + 1] = ex_inpix[i, ih];//右邊界
  }
  return ex_inpix;
 }
 //低通濾波模板
 public int[,] low_matrix(int n)
 {
  int[,] h = new int[3, 3];
  if (n == 1) //h1
  {
  h[0, 0] = 1; h[0, 1] = 1; h[0, 2] = 1;
  h[1, 0] = 1; h[1, 1] = 1; h[1, 2] = 1;
  h[2, 0] = 1; h[2, 1] = 1; h[2, 2] = 1;
  }
  else if (n == 2)//h2
  {
  h[0, 0] = 1; h[0, 1] = 1; h[0, 2] = 1;
  h[1, 0] = 1; h[1, 1] = 2; h[1, 2] = 1;
  h[2, 0] = 1; h[2, 1] = 1; h[2, 2] = 1;
  }
  else if (n == 3)//h3
  {
  h[0, 0] = 1; h[0, 1] = 2; h[0, 2] = 1;
  h[1, 0] = 2; h[1, 1] = 4; h[1, 2] = 2;
  h[2, 0] = 1; h[2, 1] = 2; h[2, 2] = 1;
  }
  return h;
 }
 
 }
}

六、參考書籍

《c#數字圖像處理算法典型實例》

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持服務器之家。

原文鏈接:https://blog.csdn.net/Lynn_whu/article/details/80725831

延伸 · 閱讀

精彩推薦
  • C#深入解析C#中的交錯數組與隱式類型的數組

    深入解析C#中的交錯數組與隱式類型的數組

    這篇文章主要介紹了深入解析C#中的交錯數組與隱式類型的數組,隱式類型的數組通常與匿名類型以及對象初始值設定項和集合初始值設定項一起使用,需要的...

    C#教程網6172021-11-09
  • C#C#設計模式之Visitor訪問者模式解決長隆歡樂世界問題實例

    C#設計模式之Visitor訪問者模式解決長隆歡樂世界問題實例

    這篇文章主要介紹了C#設計模式之Visitor訪問者模式解決長隆歡樂世界問題,簡單描述了訪問者模式的定義并結合具體實例形式分析了C#使用訪問者模式解決長...

    GhostRider9502022-01-21
  • C#WPF 自定義雷達圖開發實例教程

    WPF 自定義雷達圖開發實例教程

    這篇文章主要介紹了WPF 自定義雷達圖開發實例教程,本文介紹的非常詳細,具有參考借鑒價值,需要的朋友可以參考下...

    WinterFish13112021-12-06
  • C#C#實現XML文件讀取

    C#實現XML文件讀取

    這篇文章主要為大家詳細介紹了C#實現XML文件讀取的相關代碼,具有一定的參考價值,感興趣的小伙伴們可以參考一下...

    Just_for_Myself6702022-02-22
  • C#C# 實現對PPT文檔加密、解密及重置密碼的操作方法

    C# 實現對PPT文檔加密、解密及重置密碼的操作方法

    這篇文章主要介紹了C# 實現對PPT文檔加密、解密及重置密碼的操作方法,非常不錯,具有參考借鑒價值,需要的朋友可以參考下...

    E-iceblue5012022-02-12
  • C#Unity3D實現虛擬按鈕控制人物移動效果

    Unity3D實現虛擬按鈕控制人物移動效果

    這篇文章主要為大家詳細介紹了Unity3D實現虛擬按鈕控制人物移動效果,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一...

    shenqingyu060520232410972022-03-11
  • C#C#通過KD樹進行距離最近點的查找

    C#通過KD樹進行距離最近點的查找

    這篇文章主要為大家詳細介紹了C#通過KD樹進行距離最近點的查找,具有一定的參考價值,感興趣的小伙伴們可以參考一下...

    帆帆帆6112022-01-22
  • C#C#裁剪,縮放,清晰度,水印處理操作示例

    C#裁剪,縮放,清晰度,水印處理操作示例

    這篇文章主要為大家詳細介紹了C#裁剪,縮放,清晰度,水印處理操作示例,具有一定的參考價值,感興趣的小伙伴們可以參考一下...

    吳 劍8332021-12-08
Weibo Article 1 Weibo Article 2 Weibo Article 3 Weibo Article 4 Weibo Article 5 Weibo Article 6 Weibo Article 7 Weibo Article 8 Weibo Article 9 Weibo Article 10 Weibo Article 11 Weibo Article 12 Weibo Article 13 Weibo Article 14 Weibo Article 15 Weibo Article 16 Weibo Article 17 Weibo Article 18 Weibo Article 19 Weibo Article 20 Weibo Article 21 Weibo Article 22 Weibo Article 23 Weibo Article 24 Weibo Article 25 Weibo Article 26 Weibo Article 27 Weibo Article 28 Weibo Article 29 Weibo Article 30 Weibo Article 31 Weibo Article 32 Weibo Article 33 Weibo Article 34 Weibo Article 35 Weibo Article 36 Weibo Article 37 Weibo Article 38 Weibo Article 39 Weibo Article 40
主站蜘蛛池模板: 日韩精品视频在线播放 | 午夜影晥 | 国产高清在线精品一区二区三区 | 久久99精品久久久久婷婷暖91 | 一区二区三区久久 | 美女网站黄视频 | 一区二区视频在线观看 | 久久女人网 | 久色91| 欧美国产精品一区二区 | 国产成人一区二区三区 | 中文字幕精品一区二区精品 | bxbx成人精品一区二区三区 | 国产欧美日韩综合精品一区二区 | 日韩在线不卡一区 | 亚洲欧洲精品成人久久奇米网 | 中文字幕影视 | 麻豆乱码国产一区二区三区 | 亚洲香蕉视频 | 国产原创精品视频 | 久久久性色精品国产免费观看 | 午夜在线视频 | 国产色综合视频 | 成年人视频在线观看免费 | 日韩在线播放一区二区三区 | 久久精品亚洲精品国产欧美kt∨ | 激情总合网 | 国产精品欧美日韩 | 久久av资源 | 久久一区 | 中文字幕亚洲专区 | 久久久精品网站 | 自拍视频网站 | 成人av免费在线播放 | 日本欧美在线 | 亚洲天堂中文字幕在线观看 | 成年网站| 美女久久久 | 欧美一级黄色片网站 | 欧美日韩一区二区在线 | 日韩精品在线免费视频 |