HistogramControl.cs 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511
  1. using System;
  2. using System.Drawing;
  3. using System.Drawing.Drawing2D;
  4. using System.Windows.Forms;
  5. using OpenCvSharp;
  6. namespace PaintDotNet.CustomControl
  7. {
  8. public partial class HistogramControl : UserControl
  9. {
  10. /// <summary>
  11. /// panel画板
  12. /// </summary>
  13. private PanelEx panelEx1;
  14. /// <summary>
  15. /// 控件的宽
  16. /// </summary>
  17. private int width;
  18. /// <summary>
  19. /// 控件的高
  20. /// </summary>
  21. private int height;
  22. /// <summary>
  23. /// 区间1的起止值
  24. /// </summary>
  25. private int start = 0, end = 0;
  26. /// <summary>
  27. /// 区间2的起止值
  28. /// </summary>
  29. private int start1 = 0, end1 = 0;
  30. /// <summary>
  31. /// 画笔
  32. /// </summary>
  33. private Pen pen = new Pen(Color.Red);
  34. /// <summary>
  35. /// 直方图的bitmap
  36. /// </summary>
  37. private Bitmap bitmap;
  38. /// <summary>
  39. /// 1个区间/2个区间
  40. /// </summary>
  41. private int flag = 1;
  42. /// <summary>
  43. /// 鼠标按下标志
  44. /// </summary>
  45. private bool press = false;
  46. /// <summary>
  47. /// 标记鼠标按下的时候在那根线上 1、2、3、4
  48. /// </summary>
  49. private int x = 0;
  50. /// <summary>
  51. /// 中间的
  52. /// </summary>
  53. private GraphicsPath path1, path2;
  54. public int Flag
  55. {
  56. get
  57. {
  58. return flag;
  59. }
  60. set
  61. {
  62. this.flag = value;
  63. this.panelEx1.Refresh();
  64. }
  65. }
  66. /// <summary>
  67. /// 公开的事件,第一条线拖拽的事件
  68. /// </summary>
  69. public event EventHandler<EventArgs<int>> DragOneEventActionFinish;
  70. private void OnDragOneEventActionFinish(int start)
  71. {
  72. if (DragOneEventActionFinish != null)
  73. {
  74. DragOneEventActionFinish(this, new EventArgs<int>(start));
  75. }
  76. }
  77. public event EventHandler<EventArgs<int>> DragTwoEventActionFinish;
  78. private void OnDragTwoEventActionFinish(int end)
  79. {
  80. if (DragTwoEventActionFinish != null)
  81. {
  82. DragTwoEventActionFinish(this, new EventArgs<int>(end));
  83. }
  84. }
  85. public event EventHandler<EventArgs<int>> DragThreeEventActionFinish;
  86. private void OnDragThreeEventActionFinish(int start1)
  87. {
  88. if (DragThreeEventActionFinish != null)
  89. {
  90. DragThreeEventActionFinish(this, new EventArgs<int>(start1));
  91. }
  92. }
  93. public event EventHandler<EventArgs<int>> DragFourEventActionFinish;
  94. private void OnDragFourEventActionFinish(int start1)
  95. {
  96. if (DragFourEventActionFinish != null)
  97. {
  98. DragFourEventActionFinish(this, new EventArgs<int>(start1));
  99. }
  100. }
  101. public event EventHandler<EventArgs<int[]>> DragFiveEventActionFinish;
  102. private void OnDragFiveEventActionFinish(int[] startend)
  103. {
  104. if (DragFiveEventActionFinish != null)
  105. {
  106. DragFiveEventActionFinish(this, new EventArgs<int[]>(startend));
  107. }
  108. }
  109. public event EventHandler<EventArgs<int[]>> DragSixEventActionFinish;
  110. private void OnDragSixEventActionFinish(int[] start1end1)
  111. {
  112. if (DragSixEventActionFinish != null)
  113. {
  114. DragSixEventActionFinish(this, new EventArgs<int[]>(start1end1));
  115. }
  116. }
  117. public HistogramControl()
  118. {
  119. InitializeComponent();
  120. this.panelEx1.Paint += new PaintEventHandler(this.panelEx_Paint);
  121. this.panelEx1.MouseDown += new MouseEventHandler(this.panelEx_MouseDown);
  122. this.panelEx1.MouseMove += new MouseEventHandler(this.panelEx_MouseMove);
  123. this.panelEx1.MouseUp += new MouseEventHandler(this.panelEx_MouseUp);
  124. }
  125. int oldx = 0;
  126. /// <summary>
  127. /// 鼠标按下事件,判断是否点在了线上
  128. /// </summary>
  129. /// <param name="sender"></param>
  130. /// <param name="e"></param>
  131. private void panelEx_MouseDown(object sender, MouseEventArgs e)
  132. {
  133. if (e.Button == MouseButtons.Left)
  134. {
  135. if (flag == 1 || flag == 2)
  136. {
  137. if ((e.X > start-5 && e.X < start + 5) || (e.X > end-5 && e.X < end + 5))
  138. {
  139. x = (e.X > end - 5 && e.X < end + 5) ? 2 : 1;
  140. press = true;
  141. }
  142. if(new Region(path1).IsVisible(e.X, e.Y))
  143. {
  144. x = 5;
  145. oldx = e.X;
  146. press = true;
  147. }
  148. }
  149. if (flag == 2)
  150. {
  151. if ((e.X > start1 - 5 && e.X < start1 + 5) || (e.X > end1 - 5 && e.X < end1 + 5))
  152. {
  153. x = (e.X > end1 - 5 && e.X < end1 + 5) ? 4 : 3;
  154. press = true;
  155. }
  156. if (new Region(path2).IsVisible(e.X, e.Y))
  157. {
  158. x = 6;
  159. oldx = e.X;
  160. press = true;
  161. }
  162. }
  163. }
  164. }
  165. /// <summary>
  166. /// 鼠标移动事件
  167. /// </summary>
  168. /// <param name="sender"></param>
  169. /// <param name="e"></param>
  170. private void panelEx_MouseMove(object sender, MouseEventArgs e)
  171. {
  172. if((e.X > start - 5 && e.X < start + 5) || (e.X > end - 5 && e.X < end + 5) || (e.X > start1 - 5 && e.X < start1 + 5) || (e.X > end1 - 5 && e.X < end1 + 5))
  173. {
  174. this.Cursor = Cursors.VSplit;
  175. }
  176. else
  177. {
  178. if ((path1!=null && new Region(path1).IsVisible(e.X, e.Y)))
  179. {
  180. this.Cursor = Cursors.Hand;
  181. }
  182. else if ((path2 != null && new Region(path2).IsVisible(e.X, e.Y)))
  183. {
  184. this.Cursor = Cursors.Hand;
  185. }
  186. else if (press)
  187. {
  188. this.Cursor = Cursors.VSplit;
  189. }
  190. else
  191. {
  192. this.Cursor = Cursors.Default;
  193. }
  194. }
  195. if(e.Button == MouseButtons.Left && press && e.X>=0 && e.X<=width)
  196. {
  197. switch(x)
  198. {
  199. case 1:
  200. this.start = e.X;
  201. if (this.start > this.end) this.start = this.end;
  202. break;
  203. case 2:
  204. this.end = e.X;
  205. if (this.end < this.start) this.end = this.start;
  206. break;
  207. case 3:
  208. this.start1 = e.X;
  209. if (this.start1 > this.end1) this.start1 = this.end1;
  210. break;
  211. case 4:
  212. this.end1 = e.X;
  213. if (this.end1 < this.start1) this.end1 = this.start1;
  214. break;
  215. case 5:
  216. this.start += -oldx + e.X;
  217. this.end += -oldx + e.X;
  218. if (this.start < 0) this.start = 0;
  219. if (this.end > width) this.end = width;
  220. break;
  221. case 6:
  222. this.start1 += -oldx + e.X;
  223. this.end1 += -oldx + e.X;
  224. if (this.start1 < 0) this.start1 = 0;
  225. if (this.end1 > width) this.end1 = width;
  226. break;
  227. }
  228. oldx = e.X;
  229. this.panelEx1.Refresh();
  230. }
  231. }
  232. /// <summary>
  233. /// 鼠标抬起事件
  234. /// </summary>
  235. /// <param name="sender"></param>
  236. /// <param name="e"></param>
  237. private void panelEx_MouseUp(object sender, MouseEventArgs e)
  238. {
  239. if (this.press)
  240. {
  241. switch(this.x)
  242. {
  243. case 1:
  244. OnDragOneEventActionFinish(Convert.ToInt32(this.start * 1f * 255 / width));
  245. break;
  246. case 2:
  247. OnDragTwoEventActionFinish(Convert.ToInt32(this.end * 1f * 255 / width));
  248. break;
  249. case 3:
  250. OnDragThreeEventActionFinish(Convert.ToInt32(this.start1 * 1f * 255 / width));
  251. break;
  252. case 4:
  253. OnDragFourEventActionFinish(Convert.ToInt32(this.end1 * 1f * 255 / width));
  254. break;
  255. case 5:
  256. OnDragFiveEventActionFinish(new int[] { Convert.ToInt32(this.end * 1f * 255 / width), Convert.ToInt32(this.start * 1f * 255 / width) });
  257. //OnDragTwoEventActionFinish(Convert.ToInt32(this.end * 1f * 255 / width));
  258. //OnDragOneEventActionFinish(Convert.ToInt32(this.start * 1f * 255 / width));
  259. break;
  260. case 6:
  261. OnDragSixEventActionFinish(new int[] { Convert.ToInt32(this.end1 * 1f * 255 / width), Convert.ToInt32(this.start1 * 1f * 255 / width) });
  262. //OnDragFourEventActionFinish(Convert.ToInt32(this.end1 * 1f * 255 / width));
  263. //OnDragThreeEventActionFinish(Convert.ToInt32(this.start1 * 1f * 255 / width));
  264. break;
  265. }
  266. }
  267. this.press = false;
  268. this.x = 0;
  269. }
  270. /// <summary>
  271. /// 绘制事件
  272. /// </summary>
  273. /// <param name="sender"></param>
  274. /// <param name="e"></param>
  275. private void panelEx_Paint(object sender, PaintEventArgs e)
  276. {
  277. if (bitmap != null)
  278. {
  279. e.Graphics.DrawImage(bitmap, 0, 0, width, height);
  280. }
  281. if(flag==1 || flag==2)
  282. {
  283. e.Graphics.DrawLine(pen, new PointF(start, 0), new PointF(start, height));
  284. e.Graphics.DrawLine(pen, new PointF(end, 0), new PointF(end, height));
  285. DrawHandle(start, end, e.Graphics, 1);
  286. }
  287. if (flag == 2)
  288. {
  289. e.Graphics.DrawLine(pen, new PointF(start1, 0), new PointF(start1, height));
  290. e.Graphics.DrawLine(pen, new PointF(end1, 0), new PointF(end1, height));
  291. DrawHandle(start1, end1, e.Graphics, 2);
  292. }
  293. }
  294. public void UpdateVerticalBarWithOneScope(int start, int end)
  295. {
  296. this.flag = 1;
  297. this.start = Convert.ToInt32(start * width * 1f / 255);
  298. this.end = Convert.ToInt32(end * width * 1f / 255);
  299. this.panelEx1.Refresh();
  300. }
  301. public void UpdateVerticalBarWithTwoScope(int start, int end, int start1, int end1)
  302. {
  303. this.flag = 2;
  304. this.start = Convert.ToInt32(start * width * 1f / 255);
  305. this.end = Convert.ToInt32(end * width * 1f / 255);
  306. this.start1 = Convert.ToInt32(start1 * width * 1f / 255);
  307. this.end1 = Convert.ToInt32(end1 * width * 1f / 255);
  308. this.panelEx1.Refresh();
  309. }
  310. private void InitializeComponent()
  311. {
  312. this.panelEx1 = new PaintDotNet.PanelEx();
  313. this.SuspendLayout();
  314. //
  315. // panelEx1
  316. //
  317. this.panelEx1.BackColor = System.Drawing.SystemColors.ActiveBorder;
  318. this.panelEx1.Dock = System.Windows.Forms.DockStyle.Fill;
  319. this.panelEx1.HideHScroll = false;
  320. this.panelEx1.HideVScroll = false;
  321. this.panelEx1.IgnoreSetFocus = false;
  322. this.panelEx1.Location = new System.Drawing.Point(0, 0);
  323. this.panelEx1.Margin = new System.Windows.Forms.Padding(0);
  324. this.panelEx1.Name = "panelEx1";
  325. this.panelEx1.ScrollPosition = new System.Drawing.Point(0, 0);
  326. this.panelEx1.Size = new System.Drawing.Size(255, 150);
  327. this.panelEx1.TabIndex = 0;
  328. //
  329. // HistogramControl
  330. //
  331. this.Controls.Add(this.panelEx1);
  332. this.Name = "HistogramControl";
  333. this.Size = new System.Drawing.Size(255, 150);
  334. this.ResumeLayout(false);
  335. }
  336. /// <summary>
  337. /// 绘制直方图
  338. /// </summary>
  339. /// <param name="imageMat">imageMat</param>
  340. /// <param name="isGray">是否绘制单通道灰度图</param>
  341. /// <param name="width">需要绘制的直方图宽度</param>
  342. /// <param name="height">需要绘制的直方图高度</param>
  343. /// <param name="channel">isGray为false的时候生效,绘制RGB三通道的直方图的时候,0全部绘制,1B,2G,3R</param>
  344. public void CreateHistogram(OpenCvSharp.Mat imageMat, bool isGray, int width, int height, int channel)
  345. {
  346. this.width = width;
  347. this.height = height;
  348. //Mat mat = OpenCvSharp.Extensions.BitmapConverter.ToMat(bitmap);
  349. if (isGray)
  350. {
  351. this.DrawGrayHistogram(imageMat.CvtColor(ColorConversionCodes.BGRA2GRAY));
  352. }
  353. else
  354. {
  355. this.DrawRGBHistogram(imageMat.Clone(), channel);
  356. }
  357. }
  358. private void DrawGrayHistogram(Mat mat)
  359. {
  360. //配置输出的结果存储的 空间 ,用MatND类型来存储结果
  361. Mat hist = new Mat();
  362. //设置计算直方图的维度
  363. int dims = 1;
  364. //直方图的每一个维度的柱条的数目(就是将数值分组,共有多少组)
  365. int[] histSize = { 255 };
  366. Rangef[] pranges = new Rangef[1];//一个通道,范围
  367. pranges[0] = new Rangef(0.0F, 256.0F);
  368. //6--计算直方图
  369. Cv2.CalcHist(new Mat[1] { mat }, new int[] { 0 }, new Mat(), hist, dims, histSize, pranges);
  370. int hist_w = width;
  371. int hist_h = height;
  372. int nHistSize = 256;
  373. double bin_w = (double)hist_w / nHistSize; //区间
  374. Mat histImage = new Mat(hist_h, hist_w, MatType.CV_8UC3, Scalar.All(255));//创建一个黑底的图像,为了可以显示彩色,所以该绘制图像是一个8位的3通道图像
  375. Cv2.Normalize(hist, hist, 0, histImage.Rows-10, NormTypes.MinMax, -1, new Mat());
  376. this.bitmap = OpenCvSharp.Extensions.BitmapConverter.ToBitmap(histImage);
  377. Graphics graphics = Graphics.FromImage(this.bitmap);
  378. //在直方图画布上画出直方图
  379. for (int i = 1; i < nHistSize-1; i++)
  380. {
  381. System.Drawing.PointF point1 = new System.Drawing.PointF((float)(bin_w * (i - 1)), (float)(hist_h - 5 - Math.Round(hist.At<float>(i - 1))));
  382. System.Drawing.PointF point2 = new System.Drawing.PointF((float)(bin_w * (i)), (float)(hist_h - 2 - Math.Round(hist.At<float>(i))));
  383. graphics.SmoothingMode = SmoothingMode.AntiAlias;
  384. graphics.DrawLine(new Pen(Color.FromArgb(100, 100, 100)), point1, point2);
  385. //Cv2.Line(histImage, point1, point2, new Scalar(100, 100, 100), 1, LineTypes.AntiAlias, 0);
  386. /*OpenCvSharp.Point point1 = new OpenCvSharp.Point(bin_w * (i - 1), hist_h - 5 - Math.Round(hist.At<float>(i - 1)));
  387. OpenCvSharp.Point point2 = new OpenCvSharp.Point(bin_w * (i), hist_h - 2 - Math.Round(hist.At<float>(i)));
  388. Cv2.Line(histImage, point1, point2, new Scalar(100, 100, 100), 1, LineTypes.AntiAlias, 0);*/
  389. }
  390. //this.bitmap = OpenCvSharp.Extensions.BitmapConverter.ToBitmap(histImage);
  391. this.panelEx1.Refresh();
  392. }
  393. private void DrawRGBHistogram(Mat mat, int channel)
  394. {
  395. Mat[] src = mat.Split();
  396. //配置输出的结果存储的 空间 ,用MatND类型来存储结果
  397. Mat[] hists = new Mat[] { new Mat(), new Mat(), new Mat() };
  398. //设置计算直方图的维度
  399. int dims = 1;
  400. //直方图的每一个维度的柱条的数目(就是将数值分组,共有多少组)
  401. int[] histSize = { 255 };
  402. Rangef[] pranges = new Rangef[1];//一个通道,范围
  403. pranges[0] = new Rangef(0.0F, 256.0F);
  404. //6--计算直方图
  405. Cv2.CalcHist(new Mat[] { src[0] }, new int[] { 0 }, new Mat(), hists[0], dims, histSize, pranges);
  406. Cv2.CalcHist(new Mat[] { src[1] }, new int[] { 0 }, new Mat(), hists[1], dims, histSize, pranges);
  407. Cv2.CalcHist(new Mat[] { src[2] }, new int[] { 0 }, new Mat(), hists[2], dims, histSize, pranges);
  408. int hist_w = width;
  409. int hist_h = height;
  410. int nHistSize = 256;
  411. double bin_w = (double)hist_w / nHistSize; //区间
  412. Mat histImage = new Mat(hist_h, hist_w, MatType.CV_8UC3, Scalar.All(255));//创建一个黑底的图像,为了可以显示彩色,所以该绘制图像是一个8位的3通道图像
  413. Cv2.Normalize(hists[0], hists[0], 0, histImage.Rows - 10, NormTypes.MinMax, -1, new Mat());
  414. Cv2.Normalize(hists[1], hists[1], 0, histImage.Rows - 10, NormTypes.MinMax, -1, new Mat());
  415. Cv2.Normalize(hists[2], hists[2], 0, histImage.Rows - 10, NormTypes.MinMax, -1, new Mat());
  416. //在直方图画布上画出直方图
  417. for (int i = 1; i < nHistSize-1; i++)
  418. {
  419. if(channel == 0 || channel == 3)
  420. Cv2.Line(histImage, new OpenCvSharp.Point(bin_w * (i - 1), hist_h - 5 - Math.Round(hists[0].At<float>(i - 1))), new OpenCvSharp.Point(bin_w * (i), hist_h - 5 - Math.Round(hists[0].At<float>(i))), new Scalar(255, 0, 0), 1, LineTypes.AntiAlias, 0);
  421. if(channel == 0 || channel == 2)
  422. Cv2.Line(histImage, new OpenCvSharp.Point(bin_w * (i - 1), hist_h - 5 - Math.Round(hists[1].At<float>(i - 1))), new OpenCvSharp.Point(bin_w * (i), hist_h - 5 - Math.Round(hists[1].At<float>(i))), new Scalar(0, 255, 0), 1, LineTypes.AntiAlias, 0);
  423. if(channel == 0 || channel == 1)
  424. Cv2.Line(histImage, new OpenCvSharp.Point(bin_w * (i - 1), hist_h - 5 - Math.Round(hists[2].At<float>(i - 1))), new OpenCvSharp.Point(bin_w * (i), hist_h - 5 - Math.Round(hists[2].At<float>(i))), new Scalar(0, 0, 255), 1, LineTypes.AntiAlias, 0);
  425. }
  426. this.bitmap = OpenCvSharp.Extensions.BitmapConverter.ToBitmap(histImage);
  427. this.panelEx1.Refresh();
  428. }
  429. /// <summary>
  430. /// 绘制直线间的形状
  431. /// </summary>
  432. /// <param name="s">x轴起点</param>
  433. /// <param name="e">x轴终点</param>
  434. /// <param name="g">Graphics</param>
  435. private void DrawHandle(int s, int e, Graphics g, int type)
  436. {
  437. float v4 = height / 4;
  438. float h4 = (e - s) / 4;
  439. float h2 = (e - s) / 2;
  440. {
  441. PointF p1 = new PointF(s + h4, height / 2);
  442. PointF p2 = new PointF(s + (h4 * 3), height / 2);
  443. PointF p3 = new PointF(s + h2, v4);
  444. PointF p4 = new PointF(s + h2, v4 * 3);
  445. g.DrawLine(pen, new PointF(s, height / 2), p1);
  446. g.DrawLine(pen, p2, new PointF(e, height / 2));
  447. PointF[] points = new PointF[4];
  448. points[0] = p1;
  449. points[1] = p3;
  450. points[2] = p2;
  451. points[3] = p4;
  452. g.DrawLine(pen, p1, p3);
  453. g.DrawLine(pen, p2, p3);
  454. g.DrawLine(pen, p1, p4);
  455. g.DrawLine(pen, p2, p4);
  456. if(type==1)
  457. {
  458. path1 = new GraphicsPath();
  459. path1.AddPolygon(points);
  460. }
  461. else if(type == 2)
  462. {
  463. path2 = new GraphicsPath();
  464. path2.AddPolygon(points);
  465. }
  466. }
  467. }
  468. }
  469. }