123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690 |
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using OpenCvSharp;
- using System.Collections;
- namespace ceju
- {
- class BasFunction
- {
- /*
- * 摘要:
- * 数组矩阵求和。
- * 参数:
- * arr:
- * 输入二维数组
- * n:
- * n=1:对每列求和,得到行向量;
- * n=2:对每行求和,得到列向量;
- * n=3:对数组求和,得到和
- * 返回:
- * 一维数组,分别对应n从1到3时的行向量,列向量,和
- */
- public static int[] Sum(int[,] arr, int n)
- {
- int rows = arr.GetLength(0);
- int cols = arr.GetLength(1);
- int[] zero = new int[1] { 0 };
- switch (n)
- {
- case 1:
- int[] arrC = new int[cols];
- for (int j = 0; j < cols; j++)
- {
- arrC[j] = 0;
- for (int i = 0; i < rows; i++)
- {
- arrC[j] += arr[i, j];
- }
- }
- return arrC;
- break;
- case 2:
- int[] arrR = new int[rows];
- for (int i = 0; i < rows; i++)
- {
- arrR[i] = 0;
- for (int j = 0; j < cols; j++)
- {
- arrR[i] += arr[i, j];
- }
- }
- return arrR;
- break;
- case 3:
- int[] arrSum = new int[1] { 0 };
- for (int i = 0; i < rows; i++)
- {
- for (int j = 0; j < cols; j++)
- {
- arrSum[0] += arr[i, j];
- }
- }
- return arrSum;
- break;
- default:
- return zero;
- }
- }
- /*
- * 摘要:
- * 求数组和
- * 参数:
- * array:
- * 输入二维数组
- * sum:
- * 输出和
- */
- public static void Sum(double[,] array,out double sum)
- {
- int rows = array.GetLength(0);
- int cols = array.GetLength(1);
- sum = 0;
- for (int i = 0; i < rows; i++)
- {
- for (int j = 0; j < cols; j++)
- {
- sum += array[i, j];
- }
- }
- }
- /*
- * 一维数组求和
- */
- public static void Sum(int[] array, out int sum)
- {
- int length = array.Length;
- sum = 0;
- for (int i = 0; i < length; i++)
- {
- sum += array[i];
- }
- }
- /*
- * 摘要:
- * 求数组的最大值
- * 参数:
- * arr:
- * 输入一维数组
- * 返回:
- * 最大值
- */
- public static int Max(int[] arr)
- {
- int max = 0;
- for (int i = 0; i < arr.Length; i++)
- {
- if (max < arr[i])
- {
- max = arr[i];
- }
- }
- return max;
- }
- /*
- * 摘要:
- * 求数组的最小值
- * 参数:
- * array:
- * 输入一维数组
- * 返回:
- * 最小值
- */
- public static int Min(int[] array)
- {
- int min = array[0];
- for (int i = 0; i < array.Length; i++)
- {
- if (min > array[i])
- min = array[i];
- }
- return min;
- }
- // 摘要:
- // 将Mat类的数据存入二维数组中
- // 参数:
- // 输入Mat类图像
- // 返回:
- // 二维数组
- public static int[,] Mat2Array(Mat image)
- {
- //求行数和列数
- int rows = image.Rows;
- int cols = image.Cols;
- //将Mat类中的数据放到数组中
- int[,] arr = new int[rows, cols];
- for (int i = 0; i < rows; i++)
- {
- for (int j = 0; j < cols; j++)
- {
- arr[i, j] = image.Get<byte>(i, j);
- }
- }
- return arr;
- }
- /*
- * 摘要:
- * 截取一维数组中的某一段到新数组
- * 参数:
- * array:
- * 输入一维数组
- * begin:
- * 截取片段起始位置
- * end:
- * 截取片段结束位置
- * 返回:
- * begin和end之间的新数组
- */
- public static int[] Intercept(int[] array, int begin, int end)
- {
- //截取片段长度
- int length = end - begin + 1;
- //新数组
- int[] newArray = new int[length];
- for (int i = 0; i < length; i++)
- {
- newArray[i] = array[i + begin];
- }
- return newArray;
- }
- /*
- * 摘要:
- * 将0,255的二值数组转变为0,1的二值数组
- * 参数:
- * arr:
- * 输入二维数组
- * 返回:
- * 只有0,1的二维数组
- */
- public static int[,] ConversionRange(int[,] arr)
- {
- int[,] arrNew = arr;
- int rows = arr.GetLength(0);
- int cols = arr.GetLength(1);
- for (int i = 0; i < rows; i++)
- {
- for (int j = 0; j < cols; j++)
- {
- if(arr[i,j]>0)
- arrNew[i, j] = 1;
- }
- }
- return arrNew;
- }
- /*
- * 摘要:
- * 对一维数组进行和单个值之间的加减乘除操作
- * 参数:
- * array:
- * 输入一维数组
- * resul:
- * 输出结果
- * a:
- * 单个值
- * c:
- * 操作选项
- * ‘+’‘-’‘*’‘/’
- */
- public static void OperationArray(int[] array, out int[] result, int a, char c)
- {
- int length = array.Length;
- result = new int[length];
- switch (c)
- {
- case '+':
- for (int i = 0; i < length; i++)
- {
- result[i] = array[i] + a;
- }
- break;
- case '-':
- for (int i = 0; i < length; i++)
- {
- result[i] = array[i] - a;
- }
- break;
- case '*':
- for (int i = 0; i < length; i++)
- {
- result[i] = array[i] * a;
- }
- break;
- case '/':
- for (int i = 0; i < length; i++)
- {
- result[i] = array[i] / a;
- }
- break;
- default:
- break;
- }
- }
- /*
- * 摘要:
- * 选取一个数组中某一区间的数值
- * 参数:
- * array1:
- * 目标截取的数组
- * array2:
- * 保留数组
- * upperBound:
- * 上界
- * lowerBound:
- * 下界
- * leftBoundary:
- * 左界
- * rightBoundary:
- * 右界
- * 返回:
- * 返回截取某段区间复制下来之后的数组
- */
- public static int[,] InterceptArray(int[,] array1, int[,] array2, int upperBound, int lowerBound, int leftBoundary, int rightBoundary)
- {
- for (int i = upperBound; i < lowerBound; i++)
- {
- for (int j = leftBoundary; j < rightBoundary; j++)
- {
- array2[i, j] = array1[i, j];
- }
- }
- return array2;
- }
- /*
- * 摘要:
- * 选取某一个数减去一个数组中某一区间的数值
- * 参数:
- * array1:
- * 目标截取的数组
- * array2:
- * 保留数组
- * upperBound:
- * 上界
- * lowerBound:
- * 下界
- * leftBoundary:
- * 左界
- * rightBoundary:
- * 右界
- * minute:
- * 被减数
- * 返回:
- * 返回截取某段区间复制下来之后的数组
- */
- public static int[,] InterceptArray(int[,] array1, int[,] array2, int upperBound, int lowerBound, int leftBoundary, int rightBoundary, int minute)
- {
- for (int i = upperBound; i < lowerBound; i++)
- {
- for (int j = leftBoundary; j < rightBoundary; j++)
- {
- array2[i, j] = minute - array1[i, j];
- }
- }
- return array2;
- }
- /*
- * 摘要:
- * 截取数组
- * 参数:
- * array:
- * 输入二维数组
- * upperBound, lowerBound, leftBound, rightBound:
- * 输入上、下、左、右边界
- * 返回:
- * 截取后的数组,数组大小仅为截取区间大小
- */
- public static int[,] InterceptArray(int[,] array, int upperBound, int lowerBound, int leftBound, int rightBound)
- {
- int rows = lowerBound - upperBound;
- int cols = rightBound - leftBound;
- int[,] newArray = new int[rows, cols];
- for (int i = 0; i < rows; i++)
- {
- for (int j = 0; j < cols; j++)
- {
- newArray[i, j] = array[i + upperBound, j + leftBound];
- }
- }
- return newArray;
- }
- /*
- * 摘要:
- * 选取一个数组中某一区间的数值
- * 参数:
- * array1:
- * 目标截取的数组
- * array2:
- * 保留数组
- * upperBound:
- * 上界
- * lowerBound:
- * 下界
- * leftBoundary:
- * 左界
- * rightBoundary:
- * 右界
- * 返回:
- * 返回截取某段区间复制下来之后的数组
- */
- public static double[,] InterceptArray(double[,] array1, double[,] array2, int upperBound, int lowerBound, int leftBoundary, int rightBoundary)
- {
- for (int i = upperBound; i < lowerBound; i++)
- {
- for (int j = leftBoundary; j < rightBoundary; j++)
- {
- array2[i, j] = array1[i, j];
- }
- }
- return array2;
- }
- /*
- * 摘要:
- * 计算数组中不为0的个数
- * 参数:
- * array:
- * 输入一维数组
- * count:
- * 数量
- */
- public static void Count(int[] array, out int count)
- {
- count = 0;
- for (int i = 0; i < array.Length; i++)
- {
- if (array[i] > 0)
- {
- count++;
- }
- }
- }
- /*
- * 摘要:
- * 计算数组中不为0的个数
- * 参数:
- * array:
- * 输入二维数组
- * count:
- * 数量
- */
- public static void Count(int[,] array, out int count)
- {
- count = 0;
- //数组行列数
- int rows = array.GetLength(0);
- int cols = array.GetLength(1);
- for (int i = 0; i < rows; i++)
- {
- for(int j = 0;j<cols;j++)
- {
- if (array[i,j] > 0)
- {
- count++;
- }
- }
- }
- }
- /*
- * 摘要:
- * 计算数组中不为0的个数
- * 参数:
- * array:
- * 输入二维数组
- * count:
- * 数量
- */
- public static void Count(double[,] array, out int count)
- {
- count = 0;
- //数组行列数
- int rows = array.GetLength(0);
- int cols = array.GetLength(1);
- for (int i = 0; i < rows; i++)
- {
- for (int j = 0; j < cols; j++)
- {
- if (array[i, j] > 0)
- {
- count++;
- }
- }
- }
- }
- /*
- * 摘要:
- * 计算Mat中不为0的个数
- * 参数:
- * array:
- * 输入二维数组
- * count:
- * 数量
- */
- public static void Count(Mat image, out int count)
- {
- count = 0;
- for (int i = 0; i < image.Rows; i++)
- {
- for (int j = 0; j < image.Cols; j++)
- {
- if (image.Get<int>(i, j) > 0)
- count++;
- }
- }
- }
- /*
- * 摘要:
- * 计算两点之间的距离
- * 参数:
- * a:
- * 记录横坐标与纵坐标的数组
- * b:
- * 记录横坐标与纵坐标的数组
- * (两数组相同索引值的坐标类型要相同)
- * resul:
- * 输出的距离结果
- */
- public static void Distance(double[] a, double[] b, out double result)
- {
- result = Math.Sqrt(Math.Pow((a[0] - b[0]), 2) + Math.Pow((a[1] - b[1]), 2));
- }
- /*
- * 摘要:
- * 得到数组中大于0的点的位置
- * 参数:
- * array:
- * 输入二维数组
- * arrayNew:
- * 输出的坐标数组
- * 第一列是纵坐标(行数),第二列是横坐标(列数)
- */
- public static void Find(int[,] array,out int[,] arrayNew)
- {
- //数组中大于0的点个数
- int count = 0;
- //个数索引
- int time = 0;
- //数组行列数
- int rows = array.GetLength(0);
- int cols = array.GetLength(1);
- //计算数组中大于0的个数
- BasFunction.Count(array, out count);
- //坐标数组的大小
- arrayNew = new int[count, 2];
- //遍历,当遇到值大于0的时候,记录行列
- for (int i = 0; i < rows; i++)
- {
- for (int j = 0; j < cols; j++)
- {
- if (array[i, j] > 0)
- {
- arrayNew[time, 0] = i;
- arrayNew[time, 1] = j;
- time++;
- }
- }
- }
- }
- /*
- * 摘要:
- * 选择两个数中的较大的或者较小的数
- * 参数:
- * a,b:
- * 代比较的地方
- * world:
- * 关键词:
- * big:输出较大的数
- * small:输出较小的数
- * result:
- * 输出结果
- */
- public static void ChooseSize(double a, double b, string world, out double result)
- {
- result = 0;
- switch (world)
- {
- case "big":
- if (a > b)
- {
- result = a;
- }
- else
- {
- result = b;
- }
- break;
- case "small":
- if (a < b)
- {
- result = a;
- }
- else
- {
- result = b;
- }
- break;
- }
- }
- /*
- * 摘要:
- * 得到全0二维数组
- * 参数:
- * rows:
- * 行数
- * cols:
- * 列数
- * array:
- * 输出二维数组
- */
- public static void Zeros(int rows, int cols,out int[,] array)
- {
- array = new int[rows, cols];
- for (int i = 0; i < rows; i++)
- {
- for (int j = 0; j < cols; j++)
- {
- array[i, j] = 0;
- }
- }
- }
- /*
- * 摘要:
- * 得到全0二维数组
- * 参数:
- * rows:
- * 行数
- * cols:
- * 列数
- * array:
- * 输出二维数组
- */
- public static void Zeros(int rows, int cols, out double[,] array)
- {
- array = new double[rows, cols];
- for (int i = 0; i < rows; i++)
- {
- for (int j = 0; j < cols; j++)
- {
- array[i, j] = 0;
- }
- }
- }
- /*
- * 摘要:
- * 计算数组之间的点乘
- * 参数:
- * array1:
- * 输入数组1
- * array2:
- * 输入数组2
- * arrayResult:
- * 输出结果数组
- * upperbound:
- * 上界
- * lowerBound:
- * 下界
- * leftMargin:
- * 左边界
- * rightMargin:
- * 右边界
- */
- public static void DotMultiplication(double[,] array1, double[,] array2, out double[,] arrayResult,int upperBound, int lowerBound, int leftMargin, int rightMargin)
- {
- //数组行列数
- int rows = array1.GetLength(0);
- int cols = array1.GetLength(1);
- //结果数组
- arrayResult = new double[rows, cols];
- for (int i = upperBound; i < lowerBound; i++)
- {
- for (int j = leftMargin; j < rightMargin; j++)
- {
- arrayResult[i, j] = array1[i, j] * array2[i, j];
- }
- }
- }
- /*
- * 摘要:
- * 截取Mat中部分像素
- */
- public static void InterceptMat(Mat image, out Mat result, int upperBound, int lowerBound, int leftBound, int rightBound)
- {
- int rows = lowerBound - upperBound;
- int cols = rightBound - leftBound;
- result = new Mat(rows, cols, image.Type());
- for (int i = 0; i < rows; i++)
- {
- for (int j = 0; j < cols; j++)
- {
- int value = image.Get<int>(i+upperBound, j+leftBound);
- result.Set<int>(i, j, value);
- }
- }
-
- }
- /*
- * 摘要:
- * 将图片中的某部分置为某数(8位单通道)
- * 参数:
- * image:
- * 输入图片
- * result:
- * 输出图片
- * upperBound,lowerBound,leftBound,rightBound:
- * 上,下,左,右边界
- * value:
- * 需要设置的数
- */
- public static void SetNumber(Mat image, out Mat result, int upperBound, int lowerBound, int leftBound, int rightBound,int value)
- {
- result = image;
- for (int i = upperBound; i < lowerBound; i++)
- {
- for (int j = leftBound; j < rightBound; j++)
- {
- result.Set<int>(i, j, value);
- }
- }
- }
- }
- }
|