Form1.cs 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721
  1. using OpenCvSharp;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.ComponentModel;
  5. using System.Data;
  6. using System.Drawing;
  7. using System.Drawing.Imaging;
  8. using System.Linq;
  9. using System.Text;
  10. using System.Threading.Tasks;
  11. using System.Windows.Forms;
  12. //using OpenCvSharp;
  13. namespace NewSegDll
  14. {
  15. public partial class Form1 : Form
  16. {
  17. //External parameters
  18. public int[] m_aiTrackBarMethod3;
  19. public Mat m_MOrgImg;
  20. public Mat MGryImg;
  21. public Mat m_Mbinaryzation;
  22. public int m_iWhichBlur;
  23. public int m_iMethod3DoSeg = 0;
  24. //Internal parameters
  25. public Mat m_MShowImg = null;
  26. int m_iMaxV;
  27. int m_iMinV;
  28. Bitmap m_BMImage;
  29. public Form1()
  30. {
  31. InitializeComponent();
  32. button5.Enabled = false;
  33. button6.Enabled = false;
  34. this.Activated += new EventHandler(Form1_Activated);
  35. }
  36. protected void Form1_Activated(object sender, System.EventArgs e)
  37. {
  38. m_iMethod3DoSeg = 0;
  39. if(null == m_MShowImg)
  40. {
  41. m_MShowImg = new Mat();
  42. m_MShowImg = m_MOrgImg.Clone();
  43. //m_MOrgImg.CopyTo(m_MShowImg);
  44. }
  45. m_BMImage = OpenCvSharp.Extensions.BitmapConverter.ToBitmap(m_MShowImg);// new Bitmap(m_MShowImg.Width, m_MShowImg.Height, (int)m_MShowImg.Step(), PixelFormat.Format24bppRgb, m_MShowImg.Data);// new Bitmap((int)m_MShowImg.Cols, (int)m_MShowImg.Rows, (int)m_MShowImg.Step(), System.Drawing.Imaging.PixelFormat.Format24bppRgb, /*(System.IntPtr)*/m_MShowImg.Data);
  46. this.pictureBox1.Image = m_BMImage;
  47. trackBar1.Minimum = 0;
  48. trackBar1.Maximum = 255;
  49. trackBar1.LargeChange = 5;
  50. trackBar1.SmallChange = 1;
  51. trackBar2.Minimum = 0;
  52. trackBar2.Maximum = 255;
  53. trackBar2.LargeChange = 5;
  54. trackBar2.SmallChange = 1;
  55. trackBar3.Minimum = 0;
  56. trackBar3.Maximum = 20;
  57. trackBar3.LargeChange = 2;
  58. trackBar3.SmallChange = 1;
  59. trackBar4.Minimum = 0;
  60. trackBar4.Maximum = 20;
  61. trackBar4.LargeChange = 2;
  62. trackBar4.SmallChange = 1;
  63. trackBar5.Minimum = 0;
  64. trackBar5.Maximum = 10;
  65. trackBar5.LargeChange = 2;
  66. trackBar5.SmallChange = 1;
  67. trackBar6.Minimum = -10;
  68. trackBar6.Maximum = 20;
  69. trackBar6.LargeChange = 3;
  70. trackBar6.SmallChange = 1;
  71. trackBar1.Value = m_aiTrackBarMethod3[0];
  72. trackBar2.Value = m_aiTrackBarMethod3[1];
  73. trackBar3.Value = m_aiTrackBarMethod3[2];
  74. trackBar4.Value = m_aiTrackBarMethod3[3];
  75. if (trackBar5.Maximum >= m_aiTrackBarMethod3[4]) trackBar5.Value = m_aiTrackBarMethod3[4];
  76. trackBar6.Value = m_aiTrackBarMethod3[5];
  77. if (1 == m_iWhichBlur)
  78. {
  79. radioButton1.Checked = false;
  80. radioButton2.Checked = true;
  81. radioButton3.Checked = false;
  82. radioButton4.Checked = false;
  83. }
  84. else if (2 == m_iWhichBlur)
  85. {
  86. radioButton1.Checked = false;
  87. radioButton2.Checked = false;
  88. radioButton3.Checked = true;
  89. radioButton4.Checked = false;
  90. }
  91. else if (3 == m_iWhichBlur)
  92. {
  93. radioButton1.Checked = false;
  94. radioButton2.Checked = false;
  95. radioButton3.Checked = false;
  96. radioButton4.Checked = true;
  97. }
  98. else
  99. {
  100. radioButton1.Checked = true;
  101. radioButton2.Checked = false;
  102. radioButton3.Checked = false;
  103. radioButton4.Checked = false;
  104. }
  105. if(true == radioButton4.Checked)
  106. {
  107. trackBar4.Enabled = true;
  108. label4.ForeColor = System.Drawing.Color.Black;
  109. }
  110. else
  111. {
  112. trackBar4.Enabled = false;
  113. label4.ForeColor = System.Drawing.Color.LightGray;
  114. }
  115. this.Update();
  116. }
  117. private void trackBar1_Scroll(object sender, EventArgs e)
  118. {
  119. }
  120. private void Form1_Load(object sender, EventArgs e)
  121. {
  122. }
  123. private void label2_Click(object sender, EventArgs e)
  124. {
  125. }
  126. private void trackBar2_Scroll(object sender, EventArgs e)
  127. {
  128. }
  129. private void label3_Click(object sender, EventArgs e)
  130. {
  131. }
  132. private void trackBar3_Scroll(object sender, EventArgs e)
  133. {
  134. }
  135. private void button3_Click(object sender, EventArgs e)
  136. {
  137. m_aiTrackBarMethod3[0] = trackBar1.Value;
  138. m_aiTrackBarMethod3[1] = trackBar2.Value;
  139. m_aiTrackBarMethod3[2] = trackBar3.Value;
  140. m_aiTrackBarMethod3[3] = trackBar4.Value;
  141. m_aiTrackBarMethod3[4] = trackBar5.Value;
  142. m_aiTrackBarMethod3[5] = trackBar6.Value;
  143. Mat MBlur = new Mat();
  144. if (true == radioButton2.Checked)
  145. {
  146. m_iWhichBlur = 1;
  147. /*
  148. MatPointerMed = new Mat(m_MatRealZV.Size(), MatType.CV_32F);
  149. */
  150. int iLength = m_aiTrackBarMethod3[2] * 2 + 1;
  151. Cv2.MedianBlur(MGryImg, MBlur, iLength);
  152. }
  153. else if (true == radioButton3.Checked)
  154. {
  155. m_iWhichBlur = 2;
  156. int iLength = m_aiTrackBarMethod3[2] * 2 + 1;
  157. Cv2.Blur(MGryImg, MBlur, new OpenCvSharp.Size(iLength, iLength));
  158. }
  159. else if (true == radioButton4.Checked)
  160. {
  161. m_iWhichBlur = 3;
  162. /*
  163. MatPointerGau = new Mat(m_MatRealZV.Size(), MatType.CV_32F);
  164. */
  165. int iLength = m_aiTrackBarMethod3[2] * 2 + 1;
  166. double dSigma = m_aiTrackBarMethod3[3];
  167. Cv2.GaussianBlur(MGryImg, MBlur, new OpenCvSharp.Size(iLength, iLength), dSigma, dSigma);
  168. }
  169. else
  170. {
  171. m_iWhichBlur = 0;
  172. MBlur = MGryImg;
  173. //MGryImg.CopyTo(MBlur);
  174. }
  175. Mat mat_mean = new Mat(), mat_stddev = new Mat();
  176. double m, s, d;
  177. Cv2.MeanStdDev(MBlur, mat_mean, mat_stddev);
  178. m = mat_mean.At<double>(0, 0);
  179. s = mat_stddev.At<double>(0, 0);
  180. d = 0.2;
  181. double th;
  182. th = m + d * s;
  183. double dThr = Cv2.Threshold(MBlur, m_Mbinaryzation, th, 255, ThresholdTypes.Binary);
  184. //Cv2.BitwiseNot(m_Mbinaryzation, m_Mbinaryzation);
  185. int[] point = new int[2];
  186. m_MOrgImg.CopyTo(m_MShowImg);
  187. Vec3b redcol = new Vec3b(0, 0, 255);
  188. int Rows = m_Mbinaryzation.Rows;
  189. int Cols = m_Mbinaryzation.Cols;
  190. for (point[0] = 0; point[0] < Rows; point[0]++)
  191. {
  192. for (point[1] = 0; point[1] < Cols; point[1]++)
  193. {
  194. if (1 > m_Mbinaryzation.At<Byte>(point[0], point[1]))
  195. {
  196. //Vec3b colTemp = m_MShowImg.At<Vec3b>(point);
  197. //colTemp.Item2 = 255;
  198. //m_MShowImg.Set<Vec3b>(point, colTemp);
  199. m_MShowImg.Set<Vec3b>(point, redcol);
  200. }
  201. }
  202. }
  203. m_BMImage = OpenCvSharp.Extensions.BitmapConverter.ToBitmap(m_MShowImg);// new Bitmap((int)m_MShowImg.Cols, (int)m_MShowImg.Rows, (int)m_MShowImg.Step(), System.Drawing.Imaging.PixelFormat.Format24bppRgb, (System.IntPtr)m_MShowImg.Data);
  204. this.pictureBox1.Image = m_BMImage;
  205. button5.Enabled = true;
  206. button6.Enabled = true;
  207. this.Update();
  208. }
  209. private void button1_Click(object sender, EventArgs e)
  210. {
  211. m_aiTrackBarMethod3[0] = trackBar1.Value;
  212. m_aiTrackBarMethod3[1] = trackBar2.Value;
  213. m_aiTrackBarMethod3[2] = trackBar3.Value;
  214. m_aiTrackBarMethod3[3] = trackBar4.Value;
  215. m_aiTrackBarMethod3[4] = trackBar5.Value;
  216. m_aiTrackBarMethod3[5] = trackBar6.Value;
  217. Mat MBlur = new Mat();
  218. if (true == radioButton2.Checked)
  219. {
  220. m_iWhichBlur = 1;
  221. /*
  222. MatPointerMed = new Mat(m_MatRealZV.Size(), MatType.CV_32F);
  223. */
  224. int iLength = m_aiTrackBarMethod3[2] * 2 + 1;
  225. Cv2.MedianBlur(MGryImg, MBlur, iLength);
  226. }
  227. else if (true == radioButton3.Checked)
  228. {
  229. m_iWhichBlur = 2;
  230. int iLength = m_aiTrackBarMethod3[2] * 2 + 1;
  231. Cv2.Blur(MGryImg, MBlur, new OpenCvSharp.Size(iLength, iLength));
  232. }
  233. else if (true == radioButton4.Checked)
  234. {
  235. m_iWhichBlur = 3;
  236. /*
  237. MatPointerGau = new Mat(m_MatRealZV.Size(), MatType.CV_32F);
  238. */
  239. int iLength = m_aiTrackBarMethod3[2] * 2 + 1;
  240. double dSigma = m_aiTrackBarMethod3[3];
  241. Cv2.GaussianBlur(MGryImg, MBlur, new OpenCvSharp.Size(iLength, iLength), dSigma, dSigma);
  242. }
  243. else
  244. {
  245. m_iWhichBlur = 0;
  246. MBlur = MGryImg;
  247. //MGryImg.CopyTo(MBlur);
  248. }
  249. double dThr = Cv2.Threshold(MBlur, m_Mbinaryzation, 0, 255, ThresholdTypes.Otsu);
  250. //Cv2.BitwiseNot(m_Mbinaryzation, m_Mbinaryzation);
  251. int[] point = new int[2];
  252. m_MOrgImg.CopyTo(m_MShowImg);
  253. Vec3b redcol = new Vec3b(0, 0, 255);
  254. int Rows = m_Mbinaryzation.Rows;
  255. int Cols = m_Mbinaryzation.Cols;
  256. for (point[0] = 0; point[0] < Rows; point[0]++)
  257. {
  258. for (point[1] = 0; point[1] < Cols; point[1]++)
  259. {
  260. if (1 > m_Mbinaryzation.At<Byte>(point[0], point[1]))
  261. {
  262. //Vec3b colTemp = m_MShowImg.At<Vec3b>(point);
  263. //colTemp.Item2 = 255;
  264. //m_MShowImg.Set<Vec3b>(point, colTemp);
  265. m_MShowImg.Set<Vec3b>(point, redcol);
  266. }
  267. }
  268. }
  269. m_BMImage = OpenCvSharp.Extensions.BitmapConverter.ToBitmap(m_MShowImg);// new Bitmap((int)m_MShowImg.Cols, (int)m_MShowImg.Rows, (int)m_MShowImg.Step(), System.Drawing.Imaging.PixelFormat.Format24bppRgb, (System.IntPtr)m_MShowImg.Data);
  270. this.pictureBox1.Image = m_BMImage;
  271. button5.Enabled = true;
  272. button6.Enabled = true;
  273. this.Update();
  274. }
  275. private void radioButton1_CheckedChanged(object sender, EventArgs e)
  276. {
  277. if (true == radioButton4.Checked)
  278. {
  279. trackBar4.Enabled = true;
  280. label4.ForeColor = System.Drawing.Color.Black;
  281. }
  282. else
  283. {
  284. trackBar4.Enabled = false;
  285. label4.ForeColor = System.Drawing.Color.LightGray;
  286. }
  287. }
  288. private void radioButton2_CheckedChanged(object sender, EventArgs e)
  289. {
  290. if (true == radioButton4.Checked)
  291. {
  292. trackBar4.Enabled = true;
  293. label4.ForeColor = System.Drawing.Color.Black;
  294. }
  295. else
  296. {
  297. trackBar4.Enabled = false;
  298. label4.ForeColor = System.Drawing.Color.LightGray;
  299. }
  300. }
  301. private void pictureBox1_Click(object sender, EventArgs e)
  302. {
  303. }
  304. private void radioButton3_CheckedChanged(object sender, EventArgs e)
  305. {
  306. if (true == radioButton4.Checked)
  307. {
  308. trackBar4.Enabled = true;
  309. label4.ForeColor = System.Drawing.Color.Black;
  310. }
  311. else
  312. {
  313. trackBar4.Enabled = false;
  314. label4.ForeColor = System.Drawing.Color.LightGray;
  315. }
  316. }
  317. private void radioButton4_CheckedChanged(object sender, EventArgs e)
  318. {
  319. if (true == radioButton4.Checked)
  320. {
  321. trackBar4.Enabled = true;
  322. label4.ForeColor = System.Drawing.Color.Black;
  323. }
  324. else
  325. {
  326. trackBar4.Enabled = false;
  327. label4.ForeColor = System.Drawing.Color.LightGray;
  328. }
  329. }
  330. private void button4_Click(object sender, EventArgs e)
  331. {
  332. m_aiTrackBarMethod3[0] = trackBar1.Value;
  333. m_aiTrackBarMethod3[1] = trackBar2.Value;
  334. m_aiTrackBarMethod3[2] = trackBar3.Value;
  335. m_aiTrackBarMethod3[3] = trackBar4.Value;
  336. m_aiTrackBarMethod3[4] = trackBar5.Value;
  337. m_aiTrackBarMethod3[5] = trackBar6.Value;
  338. Mat MBlur = new Mat();
  339. if (true == radioButton2.Checked)
  340. {
  341. m_iWhichBlur = 1;
  342. /*
  343. MatPointerMed = new Mat(m_MatRealZV.Size(), MatType.CV_32F);
  344. */
  345. int iLength = m_aiTrackBarMethod3[2] * 2 + 1;
  346. Cv2.MedianBlur(MGryImg, MBlur, iLength);
  347. }
  348. else if (true == radioButton3.Checked)
  349. {
  350. m_iWhichBlur = 2;
  351. int iLength = m_aiTrackBarMethod3[2] * 2 + 1;
  352. Cv2.Blur(MGryImg, MBlur, new OpenCvSharp.Size(iLength, iLength));
  353. }
  354. else if (true == radioButton4.Checked)
  355. {
  356. m_iWhichBlur = 3;
  357. /*
  358. MatPointerGau = new Mat(m_MatRealZV.Size(), MatType.CV_32F);
  359. */
  360. int iLength = m_aiTrackBarMethod3[2] * 2 + 1;
  361. double dSigma = m_aiTrackBarMethod3[3];
  362. Cv2.GaussianBlur(MGryImg, MBlur, new OpenCvSharp.Size(iLength, iLength), dSigma, dSigma);
  363. }
  364. else
  365. {
  366. m_iWhichBlur = 0;
  367. MBlur = MGryImg;
  368. //MGryImg.CopyTo(MBlur);
  369. }
  370. if (m_aiTrackBarMethod3[0] > m_aiTrackBarMethod3[1])
  371. {
  372. int iTemp = m_aiTrackBarMethod3[0];
  373. m_aiTrackBarMethod3[0] = m_aiTrackBarMethod3[1];
  374. m_aiTrackBarMethod3[1] = iTemp;
  375. trackBar1.Value = m_aiTrackBarMethod3[0];
  376. trackBar2.Value = m_aiTrackBarMethod3[1];
  377. }
  378. m_iMaxV = m_aiTrackBarMethod3[1];
  379. m_iMinV = m_aiTrackBarMethod3[0];
  380. Mat MTempMax, MTempMin;
  381. if(0 == m_iMinV)
  382. {
  383. MTempMin = new Mat(MBlur.Rows, MBlur.Cols, MatType.CV_8UC1, Scalar.All(0));
  384. }
  385. else
  386. {
  387. MTempMin = new Mat();
  388. Cv2.Threshold(MBlur, MTempMin, m_iMinV-1, 255, ThresholdTypes.BinaryInv);
  389. }
  390. if (255 == m_iMaxV)
  391. {
  392. MTempMax = new Mat(MBlur.Rows, MBlur.Cols, MatType.CV_8UC1, Scalar.All(0));
  393. }
  394. else
  395. {
  396. MTempMax = new Mat();
  397. Cv2.Threshold(MBlur, MTempMax, m_iMaxV, 255, ThresholdTypes.Binary);
  398. }
  399. m_Mbinaryzation = MTempMin | MTempMax;
  400. //Cv2.BitwiseNot(m_Mbinaryzation, m_Mbinaryzation);
  401. int[] point = new int[2];
  402. m_MShowImg = m_MOrgImg.Clone();
  403. //m_MOrgImg.CopyTo(m_MShowImg);
  404. Vec3b redcol = new Vec3b(0, 0, 255);
  405. int Rows = m_Mbinaryzation.Rows;
  406. int Cols = m_Mbinaryzation.Cols;
  407. for (point[0] = 0; point[0] < Rows; point[0]++)
  408. {
  409. for (point[1] = 0; point[1] < Cols; point[1]++)
  410. {
  411. if (1 > m_Mbinaryzation.At<Byte>(point[0], point[1]))
  412. {
  413. //Vec3b colTemp = m_MShowImg.At<Vec3b>(point);
  414. //colTemp.Item2 = 255;
  415. //m_MShowImg.Set<Vec3b>(point, colTemp);
  416. m_MShowImg.Set<Vec3b>(point, redcol);
  417. }
  418. }
  419. }
  420. m_BMImage = OpenCvSharp.Extensions.BitmapConverter.ToBitmap(m_MShowImg);// new Bitmap((int)m_MShowImg.Cols, (int)m_MShowImg.Rows, (int)m_MShowImg.Step(), System.Drawing.Imaging.PixelFormat.Format24bppRgb, (System.IntPtr)m_MShowImg.Data);
  421. this.pictureBox1.Image = m_BMImage;
  422. button5.Enabled = true;
  423. button6.Enabled = true;
  424. this.Update();
  425. }
  426. private void button2_Click(object sender, EventArgs e)
  427. {
  428. m_aiTrackBarMethod3[0] = trackBar1.Value;
  429. m_aiTrackBarMethod3[1] = trackBar2.Value;
  430. m_aiTrackBarMethod3[2] = trackBar3.Value;
  431. m_aiTrackBarMethod3[3] = trackBar4.Value;
  432. m_aiTrackBarMethod3[4] = trackBar5.Value;// 50;// m_aiTrackBarMethod3[4] + 1;// 10;// 9;//
  433. m_aiTrackBarMethod3[5] = trackBar6.Value;
  434. Mat MBlur = new Mat();
  435. if (true == radioButton2.Checked)
  436. {
  437. m_iWhichBlur = 1;
  438. /*
  439. MatPointerMed = new Mat(m_MatRealZV.Size(), MatType.CV_32F);
  440. */
  441. int iLength = m_aiTrackBarMethod3[2] * 2 + 1;
  442. Cv2.MedianBlur(MGryImg, MBlur, iLength);
  443. }
  444. else if (true == radioButton3.Checked)
  445. {
  446. m_iWhichBlur = 2;
  447. int iLength = m_aiTrackBarMethod3[2] * 2 + 1;
  448. Cv2.Blur(MGryImg, MBlur, new OpenCvSharp.Size(iLength, iLength));
  449. }
  450. else if (true == radioButton4.Checked)
  451. {
  452. m_iWhichBlur = 3;
  453. /*
  454. MatPointerGau = new Mat(m_MatRealZV.Size(), MatType.CV_32F);
  455. */
  456. int iLength = m_aiTrackBarMethod3[2] * 2 + 1;
  457. double dSigma = m_aiTrackBarMethod3[3];
  458. Cv2.GaussianBlur(MGryImg, MBlur, new OpenCvSharp.Size(iLength, iLength), dSigma, dSigma);
  459. }
  460. else
  461. {
  462. m_iWhichBlur = 0;
  463. MBlur = MGryImg;
  464. //MGryImg.CopyTo(MBlur);
  465. }
  466. int iAPLength = m_aiTrackBarMethod3[4] * 2 + 1;
  467. int iAPThreshold = m_aiTrackBarMethod3[5];
  468. Cv2.AdaptiveThreshold(MBlur, m_Mbinaryzation, 255, AdaptiveThresholdTypes.GaussianC, ThresholdTypes.Binary, iAPLength, iAPThreshold);
  469. OpenCvSharp.Size bianjieSize = m_Mbinaryzation.Size();
  470. Mat Temp = Mat.Zeros(bianjieSize.Height + 2, bianjieSize.Width + 2, m_Mbinaryzation.Type());//延展图像
  471. m_Mbinaryzation.CopyTo(Temp.SubMat(new Range(1, bianjieSize.Height + 1), new Range(1, bianjieSize.Width + 1)));
  472. Cv2.FloodFill(Temp, new OpenCvSharp.Point(0, 0), new Scalar(255));
  473. Mat cutImg = new Mat();
  474. Temp.SubMat(new Range(1, bianjieSize.Height + 1), new Range(1, bianjieSize.Width + 1)).CopyTo(cutImg);
  475. Cv2.BitwiseNot(cutImg, cutImg);
  476. m_Mbinaryzation = m_Mbinaryzation | cutImg;
  477. //Cv2.BitwiseNot(m_Mbinaryzation, m_Mbinaryzation);
  478. int[] point = new int[2];
  479. m_MOrgImg.CopyTo(m_MShowImg);
  480. Vec3b redcol = new Vec3b(0, 0, 255);
  481. int Rows = m_Mbinaryzation.Rows;
  482. int Cols = m_Mbinaryzation.Cols;
  483. for (point[0] = 0; point[0] < Rows; point[0]++)
  484. {
  485. for (point[1] = 0; point[1] < Cols; point[1]++)
  486. {
  487. if (1 > m_Mbinaryzation.At<Byte>(point[0], point[1]))
  488. {
  489. //Vec3b colTemp = m_MShowImg.At<Vec3b>(point);
  490. //colTemp.Item2 = 255;
  491. //m_MShowImg.Set<Vec3b>(point, colTemp);
  492. m_MShowImg.Set<Vec3b>(point, redcol);
  493. }
  494. }
  495. }
  496. m_BMImage = OpenCvSharp.Extensions.BitmapConverter.ToBitmap(m_MShowImg);// new Bitmap((int)m_MShowImg.Cols, (int)m_MShowImg.Rows, (int)m_MShowImg.Step(), System.Drawing.Imaging.PixelFormat.Format24bppRgb, (System.IntPtr)m_MShowImg.Data);
  497. this.pictureBox1.Image = m_BMImage;
  498. button5.Enabled = true;
  499. button6.Enabled = true;
  500. this.Update();
  501. }
  502. private void button5_Click(object sender, EventArgs e)
  503. {
  504. m_aiTrackBarMethod3[0] = trackBar1.Value;
  505. m_aiTrackBarMethod3[1] = trackBar2.Value;
  506. m_aiTrackBarMethod3[2] = trackBar3.Value;
  507. m_aiTrackBarMethod3[3] = trackBar4.Value;
  508. m_aiTrackBarMethod3[4] = trackBar5.Value;
  509. m_aiTrackBarMethod3[5] = trackBar6.Value;
  510. if (true == radioButton2.Checked)
  511. {
  512. m_iWhichBlur = 1;
  513. }
  514. else if (true == radioButton3.Checked)
  515. {
  516. m_iWhichBlur = 2;
  517. }
  518. else if (true == radioButton4.Checked)
  519. {
  520. m_iWhichBlur = 3;
  521. }
  522. else
  523. {
  524. m_iWhichBlur = 0;
  525. }
  526. OpenCvSharp.Size bianjieSize = m_Mbinaryzation.Size();
  527. Mat Temp = Mat.Zeros(bianjieSize.Height + 2, bianjieSize.Width + 2, m_Mbinaryzation.Type());//延展图像
  528. Mat imageMask = new Mat(m_Mbinaryzation.Size(), MatType.CV_8UC1, new Scalar(255));
  529. OpenCvSharp.Mat posSrc = new OpenCvSharp.Mat(Temp, new OpenCvSharp.Rect(1, 1, bianjieSize.Width + 1, bianjieSize.Height + 1));
  530. m_Mbinaryzation.CopyTo(posSrc, imageMask);//原图像复制到新图像
  531. //m_Mbinaryzation.CopyTo( Temp.SubMat(new Range(1, bianjieSize.Height + 1), new Range(1, bianjieSize.Width + 1)) );
  532. Cv2.FloodFill(Temp, new OpenCvSharp.Point(0, 0), new Scalar(255));
  533. Mat cutImg = new Mat();
  534. //Mat imageMask = new Mat(oldmat.Size(), MatType.CV_8UC1, new Scalar(0));
  535. //OpenCvSharp.Point[][] arr = new OpenCvSharp.Point[1][];
  536. //arr[0] = new OpenCvSharp.Point[] { new OpenCvSharp.Point(rectangle.X, rectangle.Y), new OpenCvSharp.Point(rectangle.X + rectangle.Width, rectangle.Y), new OpenCvSharp.Point(rectangle.X + rectangle.Width, rectangle.Y + rectangle.Height), new OpenCvSharp.Point(rectangle.X, rectangle.Y + rectangle.Height) };
  537. //Cv2.FillPoly(imageMask, arr, new Scalar(255));
  538. //OpenCvSharp.Mat posSrc = new OpenCvSharp.Mat(result, new OpenCvSharp.Rect(newOriX, newOriY, matNew.Width, matNew.Height));
  539. //tempMatSrc.CopyTo(posSrc, mask);//原图像复制到新图像
  540. cutImg = Temp.SubMat(new Range(1, bianjieSize.Height + 1), new Range(1, bianjieSize.Width + 1)).Clone();
  541. //Temp.SubMat(new Range(1, bianjieSize.Height + 1), new Range(1, bianjieSize.Width + 1)).CopyTo(cutImg);
  542. Cv2.BitwiseNot(cutImg, cutImg);
  543. m_Mbinaryzation = m_Mbinaryzation | cutImg;
  544. //Cv2.BitwiseNot(m_Mbinaryzation, m_Mbinaryzation);
  545. int[] point = new int[2];
  546. m_MOrgImg.CopyTo(m_MShowImg);
  547. Vec3b redcol = new Vec3b(0, 0, 255);
  548. int Rows = m_Mbinaryzation.Rows;
  549. int Cols = m_Mbinaryzation.Cols;
  550. for (point[0] = 0; point[0] < Rows; point[0]++)
  551. {
  552. for (point[1] = 0; point[1] < Cols; point[1]++)
  553. {
  554. if (1 > m_Mbinaryzation.At<Byte>(point[0], point[1]))
  555. {
  556. //Vec3b colTemp = m_MShowImg.At<Vec3b>(point);
  557. //colTemp.Item2 = 255;
  558. //m_MShowImg.Set<Vec3b>(point, colTemp);
  559. m_MShowImg.Set<Vec3b>(point, redcol);
  560. }
  561. }
  562. }
  563. m_BMImage = OpenCvSharp.Extensions.BitmapConverter.ToBitmap(m_MShowImg);// new Bitmap((int)m_MShowImg.Cols, (int)m_MShowImg.Rows, (int)m_MShowImg.Step(), System.Drawing.Imaging.PixelFormat.Format24bppRgb, (System.IntPtr)m_MShowImg.Data);
  564. this.pictureBox1.Image = m_BMImage;
  565. this.Update();
  566. }
  567. public NewSeg NSeg;
  568. private void button6_Click(object sender, EventArgs e)
  569. {
  570. //m_aiTrackBarMethod3[0] = trackBar1.Value;
  571. //m_aiTrackBarMethod3[1] = trackBar2.Value;
  572. //m_aiTrackBarMethod3[2] = trackBar3.Value;
  573. //m_aiTrackBarMethod3[3] = trackBar4.Value;
  574. //m_aiTrackBarMethod3[4] = trackBar5.Value;
  575. //m_aiTrackBarMethod3[5] = trackBar6.Value;
  576. //if (true == radioButton2.Checked)
  577. //{
  578. // m_iWhichBlur = 1;
  579. //}
  580. //else if (true == radioButton3.Checked)
  581. //{
  582. // m_iWhichBlur = 2;
  583. //}
  584. //else if (true == radioButton4.Checked)
  585. //{
  586. // m_iWhichBlur = 3;
  587. //}
  588. //else
  589. //{
  590. // m_iWhichBlur = 0;
  591. //}
  592. //m_iMethod3DoSeg = 1;
  593. //m_BMImage = OpenCvSharp.Extensions.BitmapConverter.ToBitmap(m_MShowImg);// new Bitmap((int)m_MShowImg.Cols, (int)m_MShowImg.Rows, (int)m_MShowImg.Step(), System.Drawing.Imaging.PixelFormat.Format24bppRgb, (System.IntPtr)m_MShowImg.Data);
  594. //this.pictureBox1.Image = m_BMImage;
  595. //this.Update();
  596. m_aiTrackBarMethod3[0] = trackBar1.Value;
  597. m_aiTrackBarMethod3[1] = trackBar2.Value;
  598. m_aiTrackBarMethod3[2] = trackBar3.Value;
  599. m_aiTrackBarMethod3[3] = trackBar4.Value;
  600. m_aiTrackBarMethod3[4] = trackBar5.Value;
  601. m_aiTrackBarMethod3[5] = trackBar6.Value;
  602. if (true == radioButton2.Checked)
  603. {
  604. m_iWhichBlur = 1;
  605. }
  606. else if (true == radioButton3.Checked)
  607. {
  608. m_iWhichBlur = 2;
  609. }
  610. else if (true == radioButton4.Checked)
  611. {
  612. m_iWhichBlur = 3;
  613. }
  614. else
  615. {
  616. m_iWhichBlur = 0;
  617. }
  618. m_iMethod3DoSeg = 1;
  619. this.NSeg.Method3DoSegButonClickAction();
  620. Mat watermark = this.NSeg.m_watermark;
  621. int Rows = watermark.Rows;
  622. int Cols = watermark.Cols;
  623. int[] point = new int[2];
  624. m_MOrgImg.CopyTo(m_MShowImg);
  625. Vec3b redcol = new Vec3b(255, 0, /*255*/0);
  626. for (point[0] = 0; point[0] < Rows; point[0]++)
  627. {
  628. for (point[1] = 0; point[1] < Cols; point[1]++)
  629. {
  630. if (0 >= watermark.At<int>(point[0], point[1]))
  631. {
  632. m_MShowImg.Set<Vec3b>(point, redcol);
  633. //m_MOutImg.Set<Vec3b>(point, redcol);
  634. }
  635. }
  636. }
  637. m_BMImage = OpenCvSharp.Extensions.BitmapConverter.ToBitmap(/*this.NSeg.m_MOutImg*/m_MShowImg);// new Bitmap((int)m_MShowImg.Cols, (int)m_MShowImg.Rows, (int)m_MShowImg.Step(), System.Drawing.Imaging.PixelFormat.Format24bppRgb, (System.IntPtr)m_MShowImg.Data);
  638. this.pictureBox1.Image = m_BMImage;
  639. this.Update();
  640. //this.Close();
  641. }
  642. private void trackBar5_Scroll(object sender, EventArgs e)
  643. {
  644. }
  645. }
  646. }