Class3.cs 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using OpenCvSharp;
  7. using System.Collections;
  8. namespace ceju
  9. {
  10. class BasFunction
  11. {
  12. /*
  13. * 摘要:
  14. * 数组矩阵求和。
  15. * 参数:
  16. * arr:
  17. * 输入二维数组
  18. * n:
  19. * n=1:对每列求和,得到行向量;
  20. * n=2:对每行求和,得到列向量;
  21. * n=3:对数组求和,得到和
  22. * 返回:
  23. * 一维数组,分别对应n从1到3时的行向量,列向量,和
  24. */
  25. public static int[] Sum(int[,] arr, int n)
  26. {
  27. int rows = arr.GetLength(0);
  28. int cols = arr.GetLength(1);
  29. int[] zero = new int[1] { 0 };
  30. switch (n)
  31. {
  32. case 1:
  33. int[] arrC = new int[cols];
  34. for (int j = 0; j < cols; j++)
  35. {
  36. arrC[j] = 0;
  37. for (int i = 0; i < rows; i++)
  38. {
  39. arrC[j] += arr[i, j];
  40. }
  41. }
  42. return arrC;
  43. break;
  44. case 2:
  45. int[] arrR = new int[rows];
  46. for (int i = 0; i < rows; i++)
  47. {
  48. arrR[i] = 0;
  49. for (int j = 0; j < cols; j++)
  50. {
  51. arrR[i] += arr[i, j];
  52. }
  53. }
  54. return arrR;
  55. break;
  56. case 3:
  57. int[] arrSum = new int[1] { 0 };
  58. for (int i = 0; i < rows; i++)
  59. {
  60. for (int j = 0; j < cols; j++)
  61. {
  62. arrSum[0] += arr[i, j];
  63. }
  64. }
  65. return arrSum;
  66. break;
  67. default:
  68. return zero;
  69. }
  70. }
  71. /*
  72. * 摘要:
  73. * 求数组和
  74. * 参数:
  75. * array:
  76. * 输入二维数组
  77. * sum:
  78. * 输出和
  79. */
  80. public static void Sum(double[,] array,out double sum)
  81. {
  82. int rows = array.GetLength(0);
  83. int cols = array.GetLength(1);
  84. sum = 0;
  85. for (int i = 0; i < rows; i++)
  86. {
  87. for (int j = 0; j < cols; j++)
  88. {
  89. sum += array[i, j];
  90. }
  91. }
  92. }
  93. /*
  94. * 一维数组求和
  95. */
  96. public static void Sum(int[] array, out int sum)
  97. {
  98. int length = array.Length;
  99. sum = 0;
  100. for (int i = 0; i < length; i++)
  101. {
  102. sum += array[i];
  103. }
  104. }
  105. /*
  106. * 摘要:
  107. * 求数组的最大值
  108. * 参数:
  109. * arr:
  110. * 输入一维数组
  111. * 返回:
  112. * 最大值
  113. */
  114. public static int Max(int[] arr)
  115. {
  116. int max = 0;
  117. for (int i = 0; i < arr.Length; i++)
  118. {
  119. if (max < arr[i])
  120. {
  121. max = arr[i];
  122. }
  123. }
  124. return max;
  125. }
  126. /*
  127. * 摘要:
  128. * 求数组的最小值
  129. * 参数:
  130. * array:
  131. * 输入一维数组
  132. * 返回:
  133. * 最小值
  134. */
  135. public static int Min(int[] array)
  136. {
  137. int min = array[0];
  138. for (int i = 0; i < array.Length; i++)
  139. {
  140. if (min > array[i])
  141. min = array[i];
  142. }
  143. return min;
  144. }
  145. // 摘要:
  146. // 将Mat类的数据存入二维数组中
  147. // 参数:
  148. // 输入Mat类图像
  149. // 返回:
  150. // 二维数组
  151. public static int[,] Mat2Array(Mat image)
  152. {
  153. //求行数和列数
  154. int rows = image.Rows;
  155. int cols = image.Cols;
  156. //将Mat类中的数据放到数组中
  157. int[,] arr = new int[rows, cols];
  158. for (int i = 0; i < rows; i++)
  159. {
  160. for (int j = 0; j < cols; j++)
  161. {
  162. arr[i, j] = image.Get<byte>(i, j);
  163. }
  164. }
  165. return arr;
  166. }
  167. /*
  168. * 摘要:
  169. * 截取一维数组中的某一段到新数组
  170. * 参数:
  171. * array:
  172. * 输入一维数组
  173. * begin:
  174. * 截取片段起始位置
  175. * end:
  176. * 截取片段结束位置
  177. * 返回:
  178. * begin和end之间的新数组
  179. */
  180. public static int[] Intercept(int[] array, int begin, int end)
  181. {
  182. //截取片段长度
  183. int length = end - begin + 1;
  184. //新数组
  185. int[] newArray = new int[length];
  186. for (int i = 0; i < length; i++)
  187. {
  188. newArray[i] = array[i + begin];
  189. }
  190. return newArray;
  191. }
  192. /*
  193. * 摘要:
  194. * 将0,255的二值数组转变为0,1的二值数组
  195. * 参数:
  196. * arr:
  197. * 输入二维数组
  198. * 返回:
  199. * 只有0,1的二维数组
  200. */
  201. public static int[,] ConversionRange(int[,] arr)
  202. {
  203. int[,] arrNew = arr;
  204. int rows = arr.GetLength(0);
  205. int cols = arr.GetLength(1);
  206. for (int i = 0; i < rows; i++)
  207. {
  208. for (int j = 0; j < cols; j++)
  209. {
  210. if(arr[i,j]>0)
  211. arrNew[i, j] = 1;
  212. }
  213. }
  214. return arrNew;
  215. }
  216. /*
  217. * 摘要:
  218. * 对一维数组进行和单个值之间的加减乘除操作
  219. * 参数:
  220. * array:
  221. * 输入一维数组
  222. * resul:
  223. * 输出结果
  224. * a:
  225. * 单个值
  226. * c:
  227. * 操作选项
  228. * ‘+’‘-’‘*’‘/’
  229. */
  230. public static void OperationArray(int[] array, out int[] result, int a, char c)
  231. {
  232. int length = array.Length;
  233. result = new int[length];
  234. switch (c)
  235. {
  236. case '+':
  237. for (int i = 0; i < length; i++)
  238. {
  239. result[i] = array[i] + a;
  240. }
  241. break;
  242. case '-':
  243. for (int i = 0; i < length; i++)
  244. {
  245. result[i] = array[i] - a;
  246. }
  247. break;
  248. case '*':
  249. for (int i = 0; i < length; i++)
  250. {
  251. result[i] = array[i] * a;
  252. }
  253. break;
  254. case '/':
  255. for (int i = 0; i < length; i++)
  256. {
  257. result[i] = array[i] / a;
  258. }
  259. break;
  260. default:
  261. break;
  262. }
  263. }
  264. /*
  265. * 摘要:
  266. * 选取一个数组中某一区间的数值
  267. * 参数:
  268. * array1:
  269. * 目标截取的数组
  270. * array2:
  271. * 保留数组
  272. * upperBound:
  273. * 上界
  274. * lowerBound:
  275. * 下界
  276. * leftBoundary:
  277. * 左界
  278. * rightBoundary:
  279. * 右界
  280. * 返回:
  281. * 返回截取某段区间复制下来之后的数组
  282. */
  283. public static int[,] InterceptArray(int[,] array1, int[,] array2, int upperBound, int lowerBound, int leftBoundary, int rightBoundary)
  284. {
  285. for (int i = upperBound; i < lowerBound; i++)
  286. {
  287. for (int j = leftBoundary; j < rightBoundary; j++)
  288. {
  289. array2[i, j] = array1[i, j];
  290. }
  291. }
  292. return array2;
  293. }
  294. /*
  295. * 摘要:
  296. * 选取某一个数减去一个数组中某一区间的数值
  297. * 参数:
  298. * array1:
  299. * 目标截取的数组
  300. * array2:
  301. * 保留数组
  302. * upperBound:
  303. * 上界
  304. * lowerBound:
  305. * 下界
  306. * leftBoundary:
  307. * 左界
  308. * rightBoundary:
  309. * 右界
  310. * minute:
  311. * 被减数
  312. * 返回:
  313. * 返回截取某段区间复制下来之后的数组
  314. */
  315. public static int[,] InterceptArray(int[,] array1, int[,] array2, int upperBound, int lowerBound, int leftBoundary, int rightBoundary, int minute)
  316. {
  317. for (int i = upperBound; i < lowerBound; i++)
  318. {
  319. for (int j = leftBoundary; j < rightBoundary; j++)
  320. {
  321. array2[i, j] = minute - array1[i, j];
  322. }
  323. }
  324. return array2;
  325. }
  326. /*
  327. * 摘要:
  328. * 截取数组
  329. * 参数:
  330. * array:
  331. * 输入二维数组
  332. * upperBound, lowerBound, leftBound, rightBound:
  333. * 输入上、下、左、右边界
  334. * 返回:
  335. * 截取后的数组,数组大小仅为截取区间大小
  336. */
  337. public static int[,] InterceptArray(int[,] array, int upperBound, int lowerBound, int leftBound, int rightBound)
  338. {
  339. int rows = lowerBound - upperBound;
  340. int cols = rightBound - leftBound;
  341. int[,] newArray = new int[rows, cols];
  342. for (int i = 0; i < rows; i++)
  343. {
  344. for (int j = 0; j < cols; j++)
  345. {
  346. newArray[i, j] = array[i + upperBound, j + leftBound];
  347. }
  348. }
  349. return newArray;
  350. }
  351. /*
  352. * 摘要:
  353. * 选取一个数组中某一区间的数值
  354. * 参数:
  355. * array1:
  356. * 目标截取的数组
  357. * array2:
  358. * 保留数组
  359. * upperBound:
  360. * 上界
  361. * lowerBound:
  362. * 下界
  363. * leftBoundary:
  364. * 左界
  365. * rightBoundary:
  366. * 右界
  367. * 返回:
  368. * 返回截取某段区间复制下来之后的数组
  369. */
  370. public static double[,] InterceptArray(double[,] array1, double[,] array2, int upperBound, int lowerBound, int leftBoundary, int rightBoundary)
  371. {
  372. for (int i = upperBound; i < lowerBound; i++)
  373. {
  374. for (int j = leftBoundary; j < rightBoundary; j++)
  375. {
  376. array2[i, j] = array1[i, j];
  377. }
  378. }
  379. return array2;
  380. }
  381. /*
  382. * 摘要:
  383. * 计算数组中不为0的个数
  384. * 参数:
  385. * array:
  386. * 输入一维数组
  387. * count:
  388. * 数量
  389. */
  390. public static void Count(int[] array, out int count)
  391. {
  392. count = 0;
  393. for (int i = 0; i < array.Length; i++)
  394. {
  395. if (array[i] > 0)
  396. {
  397. count++;
  398. }
  399. }
  400. }
  401. /*
  402. * 摘要:
  403. * 计算数组中不为0的个数
  404. * 参数:
  405. * array:
  406. * 输入二维数组
  407. * count:
  408. * 数量
  409. */
  410. public static void Count(int[,] array, out int count)
  411. {
  412. count = 0;
  413. //数组行列数
  414. int rows = array.GetLength(0);
  415. int cols = array.GetLength(1);
  416. for (int i = 0; i < rows; i++)
  417. {
  418. for(int j = 0;j<cols;j++)
  419. {
  420. if (array[i,j] > 0)
  421. {
  422. count++;
  423. }
  424. }
  425. }
  426. }
  427. /*
  428. * 摘要:
  429. * 计算数组中不为0的个数
  430. * 参数:
  431. * array:
  432. * 输入二维数组
  433. * count:
  434. * 数量
  435. */
  436. public static void Count(double[,] array, out int count)
  437. {
  438. count = 0;
  439. //数组行列数
  440. int rows = array.GetLength(0);
  441. int cols = array.GetLength(1);
  442. for (int i = 0; i < rows; i++)
  443. {
  444. for (int j = 0; j < cols; j++)
  445. {
  446. if (array[i, j] > 0)
  447. {
  448. count++;
  449. }
  450. }
  451. }
  452. }
  453. /*
  454. * 摘要:
  455. * 计算Mat中不为0的个数
  456. * 参数:
  457. * array:
  458. * 输入二维数组
  459. * count:
  460. * 数量
  461. */
  462. public static void Count(Mat image, out int count)
  463. {
  464. count = 0;
  465. for (int i = 0; i < image.Rows; i++)
  466. {
  467. for (int j = 0; j < image.Cols; j++)
  468. {
  469. if (image.Get<int>(i, j) > 0)
  470. count++;
  471. }
  472. }
  473. }
  474. /*
  475. * 摘要:
  476. * 计算两点之间的距离
  477. * 参数:
  478. * a:
  479. * 记录横坐标与纵坐标的数组
  480. * b:
  481. * 记录横坐标与纵坐标的数组
  482. * (两数组相同索引值的坐标类型要相同)
  483. * resul:
  484. * 输出的距离结果
  485. */
  486. public static void Distance(double[] a, double[] b, out double result)
  487. {
  488. result = Math.Sqrt(Math.Pow((a[0] - b[0]), 2) + Math.Pow((a[1] - b[1]), 2));
  489. }
  490. /*
  491. * 摘要:
  492. * 得到数组中大于0的点的位置
  493. * 参数:
  494. * array:
  495. * 输入二维数组
  496. * arrayNew:
  497. * 输出的坐标数组
  498. * 第一列是纵坐标(行数),第二列是横坐标(列数)
  499. */
  500. public static void Find(int[,] array,out int[,] arrayNew)
  501. {
  502. //数组中大于0的点个数
  503. int count = 0;
  504. //个数索引
  505. int time = 0;
  506. //数组行列数
  507. int rows = array.GetLength(0);
  508. int cols = array.GetLength(1);
  509. //计算数组中大于0的个数
  510. BasFunction.Count(array, out count);
  511. //坐标数组的大小
  512. arrayNew = new int[count, 2];
  513. //遍历,当遇到值大于0的时候,记录行列
  514. for (int i = 0; i < rows; i++)
  515. {
  516. for (int j = 0; j < cols; j++)
  517. {
  518. if (array[i, j] > 0)
  519. {
  520. arrayNew[time, 0] = i;
  521. arrayNew[time, 1] = j;
  522. time++;
  523. }
  524. }
  525. }
  526. }
  527. /*
  528. * 摘要:
  529. * 选择两个数中的较大的或者较小的数
  530. * 参数:
  531. * a,b:
  532. * 代比较的地方
  533. * world:
  534. * 关键词:
  535. * big:输出较大的数
  536. * small:输出较小的数
  537. * result:
  538. * 输出结果
  539. */
  540. public static void ChooseSize(double a, double b, string world, out double result)
  541. {
  542. result = 0;
  543. switch (world)
  544. {
  545. case "big":
  546. if (a > b)
  547. {
  548. result = a;
  549. }
  550. else
  551. {
  552. result = b;
  553. }
  554. break;
  555. case "small":
  556. if (a < b)
  557. {
  558. result = a;
  559. }
  560. else
  561. {
  562. result = b;
  563. }
  564. break;
  565. }
  566. }
  567. /*
  568. * 摘要:
  569. * 得到全0二维数组
  570. * 参数:
  571. * rows:
  572. * 行数
  573. * cols:
  574. * 列数
  575. * array:
  576. * 输出二维数组
  577. */
  578. public static void Zeros(int rows, int cols,out int[,] array)
  579. {
  580. array = new int[rows, cols];
  581. for (int i = 0; i < rows; i++)
  582. {
  583. for (int j = 0; j < cols; j++)
  584. {
  585. array[i, j] = 0;
  586. }
  587. }
  588. }
  589. /*
  590. * 摘要:
  591. * 得到全0二维数组
  592. * 参数:
  593. * rows:
  594. * 行数
  595. * cols:
  596. * 列数
  597. * array:
  598. * 输出二维数组
  599. */
  600. public static void Zeros(int rows, int cols, out double[,] array)
  601. {
  602. array = new double[rows, cols];
  603. for (int i = 0; i < rows; i++)
  604. {
  605. for (int j = 0; j < cols; j++)
  606. {
  607. array[i, j] = 0;
  608. }
  609. }
  610. }
  611. /*
  612. * 摘要:
  613. * 计算数组之间的点乘
  614. * 参数:
  615. * array1:
  616. * 输入数组1
  617. * array2:
  618. * 输入数组2
  619. * arrayResult:
  620. * 输出结果数组
  621. * upperbound:
  622. * 上界
  623. * lowerBound:
  624. * 下界
  625. * leftMargin:
  626. * 左边界
  627. * rightMargin:
  628. * 右边界
  629. */
  630. public static void DotMultiplication(double[,] array1, double[,] array2, out double[,] arrayResult,int upperBound, int lowerBound, int leftMargin, int rightMargin)
  631. {
  632. //数组行列数
  633. int rows = array1.GetLength(0);
  634. int cols = array1.GetLength(1);
  635. //结果数组
  636. arrayResult = new double[rows, cols];
  637. for (int i = upperBound; i < lowerBound; i++)
  638. {
  639. for (int j = leftMargin; j < rightMargin; j++)
  640. {
  641. arrayResult[i, j] = array1[i, j] * array2[i, j];
  642. }
  643. }
  644. }
  645. /*
  646. * 摘要:
  647. * 截取Mat中部分像素
  648. */
  649. public static void InterceptMat(Mat image, out Mat result, int upperBound, int lowerBound, int leftBound, int rightBound)
  650. {
  651. int rows = lowerBound - upperBound;
  652. int cols = rightBound - leftBound;
  653. result = new Mat(rows, cols, image.Type());
  654. for (int i = 0; i < rows; i++)
  655. {
  656. for (int j = 0; j < cols; j++)
  657. {
  658. int value = image.Get<int>(i+upperBound, j+leftBound);
  659. result.Set<int>(i, j, value);
  660. }
  661. }
  662. }
  663. /*
  664. * 摘要:
  665. * 将图片中的某部分置为某数(8位单通道)
  666. * 参数:
  667. * image:
  668. * 输入图片
  669. * result:
  670. * 输出图片
  671. * upperBound,lowerBound,leftBound,rightBound:
  672. * 上,下,左,右边界
  673. * value:
  674. * 需要设置的数
  675. */
  676. public static void SetNumber(Mat image, out Mat result, int upperBound, int lowerBound, int leftBound, int rightBound,int value)
  677. {
  678. result = image;
  679. for (int i = upperBound; i < lowerBound; i++)
  680. {
  681. for (int j = leftBound; j < rightBound; j++)
  682. {
  683. result.Set<int>(i, j, value);
  684. }
  685. }
  686. }
  687. }
  688. }