From a841e49705b765f659ece74c5047a7264db4f885 Mon Sep 17 00:00:00 2001 From: LingYuZhao Date: Sun, 26 Mar 2023 18:36:56 +0800 Subject: [PATCH 01/17] =?UTF-8?q?=E6=96=87=E6=A1=A3=E6=9B=B4=E6=96=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * 更新其中的1.17版本官方文档 --- README.md | 10 +++---- src_code/README-Chinese.md | 30 +++++++++---------- src_code/README.md | 30 +++++++++---------- .../operands/matrix/ColorMatrix.java | 1 - .../operands/matrix/ColumnDoubleMatrix.java | 2 -- .../operands/matrix/ColumnIntegerMatrix.java | 2 -- .../matrix/block/IntegerMatrixSpace.java | 13 ++++---- .../operands/table/DataFrame.java | 16 +++++----- .../zhao/algorithmMagic/utils/ASClass.java | 6 ++-- 9 files changed, 52 insertions(+), 58 deletions(-) diff --git a/README.md b/README.md index 55104f2..3faeaf3 100644 --- a/README.md +++ b/README.md @@ -58,7 +58,7 @@ The AS library generates some log data when performing many calculation function ``` -#### 可选依赖项 +#### Optional Dependencies When interfacing with various platforms such as databases and Sparks, the AS library needs to use third-party dependency packages, which are optional. If you do not need to use these functions, you may not need to import dependencies. If you need to, you can refer to the following configuration. ```xml @@ -243,12 +243,12 @@ AS library runs. ### KnowledgeDocument -The knowledge base file archive is used for the storage task of ASMB series knowledge documents. You can access it +The knowledge base file archive is used for the storage task of AS-MB series knowledge documents. You can access it directly through the documents on the home page. There is no need to enter here directly. It is messy internally. ### src_code -The source code storage directory of ASMB, where you can view the relevant source code of ASMB. Of course, here is the +The source code storage directory of AS-MB, where you can view the relevant source code of AS-MB. Of course, here is the latest source code, which you can use to compile, so that you can obtain the latest version. Note: The latest version is often unstable, so we recommend you to use the version that has been released for a long @@ -256,12 +256,12 @@ time! ### README-Chinese.md -The Chinese version of the ASMB homepage document. You can switch languages on the homepage default page to access this +The Chinese version of the AS-MB homepage document. You can switch languages on the homepage default page to access this file. ### README.md -The default version of the ASMB homepage document. You can directly access this file on the homepage! +The default version of the AS-MB homepage document. You can directly access this file on the homepage! - 切换到 [中文文档](https://github.com/BeardedManZhao/algorithmStar/blob/main/README-Chinese.md) diff --git a/src_code/README-Chinese.md b/src_code/README-Chinese.md index 3c48a72..ce0123a 100644 --- a/src_code/README-Chinese.md +++ b/src_code/README-Chinese.md @@ -22,7 +22,7 @@ import zhao.algorithmMagic.operands.matrix.block.IntegerMatrixSpace; public class MAIN1 { public static void main(String[] args) { - String s1 = "C:\\Users\\Liming\\Desktop\\fsdownload\\微信图片_1.jpg"; + String s1 = "C:\\Users\\Liming\\Desktop\\fsDownload\\微信图片_1.jpg"; IntegerMatrix integerMatrix; { // 设置权重 @@ -39,7 +39,7 @@ public class MAIN1 { // 输出图片1的卷积图像文件 ImageRenderingIntegrator image = new ImageRenderingIntegrator( "image", - new ImageRenderingMarLauncher<>(integerMatrix, "C:\\Users\\Liming\\Desktop\\fsdownload\\res12.jpg", 1) + new ImageRenderingMarLauncher<>(integerMatrix, "C:\\Users\\Liming\\Desktop\\fsDownload\\res12.jpg", 1) ); if (image.run()) { System.out.println("ok!!!"); @@ -57,7 +57,7 @@ import zhao.algorithmMagic.operands.matrix.ColorMatrix; public class MAIN1 { public static void main(String[] args) throws InterruptedException { - String s1 = "C:\\Users\\Liming\\Desktop\\fsdownload\\微信图片_1.jpg"; + String s1 = "C:\\Users\\Liming\\Desktop\\fsDownload\\微信图片_1.jpg"; ColorMatrix parse = ColorMatrix.parse(s1); parse.show("image"); Thread.sleep(1024); @@ -114,7 +114,7 @@ import zhao.algorithmMagic.operands.matrix.ColorMatrix; public class MAIN1 { public static void main(String[] args) { - String s1 = "C:\\Users\\zhao\\Desktop\\fsdownload\\微信图片_2.jpg"; + String s1 = "C:\\Users\\zhao\\Desktop\\fsDownload\\微信图片_2.jpg"; // 获取到图像矩阵对象 ColorMatrix parse = ColorMatrix.parse(s1); // 将图像在原图像的基础上进行颜色反转操作 @@ -122,7 +122,7 @@ public class MAIN1 { // 查看颜色反转之后的图像 parse.show("image1"); // 输出反转之后的图像 - parse.save("C:\\Users\\zhao\\Desktop\\fsdownload\\res123.jpg"); + parse.save("C:\\Users\\zhao\\Desktop\\fsDownload\\res123.jpg"); } } ``` @@ -136,11 +136,11 @@ import zhao.algorithmMagic.operands.matrix.ColorMatrix; public class MAIN1 { public static void main(String[] args) { - String s1 = "C:\\Users\\Liming\\Desktop\\fsdownload\\微信图片_2.jpg"; + String s1 = "C:\\Users\\Liming\\Desktop\\fsDownload\\微信图片_2.jpg"; // 获取到图像矩阵对象 ColorMatrix parse = ColorMatrix.parse(s1); // 输出图像RGB文本数据 - parse.save("C:\\Users\\Liming\\Desktop\\fsdownload\\res1234.csv", ','); + parse.save("C:\\Users\\Liming\\Desktop\\fsDownload\\res1234.csv", ','); } } ``` @@ -179,7 +179,7 @@ public class MAIN1 { .count() // 将每一组进行统计 .sort("count()") // 按照统计结果进行正序排序 .limit(3); // 获取最多前3行数据 - System.out.println(select.into_outfile("C:\\Users\\zhao\\Desktop\\fsdownload\\res1.csv")); + System.out.println(select.into_outfile("C:\\Users\\zhao\\Desktop\\fsDownload\\res1.csv")); // 打印存储 FDF 中的数据行数 System.out.println("数据行数 = " + execute.count()); // 打印出其中的信息 @@ -459,7 +459,7 @@ public class MAIN1 { parse1.show("image"); // 输出图像的 ASCII 数值,输出规则为 G 通道颜色数值 大于 40 的 输出符号 'A' 其它输出符号 ' ' parse1.save( - new File("C:\\Users\\zhao\\Desktop\\fsdownload\\res.txt"), + new File("C:\\Users\\zhao\\Desktop\\fsDownload\\res.txt"), ColorMatrix._G_, 40, 'A', ' ' ); } @@ -607,7 +607,7 @@ public class MAIN1 { ); // 输出表的HTML 然后查看表中的数据 System.out.println( - insert.into_outHtml("C:\\Users\\Liming\\Desktop\\fsdownload\\res11234.html", "myTable") + insert.into_outHtml("C:\\Users\\Liming\\Desktop\\fsDownload\\res11234.html", "myTable") ); } } @@ -625,8 +625,8 @@ import java.sql.SQLException; public class MAIN1 { public static void main(String[] args) { // 将一些图像文件转换成为一个图像矩阵对象 - ColorMatrix colorMatrix1 = ColorMatrix.parse("C:\\Users\\Liming\\Desktop\\fsdownload\\test.bmp"); - ColorMatrix colorMatrix2 = ColorMatrix.parse("C:\\Users\\Liming\\Desktop\\fsdownload\\test1.bmp"); + ColorMatrix colorMatrix1 = ColorMatrix.parse("C:\\Users\\Liming\\Desktop\\fsDownload\\test.bmp"); + ColorMatrix colorMatrix2 = ColorMatrix.parse("C:\\Users\\Liming\\Desktop\\fsDownload\\test1.bmp"); // 将 colorMatrix2 + colorMatrix1 的结果图像展示出来 (colorMatrix1.add(colorMatrix2)).show("image"); } @@ -645,8 +645,8 @@ import java.sql.SQLException; public class MAIN1 { public static void main(String[] args) { // 将一些图像文件转换成为一个图像矩阵对象 - ColorMatrix colorMatrix1 = ColorMatrix.parse("C:\\Users\\Liming\\Desktop\\fsdownload\\test.bmp"); - ColorMatrix colorMatrix2 = ColorMatrix.parse("C:\\Users\\Liming\\Desktop\\fsdownload\\test1.bmp"); + ColorMatrix colorMatrix1 = ColorMatrix.parse("C:\\Users\\Liming\\Desktop\\fsDownload\\test.bmp"); + ColorMatrix colorMatrix2 = ColorMatrix.parse("C:\\Users\\Liming\\Desktop\\fsDownload\\test1.bmp"); // 使用 agg 函数以及内置的计算方案进行两个矩阵的合并 // 下面函数中的第二个形参代表的就是矩阵中元素的聚合逻辑,这里使用的是求和,并对越界颜色数值进行规整的逻辑实现 colorMatrix1.agg(colorMatrix2, ColorMatrix.COLOR_SUM_REGULATE).show("image"); @@ -667,7 +667,7 @@ import java.sql.SQLException; public class MAIN1 { public static void main(String[] args) { // 将一些图像文件转换成为一个图像矩阵对象 - ColorMatrix colorMatrix1 = ColorMatrix.parseGrayscale("C:\\Users\\Liming\\Desktop\\fsdownload\\test2.bmp"); + ColorMatrix colorMatrix1 = ColorMatrix.parseGrayscale("C:\\Users\\Liming\\Desktop\\fsDownload\\test2.bmp"); // 对图像进行二值化 colorMatrix1.globalBinary(ColorMatrix._G_, 100, 0xffffff, 0); colorMatrix1.show("腐蚀之前的 image"); diff --git a/src_code/README.md b/src_code/README.md index 983086b..c5eacca 100644 --- a/src_code/README.md +++ b/src_code/README.md @@ -23,7 +23,7 @@ import zhao.algorithmMagic.operands.matrix.block.IntegerMatrixSpace; public class MAIN1 { public static void main(String[] args) { - String s1 = "C:\\Users\\Liming\\Desktop\\fsdownload\\微信图片_1.jpg"; + String s1 = "C:\\Users\\Liming\\Desktop\\fsDownload\\微信图片_1.jpg"; IntegerMatrix integerMatrix; { // 设置权重 @@ -40,7 +40,7 @@ public class MAIN1 { // 输出图片1的卷积图像文件 ImageRenderingIntegrator image = new ImageRenderingIntegrator( "image", - new ImageRenderingMarLauncher<>(integerMatrix, "C:\\Users\\Liming\\Desktop\\fsdownload\\res12.jpg", 1) + new ImageRenderingMarLauncher<>(integerMatrix, "C:\\Users\\Liming\\Desktop\\fsDownload\\res12.jpg", 1) ); if (image.run()) { System.out.println("ok!!!"); @@ -58,7 +58,7 @@ import zhao.algorithmMagic.operands.matrix.ColorMatrix; public class MAIN1 { public static void main(String[] args) throws InterruptedException { - String s1 = "C:\\Users\\Liming\\Desktop\\fsdownload\\微信图片_1.jpg"; + String s1 = "C:\\Users\\Liming\\Desktop\\fsDownload\\微信图片_1.jpg"; ColorMatrix parse = ColorMatrix.parse(s1); parse.show("image"); Thread.sleep(1024); @@ -117,7 +117,7 @@ import zhao.algorithmMagic.operands.matrix.ColorMatrix; public class MAIN1 { public static void main(String[] args) { - String s1 = "C:\\Users\\zhao\\Desktop\\fsdownload\\微信图片_2.jpg"; + String s1 = "C:\\Users\\zhao\\Desktop\\fsDownload\\微信图片_2.jpg"; // 获取到图像矩阵对象 ColorMatrix parse = ColorMatrix.parse(s1); // 将图像在原图像的基础上进行颜色反转操作 @@ -125,7 +125,7 @@ public class MAIN1 { // 查看颜色反转之后的图像 parse.show("image1"); // 输出反转之后的图像 - parse.save("C:\\Users\\zhao\\Desktop\\fsdownload\\res123.jpg"); + parse.save("C:\\Users\\zhao\\Desktop\\fsDownload\\res123.jpg"); } } ``` @@ -140,11 +140,11 @@ import zhao.algorithmMagic.operands.matrix.ColorMatrix; public class MAIN1 { public static void main(String[] args) { - String s1 = "C:\\Users\\Liming\\Desktop\\fsdownload\\微信图片_2.jpg"; + String s1 = "C:\\Users\\Liming\\Desktop\\fsDownload\\微信图片_2.jpg"; // 获取到图像矩阵对象 ColorMatrix parse = ColorMatrix.parse(s1); // 输出图像RGB文本数据 - parse.save("C:\\Users\\Liming\\Desktop\\fsdownload\\res1234.csv", ','); + parse.save("C:\\Users\\Liming\\Desktop\\fsDownload\\res1234.csv", ','); } } ``` @@ -184,7 +184,7 @@ public class MAIN1 { .count() // 将每一组进行统计 .sort("count()") // 按照统计结果进行正序排序 .limit(3); // 获取最多前3行数据 - System.out.println(select.into_outfile("C:\\Users\\zhao\\Desktop\\fsdownload\\res1.csv")); + System.out.println(select.into_outfile("C:\\Users\\zhao\\Desktop\\fsDownload\\res1.csv")); // 打印存储 FDF 中的数据行数 System.out.println("数据行数 = " + execute.count()); // 打印出其中的信息 @@ -459,7 +459,7 @@ public class MAIN1 { parse1.show("image"); // 输出图像的 ASCII 数值,输出规则为 G 通道颜色数值 大于 40 的 输出符号 'A' 其它输出符号 ' ' parse1.save( - new File("C:\\Users\\zhao\\Desktop\\fsdownload\\res.txt"), + new File("C:\\Users\\zhao\\Desktop\\fsDownload\\res.txt"), ColorMatrix._G_, 40, 'A', ' ' ); } @@ -608,7 +608,7 @@ public class MAIN1 { ); // 输出表的HTML 然后查看表中的数据 System.out.println( - insert.into_outHtml("C:\\Users\\Liming\\Desktop\\fsdownload\\res11234.html", "myTable") + insert.into_outHtml("C:\\Users\\Liming\\Desktop\\fsDownload\\res11234.html", "myTable") ); } } @@ -627,8 +627,8 @@ import java.sql.SQLException; public class MAIN1 { public static void main(String[] args) { // 将一些图像文件转换成为一个图像矩阵对象 - ColorMatrix colorMatrix1 = ColorMatrix.parse("C:\\Users\\Liming\\Desktop\\fsdownload\\test.bmp"); - ColorMatrix colorMatrix2 = ColorMatrix.parse("C:\\Users\\Liming\\Desktop\\fsdownload\\test1.bmp"); + ColorMatrix colorMatrix1 = ColorMatrix.parse("C:\\Users\\Liming\\Desktop\\fsDownload\\test.bmp"); + ColorMatrix colorMatrix2 = ColorMatrix.parse("C:\\Users\\Liming\\Desktop\\fsDownload\\test1.bmp"); // 将 colorMatrix2 + colorMatrix1 的结果图像展示出来 (colorMatrix1.add(colorMatrix2)).show("image"); } @@ -647,8 +647,8 @@ import java.sql.SQLException; public class MAIN1 { public static void main(String[] args) { // 将一些图像文件转换成为一个图像矩阵对象 - ColorMatrix colorMatrix1 = ColorMatrix.parse("C:\\Users\\Liming\\Desktop\\fsdownload\\test.bmp"); - ColorMatrix colorMatrix2 = ColorMatrix.parse("C:\\Users\\Liming\\Desktop\\fsdownload\\test1.bmp"); + ColorMatrix colorMatrix1 = ColorMatrix.parse("C:\\Users\\Liming\\Desktop\\fsDownload\\test.bmp"); + ColorMatrix colorMatrix2 = ColorMatrix.parse("C:\\Users\\Liming\\Desktop\\fsDownload\\test1.bmp"); // 使用 agg 函数以及内置的计算方案进行两个矩阵的合并 // 下面函数中的第二个形参代表的就是矩阵中元素的聚合逻辑,这里使用的是求和,并对越界颜色数值进行规整的逻辑实现 colorMatrix1.agg(colorMatrix2, ColorMatrix.COLOR_SUM_REGULATE).show("image"); @@ -669,7 +669,7 @@ import java.sql.SQLException; public class MAIN1 { public static void main(String[] args) { // 将一些图像文件转换成为一个图像矩阵对象 - ColorMatrix colorMatrix1 = ColorMatrix.parseGrayscale("C:\\Users\\Liming\\Desktop\\fsdownload\\test2.bmp"); + ColorMatrix colorMatrix1 = ColorMatrix.parseGrayscale("C:\\Users\\Liming\\Desktop\\fsDownload\\test2.bmp"); // 对图像进行二值化 colorMatrix1.globalBinary(ColorMatrix._G_, 100, 0xffffff, 0); colorMatrix1.show("腐蚀之前的 image"); diff --git a/src_code/src/main/java/zhao/algorithmMagic/operands/matrix/ColorMatrix.java b/src_code/src/main/java/zhao/algorithmMagic/operands/matrix/ColorMatrix.java index 2b2f76a..3c59103 100644 --- a/src_code/src/main/java/zhao/algorithmMagic/operands/matrix/ColorMatrix.java +++ b/src_code/src/main/java/zhao/algorithmMagic/operands/matrix/ColorMatrix.java @@ -1279,7 +1279,6 @@ public void save(String path, char sep) { *

* Directory path to save. * @param sep 保存时使用的分隔符。 - * */ @Override public void save(File path, char sep) { diff --git a/src_code/src/main/java/zhao/algorithmMagic/operands/matrix/ColumnDoubleMatrix.java b/src_code/src/main/java/zhao/algorithmMagic/operands/matrix/ColumnDoubleMatrix.java index f705c4a..285c828 100644 --- a/src_code/src/main/java/zhao/algorithmMagic/operands/matrix/ColumnDoubleMatrix.java +++ b/src_code/src/main/java/zhao/algorithmMagic/operands/matrix/ColumnDoubleMatrix.java @@ -636,7 +636,6 @@ public void save(String path) { *

* Directory path to save. * @param sep 保存时使用的分隔符。 - * */ @Override public void save(String path, char sep) { @@ -652,7 +651,6 @@ public void save(String path, char sep) { *

* Directory path to save. * @param sep 保存时使用的分隔符。 - * */ @Override public void save(File path, char sep) { diff --git a/src_code/src/main/java/zhao/algorithmMagic/operands/matrix/ColumnIntegerMatrix.java b/src_code/src/main/java/zhao/algorithmMagic/operands/matrix/ColumnIntegerMatrix.java index 73ddc3b..7549490 100644 --- a/src_code/src/main/java/zhao/algorithmMagic/operands/matrix/ColumnIntegerMatrix.java +++ b/src_code/src/main/java/zhao/algorithmMagic/operands/matrix/ColumnIntegerMatrix.java @@ -632,7 +632,6 @@ public void save(String path) { *

* Directory path to save. * @param sep 保存时使用的分隔符。 - * */ @Override public void save(String path, char sep) { @@ -648,7 +647,6 @@ public void save(String path, char sep) { *

* Directory path to save. * @param sep 保存时使用的分隔符。 - * */ @Override public void save(File path, char sep) { diff --git a/src_code/src/main/java/zhao/algorithmMagic/operands/matrix/block/IntegerMatrixSpace.java b/src_code/src/main/java/zhao/algorithmMagic/operands/matrix/block/IntegerMatrixSpace.java index 0ef72d8..a3f9acf 100644 --- a/src_code/src/main/java/zhao/algorithmMagic/operands/matrix/block/IntegerMatrixSpace.java +++ b/src_code/src/main/java/zhao/algorithmMagic/operands/matrix/block/IntegerMatrixSpace.java @@ -183,10 +183,10 @@ public IntegerMatrixSpace transpose() { /** * 对矩阵空间进行卷积计算,在卷积计算的时候会产生出一个更小的特征矩阵。 * - * @param width 矩阵进行卷积运算的时的子图像宽度,最好选择能够被矩阵的列数整除的数值。 - * @param height 矩阵进行卷积运算时的子图像高度,最好选中能够被矩阵的行数整除的数值。 + * @param width 矩阵进行卷积运算的时的子图像宽度,最好选择能够被矩阵的列数整除的数值。 + * @param height 矩阵进行卷积运算时的子图像高度,最好选中能够被矩阵的行数整除的数值。 * @param weightMat 在进行卷积计算时需要的卷积核矩阵对象,该对象的宽高应与卷积函数的形参一致。 - * + *

* The convolution kernel matrix object required for convolution computation, whose width and height should be consistent with the formal parameters of the convolution function. * @return 矩阵空间卷积结果特征图,保持三通道的格式返回。 */ @@ -252,12 +252,11 @@ public IntegerMatrixSpace folding(int width, int height, IntegerMatrixSpace weig /** * 对矩阵空间进行卷积计算,在卷积计算的时候会产生出一个更小的特征矩阵。 * - * @param width 矩阵进行卷积运算的时的子图像宽度,最好选择能够被矩阵的列数整除的数值。 - * @param height 矩阵进行卷积运算时的子图像高度,最好选中能够被矩阵的行数整除的数值。 + * @param width 矩阵进行卷积运算的时的子图像宽度,最好选择能够被矩阵的列数整除的数值。 + * @param height 矩阵进行卷积运算时的子图像高度,最好选中能够被矩阵的行数整除的数值。 * @param weightMat 在进行卷积计算时需要的卷积核矩阵对象,该对象的宽高应与卷积函数的形参一致。 - * + *

* The convolution kernel matrix object required for convolution computation, whose width and height should be consistent with the formal parameters of the convolution function. - * * @return 矩阵空间卷积结果特征图,以三原色通道之和的方式返回一个矩阵。 */ public final IntegerMatrix foldingAndSum(int width, int height, IntegerMatrixSpace weightMat) { diff --git a/src_code/src/main/java/zhao/algorithmMagic/operands/table/DataFrame.java b/src_code/src/main/java/zhao/algorithmMagic/operands/table/DataFrame.java index afc01b3..8ee0f37 100644 --- a/src_code/src/main/java/zhao/algorithmMagic/operands/table/DataFrame.java +++ b/src_code/src/main/java/zhao/algorithmMagic/operands/table/DataFrame.java @@ -192,11 +192,11 @@ public interface DataFrame extends AggDataFrameData, Iterable, Serializa *

* Update all data corresponding to a column of fields according to the specified function. * - * @param fieldCell 需要被提取的列字段名称。 - *

- * The name of the column field to be extracted. + * @param fieldCell 需要被提取的列字段名称。 + *

+ * The name of the column field to be extracted. * @param transformation 列数据更新逻辑实现,在这里传递进来的是被修改的列数据字段。 - * + *

* Column data update logic implementation, where the modified column data field is passed in. * @return 更新之后的DF数据对象。 *

@@ -209,11 +209,11 @@ public interface DataFrame extends AggDataFrameData, Iterable, Serializa *

* Update all data corresponding to a column of fields according to the specified function. * - * @param rowName 需要被提取的列字段名称。 - *

- * The name of the column field to be extracted. + * @param rowName 需要被提取的列字段名称。 + *

+ * The name of the column field to be extracted. * @param transformation 列数据更新逻辑实现,在这里传递进来的是被修改的列数据字段。 - * + *

* Column data update logic implementation, where the modified column data field is passed in. * @return 更新之后的DF数据对象。 *

diff --git a/src_code/src/main/java/zhao/algorithmMagic/utils/ASClass.java b/src_code/src/main/java/zhao/algorithmMagic/utils/ASClass.java index cf5eb95..76a9317 100644 --- a/src_code/src/main/java/zhao/algorithmMagic/utils/ASClass.java +++ b/src_code/src/main/java/zhao/algorithmMagic/utils/ASClass.java @@ -324,9 +324,9 @@ public static void extractedIndexMap(HashMap RowOrColIndex, Str /** * 将两个数组合并到同一个数组中。 * - * @param res 结果数组容器 - * @param arr1 需要被合并的第一个数组 - * @param arr2 需要被合并的第二个数组 + * @param res 结果数组容器 + * @param arr1 需要被合并的第一个数组 + * @param arr2 需要被合并的第二个数组 * @param 数组中所包含的元素数据类型,此处为自动类型推断。 */ public static void mergeArray(arr[] res, arr[] arr1, arr[] arr2) { From 4b0e1b53045be3c30c4cc1c98e2f28c1c5da6a7b Mon Sep 17 00:00:00 2001 From: LingYuZhao Date: Mon, 27 Mar 2023 10:03:27 +0800 Subject: [PATCH 02/17] =?UTF-8?q?=E5=BC=80=E5=A7=8B=E6=96=B0=E7=89=88?= =?UTF-8?q?=E6=9C=AC1.18=E7=9A=84=E5=BC=80=E5=8F=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 开始开发任务 --- src_code/Case.md | 35 + src_code/README-Chinese.md | 795 +---------------- src_code/README.md | 775 +---------------- .../main/java/zhao/algorithmMagic/MAIN1.java | 3 - .../operands/matrix/ColorMatrix.java | 34 +- .../operands/vector/SparkVector.scala | 16 +- .../zhao/algorithmMagic/utils/ASClass.java | 38 +- src_code/update/1.16_1.17-Chinese.md | 814 ++++++++++++++++++ src_code/update/1.16_1.17.md | 791 +++++++++++++++++ 9 files changed, 1733 insertions(+), 1568 deletions(-) create mode 100644 src_code/Case.md create mode 100644 src_code/update/1.16_1.17-Chinese.md create mode 100644 src_code/update/1.16_1.17.md diff --git a/src_code/Case.md b/src_code/Case.md new file mode 100644 index 0000000..1046b7e --- /dev/null +++ b/src_code/Case.md @@ -0,0 +1,35 @@ +# AS 机器学习库案例集 + +## 机器视觉类案例 + +### 获取图像边缘线 + +```java +package zhao.algorithmMagic; + +import zhao.algorithmMagic.operands.matrix.ColorMatrix; + +import java.awt.*; + +public class MAIN1 { + public static void main(String[] args) { + // 获取一张图像的像素矩阵 + ColorMatrix parse1 = ColorMatrix.parseGrayscale("C:\\Users\\Liming\\Desktop\\fsdownload\\test1.bmp"); + // 将 parse1 进行二值化 + parse1.localBinary(ColorMatrix._G_, 30, 0, 0xffffff, 1); + // 将 parse1 矩阵腐蚀,然后将腐蚀的结果获取到 + ColorMatrix parse2 = parse1.erode(3, 3, true, Color.BLACK); + // 将 parse1 矩阵中的白色作为腐蚀背景色(膨胀) + parse1.erode(3, 3, false, Color.WHITE); + // 查看临时结果 + parse1.show("image1"); + parse2.show("image2"); + // 将两个矩阵进行做差运算,并查看做差之后的图像 + (parse1.diff(parse2)).show("结果图像"); + } +} +``` + +## 数据计算类案例 + +## 机器学习类案例 diff --git a/src_code/README-Chinese.md b/src_code/README-Chinese.md index ce0123a..21699f6 100644 --- a/src_code/README-Chinese.md +++ b/src_code/README-Chinese.md @@ -8,807 +8,48 @@ ### 更新日志 -* 框架版本:1.16 - 1.17 -* 将集成器的名称修改为“Integrator”。 -* 提供卷积函数的计算支持,能够通过卷积函数将特征放大同时缩小图像矩阵的元素数量。 - -```java -package zhao.algorithmMagic; - -import zhao.algorithmMagic.integrator.ImageRenderingIntegrator; -import zhao.algorithmMagic.integrator.launcher.ImageRenderingMarLauncher; -import zhao.algorithmMagic.operands.matrix.IntegerMatrix; -import zhao.algorithmMagic.operands.matrix.block.IntegerMatrixSpace; - -public class MAIN1 { - public static void main(String[] args) { - String s1 = "C:\\Users\\Liming\\Desktop\\fsDownload\\微信图片_1.jpg"; - IntegerMatrix integerMatrix; - { - // 设置权重 - IntegerMatrix weight = IntegerMatrix.parse( - new int[]{0, -1}, - new int[]{-1, 2}, - new int[]{0, 0} - ); - // 读取图像并获取到三通道矩阵空间 - IntegerMatrixSpace parse = IntegerMatrixSpace.parse(s1); - // 对图像进行卷积,获取三个色彩通道的矩阵空间的和 - integerMatrix = parse.foldingAndSum(2, 3, IntegerMatrixSpace.parse(weight, weight, weight)); - } - // 输出图片1的卷积图像文件 - ImageRenderingIntegrator image = new ImageRenderingIntegrator( - "image", - new ImageRenderingMarLauncher<>(integerMatrix, "C:\\Users\\Liming\\Desktop\\fsDownload\\res12.jpg", 1) - ); - if (image.run()) { - System.out.println("ok!!!"); - } - } -} -``` - -* 图像矩阵支持直接通过 show 函数在屏幕中显示出图像。 +* 框架版本:1.17 - x.xx +* 为图像矩阵添加diff函数的实现支持,同时为该矩阵对象添加强大的求差聚合计算功能。 ```java package zhao.algorithmMagic; import zhao.algorithmMagic.operands.matrix.ColorMatrix; -public class MAIN1 { - public static void main(String[] args) throws InterruptedException { - String s1 = "C:\\Users\\Liming\\Desktop\\fsDownload\\微信图片_1.jpg"; - ColorMatrix parse = ColorMatrix.parse(s1); - parse.show("image"); - Thread.sleep(1024); - parse.colorReversal(false); - parse.show("image"); - } -} -``` - -* 优化随机打乱函数 shuffle 中的最大随机打乱次数算法,使得其不会出现越界异常。 -* 优化通过列名随机访问矩阵中数据的函数逻辑,采用hash进行地址的映射。 - -```java -package zhao.algorithmMagic; - -import zhao.algorithmMagic.operands.matrix.ColumnDoubleMatrix; - -import java.util.Arrays; -import java.util.Random; +import java.awt.*; public class MAIN1 { public static void main(String[] args) { - // 构建一份矩阵数据 - double[][] data = new double[][]{ - new double[]{10, 11, 14, 10, 100}, - new double[]{11, 11, 14, 10, 100}, - new double[]{25, 20, 28, 20, 100}, - new double[]{26, 20, 28, 20, 100} - }; - // 将矩阵数据使用指定列与行名称的方式创建出来 - ColumnDoubleMatrix columnDoubleMatrix = ColumnDoubleMatrix.parse( - new String[]{"col1", "col2", "col3", "col4", "col5"}, - new String[]{"row1", "row2", "row3", "row4"}, - data - ); - // 使用不创建新矩阵的方式打乱其中的数据顺序 且最多打乱 10 次 - columnDoubleMatrix.shuffle(new Random(), false, 10); - // 打印出矩阵数据 - System.out.println(columnDoubleMatrix); - // 打印出矩阵中的 col2 列 在新版中该函数采取哈希实现 速度提升很明显 - System.out.println(Arrays.toString(columnDoubleMatrix.getArrayByColName("col2"))); - // 打印出矩阵中的 row2 行 在新版中该函数采取哈希实现 速度提升很明显 - System.out.println(Arrays.toString(columnDoubleMatrix.getArrayByRowName("row2"))); + // 获取一张图像的像素矩阵 + ColorMatrix parse1 = ColorMatrix.parseGrayscale("C:\\Users\\Liming\\Desktop\\fsdownload\\test1.bmp"); + // 将 parse1 进行二值化 + parse1.localBinary(ColorMatrix._G_, 30, 0, 0xffffff, 1); + // 将 parse1 矩阵腐蚀,然后将腐蚀的结果获取到 + ColorMatrix parse2 = parse1.erode(3, 3, true, Color.BLACK); + // 将两个矩阵进行做差运算,并查看做差之后的图像 该函数等同于取余做差 + (parse1.diff(parse2)).show("结果图像1"); + // 将两个矩阵进行 agg 函数的做差运算,这里使用的是 规整做差 + (parse1.agg(parse2, ColorMatrix.COLOR_DIFF_REGULATE)).show("结果图像2"); } } ``` -* 支持图像的快捷保存函数的调用操作,使得图像文件的保存变得更简单。 +* 修正二维数组拷贝时,只在在数组层深拷贝问题(二维度数组中的一维数组没有进行深拷贝)现在已经重新设计,使得深拷贝操作开始重新生效。 ```java package zhao.algorithmMagic; import zhao.algorithmMagic.operands.matrix.ColorMatrix; -public class MAIN1 { - public static void main(String[] args) { - String s1 = "C:\\Users\\zhao\\Desktop\\fsDownload\\微信图片_2.jpg"; - // 获取到图像矩阵对象 - ColorMatrix parse = ColorMatrix.parse(s1); - // 将图像在原图像的基础上进行颜色反转操作 - parse.colorReversal(false); - // 查看颜色反转之后的图像 - parse.show("image1"); - // 输出反转之后的图像 - parse.save("C:\\Users\\zhao\\Desktop\\fsDownload\\res123.jpg"); - } -} -``` - -* 增加了 "SaveMatrix" 接口,实现了该接口的一切矩阵都可以使用结构化或非结构化的保存。 - -```java -package zhao.algorithmMagic; - -import zhao.algorithmMagic.operands.matrix.ColorMatrix; +import java.awt.*; public class MAIN1 { public static void main(String[] args) { - String s1 = "C:\\Users\\Liming\\Desktop\\fsDownload\\微信图片_2.jpg"; - // 获取到图像矩阵对象 - ColorMatrix parse = ColorMatrix.parse(s1); - // 输出图像RGB文本数据 - parse.save("C:\\Users\\Liming\\Desktop\\fsDownload\\res1234.csv", ','); + // 获取一张图像的像素矩阵 + ColorMatrix parse1 = ColorMatrix.parseGrayscale("C:\\Users\\Liming\\Desktop\\fsdownload\\test1.bmp"); + Color[][] colors = parse1.copyToNewArrays(); } } ``` -* AS库中增加了数据分析库,使用SQL风格处理数据,通过该库可以方便的将数据库中的数据获取到内存中进行处理。 - -```java -package zhao.algorithmMagic; - -import zhao.algorithmMagic.operands.table.DataFrame; -import zhao.algorithmMagic.operands.table.FDataFrame; - -import java.sql.Connection; -import java.sql.DriverManager; -import java.sql.SQLException; - -public class MAIN1 { - public static void main(String[] args) throws SQLException { - // 准备数据库连接对象 - Connection connection = DriverManager.getConnection("jdbc:mysql://192.168.0.101:38243/tq_SCHOOL", "liming", "liming7887"); - // 将数据库数据对象加载成 FDF - DataFrame execute = FDataFrame.builder(connection) // 指定数据库连接 - .create("*") // 指定查询字段 - .from("STU_FIVE") // 指定查询表 - .primaryKey(0) // 指定内存AS表的主键(AS会自动建立行索引)数据库查询需要使用索引指定哦! - .execute();// 开始查询 - // 打印出 FDF 中的数据 - System.out.println(execute); - - // 开始查询 FDF - // 正序打印出 FDF 中 所有name长度为3的人员中男女生的人数 打印出前 3 行数据 - DataFrame select = execute - .select("name", "sex") // 查询其中的 name sex 列 - .where(v -> v.getCell(0).getStringValue().length() == 3) // 获取到其中的名字长度为 3 的数据行 - .groupBy("sex") // 按照 sex 列分组 - .count() // 将每一组进行统计 - .sort("count()") // 按照统计结果进行正序排序 - .limit(3); // 获取最多前3行数据 - System.out.println(select.into_outfile("C:\\Users\\zhao\\Desktop\\fsDownload\\res1.csv")); - // 打印存储 FDF 中的数据行数 - System.out.println("数据行数 = " + execute.count()); - // 打印出其中的信息 - System.out.println(execute.desc()); - // 获取到 赵凌宇 的信息 由于 name 列是主键,因此可以直接通过这里获取到数据 - System.out.println(execute.selectRow("赵凌宇")); - } -} -``` - -* 使用 FDataFrame 加载数据库与文件中的数据。 - -```java -package zhao.algorithmMagic; - -import zhao.algorithmMagic.operands.table.*; - -import java.sql.Connection; -import java.sql.DriverManager; -import java.sql.SQLException; - -public class MAIN1 { - public static void main(String[] args) throws SQLException { - // 准备数据库连接对象 - Connection connection = DriverManager.getConnection("jdbc:mysql://192.168.0.101:38243/tq_SCHOOL", "liming", "liming7887"); - // 将数据库数据对象加载成 FDF - DataFrame execute = FDataFrame.builder(connection) // 指定数据库连接 - .create("*") // 指定查询字段 - .from("STU_FIVE") // 指定查询表 - .where(" name = '赵凌宇' ") // 指定查询条件 - .primaryKey(0) // 指定内存AS表的主键(AS会自动建立行索引)数据库查询需要使用索引指定哦! - .execute();// 开始查询 - // 打印出 FDF 中的数据 - System.out.println(execute); - } -} -``` - -```java -package zhao.algorithmMagic; - -import zhao.algorithmMagic.operands.table.DataFrame; -import zhao.algorithmMagic.operands.table.FDataFrame; - -import java.io.File; -import java.sql.SQLException; -import java.util.Objects; - -public class MAIN1 { - public static void main(String[] args) { - // 准备文件对象 - File file = new File("C:\\Users\\zhao\\Desktop\\out\\res1.csv"); - // 使用 FDF 加载文件 - DataFrame execute1 = FDataFrame.builder(file) - // 文件对象的读取需要指定文本分隔符 - .setSep(',') - // 文件对象需要指定好列名称,不能使用 * 这里代表的不是查询,而是创建一个DF的列字段 - .create("id", "name", "sex") - // 文件对象需要使用lambda表达式进行数据的过滤 - .where(v -> Objects.equals(v.getCell(1).getStringValue(), "赵凌宇")) - // 文件对象的主键指定允许使用列名称 - .primaryKey("name") - // 执行查询 - .execute(); - // 打印出结果数据 - System.out.println(execute1); - } -} - ``` - -* 对FDF按照行名称进行limit操作 - -```java -package zhao.algorithmMagic; - -import zhao.algorithmMagic.operands.table.DataFrame; -import zhao.algorithmMagic.operands.table.FDataFrame; - -import java.io.File; -import java.sql.SQLException; - -public class MAIN1 { - public static void main(String[] args) { - // 准备文件对象 - File file = new File("C:\\Users\\zhao\\Desktop\\out\\res1.csv"); - // 使用 FDF 加载文件 - DataFrame execute1 = FDataFrame.builder(file) - // 文件对象的读取需要指定文本分隔符 - .setSep(',') - // 文件对象需要指定好列名称,不能使用 * 这里代表的不是查询,而是创建一个DF的列字段 - .create("id", "name", "sex") - // 文件对象的主键指定允许使用列名称 - .primaryKey("name") - // 执行查询 - .execute(); - // 打印出结果数据 这里打印出从 赵 到 贾 之间的数据行 - System.out.println(execute1.limit("赵", "贾")); - } -} -``` - -* 支持为列取别名以及新增数据行的操作。 - -```java -package zhao.algorithmMagic; - -import zhao.algorithmMagic.operands.table.DataFrame; -import zhao.algorithmMagic.operands.table.FDataFrame; -import zhao.algorithmMagic.operands.table.FieldCell; -import zhao.algorithmMagic.operands.table.FinalSeries; - -import java.io.File; -import java.io.IOException; -import java.sql.SQLException; - -public class MAIN1 { - public static void main(String[] args) { - // 准备文件对象 - File file = new File("C:\\Users\\zhao\\Desktop\\out\\res1.csv"); - // 使用 FDF 加载文件 - DataFrame execute1 = FDataFrame.builder(file) - // 文件对象的读取需要指定文本分隔符 - .setSep(',') - // 文件对象需要指定好列名称,不能使用 * 这里代表的不是查询,而是创建一个DF的列字段 - .create("id", "name", "sex") - // 文件对象的主键指定允许使用列名称 - .primaryKey("name") - // 执行查询 - .execute(); - // 按照性别分组,计算出男女生人数 - long start = System.currentTimeMillis(); - System.out.println( - execute1 - // 指定查询的列,并起别名 - .select(FieldCell.$("sex").as("性别")) - // 按照 sex 分组 - .groupBy("性别") - // 进行 组内的计数 - .count() - // 指定查询的列 并起别名 - .select( - FieldCell.$("性别"), - FieldCell.$("count()").as("人数") - ) - // 新增一行数据 - .insert(FinalSeries.parse("新", "10")) - ); - System.out.print("处理耗时(MS):"); - System.out.println(System.currentTimeMillis() - start); - } -} - -``` - -* 能够针对DF数据集对象中的所有数值进行行或列数据的函数式更新,同时支持DF数据对象到矩阵之间的转换。 - -```java -package zhao.algorithmMagic; - -import zhao.algorithmMagic.operands.matrix.IntegerMatrix; -import zhao.algorithmMagic.operands.table.DataFrame; -import zhao.algorithmMagic.operands.table.FDataFrame; -import zhao.algorithmMagic.operands.table.FieldCell; -import zhao.algorithmMagic.operands.table.FinalCell; -import zhao.algorithmMagic.operands.vector.IntegerVector; - -import java.io.File; - -public class MAIN1 { - public static void main(String[] args) { - // 准备文件对象 - File file = new File("C:\\Users\\zhao\\Desktop\\out\\res1.csv"); - // 使用 FDF 加载文件 - DataFrame execute1 = FDataFrame.builder(file) - // 文件对象的读取需要指定文本分隔符 - .setSep(',') - // 文件对象需要指定好列名称,不能使用 * 这里代表的不是查询,而是创建一个DF的列字段 - .create("id", "name", "sex") - // 文件对象的主键指定允许使用列名称 - .primaryKey("name") - // 执行查询 - .execute() - // 为列起别名 - .select( - FieldCell.$("id"), - FieldCell.$("name").as("名称"), - FieldCell.$("sex").as("性别") - ) - // 将性别列进行转换,男生为1 女生为0 - .updateCol(FieldCell.$("性别"), cell -> new FinalCell<>(cell.getStringValue().equals("男") ? 1 : 0)) - // 将行主键数值为ZLY的数据行中的所有单元格替换成为数据 405 - .updateRow("ZLY", cell -> new FinalCell<>(405)); - - // 打印出表中的行主键名称为 405 的数据行 - System.out.println(execute1.selectRow("405")); - long start = System.currentTimeMillis(); - // 将表转换成为一个整形矩阵对象,该操作会将DF对象中的所有数值试图转换成为 col.count()*3 的矩阵对象 - IntegerMatrix parse = IntegerMatrix.parse(execute1, execute1.count().getIntValue(), 3); - System.out.println(IntegerVector.parse(parse.getArrayByColIndex(2))); - System.out.print("处理耗时(MS):"); - System.out.println(System.currentTimeMillis() - start); - } -} -``` - -* 支持通过一个网络 URL 对象获取到有关图像的数据,并将其转换成为一个图像矩阵。 - -```java -package zhao.algorithmMagic; - -import zhao.algorithmMagic.operands.matrix.ColorMatrix; - -import java.net.MalformedURLException; -import java.net.URL; - -public class MAIN1 { - public static void main(String[] args) throws MalformedURLException { - // 准备图像的URL对象 - URL url = new URL("https://user-images.githubusercontent.com/113756063/194830221-abe24fcc-484b-4769-b3b7-ec6d8138f436.png"); - // 解析URL获取到图像矩阵 - ColorMatrix parse1 = ColorMatrix.parse(url); - // 解析URL获取到图像的灰度矩阵 - ColorMatrix parse2 = ColorMatrix.parseGrayscale(url); - // 查看图像 - parse1.show("image"); - parse2.show("image"); - } -} -``` - -* 支持进行色彩二值化规整覆盖,能够通过指定通道的色彩数值,显示出更多的图像特征,或去除更多的冗余特征。 - -```java -package zhao.algorithmMagic; - -import zhao.algorithmMagic.operands.matrix.ColorMatrix; - -import java.net.MalformedURLException; -import java.net.URL; - -public class MAIN1 { - public static void main(String[] args) throws MalformedURLException { - // 准备图像的URL对象 - URL url = new URL("https://user-images.githubusercontent.com/113756063/194830221-abe24fcc-484b-4769-b3b7-ec6d8138f436.png"); - // 解析URL获取到图像矩阵 - ColorMatrix parse1 = ColorMatrix.parse(url); - // 将 URL 图像矩阵中所有 G 通道颜色数值大于 40 的颜色变更为黑色,反之变更为白色 - // 在这里由于选择了 G 通道 因此 绿色越深 越有可能变为白色 - parse1.globalBinary(ColorMatrix._G_, 40, 0, 0xffffff); - // 也可以使用其它颜色通道进行色彩的调整 - parse1.globalBinary(ColorMatrix._R_, 40, 0, 0xffffff); - parse1.globalBinary(ColorMatrix._B_, 40, 0, 0xffffff); - // 查看结果图像 - parse1.show("image"); - } -} -``` - -* 支持进行图像 ASCII 图的构造,您可以快速的将一个图片的 ASCII 构造出来,不过请您确保图像尺寸足够小。 - -```java -package zhao.algorithmMagic; - -import zhao.algorithmMagic.operands.matrix.ColorMatrix; - -import java.io.File; -import java.net.MalformedURLException; -import java.net.URL; - -public class MAIN1 { - public static void main(String[] args) throws MalformedURLException { - // 准备图像的URL对象 - URL url = new URL("https://user-images.githubusercontent.com/113756063/194830221-abe24fcc-484b-4769-b3b7-ec6d8138f436.png"); - // 解析URL获取到图像矩阵 - ColorMatrix parse1 = ColorMatrix.parse(url); - // 查看结果图像 - parse1.show("image"); - // 输出图像的 ASCII 数值,输出规则为 G 通道颜色数值 大于 40 的 输出符号 'A' 其它输出符号 ' ' - parse1.save( - new File("C:\\Users\\zhao\\Desktop\\fsDownload\\res.txt"), - ColorMatrix._G_, 40, 'A', ' ' - ); - } -} -``` - -* 能够手动创建一个空的 DataFrame 对象,并自主操作其中的数据。 - -```java -package zhao.algorithmMagic; - -import zhao.algorithmMagic.operands.table.DataFrame; -import zhao.algorithmMagic.operands.table.FDataFrame; -import zhao.algorithmMagic.operands.table.FieldCell; -import zhao.algorithmMagic.operands.table.FinalSeries; - -public class MAIN1 { - public static void main(String[] args) { - // 创建一个空 DF 对象 以 name 列作为行主键索引 - FDataFrame create = FDataFrame.select(FieldCell.parse("name", "sex", "phoneNum"), 1); - // 插入一些数据 - DataFrame insert = create.insert( - FinalSeries.parse("zhao", "M", "110xxxxxxxx"), - FinalSeries.parse("tang", "W", "110xxxxxxxx"), - FinalSeries.parse("yang", "M", "110xxxxxxxx") - ); - // 查看数据集 - System.out.println(insert); - // 将其中的 name 列 sex 列 查询 - System.out.println( - insert.select( - FieldCell.$("name").as("AllName"), - FieldCell.$("sex").as("AllSex") - ) - ); - } -} -``` - -* 支持在 group 的时候指定 where子句,使得计算效率大大增强 - -```java -package zhao.algorithmMagic; - -import zhao.algorithmMagic.operands.table.DataFrame; -import zhao.algorithmMagic.operands.table.FDataFrame; -import zhao.algorithmMagic.operands.table.FieldCell; -import zhao.algorithmMagic.operands.table.FinalSeries; - -import java.net.MalformedURLException; - -public class MAIN1 { - public static void main(String[] args) { - // 创建一个空 DF 对象 以 name 列作为行主键索引 - FDataFrame create = FDataFrame.select(FieldCell.parse("name", "sex", "phoneNum"), 1); - // 插入一些数据 - DataFrame insert = create.insert( - FinalSeries.parse("zhao1", "M", "110xxxxxxxx"), - FinalSeries.parse("tang2", "W", "120xxxxxxxx"), - FinalSeries.parse("yang3", "W", "110xxxxxxxx"), - FinalSeries.parse("zhao4", "M", "120xxxxxxxx"), - FinalSeries.parse("tang5", "W", "110xxxxxxxx"), - FinalSeries.parse("yang6", "W", "110xxxxxxxx"), - FinalSeries.parse("zhao7", "M", "120xxxxxxxx"), - FinalSeries.parse("tang8", "W", "110xxxxxxxx"), - FinalSeries.parse("yang9", "W", "110xxxxxxxx") - ); - // 查看数据集 - System.out.println(insert); - // 将其中手机号前三位不为 120 的数据行按照其中的 sex 分组 在这里直接使用分组时过滤即可 - System.out.println( - insert.groupBy("sex", v -> { - // 获取到手机号的字符串 - String s = v.getCell(2).toString(); - // 判断前 3 个字符是否为 120 (是否以 120 开头) 如果是就不添加 - return !s.startsWith("120"); - }).count() - ); - } -} -``` - -* 支持局部二值化操作,能够有效的实现图像的二值化处理,相较于全局二值化,函数更加灵活。 - -```java -package zhao.algorithmMagic; - -import zhao.algorithmMagic.operands.matrix.ColorMatrix; - -import java.net.MalformedURLException; -import java.net.URL; - -public class MAIN1 { - public static void main(String[] args) throws MalformedURLException { - // 获取到图像矩阵 - ColorMatrix parse = ColorMatrix.parse(new URL("https://img-blog.csdnimg.cn/img_convert/5765bdab08ef6e117d434e7e225b9013.png")); - parse.show("image"); - System.out.println("ok!!!"); - System.out.println("长 = " + parse.getRowCount()); - System.out.println("宽 = " + parse.getColCount()); - // 将图像进行局部二值化 - parse.localBinary( - // 指定本次二值化选择的颜色通道 - ColorMatrix._G_, - // 指定本次二值化选出的局部图像矩阵数量 - 100, - // 指定本次二值化中局部矩阵中大于局部阈值的颜色编码 - 0xffffff, - // 指定本次二值化中局部矩阵中小于局部阈值的颜色编码 - 0, - // 指定本次二值化中局部阈值生成后要进行的微调数值,这里是降低20个阈值数值 - -30 - ); - // 查看结果数据 - parse.show("image"); - } -} -``` - -* DF对象数据支持保存为 HTML 文件,能够实现有效的自动化页面绘制操作。 - -```java -package zhao.algorithmMagic; - -import zhao.algorithmMagic.operands.table.DataFrame; -import zhao.algorithmMagic.operands.table.FDataFrame; -import zhao.algorithmMagic.operands.table.FieldCell; -import zhao.algorithmMagic.operands.table.FinalSeries; - -public class MAIN1 { - public static void main(String[] args) { - // 创建一个空 DF 对象 以 name 列作为行主键索引 - FDataFrame create = FDataFrame.select(FieldCell.parse("name", "sex", "phoneNum", "salary"), 1); - // 插入一些数据 - DataFrame insert = create.insert( - FinalSeries.parse("zhao1", "M", "110xxxxxxxx", "30000"), - FinalSeries.parse("tang2", "W", "120xxxxxxxx", "30000"), - FinalSeries.parse("yang3", "W", "110xxxxxxxx", "30000"), - FinalSeries.parse("zhao4", "M", "120xxxxxxxx", "30000"), - FinalSeries.parse("tang5", "W", "110xxxxxxxx", "30000"), - FinalSeries.parse("yang6", "W", "110xxxxxxxx", "30000"), - FinalSeries.parse("zhao7", "M", "120xxxxxxxx", "30000"), - FinalSeries.parse("tang8", "W", "110xxxxxxxx", "30000"), - FinalSeries.parse("yang9", "W", "110xxxxxxxx", "30000") - ); - // 输出表的HTML 然后查看表中的数据 - System.out.println( - insert.into_outHtml("C:\\Users\\Liming\\Desktop\\fsDownload\\res11234.html", "myTable") - ); - } -} -``` - -* 图像开始支持 add 函数,在add函数的操作下,可以实现图像函数的重叠合并! - -```java -package zhao.algorithmMagic; - -import zhao.algorithmMagic.operands.matrix.ColorMatrix; - -import java.sql.SQLException; - -public class MAIN1 { - public static void main(String[] args) { - // 将一些图像文件转换成为一个图像矩阵对象 - ColorMatrix colorMatrix1 = ColorMatrix.parse("C:\\Users\\Liming\\Desktop\\fsDownload\\test.bmp"); - ColorMatrix colorMatrix2 = ColorMatrix.parse("C:\\Users\\Liming\\Desktop\\fsDownload\\test1.bmp"); - // 将 colorMatrix2 + colorMatrix1 的结果图像展示出来 - (colorMatrix1.add(colorMatrix2)).show("image"); - } -} -``` - -* 支持自定义聚合以及内置数据方案的聚合操作。 - -```java -package zhao.algorithmMagic; - -import zhao.algorithmMagic.operands.matrix.ColorMatrix; - -import java.sql.SQLException; - -public class MAIN1 { - public static void main(String[] args) { - // 将一些图像文件转换成为一个图像矩阵对象 - ColorMatrix colorMatrix1 = ColorMatrix.parse("C:\\Users\\Liming\\Desktop\\fsDownload\\test.bmp"); - ColorMatrix colorMatrix2 = ColorMatrix.parse("C:\\Users\\Liming\\Desktop\\fsDownload\\test1.bmp"); - // 使用 agg 函数以及内置的计算方案进行两个矩阵的合并 - // 下面函数中的第二个形参代表的就是矩阵中元素的聚合逻辑,这里使用的是求和,并对越界颜色数值进行规整的逻辑实现 - colorMatrix1.agg(colorMatrix2, ColorMatrix.COLOR_SUM_REGULATE).show("image"); - } -} -``` - -* 图像矩阵支持腐蚀运算函数,在该函数中可以去除掉冗余特征数据。 - -```java -package zhao.algorithmMagic; - -import zhao.algorithmMagic.operands.matrix.ColorMatrix; - -import java.sql.SQLException; - - -public class MAIN1 { - public static void main(String[] args) { - // 将一些图像文件转换成为一个图像矩阵对象 - ColorMatrix colorMatrix1 = ColorMatrix.parseGrayscale("C:\\Users\\Liming\\Desktop\\fsDownload\\test2.bmp"); - // 对图像进行二值化 - colorMatrix1.globalBinary(ColorMatrix._G_, 100, 0xffffff, 0); - colorMatrix1.show("腐蚀之前的 image"); - // 开始对图像矩阵进行腐蚀操作 - colorMatrix1.erode(2, 2, false).show("腐蚀之后的 image"); - } -} -``` - -* 支持列的添加,但是需要注意的是列的添加会返回一个新的 DataFrame DF中的数据将会被浅拷贝出来。 - -```java -package zhao.algorithmMagic; - -import zhao.algorithmMagic.operands.table.*; - -import java.sql.SQLException; - -public class MAIN1 { - public static void main(String[] args) { - // 创建一个空的 DataFrame 对象 - FDataFrame select = FDataFrame.select( - FieldCell.parse("id", "name", "sex", "age"), 1 - ); - // 手动插入数据 - select.insert( - FinalSeries.parse("1", "zhao", "M", "19"), - FinalSeries.parse("2", "tang", "W", "18"), - FinalSeries.parse("3", "yang", "W", "20"), - FinalSeries.parse("4", "shen", "W", "19") - ); - // 打印出 DF 对象有关的信息 - System.out.println( - select.desc() - ); - // 添加一列数据,用于表示年龄是否为偶数 - DataFrame ageIsE = select.insertColGetNew( - // 新列的列名称 - FieldCell.$("AgeIsE"), - // 新列的数值生成逻辑 如果第 4 列(index == 3)的数值 % 2 == 0 就是true - cells -> cells.getCell(3).getIntValue() % 2 == 0 ? new FinalCell<>(true) : new FinalCell<>(false) - ); - System.out.println(ageIsE); - } -} -``` - -* 支持表数据之间的运算操作,能够将表中所有的数值类型进行求和与做差计算,计算之后将返回新DF对象。 - -```java -package zhao.algorithmMagic; - -import zhao.algorithmMagic.operands.table.*; - -public class MAIN1 { - public static void main(String[] args) { - // 创建一个空的 DataFrame 对象 - FDataFrame select1 = FDataFrame.select( - FieldCell.parse("id", "name", "sex", "age"), 1 - ); - // 手动插入数据 - select1.insert( - FinalSeries.parse("1", "zhao", "M", "19"), - FinalSeries.parse("2", "tang", "W", "18"), - FinalSeries.parse("3", "yang", "W", "20"), - FinalSeries.parse("4", "shen", "W", "19") - ); - // 创建一个空的 DataFrame 对象 - FDataFrame select2 = FDataFrame.select( - FieldCell.parse("id", "name", "sex", "age"), 1 - ); - // 手动插入数据 - select2.insert( - FinalSeries.parse("1", "zhao", "M", "19"), - FinalSeries.parse("2", "tang", "W", "18"), - FinalSeries.parse("3", "yang", "W", "20") - ); - // 进行求和与做差的运算操作 - System.out.println(select1.add(select2)); - System.out.println(select1.diff(select2)); - } -} -``` - -* DataFrame数据对象指定列合并 - -```java -package zhao.algorithmMagic; - -import zhao.algorithmMagic.operands.table.*; - -import java.sql.SQLException; - -public class MAIN1 { - public static void main(String[] args) { - // 创建一个空的 DataFrame 对象 - FDataFrame select = FDataFrame.select( - FieldCell.parse("id", "name", "sex", "age"), 1 - ); - // 手动插入数据 然后进行分组 - GroupDataFrameData groupDataFrameData = select.insert( - FinalSeries.parse("1", "zhao", "M", "19"), - FinalSeries.parse("2", "tang", "W", "18"), - FinalSeries.parse("3", "yang", "W", "20") - ).groupBy("sex"); - // 打印出 M 组的数据 - System.out.println(groupDataFrameData.getDFByGroup("M")); - // 打印出 W 组的数据 - System.out.println(groupDataFrameData.getDFByGroup("W")); - } -} -``` - -* 新版本注意事项:此版本中的所有依赖被标记为 provided 这样可以最大化的降低冗余程度,能够根据自己的实际项目来进行相关的配置,因此在进行AS库注入的时候请将以下的必须项一键导入。 - -```xml - - - - - org.apache.logging.log4j - log4j-slf4j-impl - 2.20.0 - - - - - - org.apache.logging.log4j - log4j-api - 2.20.0 - - - - - org.apache.logging.log4j - log4j-core - 2.20.0 - - - -``` - ### Version update date : xx xx-xx-xx \ No newline at end of file diff --git a/src_code/README.md b/src_code/README.md index c5eacca..9a47cca 100644 --- a/src_code/README.md +++ b/src_code/README.md @@ -8,784 +8,51 @@ ### Update log: -* Framework version: 1.16 - 1.17 -* Change the name of the integrator to Integrator. -* It provides the calculation support of convolution function, which can enlarge the features and reduce the number of - elements in the image matrix. - -```java -package zhao.algorithmMagic; - -import zhao.algorithmMagic.integrator.ImageRenderingIntegrator; -import zhao.algorithmMagic.integrator.launcher.ImageRenderingMarLauncher; -import zhao.algorithmMagic.operands.matrix.IntegerMatrix; -import zhao.algorithmMagic.operands.matrix.block.IntegerMatrixSpace; - -public class MAIN1 { - public static void main(String[] args) { - String s1 = "C:\\Users\\Liming\\Desktop\\fsDownload\\微信图片_1.jpg"; - IntegerMatrix integerMatrix; - { - // 设置权重 - IntegerMatrix weight = IntegerMatrix.parse( - new int[]{0, -1}, - new int[]{-1, 2}, - new int[]{0, 0} - ); - // 读取图像并获取到三通道矩阵空间 - IntegerMatrixSpace parse = IntegerMatrixSpace.parse(s1); - // 对图像进行卷积,获取三个色彩通道的矩阵空间的和 - integerMatrix = parse.foldingAndSum(2, 3, IntegerMatrixSpace.parse(weight, weight, weight)); - } - // 输出图片1的卷积图像文件 - ImageRenderingIntegrator image = new ImageRenderingIntegrator( - "image", - new ImageRenderingMarLauncher<>(integerMatrix, "C:\\Users\\Liming\\Desktop\\fsDownload\\res12.jpg", 1) - ); - if (image.run()) { - System.out.println("ok!!!"); - } - } -} -``` - -* The image matrix supports displaying images on the screen directly through the show function. - -```java -package zhao.algorithmMagic; - -import zhao.algorithmMagic.operands.matrix.ColorMatrix; - -public class MAIN1 { - public static void main(String[] args) throws InterruptedException { - String s1 = "C:\\Users\\Liming\\Desktop\\fsDownload\\微信图片_1.jpg"; - ColorMatrix parse = ColorMatrix.parse(s1); - parse.show("image"); - Thread.sleep(1024); - parse.colorReversal(false); - parse.show("image"); - } -} -``` - -* Optimize the algorithm of maximum random scrambling times in the random scrambling function shuffle, so that it will - not have out-of-bounds exceptions. -* Optimize the function logic of random access to data in the matrix through column names, and use hash to map - addresses. - -```java -package zhao.algorithmMagic; - -import zhao.algorithmMagic.operands.matrix.ColumnDoubleMatrix; - -import java.util.Arrays; -import java.util.Random; - -public class MAIN1 { - public static void main(String[] args) { - // 构建一份矩阵数据 - double[][] data = new double[][]{ - new double[]{10, 11, 14, 10, 100}, - new double[]{11, 11, 14, 10, 100}, - new double[]{25, 20, 28, 20, 100}, - new double[]{26, 20, 28, 20, 100} - }; - // 将矩阵数据使用指定列与行名称的方式创建出来 - ColumnDoubleMatrix columnDoubleMatrix = ColumnDoubleMatrix.parse( - new String[]{"col1", "col2", "col3", "col4", "col5"}, - new String[]{"row1", "row2", "row3", "row4"}, - data - ); - // 使用不创建新矩阵的方式打乱其中的数据顺序 且最多打乱 10 次 - columnDoubleMatrix.shuffle(new Random(), false, 10); - // 打印出矩阵数据 - System.out.println(columnDoubleMatrix); - // 打印出矩阵中的 col2 列 在新版中该函数采取哈希实现 速度提升很明显 - System.out.println(Arrays.toString(columnDoubleMatrix.getArrayByColName("col2"))); - // 打印出矩阵中的 row2 行 在新版中该函数采取哈希实现 速度提升很明显 - System.out.println(Arrays.toString(columnDoubleMatrix.getArrayByRowName("row2"))); - } -} -``` - -* It supports the call operation of the quick save function of the image, making the saving of the image file easier. - -```java -package zhao.algorithmMagic; - -import zhao.algorithmMagic.operands.matrix.ColorMatrix; - -public class MAIN1 { - public static void main(String[] args) { - String s1 = "C:\\Users\\zhao\\Desktop\\fsDownload\\微信图片_2.jpg"; - // 获取到图像矩阵对象 - ColorMatrix parse = ColorMatrix.parse(s1); - // 将图像在原图像的基础上进行颜色反转操作 - parse.colorReversal(false); - // 查看颜色反转之后的图像 - parse.show("image1"); - // 输出反转之后的图像 - parse.save("C:\\Users\\zhao\\Desktop\\fsDownload\\res123.jpg"); - } -} -``` - -* The "SaveMatrix" interface is added. All matrices that implement this interface can be saved in structured or - unstructured way. - -```java -package zhao.algorithmMagic; - -import zhao.algorithmMagic.operands.matrix.ColorMatrix; - -public class MAIN1 { - public static void main(String[] args) { - String s1 = "C:\\Users\\Liming\\Desktop\\fsDownload\\微信图片_2.jpg"; - // 获取到图像矩阵对象 - ColorMatrix parse = ColorMatrix.parse(s1); - // 输出图像RGB文本数据 - parse.save("C:\\Users\\Liming\\Desktop\\fsDownload\\res1234.csv", ','); - } -} -``` - -* A data analysis library is added to the AS library, which uses SQL styles to process data. Through this library, data - in the database can be easily obtained into memory for processing. - -```java -package zhao.algorithmMagic; - -import zhao.algorithmMagic.operands.table.DataFrame; -import zhao.algorithmMagic.operands.table.FDataFrame; - -import java.sql.Connection; -import java.sql.DriverManager; -import java.sql.SQLException; - -public class MAIN1 { - public static void main(String[] args) throws SQLException { - // 准备数据库连接对象 - Connection connection = DriverManager.getConnection("jdbc:mysql://192.168.0.101:38243/tq_SCHOOL", "liming", "liming7887"); - // 将数据库数据对象加载成 FDF - DataFrame execute = FDataFrame.builder(connection) // 指定数据库连接 - .create("*") // 指定查询字段 - .from("STU_FIVE") // 指定查询表 - .primaryKey(0) // 指定内存AS表的主键(AS会自动建立行索引)数据库查询需要使用索引指定哦! - .execute();// 开始查询 - // 打印出 FDF 中的数据 - System.out.println(execute); - - // 开始查询 FDF - // 正序打印出 FDF 中 所有name长度为3的人员中男女生的人数 打印出前 3 行数据 - DataFrame select = execute - .select("name", "sex") // 查询其中的 name sex 列 - .where(v -> v.getCell(0).getStringValue().length() == 3) // 获取到其中的名字长度为 3 的数据行 - .groupBy("sex") // 按照 sex 列分组 - .count() // 将每一组进行统计 - .sort("count()") // 按照统计结果进行正序排序 - .limit(3); // 获取最多前3行数据 - System.out.println(select.into_outfile("C:\\Users\\zhao\\Desktop\\fsDownload\\res1.csv")); - // 打印存储 FDF 中的数据行数 - System.out.println("数据行数 = " + execute.count()); - // 打印出其中的信息 - System.out.println(execute.desc()); - // 获取到 赵凌宇 的信息 由于 name 列是主键,因此可以直接通过这里获取到数据 - System.out.println(execute.selectRow("赵凌宇")); - } -} -``` - -* Use FDataFrame to load the data in the database and file. - -```java -package zhao.algorithmMagic; - -import zhao.algorithmMagic.operands.table.*; - -import java.sql.Connection; -import java.sql.DriverManager; -import java.sql.SQLException; - -public class MAIN1 { - public static void main(String[] args) throws SQLException { - // 准备数据库连接对象 - Connection connection = DriverManager.getConnection("jdbc:mysql://192.168.0.101:38243/tq_SCHOOL", "liming", "liming7887"); - // 将数据库数据对象加载成 FDF - DataFrame execute = FDataFrame.builder(connection) // 指定数据库连接 - .create("*") // 指定查询字段 - .from("STU_FIVE") // 指定查询表 - .where(" name = '赵凌宇' ") // 指定查询条件 - .primaryKey(0) // 指定内存AS表的主键(AS会自动建立行索引)数据库查询需要使用索引指定哦! - .execute();// 开始查询 - // 打印出 FDF 中的数据 - System.out.println(execute); - } -} -``` - -```java -package zhao.algorithmMagic; - -import zhao.algorithmMagic.operands.table.DataFrame; -import zhao.algorithmMagic.operands.table.FDataFrame; - -import java.io.File; -import java.sql.SQLException; -import java.util.Objects; - -public class MAIN1 { - public static void main(String[] args) { - // 准备文件对象 - File file = new File("C:\\Users\\zhao\\Desktop\\out\\res1.csv"); - // 使用 FDF 加载文件 - DataFrame execute1 = FDataFrame.builder(file) - // 文件对象的读取需要指定文本分隔符 - .setSep(',') - // 文件对象需要指定好列名称,不能使用 * 这里代表的不是查询,而是创建一个DF的列字段 - .create("id", "name", "sex") - // 文件对象需要使用lambda表达式进行数据的过滤 - .where(v -> Objects.equals(v.getCell(1).getStringValue(), "赵凌宇")) - // 文件对象的主键指定允许使用列名称 - .primaryKey("name") - // 执行查询 - .execute(); - // 打印出结果数据 - System.out.println(execute1); - } -} - ``` - -* Limit FDF by line name - -```java -package zhao.algorithmMagic; - -import zhao.algorithmMagic.operands.table.DataFrame; -import zhao.algorithmMagic.operands.table.FDataFrame; - -import java.io.File; -import java.sql.SQLException; - -public class MAIN1 { - public static void main(String[] args) { - // 准备文件对象 - File file = new File("C:\\Users\\zhao\\Desktop\\out\\res1.csv"); - // 使用 FDF 加载文件 - DataFrame execute1 = FDataFrame.builder(file) - // 文件对象的读取需要指定文本分隔符 - .setSep(',') - // 文件对象需要指定好列名称,不能使用 * 这里代表的不是查询,而是创建一个DF的列字段 - .create("id", "name", "sex") - // 文件对象的主键指定允许使用列名称 - .primaryKey("name") - // 执行查询 - .execute(); - // 打印出结果数据 这里打印出从 赵 到 贾 之间的数据行 - System.out.println(execute1.limit("赵", "贾")); - } -} -``` - -* Support the operations of aliasing columns and adding data rows. - -```java -package zhao.algorithmMagic; - -import zhao.algorithmMagic.operands.table.DataFrame; -import zhao.algorithmMagic.operands.table.FDataFrame; -import zhao.algorithmMagic.operands.table.FieldCell; - -import java.io.File; -import java.sql.SQLException; - -public class MAIN1 { - public static void main(String[] args) { - // 准备文件对象 - File file = new File("C:\\Users\\zhao\\Desktop\\out\\res1.csv"); - // 使用 FDF 加载文件 - DataFrame execute1 = FDataFrame.builder(file) - // 文件对象的读取需要指定文本分隔符 - .setSep(',') - // 文件对象需要指定好列名称,不能使用 * 这里代表的不是查询,而是创建一个DF的列字段 - .create("id", "name", "sex") - // 文件对象的主键指定允许使用列名称 - .primaryKey("name") - // 执行查询 - .execute(); - // 按照性别分组,计算出男女生人数 - System.out.println( - execute1 - // 指定查询的列,并起别名 - .select(FieldCell.$("sex").as("性别")) - // 按照 sex 分组 - .groupBy("性别") - // 进行 组内的计数 - .count() - // 指定查询的列 并起别名 - .select( - FieldCell.$("性别"), - FieldCell.$("count()").as("人数") - ) - ); - } -} -``` - -* Capable of functional update of row or column data for all values in DF data set objects, and supports conversion from - DF data objects to matrices. - -```java -package zhao.algorithmMagic; - -import zhao.algorithmMagic.operands.matrix.IntegerMatrix; -import zhao.algorithmMagic.operands.table.DataFrame; -import zhao.algorithmMagic.operands.table.FDataFrame; -import zhao.algorithmMagic.operands.table.FieldCell; -import zhao.algorithmMagic.operands.table.FinalCell; -import zhao.algorithmMagic.operands.vector.IntegerVector; - -import java.io.File; - -public class MAIN1 { - public static void main(String[] args) { - // 准备文件对象 - File file = new File("C:\\Users\\zhao\\Desktop\\out\\res1.csv"); - // 使用 FDF 加载文件 - DataFrame execute1 = FDataFrame.builder(file) - // 文件对象的读取需要指定文本分隔符 - .setSep(',') - // 文件对象需要指定好列名称,不能使用 * 这里代表的不是查询,而是创建一个DF的列字段 - .create("id", "name", "sex") - // 文件对象的主键指定允许使用列名称 - .primaryKey("name") - // 执行查询 - .execute() - // 为列起别名 - .select( - FieldCell.$("id"), - FieldCell.$("name").as("名称"), - FieldCell.$("sex").as("性别") - ) - // 将性别列进行转换,男生为1 女生为0 - .updateCol(FieldCell.$("性别"), cell -> new FinalCell<>(cell.getStringValue().equals("男") ? 1 : 0)) - // 将行主键数值为ZLY的数据行中的所有单元格替换成为数据 405 - .updateRow("ZLY", cell -> new FinalCell<>(405)); - - // 打印出表中的行主键名称为 405 的数据行 - System.out.println(execute1.selectRow("405")); - long start = System.currentTimeMillis(); - // 将表转换成为一个整形矩阵对象,该操作会将DF对象中的所有数值试图转换成为 col.count()*3 的矩阵对象 - IntegerMatrix parse = IntegerMatrix.parse(execute1, execute1.count().getIntValue(), 3); - System.out.println(IntegerVector.parse(parse.getArrayByColIndex(2))); - System.out.print("处理耗时(MS):"); - System.out.println(System.currentTimeMillis() - start); - } -} -``` - -* Support for obtaining image related data through a web URL object and converting it into an image matrix. - -```java -package zhao.algorithmMagic; - -import zhao.algorithmMagic.operands.matrix.ColorMatrix; - -import java.net.MalformedURLException; -import java.net.URL; - -public class MAIN1 { - public static void main(String[] args) throws MalformedURLException { - // 准备图像的URL对象 - URL url = new URL("https://user-images.githubusercontent.com/113756063/194830221-abe24fcc-484b-4769-b3b7-ec6d8138f436.png"); - // 解析URL获取到图像矩阵 - ColorMatrix parse1 = ColorMatrix.parse(url); - // 解析URL获取到图像的灰度矩阵 - ColorMatrix parse2 = ColorMatrix.parseGrayscale(url); - // 查看图像 - parse1.show("image"); - parse2.show("image"); - } -} -``` - -* It supports color binarization and regularization coverage, and can display more image features or remove more - redundant features by specifying the color values of the channel. - -```java -package zhao.algorithmMagic; - -import zhao.algorithmMagic.operands.matrix.ColorMatrix; - -import java.net.MalformedURLException; -import java.net.URL; - -public class MAIN1 { - public static void main(String[] args) throws MalformedURLException { - // 准备图像的URL对象 - URL url = new URL("https://user-images.githubusercontent.com/113756063/194830221-abe24fcc-484b-4769-b3b7-ec6d8138f436.png"); - // 解析URL获取到图像矩阵 - ColorMatrix parse1 = ColorMatrix.parse(url); - // 将 URL 图像矩阵中所有 G 通道颜色数值大于 40 的颜色变更为黑色,反之变更为白色 - // 在这里由于选择了 G 通道 因此 绿色越深 越有可能变为白色 - parse1.globalBinary(ColorMatrix._G_, 40, 0, 0xffffff); - // 也可以使用其它颜色通道进行色彩的调整 - parse1.globalBinary(ColorMatrix._R_, 40, 0, 0xffffff); - parse1.globalBinary(ColorMatrix._B_, 40, 0, 0xffffff); - // 查看结果图像 - parse1.show("image"); - } -} -``` - -* Supports the construction of image ASCII images. You can quickly construct an image in ASCII, but please ensure that - the image size is small enough. - -```java -package zhao.algorithmMagic; - -import zhao.algorithmMagic.operands.matrix.ColorMatrix; - -import java.io.File; -import java.net.MalformedURLException; -import java.net.URL; - -public class MAIN1 { - public static void main(String[] args) throws MalformedURLException { - // 准备图像的URL对象 - URL url = new URL("https://user-images.githubusercontent.com/113756063/194830221-abe24fcc-484b-4769-b3b7-ec6d8138f436.png"); - // 解析URL获取到图像矩阵 - ColorMatrix parse1 = ColorMatrix.parse(url); - // 查看结果图像 - parse1.show("image"); - // 输出图像的 ASCII 数值,输出规则为 G 通道颜色数值 大于 40 的 输出符号 'A' 其它输出符号 ' ' - parse1.save( - new File("C:\\Users\\zhao\\Desktop\\fsDownload\\res.txt"), - ColorMatrix._G_, 40, 'A', ' ' - ); - } -} -``` - -* Ability to manually create an empty DataFrame object and independently manipulate the data therein. - -```java -package zhao.algorithmMagic; - -import zhao.algorithmMagic.operands.table.DataFrame; -import zhao.algorithmMagic.operands.table.FDataFrame; -import zhao.algorithmMagic.operands.table.FieldCell; -import zhao.algorithmMagic.operands.table.FinalSeries; - -public class MAIN1 { - public static void main(String[] args) { - // 创建一个空 DF 对象 以 name 列作为行主键索引 - FDataFrame create = FDataFrame.select(FieldCell.parse("name", "sex", "phoneNum"), 1); - // 插入一些数据 - DataFrame insert = create.insert( - FinalSeries.parse("zhao", "M", "110xxxxxxxx"), - FinalSeries.parse("tang", "W", "110xxxxxxxx"), - FinalSeries.parse("yang", "M", "110xxxxxxxx") - ); - // 查看数据集 - System.out.println(insert); - // 将其中的 name 列 sex 列 查询 - System.out.println( - insert.select( - FieldCell.$("name").as("AllName"), - FieldCell.$("sex").as("AllSex") - ) - ); - } -} -``` - -* 支持在 group 的时候指定 where子句,使得计算效率大大增强 - -```java -package zhao.algorithmMagic; - -import zhao.algorithmMagic.operands.table.DataFrame; -import zhao.algorithmMagic.operands.table.FDataFrame; -import zhao.algorithmMagic.operands.table.FieldCell; -import zhao.algorithmMagic.operands.table.FinalSeries; - -import java.net.MalformedURLException; - -public class MAIN1 { - public static void main(String[] args) { - // 创建一个空 DF 对象 以 name 列作为行主键索引 - FDataFrame create = FDataFrame.select(FieldCell.parse("name", "sex", "phoneNum"), 1); - // 插入一些数据 - DataFrame insert = create.insert( - FinalSeries.parse("zhao1", "M", "110xxxxxxxx"), - FinalSeries.parse("tang2", "W", "120xxxxxxxx"), - FinalSeries.parse("yang3", "W", "110xxxxxxxx"), - FinalSeries.parse("zhao4", "M", "120xxxxxxxx"), - FinalSeries.parse("tang5", "W", "110xxxxxxxx"), - FinalSeries.parse("yang6", "W", "110xxxxxxxx"), - FinalSeries.parse("zhao7", "M", "120xxxxxxxx"), - FinalSeries.parse("tang8", "W", "110xxxxxxxx"), - FinalSeries.parse("yang9", "W", "110xxxxxxxx") - ); - // 查看数据集 - System.out.println(insert); - // 将其中手机号前三位不为 120 的数据行按照其中的 sex 分组 在这里直接使用分组时过滤即可 - System.out.println( - insert.groupBy("sex", v -> { - // 获取到手机号的字符串 - String s = v.getCell(2).toString(); - // 判断前 3 个字符是否为 120 (是否以 120 开头) 如果是就不添加 - return !s.startsWith("120"); - }).count() - ); - } -} -``` - -* Support for local binarization operations, which can effectively achieve image binarization processing. Compared to - global binarization, functions are more flexible. - -```java -package zhao.algorithmMagic; - -import zhao.algorithmMagic.operands.matrix.ColorMatrix; - -import java.net.MalformedURLException; -import java.net.URL; - -public class MAIN1 { - public static void main(String[] args) throws MalformedURLException { - // 获取到图像矩阵 - ColorMatrix parse = ColorMatrix.parse(new URL("https://img-blog.csdnimg.cn/img_convert/5765bdab08ef6e117d434e7e225b9013.png")); - parse.show("image"); - System.out.println("ok!!!"); - System.out.println("长 = " + parse.getRowCount()); - System.out.println("宽 = " + parse.getColCount()); - // 将图像进行局部二值化 - parse.localBinary( - // 指定本次二值化选择的颜色通道 - ColorMatrix._G_, - // 指定本次二值化选出的局部图像矩阵数量 - 100, - // 指定本次二值化中局部矩阵中大于局部阈值的颜色编码 - 0xffffff, - // 指定本次二值化中局部矩阵中小于局部阈值的颜色编码 - 0, - // 指定本次二值化中局部阈值生成后要进行的微调数值,这里是降低20个阈值数值 - -30 - ); - // 查看结果数据 - parse.show("image"); - } -} -``` - -* DF object data supports saving as HTML files, enabling effective automated page rendering operations. - -```java -package zhao.algorithmMagic; - -import zhao.algorithmMagic.operands.table.DataFrame; -import zhao.algorithmMagic.operands.table.FDataFrame; -import zhao.algorithmMagic.operands.table.FieldCell; -import zhao.algorithmMagic.operands.table.FinalSeries; - -public class MAIN1 { - public static void main(String[] args) { - // 创建一个空 DF 对象 以 name 列作为行主键索引 - FDataFrame create = FDataFrame.select(FieldCell.parse("name", "sex", "phoneNum", "salary"), 1); - // 插入一些数据 - DataFrame insert = create.insert( - FinalSeries.parse("zhao1", "M", "110xxxxxxxx", "30000"), - FinalSeries.parse("tang2", "W", "120xxxxxxxx", "30000"), - FinalSeries.parse("yang3", "W", "110xxxxxxxx", "30000"), - FinalSeries.parse("zhao4", "M", "120xxxxxxxx", "30000"), - FinalSeries.parse("tang5", "W", "110xxxxxxxx", "30000"), - FinalSeries.parse("yang6", "W", "110xxxxxxxx", "30000"), - FinalSeries.parse("zhao7", "M", "120xxxxxxxx", "30000"), - FinalSeries.parse("tang8", "W", "110xxxxxxxx", "30000"), - FinalSeries.parse("yang9", "W", "110xxxxxxxx", "30000") - ); - // 输出表的HTML 然后查看表中的数据 - System.out.println( - insert.into_outHtml("C:\\Users\\Liming\\Desktop\\fsDownload\\res11234.html", "myTable") - ); - } -} -``` - -* Images begin to support the add function, which enables overlapping and merging of image functions under the operation - of the add function! +* Framework version: 1.17 - x.xx +* Add implementation support for the diff function to the image matrix, and add powerful subtractive aggregation + calculation functions to the matrix object. ```java package zhao.algorithmMagic; import zhao.algorithmMagic.operands.matrix.ColorMatrix; -import java.sql.SQLException; +import java.awt.*; public class MAIN1 { public static void main(String[] args) { - // 将一些图像文件转换成为一个图像矩阵对象 - ColorMatrix colorMatrix1 = ColorMatrix.parse("C:\\Users\\Liming\\Desktop\\fsDownload\\test.bmp"); - ColorMatrix colorMatrix2 = ColorMatrix.parse("C:\\Users\\Liming\\Desktop\\fsDownload\\test1.bmp"); - // 将 colorMatrix2 + colorMatrix1 的结果图像展示出来 - (colorMatrix1.add(colorMatrix2)).show("image"); + // 获取一张图像的像素矩阵 + ColorMatrix parse1 = ColorMatrix.parseGrayscale("C:\\Users\\Liming\\Desktop\\fsdownload\\test1.bmp"); + // 将 parse1 进行二值化 + parse1.localBinary(ColorMatrix._G_, 30, 0, 0xffffff, 1); + // 将 parse1 矩阵腐蚀,然后将腐蚀的结果获取到 + ColorMatrix parse2 = parse1.erode(3, 3, true, Color.BLACK); + // 将两个矩阵进行做差运算,并查看做差之后的图像 该函数等同于取余做差 + (parse1.diff(parse2)).show("结果图像1"); + // 将两个矩阵进行 agg 函数的做差运算,这里使用的是 规整做差 + (parse1.agg(parse2, ColorMatrix.COLOR_DIFF_REGULATE)).show("结果图像2"); } } ``` -* Support aggregation operations for custom aggregation and built-in data schemes. +* When fixing the problem of copying two-dimensional arrays, only deep copies were made at the array level ( + one-dimensional arrays in two-dimensional arrays did not undergo deep copies). This has now been redesigned to make + the deep copy operation effective again. ```java package zhao.algorithmMagic; import zhao.algorithmMagic.operands.matrix.ColorMatrix; -import java.sql.SQLException; +import java.awt.*; public class MAIN1 { public static void main(String[] args) { - // 将一些图像文件转换成为一个图像矩阵对象 - ColorMatrix colorMatrix1 = ColorMatrix.parse("C:\\Users\\Liming\\Desktop\\fsDownload\\test.bmp"); - ColorMatrix colorMatrix2 = ColorMatrix.parse("C:\\Users\\Liming\\Desktop\\fsDownload\\test1.bmp"); - // 使用 agg 函数以及内置的计算方案进行两个矩阵的合并 - // 下面函数中的第二个形参代表的就是矩阵中元素的聚合逻辑,这里使用的是求和,并对越界颜色数值进行规整的逻辑实现 - colorMatrix1.agg(colorMatrix2, ColorMatrix.COLOR_SUM_REGULATE).show("image"); + // 获取一张图像的像素矩阵 + ColorMatrix parse1 = ColorMatrix.parseGrayscale("C:\\Users\\Liming\\Desktop\\fsdownload\\test1.bmp"); + Color[][] colors = parse1.copyToNewArrays(); } } ``` -* The image matrix supports a corrosion operation function in which redundant feature data can be removed. - -```java -package zhao.algorithmMagic; - -import zhao.algorithmMagic.operands.matrix.ColorMatrix; - -import java.sql.SQLException; - - -public class MAIN1 { - public static void main(String[] args) { - // 将一些图像文件转换成为一个图像矩阵对象 - ColorMatrix colorMatrix1 = ColorMatrix.parseGrayscale("C:\\Users\\Liming\\Desktop\\fsDownload\\test2.bmp"); - // 对图像进行二值化 - colorMatrix1.globalBinary(ColorMatrix._G_, 100, 0xffffff, 0); - colorMatrix1.show("腐蚀之前的 image"); - // 开始对图像矩阵进行腐蚀操作 - colorMatrix1.erode(2, 2, false).show("腐蚀之后的 image"); - } -} -``` - -* The addition of columns is supported, but it should be noted that the addition of columns will return a new DataFrame - DF in which the data will be lightly copied. - -```java -package zhao.algorithmMagic; - -import zhao.algorithmMagic.operands.table.*; - -import java.sql.SQLException; - -public class MAIN1 { - public static void main(String[] args) { - // 创建一个空的 DataFrame 对象 - FDataFrame select = FDataFrame.select( - FieldCell.parse("id", "name", "sex", "age"), 1 - ); - // 手动插入数据 - select.insert( - FinalSeries.parse("1", "zhao", "M", "19"), - FinalSeries.parse("2", "tang", "W", "18"), - FinalSeries.parse("3", "yang", "W", "20"), - FinalSeries.parse("4", "shen", "W", "19") - ); - // 打印出 DF 对象有关的信息 - System.out.println( - select.desc() - ); - // 添加一列数据,用于表示年龄是否为偶数 - DataFrame ageIsE = select.insertColGetNew( - // 新列的列名称 - FieldCell.$("AgeIsE"), - // 新列的数值生成逻辑 如果第 4 列(index == 3)的数值 % 2 == 0 就是true - cells -> cells.getCell(3).getIntValue() % 2 == 0 ? new FinalCell<>(true) : new FinalCell<>(false) - ); - System.out.println(ageIsE); - } -} -``` - -* Supports operations between table data, and can perform summation and subtraction calculations on all numeric types in - the table. After calculation, a new DF object will be returned. - -```java -package zhao.algorithmMagic; - -import zhao.algorithmMagic.operands.table.*; - -public class MAIN1 { - public static void main(String[] args) { - // 创建一个空的 DataFrame 对象 - FDataFrame select1 = FDataFrame.select( - FieldCell.parse("id", "name", "sex", "age"), 1 - ); - // 手动插入数据 - select1.insert( - FinalSeries.parse("1", "zhao", "M", "19"), - FinalSeries.parse("2", "tang", "W", "18"), - FinalSeries.parse("3", "yang", "W", "20"), - FinalSeries.parse("4", "shen", "W", "19") - ); - // 创建一个空的 DataFrame 对象 - FDataFrame select2 = FDataFrame.select( - FieldCell.parse("id", "name", "sex", "age"), 1 - ); - // 手动插入数据 - select2.insert( - FinalSeries.parse("1", "zhao", "M", "19"), - FinalSeries.parse("2", "tang", "W", "18"), - FinalSeries.parse("3", "yang", "W", "20") - ); - // 进行求和与做差的运算操作 - System.out.println(select1.add(select2)); - System.out.println(select1.diff(select2)); - } -} -``` - -* Note for the new version: All dependencies in this version are marked as provided, which can minimize redundancy and - enable related configuration based on your actual project. Therefore, when performing AS library injection, please - import the following required items in one click. - -```xml - - - - - org.apache.logging.log4j - log4j-slf4j-impl - 2.20.0 - - - - - - org.apache.logging.log4j - log4j-api - 2.20.0 - - - - - org.apache.logging.log4j - log4j-core - 2.20.0 - - - -``` - ### Version update date : xx xx-xx-xx \ No newline at end of file diff --git a/src_code/src/main/java/zhao/algorithmMagic/MAIN1.java b/src_code/src/main/java/zhao/algorithmMagic/MAIN1.java index 8483572..319cb52 100644 --- a/src_code/src/main/java/zhao/algorithmMagic/MAIN1.java +++ b/src_code/src/main/java/zhao/algorithmMagic/MAIN1.java @@ -1,9 +1,6 @@ package zhao.algorithmMagic; -import zhao.algorithmMagic.algorithm.OperationAlgorithmManager; - public class MAIN1 { public static void main(String[] args) { - System.out.println(OperationAlgorithmManager.VERSION); } } diff --git a/src_code/src/main/java/zhao/algorithmMagic/operands/matrix/ColorMatrix.java b/src_code/src/main/java/zhao/algorithmMagic/operands/matrix/ColorMatrix.java index 3c59103..f772e22 100644 --- a/src_code/src/main/java/zhao/algorithmMagic/operands/matrix/ColorMatrix.java +++ b/src_code/src/main/java/zhao/algorithmMagic/operands/matrix/ColorMatrix.java @@ -3,6 +3,7 @@ import zhao.algorithmMagic.exception.OperatorOperationException; import zhao.algorithmMagic.integrator.ImageRenderingIntegrator; import zhao.algorithmMagic.operands.matrix.block.IntegerMatrixSpace; +import zhao.algorithmMagic.utils.ASClass; import zhao.algorithmMagic.utils.ASIO; import zhao.algorithmMagic.utils.ASMath; import zhao.algorithmMagic.utils.transformation.ManyTrans; @@ -74,6 +75,33 @@ public class ColorMatrix extends Matrix + * The processing logic in the color value difference calculation scheme is to take 0 or 255 if the value exceeds the range, and take the value itself if the value does not exceed the range, achieving an efficient calculation scheme for limiting color values. + */ + public final static ManyTrans COLOR_DIFF_REGULATE = (inputType1, inputType2) -> new Color( + ASMath.regularTricolor(inputType1.getRed() - inputType2.getRed()), + ASMath.regularTricolor(inputType1.getGreen() - inputType2.getGreen()), + ASMath.regularTricolor(inputType1.getBlue() - inputType2.getGreen()) + ); + + /** + * 颜色数值求差计算方案,在该方案中的处理逻辑为:如果x为求和后的越界数值,则取 x % 256 的结果作为颜色数值。 + *

+ * The processing logic in the color value difference calculation scheme is: if x is the out-of-boundary value after the sum, the result of x% 256 is taken as the color value. + */ + public final static ManyTrans COLOR_DIFF_REMAINDER = (inputType1, inputType2) -> { + int red = inputType1.getRed() - inputType2.getRed(); + int green = inputType1.getGreen() - inputType2.getGreen(); + int blue = inputType1.getBlue() - inputType2.getGreen(); + return new Color( + red - (red >> _G_ << _G_), + green - (green >> _G_ << _G_), + blue - (blue >> _G_ << _G_) + ); + }; + private boolean isGrayscale; /** @@ -309,7 +337,7 @@ public ColorMatrix add(ColorMatrix value) { */ @Override public ColorMatrix diff(ColorMatrix value) { - throw new UnsupportedOperationException("The color matrix object does not support the operation of \"addition\", \"subtraction\" and \"multiplication\", because the calculation of such operations on the color object is not necessary!"); + return agg(value, COLOR_DIFF_REMAINDER); } /** @@ -516,7 +544,7 @@ public ColorMatrix shuffle(long seed) { public Color[][] copyToNewArrays() { Color[][] colors = this.toArrays(); Color[][] res = new Color[colors.length][colors[0].length]; - System.arraycopy(colors, 0, res, 0, colors.length); + ASClass.array2DCopy(colors, res); return res; } @@ -1203,7 +1231,7 @@ public ColorMatrix erode(int width, int height, boolean isCopy, Color backColor) for (Color[] colors : nowSub.toArrays()) { for (Color color : colors) { if (color.getRGB() == colorNum) { - // 发现了黑色 直接将该子矩阵中的所有颜色更换为黑色 + // 发现了腐蚀色 直接将该子矩阵中的所有颜色更换为腐蚀色 isR = true; } } diff --git a/src_code/src/main/java/zhao/algorithmMagic/operands/vector/SparkVector.scala b/src_code/src/main/java/zhao/algorithmMagic/operands/vector/SparkVector.scala index 366e195..35b2b00 100644 --- a/src_code/src/main/java/zhao/algorithmMagic/operands/vector/SparkVector.scala +++ b/src_code/src/main/java/zhao/algorithmMagic/operands/vector/SparkVector.scala @@ -77,14 +77,6 @@ final class SparkVector(sparkContext: SparkContext, vector: org.apache.spark.mll else throw new OperatorOperationException("'DoubleVector1 innerProduct DoubleVector2' 时,两个'DoubleVector'的向量所包含的数量不同,DoubleVector1=[" + doubles1.length + "],DoubleVector2=[" + doubles2.length + "]\n" + "When 'DoubleVector1 innerProduct DoubleVector2', the two vectors of 'DoubleVector' contain different quantities, DoubleVector1=[" + doubles1.length + "], DoubleVector2=[" + doubles2.length + "]") } - /** - * - * @return 将本对象中存储的向量序列数组拷贝到一个新数组并将新数组返回,这里返回的是一个新数组,支持修改等操作。 - * - * Copy the vector sequence array stored in this object to a new array and return the new array. Here, a new array is returned, which supports modification and other operations. - */ - override def copyToNewArray(): Array[Double] = vector.toArray - /** * @return 该类的实现类对象,用于拓展该接口的子类 */ @@ -115,6 +107,14 @@ final class SparkVector(sparkContext: SparkContext, vector: org.apache.spark.mll else throw new OperatorOperationException("'DoubleVector1 add DoubleVector2' 时,两个'DoubleVector'的向量所包含的数量不同,DoubleVector1=[" + numberOfDimensions1 + "],DoubleVector2=[" + numberOfDimensions2 + "]\n" + "When 'DoubleVector1 add DoubleVector2', the two vectors of 'DoubleVector' contain different quantities, DoubleVector1=[" + numberOfDimensions1 + "], DoubleVector2=[" + numberOfDimensions2 + "]") } + /** + * + * @return 将本对象中存储的向量序列数组拷贝到一个新数组并将新数组返回,这里返回的是一个新数组,支持修改等操作。 + * + * Copy the vector sequence array stored in this object to a new array and return the new array. Here, a new array is returned, which supports modification and other operations. + */ + override def copyToNewArray(): Array[Double] = vector.toArray + /** * @return 向量中包含的维度数量 *

diff --git a/src_code/src/main/java/zhao/algorithmMagic/utils/ASClass.java b/src_code/src/main/java/zhao/algorithmMagic/utils/ASClass.java index 76a9317..5ed3308 100644 --- a/src_code/src/main/java/zhao/algorithmMagic/utils/ASClass.java +++ b/src_code/src/main/java/zhao/algorithmMagic/utils/ASClass.java @@ -262,20 +262,14 @@ public static int[] DoubleArray_To_IntArray(double[] doubles) { * @return 拷贝完成的数组对象,其本身是 dest */ public static int[][] array2DCopy(int[][] src, int[][] dest) { - array2DCopy(src, dest, Math.min(src.length, dest.length)); + int index = -1; + for (int[] objects : src) { + int[] res = dest[++index]; + System.arraycopy(objects, 0, res, 0, Math.min(objects.length, res.length)); + } return dest; } - /** - * 将一个二维数组中的数据原样拷贝到新的二维数组中 - * - * @param src 需要被拷贝的原数组 - * @param dest 需要被拷贝的目标数组 - */ - private static void array2DCopy(int[][] src, int[][] dest, int length) { - System.arraycopy(src, 0, dest, 0, length); - } - /** * 将一个二维数组中的数据原样拷贝到新的二维数组中 * @@ -284,20 +278,14 @@ private static void array2DCopy(int[][] src, int[][] dest, int length) { * @return 拷贝完成的数组对象,其本身是 dest */ public static double[][] array2DCopy(double[][] src, double[][] dest) { - array2DCopy(src, dest, Math.min(src.length, dest.length)); + int index = -1; + for (double[] objects : src) { + double[] res = dest[++index]; + System.arraycopy(objects, 0, res, 0, Math.min(objects.length, res.length)); + } return dest; } - /** - * 将一个二维数组中的数据原样拷贝到新的二维数组中 - * - * @param src 需要被拷贝的原数组 - * @param dest 需要被拷贝的目标数组 - */ - private static void array2DCopy(double[][] src, double[][] dest, int length) { - System.arraycopy(src, 0, dest, 0, length); - } - /** * 将一个二维数组中的数据原样拷贝到新的二维数组中 * @@ -305,7 +293,11 @@ private static void array2DCopy(double[][] src, double[][] dest, int length) { * @param dest 需要被拷贝的目标数组 */ public static void array2DCopy(Object[][] src, Object[][] dest) { - System.arraycopy(src, 0, dest, 0, Math.min(src.length, dest.length)); + int index = -1; + for (Object[] objects : src) { + Object[] res = dest[++index]; + System.arraycopy(objects, 0, res, 0, Math.min(objects.length, res.length)); + } } /** diff --git a/src_code/update/1.16_1.17-Chinese.md b/src_code/update/1.16_1.17-Chinese.md new file mode 100644 index 0000000..58ce62c --- /dev/null +++ b/src_code/update/1.16_1.17-Chinese.md @@ -0,0 +1,814 @@ +# ![image](https://user-images.githubusercontent.com/113756063/194830221-abe24fcc-484b-4769-b3b7-ec6d8138f436.png) 算法之星-机器大脑 + +- Switch to [English Document](https://github.com/BeardedManZhao/algorithmStar/blob/main/src_code/update/1.16_1.17.md) +- knowledge base + + + + +### 更新日志 + +* 框架版本:1.16 - 1.17 +* 将集成器的名称修改为“Integrator”。 +* 提供卷积函数的计算支持,能够通过卷积函数将特征放大同时缩小图像矩阵的元素数量。 + +```java +package zhao.algorithmMagic; + +import zhao.algorithmMagic.integrator.ImageRenderingIntegrator; +import zhao.algorithmMagic.integrator.launcher.ImageRenderingMarLauncher; +import zhao.algorithmMagic.operands.matrix.IntegerMatrix; +import zhao.algorithmMagic.operands.matrix.block.IntegerMatrixSpace; + +public class MAIN1 { + public static void main(String[] args) { + String s1 = "C:\\Users\\Liming\\Desktop\\fsDownload\\微信图片_1.jpg"; + IntegerMatrix integerMatrix; + { + // 设置权重 + IntegerMatrix weight = IntegerMatrix.parse( + new int[]{0, -1}, + new int[]{-1, 2}, + new int[]{0, 0} + ); + // 读取图像并获取到三通道矩阵空间 + IntegerMatrixSpace parse = IntegerMatrixSpace.parse(s1); + // 对图像进行卷积,获取三个色彩通道的矩阵空间的和 + integerMatrix = parse.foldingAndSum(2, 3, IntegerMatrixSpace.parse(weight, weight, weight)); + } + // 输出图片1的卷积图像文件 + ImageRenderingIntegrator image = new ImageRenderingIntegrator( + "image", + new ImageRenderingMarLauncher<>(integerMatrix, "C:\\Users\\Liming\\Desktop\\fsDownload\\res12.jpg", 1) + ); + if (image.run()) { + System.out.println("ok!!!"); + } + } +} +``` + +* 图像矩阵支持直接通过 show 函数在屏幕中显示出图像。 + +```java +package zhao.algorithmMagic; + +import zhao.algorithmMagic.operands.matrix.ColorMatrix; + +public class MAIN1 { + public static void main(String[] args) throws InterruptedException { + String s1 = "C:\\Users\\Liming\\Desktop\\fsDownload\\微信图片_1.jpg"; + ColorMatrix parse = ColorMatrix.parse(s1); + parse.show("image"); + Thread.sleep(1024); + parse.colorReversal(false); + parse.show("image"); + } +} +``` + +* 优化随机打乱函数 shuffle 中的最大随机打乱次数算法,使得其不会出现越界异常。 +* 优化通过列名随机访问矩阵中数据的函数逻辑,采用hash进行地址的映射。 + +```java +package zhao.algorithmMagic; + +import zhao.algorithmMagic.operands.matrix.ColumnDoubleMatrix; + +import java.util.Arrays; +import java.util.Random; + +public class MAIN1 { + public static void main(String[] args) { + // 构建一份矩阵数据 + double[][] data = new double[][]{ + new double[]{10, 11, 14, 10, 100}, + new double[]{11, 11, 14, 10, 100}, + new double[]{25, 20, 28, 20, 100}, + new double[]{26, 20, 28, 20, 100} + }; + // 将矩阵数据使用指定列与行名称的方式创建出来 + ColumnDoubleMatrix columnDoubleMatrix = ColumnDoubleMatrix.parse( + new String[]{"col1", "col2", "col3", "col4", "col5"}, + new String[]{"row1", "row2", "row3", "row4"}, + data + ); + // 使用不创建新矩阵的方式打乱其中的数据顺序 且最多打乱 10 次 + columnDoubleMatrix.shuffle(new Random(), false, 10); + // 打印出矩阵数据 + System.out.println(columnDoubleMatrix); + // 打印出矩阵中的 col2 列 在新版中该函数采取哈希实现 速度提升很明显 + System.out.println(Arrays.toString(columnDoubleMatrix.getArrayByColName("col2"))); + // 打印出矩阵中的 row2 行 在新版中该函数采取哈希实现 速度提升很明显 + System.out.println(Arrays.toString(columnDoubleMatrix.getArrayByRowName("row2"))); + } +} +``` + +* 支持图像的快捷保存函数的调用操作,使得图像文件的保存变得更简单。 + +```java +package zhao.algorithmMagic; + +import zhao.algorithmMagic.operands.matrix.ColorMatrix; + +public class MAIN1 { + public static void main(String[] args) { + String s1 = "C:\\Users\\zhao\\Desktop\\fsDownload\\微信图片_2.jpg"; + // 获取到图像矩阵对象 + ColorMatrix parse = ColorMatrix.parse(s1); + // 将图像在原图像的基础上进行颜色反转操作 + parse.colorReversal(false); + // 查看颜色反转之后的图像 + parse.show("image1"); + // 输出反转之后的图像 + parse.save("C:\\Users\\zhao\\Desktop\\fsDownload\\res123.jpg"); + } +} +``` + +* 增加了 "SaveMatrix" 接口,实现了该接口的一切矩阵都可以使用结构化或非结构化的保存。 + +```java +package zhao.algorithmMagic; + +import zhao.algorithmMagic.operands.matrix.ColorMatrix; + +public class MAIN1 { + public static void main(String[] args) { + String s1 = "C:\\Users\\Liming\\Desktop\\fsDownload\\微信图片_2.jpg"; + // 获取到图像矩阵对象 + ColorMatrix parse = ColorMatrix.parse(s1); + // 输出图像RGB文本数据 + parse.save("C:\\Users\\Liming\\Desktop\\fsDownload\\res1234.csv", ','); + } +} +``` + +* AS库中增加了数据分析库,使用SQL风格处理数据,通过该库可以方便的将数据库中的数据获取到内存中进行处理。 + +```java +package zhao.algorithmMagic; + +import zhao.algorithmMagic.operands.table.DataFrame; +import zhao.algorithmMagic.operands.table.FDataFrame; + +import java.sql.Connection; +import java.sql.DriverManager; +import java.sql.SQLException; + +public class MAIN1 { + public static void main(String[] args) throws SQLException { + // 准备数据库连接对象 + Connection connection = DriverManager.getConnection("jdbc:mysql://192.168.0.101:38243/tq_SCHOOL", "liming", "liming7887"); + // 将数据库数据对象加载成 FDF + DataFrame execute = FDataFrame.builder(connection) // 指定数据库连接 + .create("*") // 指定查询字段 + .from("STU_FIVE") // 指定查询表 + .primaryKey(0) // 指定内存AS表的主键(AS会自动建立行索引)数据库查询需要使用索引指定哦! + .execute();// 开始查询 + // 打印出 FDF 中的数据 + System.out.println(execute); + + // 开始查询 FDF + // 正序打印出 FDF 中 所有name长度为3的人员中男女生的人数 打印出前 3 行数据 + DataFrame select = execute + .select("name", "sex") // 查询其中的 name sex 列 + .where(v -> v.getCell(0).getStringValue().length() == 3) // 获取到其中的名字长度为 3 的数据行 + .groupBy("sex") // 按照 sex 列分组 + .count() // 将每一组进行统计 + .sort("count()") // 按照统计结果进行正序排序 + .limit(3); // 获取最多前3行数据 + System.out.println(select.into_outfile("C:\\Users\\zhao\\Desktop\\fsDownload\\res1.csv")); + // 打印存储 FDF 中的数据行数 + System.out.println("数据行数 = " + execute.count()); + // 打印出其中的信息 + System.out.println(execute.desc()); + // 获取到 赵凌宇 的信息 由于 name 列是主键,因此可以直接通过这里获取到数据 + System.out.println(execute.selectRow("赵凌宇")); + } +} +``` + +* 使用 FDataFrame 加载数据库与文件中的数据。 + +```java +package zhao.algorithmMagic; + +import zhao.algorithmMagic.operands.table.*; + +import java.sql.Connection; +import java.sql.DriverManager; +import java.sql.SQLException; + +public class MAIN1 { + public static void main(String[] args) throws SQLException { + // 准备数据库连接对象 + Connection connection = DriverManager.getConnection("jdbc:mysql://192.168.0.101:38243/tq_SCHOOL", "liming", "liming7887"); + // 将数据库数据对象加载成 FDF + DataFrame execute = FDataFrame.builder(connection) // 指定数据库连接 + .create("*") // 指定查询字段 + .from("STU_FIVE") // 指定查询表 + .where(" name = '赵凌宇' ") // 指定查询条件 + .primaryKey(0) // 指定内存AS表的主键(AS会自动建立行索引)数据库查询需要使用索引指定哦! + .execute();// 开始查询 + // 打印出 FDF 中的数据 + System.out.println(execute); + } +} +``` + +```java +package zhao.algorithmMagic; + +import zhao.algorithmMagic.operands.table.DataFrame; +import zhao.algorithmMagic.operands.table.FDataFrame; + +import java.io.File; +import java.sql.SQLException; +import java.util.Objects; + +public class MAIN1 { + public static void main(String[] args) { + // 准备文件对象 + File file = new File("C:\\Users\\zhao\\Desktop\\out\\res1.csv"); + // 使用 FDF 加载文件 + DataFrame execute1 = FDataFrame.builder(file) + // 文件对象的读取需要指定文本分隔符 + .setSep(',') + // 文件对象需要指定好列名称,不能使用 * 这里代表的不是查询,而是创建一个DF的列字段 + .create("id", "name", "sex") + // 文件对象需要使用lambda表达式进行数据的过滤 + .where(v -> Objects.equals(v.getCell(1).getStringValue(), "赵凌宇")) + // 文件对象的主键指定允许使用列名称 + .primaryKey("name") + // 执行查询 + .execute(); + // 打印出结果数据 + System.out.println(execute1); + } +} + ``` + +* 对FDF按照行名称进行limit操作 + +```java +package zhao.algorithmMagic; + +import zhao.algorithmMagic.operands.table.DataFrame; +import zhao.algorithmMagic.operands.table.FDataFrame; + +import java.io.File; +import java.sql.SQLException; + +public class MAIN1 { + public static void main(String[] args) { + // 准备文件对象 + File file = new File("C:\\Users\\zhao\\Desktop\\out\\res1.csv"); + // 使用 FDF 加载文件 + DataFrame execute1 = FDataFrame.builder(file) + // 文件对象的读取需要指定文本分隔符 + .setSep(',') + // 文件对象需要指定好列名称,不能使用 * 这里代表的不是查询,而是创建一个DF的列字段 + .create("id", "name", "sex") + // 文件对象的主键指定允许使用列名称 + .primaryKey("name") + // 执行查询 + .execute(); + // 打印出结果数据 这里打印出从 赵 到 贾 之间的数据行 + System.out.println(execute1.limit("赵", "贾")); + } +} +``` + +* 支持为列取别名以及新增数据行的操作。 + +```java +package zhao.algorithmMagic; + +import zhao.algorithmMagic.operands.table.DataFrame; +import zhao.algorithmMagic.operands.table.FDataFrame; +import zhao.algorithmMagic.operands.table.FieldCell; +import zhao.algorithmMagic.operands.table.FinalSeries; + +import java.io.File; +import java.io.IOException; +import java.sql.SQLException; + +public class MAIN1 { + public static void main(String[] args) { + // 准备文件对象 + File file = new File("C:\\Users\\zhao\\Desktop\\out\\res1.csv"); + // 使用 FDF 加载文件 + DataFrame execute1 = FDataFrame.builder(file) + // 文件对象的读取需要指定文本分隔符 + .setSep(',') + // 文件对象需要指定好列名称,不能使用 * 这里代表的不是查询,而是创建一个DF的列字段 + .create("id", "name", "sex") + // 文件对象的主键指定允许使用列名称 + .primaryKey("name") + // 执行查询 + .execute(); + // 按照性别分组,计算出男女生人数 + long start = System.currentTimeMillis(); + System.out.println( + execute1 + // 指定查询的列,并起别名 + .select(FieldCell.$("sex").as("性别")) + // 按照 sex 分组 + .groupBy("性别") + // 进行 组内的计数 + .count() + // 指定查询的列 并起别名 + .select( + FieldCell.$("性别"), + FieldCell.$("count()").as("人数") + ) + // 新增一行数据 + .insert(FinalSeries.parse("新", "10")) + ); + System.out.print("处理耗时(MS):"); + System.out.println(System.currentTimeMillis() - start); + } +} + +``` + +* 能够针对DF数据集对象中的所有数值进行行或列数据的函数式更新,同时支持DF数据对象到矩阵之间的转换。 + +```java +package zhao.algorithmMagic; + +import zhao.algorithmMagic.operands.matrix.IntegerMatrix; +import zhao.algorithmMagic.operands.table.DataFrame; +import zhao.algorithmMagic.operands.table.FDataFrame; +import zhao.algorithmMagic.operands.table.FieldCell; +import zhao.algorithmMagic.operands.table.FinalCell; +import zhao.algorithmMagic.operands.vector.IntegerVector; + +import java.io.File; + +public class MAIN1 { + public static void main(String[] args) { + // 准备文件对象 + File file = new File("C:\\Users\\zhao\\Desktop\\out\\res1.csv"); + // 使用 FDF 加载文件 + DataFrame execute1 = FDataFrame.builder(file) + // 文件对象的读取需要指定文本分隔符 + .setSep(',') + // 文件对象需要指定好列名称,不能使用 * 这里代表的不是查询,而是创建一个DF的列字段 + .create("id", "name", "sex") + // 文件对象的主键指定允许使用列名称 + .primaryKey("name") + // 执行查询 + .execute() + // 为列起别名 + .select( + FieldCell.$("id"), + FieldCell.$("name").as("名称"), + FieldCell.$("sex").as("性别") + ) + // 将性别列进行转换,男生为1 女生为0 + .updateCol(FieldCell.$("性别"), cell -> new FinalCell<>(cell.getStringValue().equals("男") ? 1 : 0)) + // 将行主键数值为ZLY的数据行中的所有单元格替换成为数据 405 + .updateRow("ZLY", cell -> new FinalCell<>(405)); + + // 打印出表中的行主键名称为 405 的数据行 + System.out.println(execute1.selectRow("405")); + long start = System.currentTimeMillis(); + // 将表转换成为一个整形矩阵对象,该操作会将DF对象中的所有数值试图转换成为 col.count()*3 的矩阵对象 + IntegerMatrix parse = IntegerMatrix.parse(execute1, execute1.count().getIntValue(), 3); + System.out.println(IntegerVector.parse(parse.getArrayByColIndex(2))); + System.out.print("处理耗时(MS):"); + System.out.println(System.currentTimeMillis() - start); + } +} +``` + +* 支持通过一个网络 URL 对象获取到有关图像的数据,并将其转换成为一个图像矩阵。 + +```java +package zhao.algorithmMagic; + +import zhao.algorithmMagic.operands.matrix.ColorMatrix; + +import java.net.MalformedURLException; +import java.net.URL; + +public class MAIN1 { + public static void main(String[] args) throws MalformedURLException { + // 准备图像的URL对象 + URL url = new URL("https://user-images.githubusercontent.com/113756063/194830221-abe24fcc-484b-4769-b3b7-ec6d8138f436.png"); + // 解析URL获取到图像矩阵 + ColorMatrix parse1 = ColorMatrix.parse(url); + // 解析URL获取到图像的灰度矩阵 + ColorMatrix parse2 = ColorMatrix.parseGrayscale(url); + // 查看图像 + parse1.show("image"); + parse2.show("image"); + } +} +``` + +* 支持进行色彩二值化规整覆盖,能够通过指定通道的色彩数值,显示出更多的图像特征,或去除更多的冗余特征。 + +```java +package zhao.algorithmMagic; + +import zhao.algorithmMagic.operands.matrix.ColorMatrix; + +import java.net.MalformedURLException; +import java.net.URL; + +public class MAIN1 { + public static void main(String[] args) throws MalformedURLException { + // 准备图像的URL对象 + URL url = new URL("https://user-images.githubusercontent.com/113756063/194830221-abe24fcc-484b-4769-b3b7-ec6d8138f436.png"); + // 解析URL获取到图像矩阵 + ColorMatrix parse1 = ColorMatrix.parse(url); + // 将 URL 图像矩阵中所有 G 通道颜色数值大于 40 的颜色变更为黑色,反之变更为白色 + // 在这里由于选择了 G 通道 因此 绿色越深 越有可能变为白色 + parse1.globalBinary(ColorMatrix._G_, 40, 0, 0xffffff); + // 也可以使用其它颜色通道进行色彩的调整 + parse1.globalBinary(ColorMatrix._R_, 40, 0, 0xffffff); + parse1.globalBinary(ColorMatrix._B_, 40, 0, 0xffffff); + // 查看结果图像 + parse1.show("image"); + } +} +``` + +* 支持进行图像 ASCII 图的构造,您可以快速的将一个图片的 ASCII 构造出来,不过请您确保图像尺寸足够小。 + +```java +package zhao.algorithmMagic; + +import zhao.algorithmMagic.operands.matrix.ColorMatrix; + +import java.io.File; +import java.net.MalformedURLException; +import java.net.URL; + +public class MAIN1 { + public static void main(String[] args) throws MalformedURLException { + // 准备图像的URL对象 + URL url = new URL("https://user-images.githubusercontent.com/113756063/194830221-abe24fcc-484b-4769-b3b7-ec6d8138f436.png"); + // 解析URL获取到图像矩阵 + ColorMatrix parse1 = ColorMatrix.parse(url); + // 查看结果图像 + parse1.show("image"); + // 输出图像的 ASCII 数值,输出规则为 G 通道颜色数值 大于 40 的 输出符号 'A' 其它输出符号 ' ' + parse1.save( + new File("C:\\Users\\zhao\\Desktop\\fsDownload\\res.txt"), + ColorMatrix._G_, 40, 'A', ' ' + ); + } +} +``` + +* 能够手动创建一个空的 DataFrame 对象,并自主操作其中的数据。 + +```java +package zhao.algorithmMagic; + +import zhao.algorithmMagic.operands.table.DataFrame; +import zhao.algorithmMagic.operands.table.FDataFrame; +import zhao.algorithmMagic.operands.table.FieldCell; +import zhao.algorithmMagic.operands.table.FinalSeries; + +public class MAIN1 { + public static void main(String[] args) { + // 创建一个空 DF 对象 以 name 列作为行主键索引 + FDataFrame create = FDataFrame.select(FieldCell.parse("name", "sex", "phoneNum"), 1); + // 插入一些数据 + DataFrame insert = create.insert( + FinalSeries.parse("zhao", "M", "110xxxxxxxx"), + FinalSeries.parse("tang", "W", "110xxxxxxxx"), + FinalSeries.parse("yang", "M", "110xxxxxxxx") + ); + // 查看数据集 + System.out.println(insert); + // 将其中的 name 列 sex 列 查询 + System.out.println( + insert.select( + FieldCell.$("name").as("AllName"), + FieldCell.$("sex").as("AllSex") + ) + ); + } +} +``` + +* 支持在 group 的时候指定 where子句,使得计算效率大大增强 + +```java +package zhao.algorithmMagic; + +import zhao.algorithmMagic.operands.table.DataFrame; +import zhao.algorithmMagic.operands.table.FDataFrame; +import zhao.algorithmMagic.operands.table.FieldCell; +import zhao.algorithmMagic.operands.table.FinalSeries; + +import java.net.MalformedURLException; + +public class MAIN1 { + public static void main(String[] args) { + // 创建一个空 DF 对象 以 name 列作为行主键索引 + FDataFrame create = FDataFrame.select(FieldCell.parse("name", "sex", "phoneNum"), 1); + // 插入一些数据 + DataFrame insert = create.insert( + FinalSeries.parse("zhao1", "M", "110xxxxxxxx"), + FinalSeries.parse("tang2", "W", "120xxxxxxxx"), + FinalSeries.parse("yang3", "W", "110xxxxxxxx"), + FinalSeries.parse("zhao4", "M", "120xxxxxxxx"), + FinalSeries.parse("tang5", "W", "110xxxxxxxx"), + FinalSeries.parse("yang6", "W", "110xxxxxxxx"), + FinalSeries.parse("zhao7", "M", "120xxxxxxxx"), + FinalSeries.parse("tang8", "W", "110xxxxxxxx"), + FinalSeries.parse("yang9", "W", "110xxxxxxxx") + ); + // 查看数据集 + System.out.println(insert); + // 将其中手机号前三位不为 120 的数据行按照其中的 sex 分组 在这里直接使用分组时过滤即可 + System.out.println( + insert.groupBy("sex", v -> { + // 获取到手机号的字符串 + String s = v.getCell(2).toString(); + // 判断前 3 个字符是否为 120 (是否以 120 开头) 如果是就不添加 + return !s.startsWith("120"); + }).count() + ); + } +} +``` + +* 支持局部二值化操作,能够有效地实现图像的二值化处理,相较于全局二值化,函数更加灵活。 + +```java +package zhao.algorithmMagic; + +import zhao.algorithmMagic.operands.matrix.ColorMatrix; + +import java.net.MalformedURLException; +import java.net.URL; + +public class MAIN1 { + public static void main(String[] args) throws MalformedURLException { + // 获取到图像矩阵 + ColorMatrix parse = ColorMatrix.parse(new URL("https://img-blog.csdnimg.cn/img_convert/5765bdab08ef6e117d434e7e225b9013.png")); + parse.show("image"); + System.out.println("ok!!!"); + System.out.println("长 = " + parse.getRowCount()); + System.out.println("宽 = " + parse.getColCount()); + // 将图像进行局部二值化 + parse.localBinary( + // 指定本次二值化选择的颜色通道 + ColorMatrix._G_, + // 指定本次二值化选出的局部图像矩阵数量 + 100, + // 指定本次二值化中局部矩阵中大于局部阈值的颜色编码 + 0xffffff, + // 指定本次二值化中局部矩阵中小于局部阈值的颜色编码 + 0, + // 指定本次二值化中局部阈值生成后要进行的微调数值,这里是降低20个阈值数值 + -30 + ); + // 查看结果数据 + parse.show("image"); + } +} +``` + +* DF对象数据支持保存为 HTML 文件,能够实现有效的自动化页面绘制操作。 + +```java +package zhao.algorithmMagic; + +import zhao.algorithmMagic.operands.table.DataFrame; +import zhao.algorithmMagic.operands.table.FDataFrame; +import zhao.algorithmMagic.operands.table.FieldCell; +import zhao.algorithmMagic.operands.table.FinalSeries; + +public class MAIN1 { + public static void main(String[] args) { + // 创建一个空 DF 对象 以 name 列作为行主键索引 + FDataFrame create = FDataFrame.select(FieldCell.parse("name", "sex", "phoneNum", "salary"), 1); + // 插入一些数据 + DataFrame insert = create.insert( + FinalSeries.parse("zhao1", "M", "110xxxxxxxx", "30000"), + FinalSeries.parse("tang2", "W", "120xxxxxxxx", "30000"), + FinalSeries.parse("yang3", "W", "110xxxxxxxx", "30000"), + FinalSeries.parse("zhao4", "M", "120xxxxxxxx", "30000"), + FinalSeries.parse("tang5", "W", "110xxxxxxxx", "30000"), + FinalSeries.parse("yang6", "W", "110xxxxxxxx", "30000"), + FinalSeries.parse("zhao7", "M", "120xxxxxxxx", "30000"), + FinalSeries.parse("tang8", "W", "110xxxxxxxx", "30000"), + FinalSeries.parse("yang9", "W", "110xxxxxxxx", "30000") + ); + // 输出表的HTML 然后查看表中的数据 + System.out.println( + insert.into_outHtml("C:\\Users\\Liming\\Desktop\\fsDownload\\res11234.html", "myTable") + ); + } +} +``` + +* 图像开始支持 add 函数,在add函数的操作下,可以实现图像函数的重叠合并! + +```java +package zhao.algorithmMagic; + +import zhao.algorithmMagic.operands.matrix.ColorMatrix; + +import java.sql.SQLException; + +public class MAIN1 { + public static void main(String[] args) { + // 将一些图像文件转换成为一个图像矩阵对象 + ColorMatrix colorMatrix1 = ColorMatrix.parse("C:\\Users\\Liming\\Desktop\\fsDownload\\test.bmp"); + ColorMatrix colorMatrix2 = ColorMatrix.parse("C:\\Users\\Liming\\Desktop\\fsDownload\\test1.bmp"); + // 将 colorMatrix2 + colorMatrix1 的结果图像展示出来 + (colorMatrix1.add(colorMatrix2)).show("image"); + } +} +``` + +* 支持自定义聚合以及内置数据方案的聚合操作。 + +```java +package zhao.algorithmMagic; + +import zhao.algorithmMagic.operands.matrix.ColorMatrix; + +import java.sql.SQLException; + +public class MAIN1 { + public static void main(String[] args) { + // 将一些图像文件转换成为一个图像矩阵对象 + ColorMatrix colorMatrix1 = ColorMatrix.parse("C:\\Users\\Liming\\Desktop\\fsDownload\\test.bmp"); + ColorMatrix colorMatrix2 = ColorMatrix.parse("C:\\Users\\Liming\\Desktop\\fsDownload\\test1.bmp"); + // 使用 agg 函数以及内置的计算方案进行两个矩阵的合并 + // 下面函数中的第二个形参代表的就是矩阵中元素的聚合逻辑,这里使用的是求和,并对越界颜色数值进行规整的逻辑实现 + colorMatrix1.agg(colorMatrix2, ColorMatrix.COLOR_SUM_REGULATE).show("image"); + } +} +``` + +* 图像矩阵支持腐蚀运算函数,在该函数中可以去除掉冗余特征数据。 + +```java +package zhao.algorithmMagic; + +import zhao.algorithmMagic.operands.matrix.ColorMatrix; + +import java.sql.SQLException; + + +public class MAIN1 { + public static void main(String[] args) { + // 将一些图像文件转换成为一个图像矩阵对象 + ColorMatrix colorMatrix1 = ColorMatrix.parseGrayscale("C:\\Users\\Liming\\Desktop\\fsDownload\\test2.bmp"); + // 对图像进行二值化 + colorMatrix1.globalBinary(ColorMatrix._G_, 100, 0xffffff, 0); + colorMatrix1.show("腐蚀之前的 image"); + // 开始对图像矩阵进行腐蚀操作 + colorMatrix1.erode(2, 2, false).show("腐蚀之后的 image"); + } +} +``` + +* 支持列的添加,但是需要注意的是列的添加会返回一个新的 DataFrame DF中的数据将会被浅拷贝出来。 + +```java +package zhao.algorithmMagic; + +import zhao.algorithmMagic.operands.table.*; + +import java.sql.SQLException; + +public class MAIN1 { + public static void main(String[] args) { + // 创建一个空的 DataFrame 对象 + FDataFrame select = FDataFrame.select( + FieldCell.parse("id", "name", "sex", "age"), 1 + ); + // 手动插入数据 + select.insert( + FinalSeries.parse("1", "zhao", "M", "19"), + FinalSeries.parse("2", "tang", "W", "18"), + FinalSeries.parse("3", "yang", "W", "20"), + FinalSeries.parse("4", "shen", "W", "19") + ); + // 打印出 DF 对象有关的信息 + System.out.println( + select.desc() + ); + // 添加一列数据,用于表示年龄是否为偶数 + DataFrame ageIsE = select.insertColGetNew( + // 新列的列名称 + FieldCell.$("AgeIsE"), + // 新列的数值生成逻辑 如果第 4 列(index == 3)的数值 % 2 == 0 就是true + cells -> cells.getCell(3).getIntValue() % 2 == 0 ? new FinalCell<>(true) : new FinalCell<>(false) + ); + System.out.println(ageIsE); + } +} +``` + +* 支持表数据之间的运算操作,能够将表中所有的数值类型进行求和与做差计算,计算之后将返回新DF对象。 + +```java +package zhao.algorithmMagic; + +import zhao.algorithmMagic.operands.table.*; + +public class MAIN1 { + public static void main(String[] args) { + // 创建一个空的 DataFrame 对象 + FDataFrame select1 = FDataFrame.select( + FieldCell.parse("id", "name", "sex", "age"), 1 + ); + // 手动插入数据 + select1.insert( + FinalSeries.parse("1", "zhao", "M", "19"), + FinalSeries.parse("2", "tang", "W", "18"), + FinalSeries.parse("3", "yang", "W", "20"), + FinalSeries.parse("4", "shen", "W", "19") + ); + // 创建一个空的 DataFrame 对象 + FDataFrame select2 = FDataFrame.select( + FieldCell.parse("id", "name", "sex", "age"), 1 + ); + // 手动插入数据 + select2.insert( + FinalSeries.parse("1", "zhao", "M", "19"), + FinalSeries.parse("2", "tang", "W", "18"), + FinalSeries.parse("3", "yang", "W", "20") + ); + // 进行求和与做差的运算操作 + System.out.println(select1.add(select2)); + System.out.println(select1.diff(select2)); + } +} +``` + +* DataFrame数据对象指定列合并 + +```java +package zhao.algorithmMagic; + +import zhao.algorithmMagic.operands.table.*; + +import java.sql.SQLException; + +public class MAIN1 { + public static void main(String[] args) { + // 创建一个空的 DataFrame 对象 + FDataFrame select = FDataFrame.select( + FieldCell.parse("id", "name", "sex", "age"), 1 + ); + // 手动插入数据 然后进行分组 + GroupDataFrameData groupDataFrameData = select.insert( + FinalSeries.parse("1", "zhao", "M", "19"), + FinalSeries.parse("2", "tang", "W", "18"), + FinalSeries.parse("3", "yang", "W", "20") + ).groupBy("sex"); + // 打印出 M 组的数据 + System.out.println(groupDataFrameData.getDFByGroup("M")); + // 打印出 W 组的数据 + System.out.println(groupDataFrameData.getDFByGroup("W")); + } +} +``` + +* 新版本注意事项:此版本中的所有依赖被标记为 provided 这样可以最大化的降低冗余程度,能够根据自己的实际项目来进行相关的配置,因此在进行AS库注入的时候请将以下的必须项一键导入。 + +```xml + + + + + org.apache.logging.log4j + log4j-slf4j-impl + 2.20.0 + + + + + + org.apache.logging.log4j + log4j-api + 2.20.0 + + + + + org.apache.logging.log4j + log4j-core + 2.20.0 + + + +``` + +### Version update date : 2023-03-26 \ No newline at end of file diff --git a/src_code/update/1.16_1.17.md b/src_code/update/1.16_1.17.md new file mode 100644 index 0000000..e9e3e81 --- /dev/null +++ b/src_code/update/1.16_1.17.md @@ -0,0 +1,791 @@ +# ![image](https://user-images.githubusercontent.com/113756063/194830221-abe24fcc-484b-4769-b3b7-ec6d8138f436.png) Algorithm Star-MachineBrain + +- 切换到 [中文文档](https://github.com/BeardedManZhao/algorithmStar/blob/main/src_code/update/1.16_1.17-Chinese.md) +- knowledge base + + + + +### Update log: + +* Framework version: 1.16 - 1.17 +* Change the name of the integrator to Integrator. +* It provides the calculation support of convolution function, which can enlarge the features and reduce the number of + elements in the image matrix. + +```java +package zhao.algorithmMagic; + +import zhao.algorithmMagic.integrator.ImageRenderingIntegrator; +import zhao.algorithmMagic.integrator.launcher.ImageRenderingMarLauncher; +import zhao.algorithmMagic.operands.matrix.IntegerMatrix; +import zhao.algorithmMagic.operands.matrix.block.IntegerMatrixSpace; + +public class MAIN1 { + public static void main(String[] args) { + String s1 = "C:\\Users\\Liming\\Desktop\\fsDownload\\微信图片_1.jpg"; + IntegerMatrix integerMatrix; + { + // 设置权重 + IntegerMatrix weight = IntegerMatrix.parse( + new int[]{0, -1}, + new int[]{-1, 2}, + new int[]{0, 0} + ); + // 读取图像并获取到三通道矩阵空间 + IntegerMatrixSpace parse = IntegerMatrixSpace.parse(s1); + // 对图像进行卷积,获取三个色彩通道的矩阵空间的和 + integerMatrix = parse.foldingAndSum(2, 3, IntegerMatrixSpace.parse(weight, weight, weight)); + } + // 输出图片1的卷积图像文件 + ImageRenderingIntegrator image = new ImageRenderingIntegrator( + "image", + new ImageRenderingMarLauncher<>(integerMatrix, "C:\\Users\\Liming\\Desktop\\fsDownload\\res12.jpg", 1) + ); + if (image.run()) { + System.out.println("ok!!!"); + } + } +} +``` + +* The image matrix supports displaying images on the screen directly through the show function. + +```java +package zhao.algorithmMagic; + +import zhao.algorithmMagic.operands.matrix.ColorMatrix; + +public class MAIN1 { + public static void main(String[] args) throws InterruptedException { + String s1 = "C:\\Users\\Liming\\Desktop\\fsDownload\\微信图片_1.jpg"; + ColorMatrix parse = ColorMatrix.parse(s1); + parse.show("image"); + Thread.sleep(1024); + parse.colorReversal(false); + parse.show("image"); + } +} +``` + +* Optimize the algorithm of maximum random scrambling times in the random scrambling function shuffle, so that it will + not have out-of-bounds exceptions. +* Optimize the function logic of random access to data in the matrix through column names, and use hash to map + addresses. + +```java +package zhao.algorithmMagic; + +import zhao.algorithmMagic.operands.matrix.ColumnDoubleMatrix; + +import java.util.Arrays; +import java.util.Random; + +public class MAIN1 { + public static void main(String[] args) { + // 构建一份矩阵数据 + double[][] data = new double[][]{ + new double[]{10, 11, 14, 10, 100}, + new double[]{11, 11, 14, 10, 100}, + new double[]{25, 20, 28, 20, 100}, + new double[]{26, 20, 28, 20, 100} + }; + // 将矩阵数据使用指定列与行名称的方式创建出来 + ColumnDoubleMatrix columnDoubleMatrix = ColumnDoubleMatrix.parse( + new String[]{"col1", "col2", "col3", "col4", "col5"}, + new String[]{"row1", "row2", "row3", "row4"}, + data + ); + // 使用不创建新矩阵的方式打乱其中的数据顺序 且最多打乱 10 次 + columnDoubleMatrix.shuffle(new Random(), false, 10); + // 打印出矩阵数据 + System.out.println(columnDoubleMatrix); + // 打印出矩阵中的 col2 列 在新版中该函数采取哈希实现 速度提升很明显 + System.out.println(Arrays.toString(columnDoubleMatrix.getArrayByColName("col2"))); + // 打印出矩阵中的 row2 行 在新版中该函数采取哈希实现 速度提升很明显 + System.out.println(Arrays.toString(columnDoubleMatrix.getArrayByRowName("row2"))); + } +} +``` + +* It supports the call operation of the quick save function of the image, making the saving of the image file easier. + +```java +package zhao.algorithmMagic; + +import zhao.algorithmMagic.operands.matrix.ColorMatrix; + +public class MAIN1 { + public static void main(String[] args) { + String s1 = "C:\\Users\\zhao\\Desktop\\fsDownload\\微信图片_2.jpg"; + // 获取到图像矩阵对象 + ColorMatrix parse = ColorMatrix.parse(s1); + // 将图像在原图像的基础上进行颜色反转操作 + parse.colorReversal(false); + // 查看颜色反转之后的图像 + parse.show("image1"); + // 输出反转之后的图像 + parse.save("C:\\Users\\zhao\\Desktop\\fsDownload\\res123.jpg"); + } +} +``` + +* The "SaveMatrix" interface is added. All matrices that implement this interface can be saved in structured or + unstructured way. + +```java +package zhao.algorithmMagic; + +import zhao.algorithmMagic.operands.matrix.ColorMatrix; + +public class MAIN1 { + public static void main(String[] args) { + String s1 = "C:\\Users\\Liming\\Desktop\\fsDownload\\微信图片_2.jpg"; + // 获取到图像矩阵对象 + ColorMatrix parse = ColorMatrix.parse(s1); + // 输出图像RGB文本数据 + parse.save("C:\\Users\\Liming\\Desktop\\fsDownload\\res1234.csv", ','); + } +} +``` + +* A data analysis library is added to the AS library, which uses SQL styles to process data. Through this library, data + in the database can be easily obtained into memory for processing. + +```java +package zhao.algorithmMagic; + +import zhao.algorithmMagic.operands.table.DataFrame; +import zhao.algorithmMagic.operands.table.FDataFrame; + +import java.sql.Connection; +import java.sql.DriverManager; +import java.sql.SQLException; + +public class MAIN1 { + public static void main(String[] args) throws SQLException { + // 准备数据库连接对象 + Connection connection = DriverManager.getConnection("jdbc:mysql://192.168.0.101:38243/tq_SCHOOL", "liming", "liming7887"); + // 将数据库数据对象加载成 FDF + DataFrame execute = FDataFrame.builder(connection) // 指定数据库连接 + .create("*") // 指定查询字段 + .from("STU_FIVE") // 指定查询表 + .primaryKey(0) // 指定内存AS表的主键(AS会自动建立行索引)数据库查询需要使用索引指定哦! + .execute();// 开始查询 + // 打印出 FDF 中的数据 + System.out.println(execute); + + // 开始查询 FDF + // 正序打印出 FDF 中 所有name长度为3的人员中男女生的人数 打印出前 3 行数据 + DataFrame select = execute + .select("name", "sex") // 查询其中的 name sex 列 + .where(v -> v.getCell(0).getStringValue().length() == 3) // 获取到其中的名字长度为 3 的数据行 + .groupBy("sex") // 按照 sex 列分组 + .count() // 将每一组进行统计 + .sort("count()") // 按照统计结果进行正序排序 + .limit(3); // 获取最多前3行数据 + System.out.println(select.into_outfile("C:\\Users\\zhao\\Desktop\\fsDownload\\res1.csv")); + // 打印存储 FDF 中的数据行数 + System.out.println("数据行数 = " + execute.count()); + // 打印出其中的信息 + System.out.println(execute.desc()); + // 获取到 赵凌宇 的信息 由于 name 列是主键,因此可以直接通过这里获取到数据 + System.out.println(execute.selectRow("赵凌宇")); + } +} +``` + +* Use FDataFrame to load the data in the database and file. + +```java +package zhao.algorithmMagic; + +import zhao.algorithmMagic.operands.table.*; + +import java.sql.Connection; +import java.sql.DriverManager; +import java.sql.SQLException; + +public class MAIN1 { + public static void main(String[] args) throws SQLException { + // 准备数据库连接对象 + Connection connection = DriverManager.getConnection("jdbc:mysql://192.168.0.101:38243/tq_SCHOOL", "liming", "liming7887"); + // 将数据库数据对象加载成 FDF + DataFrame execute = FDataFrame.builder(connection) // 指定数据库连接 + .create("*") // 指定查询字段 + .from("STU_FIVE") // 指定查询表 + .where(" name = '赵凌宇' ") // 指定查询条件 + .primaryKey(0) // 指定内存AS表的主键(AS会自动建立行索引)数据库查询需要使用索引指定哦! + .execute();// 开始查询 + // 打印出 FDF 中的数据 + System.out.println(execute); + } +} +``` + +```java +package zhao.algorithmMagic; + +import zhao.algorithmMagic.operands.table.DataFrame; +import zhao.algorithmMagic.operands.table.FDataFrame; + +import java.io.File; +import java.sql.SQLException; +import java.util.Objects; + +public class MAIN1 { + public static void main(String[] args) { + // 准备文件对象 + File file = new File("C:\\Users\\zhao\\Desktop\\out\\res1.csv"); + // 使用 FDF 加载文件 + DataFrame execute1 = FDataFrame.builder(file) + // 文件对象的读取需要指定文本分隔符 + .setSep(',') + // 文件对象需要指定好列名称,不能使用 * 这里代表的不是查询,而是创建一个DF的列字段 + .create("id", "name", "sex") + // 文件对象需要使用lambda表达式进行数据的过滤 + .where(v -> Objects.equals(v.getCell(1).getStringValue(), "赵凌宇")) + // 文件对象的主键指定允许使用列名称 + .primaryKey("name") + // 执行查询 + .execute(); + // 打印出结果数据 + System.out.println(execute1); + } +} + ``` + +* Limit FDF by line name + +```java +package zhao.algorithmMagic; + +import zhao.algorithmMagic.operands.table.DataFrame; +import zhao.algorithmMagic.operands.table.FDataFrame; + +import java.io.File; +import java.sql.SQLException; + +public class MAIN1 { + public static void main(String[] args) { + // 准备文件对象 + File file = new File("C:\\Users\\zhao\\Desktop\\out\\res1.csv"); + // 使用 FDF 加载文件 + DataFrame execute1 = FDataFrame.builder(file) + // 文件对象的读取需要指定文本分隔符 + .setSep(',') + // 文件对象需要指定好列名称,不能使用 * 这里代表的不是查询,而是创建一个DF的列字段 + .create("id", "name", "sex") + // 文件对象的主键指定允许使用列名称 + .primaryKey("name") + // 执行查询 + .execute(); + // 打印出结果数据 这里打印出从 赵 到 贾 之间的数据行 + System.out.println(execute1.limit("赵", "贾")); + } +} +``` + +* Support the operations of aliasing columns and adding data rows. + +```java +package zhao.algorithmMagic; + +import zhao.algorithmMagic.operands.table.DataFrame; +import zhao.algorithmMagic.operands.table.FDataFrame; +import zhao.algorithmMagic.operands.table.FieldCell; + +import java.io.File; +import java.sql.SQLException; + +public class MAIN1 { + public static void main(String[] args) { + // 准备文件对象 + File file = new File("C:\\Users\\zhao\\Desktop\\out\\res1.csv"); + // 使用 FDF 加载文件 + DataFrame execute1 = FDataFrame.builder(file) + // 文件对象的读取需要指定文本分隔符 + .setSep(',') + // 文件对象需要指定好列名称,不能使用 * 这里代表的不是查询,而是创建一个DF的列字段 + .create("id", "name", "sex") + // 文件对象的主键指定允许使用列名称 + .primaryKey("name") + // 执行查询 + .execute(); + // 按照性别分组,计算出男女生人数 + System.out.println( + execute1 + // 指定查询的列,并起别名 + .select(FieldCell.$("sex").as("性别")) + // 按照 sex 分组 + .groupBy("性别") + // 进行 组内的计数 + .count() + // 指定查询的列 并起别名 + .select( + FieldCell.$("性别"), + FieldCell.$("count()").as("人数") + ) + ); + } +} +``` + +* Capable of functional update of row or column data for all values in DF data set objects, and supports conversion from + DF data objects to matrices. + +```java +package zhao.algorithmMagic; + +import zhao.algorithmMagic.operands.matrix.IntegerMatrix; +import zhao.algorithmMagic.operands.table.DataFrame; +import zhao.algorithmMagic.operands.table.FDataFrame; +import zhao.algorithmMagic.operands.table.FieldCell; +import zhao.algorithmMagic.operands.table.FinalCell; +import zhao.algorithmMagic.operands.vector.IntegerVector; + +import java.io.File; + +public class MAIN1 { + public static void main(String[] args) { + // 准备文件对象 + File file = new File("C:\\Users\\zhao\\Desktop\\out\\res1.csv"); + // 使用 FDF 加载文件 + DataFrame execute1 = FDataFrame.builder(file) + // 文件对象的读取需要指定文本分隔符 + .setSep(',') + // 文件对象需要指定好列名称,不能使用 * 这里代表的不是查询,而是创建一个DF的列字段 + .create("id", "name", "sex") + // 文件对象的主键指定允许使用列名称 + .primaryKey("name") + // 执行查询 + .execute() + // 为列起别名 + .select( + FieldCell.$("id"), + FieldCell.$("name").as("名称"), + FieldCell.$("sex").as("性别") + ) + // 将性别列进行转换,男生为1 女生为0 + .updateCol(FieldCell.$("性别"), cell -> new FinalCell<>(cell.getStringValue().equals("男") ? 1 : 0)) + // 将行主键数值为ZLY的数据行中的所有单元格替换成为数据 405 + .updateRow("ZLY", cell -> new FinalCell<>(405)); + + // 打印出表中的行主键名称为 405 的数据行 + System.out.println(execute1.selectRow("405")); + long start = System.currentTimeMillis(); + // 将表转换成为一个整形矩阵对象,该操作会将DF对象中的所有数值试图转换成为 col.count()*3 的矩阵对象 + IntegerMatrix parse = IntegerMatrix.parse(execute1, execute1.count().getIntValue(), 3); + System.out.println(IntegerVector.parse(parse.getArrayByColIndex(2))); + System.out.print("处理耗时(MS):"); + System.out.println(System.currentTimeMillis() - start); + } +} +``` + +* Support for obtaining image related data through a web URL object and converting it into an image matrix. + +```java +package zhao.algorithmMagic; + +import zhao.algorithmMagic.operands.matrix.ColorMatrix; + +import java.net.MalformedURLException; +import java.net.URL; + +public class MAIN1 { + public static void main(String[] args) throws MalformedURLException { + // 准备图像的URL对象 + URL url = new URL("https://user-images.githubusercontent.com/113756063/194830221-abe24fcc-484b-4769-b3b7-ec6d8138f436.png"); + // 解析URL获取到图像矩阵 + ColorMatrix parse1 = ColorMatrix.parse(url); + // 解析URL获取到图像的灰度矩阵 + ColorMatrix parse2 = ColorMatrix.parseGrayscale(url); + // 查看图像 + parse1.show("image"); + parse2.show("image"); + } +} +``` + +* It supports color binarization and regularization coverage, and can display more image features or remove more + redundant features by specifying the color values of the channel. + +```java +package zhao.algorithmMagic; + +import zhao.algorithmMagic.operands.matrix.ColorMatrix; + +import java.net.MalformedURLException; +import java.net.URL; + +public class MAIN1 { + public static void main(String[] args) throws MalformedURLException { + // 准备图像的URL对象 + URL url = new URL("https://user-images.githubusercontent.com/113756063/194830221-abe24fcc-484b-4769-b3b7-ec6d8138f436.png"); + // 解析URL获取到图像矩阵 + ColorMatrix parse1 = ColorMatrix.parse(url); + // 将 URL 图像矩阵中所有 G 通道颜色数值大于 40 的颜色变更为黑色,反之变更为白色 + // 在这里由于选择了 G 通道 因此 绿色越深 越有可能变为白色 + parse1.globalBinary(ColorMatrix._G_, 40, 0, 0xffffff); + // 也可以使用其它颜色通道进行色彩的调整 + parse1.globalBinary(ColorMatrix._R_, 40, 0, 0xffffff); + parse1.globalBinary(ColorMatrix._B_, 40, 0, 0xffffff); + // 查看结果图像 + parse1.show("image"); + } +} +``` + +* Supports the construction of image ASCII images. You can quickly construct an image in ASCII, but please ensure that + the image size is small enough. + +```java +package zhao.algorithmMagic; + +import zhao.algorithmMagic.operands.matrix.ColorMatrix; + +import java.io.File; +import java.net.MalformedURLException; +import java.net.URL; + +public class MAIN1 { + public static void main(String[] args) throws MalformedURLException { + // 准备图像的URL对象 + URL url = new URL("https://user-images.githubusercontent.com/113756063/194830221-abe24fcc-484b-4769-b3b7-ec6d8138f436.png"); + // 解析URL获取到图像矩阵 + ColorMatrix parse1 = ColorMatrix.parse(url); + // 查看结果图像 + parse1.show("image"); + // 输出图像的 ASCII 数值,输出规则为 G 通道颜色数值 大于 40 的 输出符号 'A' 其它输出符号 ' ' + parse1.save( + new File("C:\\Users\\zhao\\Desktop\\fsDownload\\res.txt"), + ColorMatrix._G_, 40, 'A', ' ' + ); + } +} +``` + +* Ability to manually create an empty DataFrame object and independently manipulate the data therein. + +```java +package zhao.algorithmMagic; + +import zhao.algorithmMagic.operands.table.DataFrame; +import zhao.algorithmMagic.operands.table.FDataFrame; +import zhao.algorithmMagic.operands.table.FieldCell; +import zhao.algorithmMagic.operands.table.FinalSeries; + +public class MAIN1 { + public static void main(String[] args) { + // 创建一个空 DF 对象 以 name 列作为行主键索引 + FDataFrame create = FDataFrame.select(FieldCell.parse("name", "sex", "phoneNum"), 1); + // 插入一些数据 + DataFrame insert = create.insert( + FinalSeries.parse("zhao", "M", "110xxxxxxxx"), + FinalSeries.parse("tang", "W", "110xxxxxxxx"), + FinalSeries.parse("yang", "M", "110xxxxxxxx") + ); + // 查看数据集 + System.out.println(insert); + // 将其中的 name 列 sex 列 查询 + System.out.println( + insert.select( + FieldCell.$("name").as("AllName"), + FieldCell.$("sex").as("AllSex") + ) + ); + } +} +``` + +* 支持在 group 的时候指定 where子句,使得计算效率大大增强 + +```java +package zhao.algorithmMagic; + +import zhao.algorithmMagic.operands.table.DataFrame; +import zhao.algorithmMagic.operands.table.FDataFrame; +import zhao.algorithmMagic.operands.table.FieldCell; +import zhao.algorithmMagic.operands.table.FinalSeries; + +import java.net.MalformedURLException; + +public class MAIN1 { + public static void main(String[] args) { + // 创建一个空 DF 对象 以 name 列作为行主键索引 + FDataFrame create = FDataFrame.select(FieldCell.parse("name", "sex", "phoneNum"), 1); + // 插入一些数据 + DataFrame insert = create.insert( + FinalSeries.parse("zhao1", "M", "110xxxxxxxx"), + FinalSeries.parse("tang2", "W", "120xxxxxxxx"), + FinalSeries.parse("yang3", "W", "110xxxxxxxx"), + FinalSeries.parse("zhao4", "M", "120xxxxxxxx"), + FinalSeries.parse("tang5", "W", "110xxxxxxxx"), + FinalSeries.parse("yang6", "W", "110xxxxxxxx"), + FinalSeries.parse("zhao7", "M", "120xxxxxxxx"), + FinalSeries.parse("tang8", "W", "110xxxxxxxx"), + FinalSeries.parse("yang9", "W", "110xxxxxxxx") + ); + // 查看数据集 + System.out.println(insert); + // 将其中手机号前三位不为 120 的数据行按照其中的 sex 分组 在这里直接使用分组时过滤即可 + System.out.println( + insert.groupBy("sex", v -> { + // 获取到手机号的字符串 + String s = v.getCell(2).toString(); + // 判断前 3 个字符是否为 120 (是否以 120 开头) 如果是就不添加 + return !s.startsWith("120"); + }).count() + ); + } +} +``` + +* Support for local binarization operations, which can effectively achieve image binarization processing. Compared to + global binarization, functions are more flexible. + +```java +package zhao.algorithmMagic; + +import zhao.algorithmMagic.operands.matrix.ColorMatrix; + +import java.net.MalformedURLException; +import java.net.URL; + +public class MAIN1 { + public static void main(String[] args) throws MalformedURLException { + // 获取到图像矩阵 + ColorMatrix parse = ColorMatrix.parse(new URL("https://img-blog.csdnimg.cn/img_convert/5765bdab08ef6e117d434e7e225b9013.png")); + parse.show("image"); + System.out.println("ok!!!"); + System.out.println("长 = " + parse.getRowCount()); + System.out.println("宽 = " + parse.getColCount()); + // 将图像进行局部二值化 + parse.localBinary( + // 指定本次二值化选择的颜色通道 + ColorMatrix._G_, + // 指定本次二值化选出的局部图像矩阵数量 + 100, + // 指定本次二值化中局部矩阵中大于局部阈值的颜色编码 + 0xffffff, + // 指定本次二值化中局部矩阵中小于局部阈值的颜色编码 + 0, + // 指定本次二值化中局部阈值生成后要进行的微调数值,这里是降低20个阈值数值 + -30 + ); + // 查看结果数据 + parse.show("image"); + } +} +``` + +* DF object data supports saving as HTML files, enabling effective automated page rendering operations. + +```java +package zhao.algorithmMagic; + +import zhao.algorithmMagic.operands.table.DataFrame; +import zhao.algorithmMagic.operands.table.FDataFrame; +import zhao.algorithmMagic.operands.table.FieldCell; +import zhao.algorithmMagic.operands.table.FinalSeries; + +public class MAIN1 { + public static void main(String[] args) { + // 创建一个空 DF 对象 以 name 列作为行主键索引 + FDataFrame create = FDataFrame.select(FieldCell.parse("name", "sex", "phoneNum", "salary"), 1); + // 插入一些数据 + DataFrame insert = create.insert( + FinalSeries.parse("zhao1", "M", "110xxxxxxxx", "30000"), + FinalSeries.parse("tang2", "W", "120xxxxxxxx", "30000"), + FinalSeries.parse("yang3", "W", "110xxxxxxxx", "30000"), + FinalSeries.parse("zhao4", "M", "120xxxxxxxx", "30000"), + FinalSeries.parse("tang5", "W", "110xxxxxxxx", "30000"), + FinalSeries.parse("yang6", "W", "110xxxxxxxx", "30000"), + FinalSeries.parse("zhao7", "M", "120xxxxxxxx", "30000"), + FinalSeries.parse("tang8", "W", "110xxxxxxxx", "30000"), + FinalSeries.parse("yang9", "W", "110xxxxxxxx", "30000") + ); + // 输出表的HTML 然后查看表中的数据 + System.out.println( + insert.into_outHtml("C:\\Users\\Liming\\Desktop\\fsDownload\\res11234.html", "myTable") + ); + } +} +``` + +* Images begin to support the add function, which enables overlapping and merging of image functions under the operation + of the add function! + +```java +package zhao.algorithmMagic; + +import zhao.algorithmMagic.operands.matrix.ColorMatrix; + +import java.sql.SQLException; + +public class MAIN1 { + public static void main(String[] args) { + // 将一些图像文件转换成为一个图像矩阵对象 + ColorMatrix colorMatrix1 = ColorMatrix.parse("C:\\Users\\Liming\\Desktop\\fsDownload\\test.bmp"); + ColorMatrix colorMatrix2 = ColorMatrix.parse("C:\\Users\\Liming\\Desktop\\fsDownload\\test1.bmp"); + // 将 colorMatrix2 + colorMatrix1 的结果图像展示出来 + (colorMatrix1.add(colorMatrix2)).show("image"); + } +} +``` + +* Support aggregation operations for custom aggregation and built-in data schemes. + +```java +package zhao.algorithmMagic; + +import zhao.algorithmMagic.operands.matrix.ColorMatrix; + +import java.sql.SQLException; + +public class MAIN1 { + public static void main(String[] args) { + // 将一些图像文件转换成为一个图像矩阵对象 + ColorMatrix colorMatrix1 = ColorMatrix.parse("C:\\Users\\Liming\\Desktop\\fsDownload\\test.bmp"); + ColorMatrix colorMatrix2 = ColorMatrix.parse("C:\\Users\\Liming\\Desktop\\fsDownload\\test1.bmp"); + // 使用 agg 函数以及内置的计算方案进行两个矩阵的合并 + // 下面函数中的第二个形参代表的就是矩阵中元素的聚合逻辑,这里使用的是求和,并对越界颜色数值进行规整的逻辑实现 + colorMatrix1.agg(colorMatrix2, ColorMatrix.COLOR_SUM_REGULATE).show("image"); + } +} +``` + +* The image matrix supports a corrosion operation function in which redundant feature data can be removed. + +```java +package zhao.algorithmMagic; + +import zhao.algorithmMagic.operands.matrix.ColorMatrix; + +import java.sql.SQLException; + + +public class MAIN1 { + public static void main(String[] args) { + // 将一些图像文件转换成为一个图像矩阵对象 + ColorMatrix colorMatrix1 = ColorMatrix.parseGrayscale("C:\\Users\\Liming\\Desktop\\fsDownload\\test2.bmp"); + // 对图像进行二值化 + colorMatrix1.globalBinary(ColorMatrix._G_, 100, 0xffffff, 0); + colorMatrix1.show("腐蚀之前的 image"); + // 开始对图像矩阵进行腐蚀操作 + colorMatrix1.erode(2, 2, false).show("腐蚀之后的 image"); + } +} +``` + +* The addition of columns is supported, but it should be noted that the addition of columns will return a new DataFrame + DF in which the data will be lightly copied. + +```java +package zhao.algorithmMagic; + +import zhao.algorithmMagic.operands.table.*; + +import java.sql.SQLException; + +public class MAIN1 { + public static void main(String[] args) { + // 创建一个空的 DataFrame 对象 + FDataFrame select = FDataFrame.select( + FieldCell.parse("id", "name", "sex", "age"), 1 + ); + // 手动插入数据 + select.insert( + FinalSeries.parse("1", "zhao", "M", "19"), + FinalSeries.parse("2", "tang", "W", "18"), + FinalSeries.parse("3", "yang", "W", "20"), + FinalSeries.parse("4", "shen", "W", "19") + ); + // 打印出 DF 对象有关的信息 + System.out.println( + select.desc() + ); + // 添加一列数据,用于表示年龄是否为偶数 + DataFrame ageIsE = select.insertColGetNew( + // 新列的列名称 + FieldCell.$("AgeIsE"), + // 新列的数值生成逻辑 如果第 4 列(index == 3)的数值 % 2 == 0 就是true + cells -> cells.getCell(3).getIntValue() % 2 == 0 ? new FinalCell<>(true) : new FinalCell<>(false) + ); + System.out.println(ageIsE); + } +} +``` + +* Supports operations between table data, and can perform summation and subtraction calculations on all numeric types in + the table. After calculation, a new DF object will be returned. + +```java +package zhao.algorithmMagic; + +import zhao.algorithmMagic.operands.table.*; + +public class MAIN1 { + public static void main(String[] args) { + // 创建一个空的 DataFrame 对象 + FDataFrame select1 = FDataFrame.select( + FieldCell.parse("id", "name", "sex", "age"), 1 + ); + // 手动插入数据 + select1.insert( + FinalSeries.parse("1", "zhao", "M", "19"), + FinalSeries.parse("2", "tang", "W", "18"), + FinalSeries.parse("3", "yang", "W", "20"), + FinalSeries.parse("4", "shen", "W", "19") + ); + // 创建一个空的 DataFrame 对象 + FDataFrame select2 = FDataFrame.select( + FieldCell.parse("id", "name", "sex", "age"), 1 + ); + // 手动插入数据 + select2.insert( + FinalSeries.parse("1", "zhao", "M", "19"), + FinalSeries.parse("2", "tang", "W", "18"), + FinalSeries.parse("3", "yang", "W", "20") + ); + // 进行求和与做差的运算操作 + System.out.println(select1.add(select2)); + System.out.println(select1.diff(select2)); + } +} +``` + +* Note for the new version: All dependencies in this version are marked as provided, which can minimize redundancy and + enable related configuration based on your actual project. Therefore, when performing AS library injection, please + import the following required items in one click. + +```xml + + + + + org.apache.logging.log4j + log4j-slf4j-impl + 2.20.0 + + + + + + org.apache.logging.log4j + log4j-api + 2.20.0 + + + + + org.apache.logging.log4j + log4j-core + 2.20.0 + + + +``` + +### Version update date : 2023-03-26 \ No newline at end of file From c5da8a04eb6ab6874ba04c7b254eb5fc4220e690 Mon Sep 17 00:00:00 2001 From: LingYuZhao Date: Tue, 28 Mar 2023 21:59:20 +0800 Subject: [PATCH 03/17] update MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * 新增矩阵 AGG 函数的逻辑实现 --- src_code/README-Chinese.md | 119 +++++++++++++++- src_code/README.md | 118 ++++++++++++++++ src_code/pom.xml | 6 +- .../main/java/zhao/algorithmMagic/MAIN1.java | 20 ++- .../operands/matrix/ColorMatrix.java | 133 ++++++++++++++++++ .../operands/table/FDataFrame.java | 3 + .../operands/vector/SparkVector.scala | 30 ++-- 7 files changed, 409 insertions(+), 20 deletions(-) diff --git a/src_code/README-Chinese.md b/src_code/README-Chinese.md index 21699f6..71f0da7 100644 --- a/src_code/README-Chinese.md +++ b/src_code/README-Chinese.md @@ -34,7 +34,29 @@ public class MAIN1 { } ``` -* 修正二维数组拷贝时,只在在数组层深拷贝问题(二维度数组中的一维数组没有进行深拷贝)现在已经重新设计,使得深拷贝操作开始重新生效。 +* 为图像矩阵添加了一种聚合实现逻辑,差值绝对值,在做差之后返回绝对值。 + +```java +package zhao.algorithmMagic; + +import zhao.algorithmMagic.operands.matrix.ColorMatrix; + +public class MAIN1 { + public static void main(String[] args) { + // 创建一个图像矩阵 + ColorMatrix parse = ColorMatrix.parseGrayscale("C:\\Users\\Liming\\Desktop\\fsdownload\\test3.bmp"); + // 为图像矩阵进行腐蚀和膨胀操作 + // 黑色作为背景色腐蚀 + ColorMatrix erode1 = parse.erode(3, 3, true); + // 白色作为背景色膨胀 + ColorMatrix erode2 = parse.erode(3, 3, true, ColorMatrix.WHITE); + // 将膨胀的矩阵对象 使用agg函数 减去 腐蚀的矩阵对象 + (erode2.agg(erode1, ColorMatrix.COLOR_DIFF_ABS)).show("image"); + } +} +``` + +* 修正二维数组拷贝时,只在数组层深拷贝问题(二维度数组中的一维数组没有进行深拷贝)现在已经重新设计,使得深拷贝操作开始重新生效。 ```java package zhao.algorithmMagic; @@ -52,4 +74,99 @@ public class MAIN1 { } ``` +* 新增矩阵内运算函数,该函数能够实现矩阵中所有坐标元素的计算操作,下面是所有支持的计算操作,并返回一个计算之后的矩阵对象。 + +| 计算常量 | 效果 | 作用 | +|---------------------------|-------------------------|----------------| +| CALCULATE_GRADIENT_RL | mid = right - left | 计算图像在左右的梯度。 | +| CALCULATE_GRADIENT_LH | mid = low - high | 计算图像在上下的梯度。 | +| CALCULATE_GRADIENT_RL_ABS | mid = abs(right - left) | 计算图像在左右的梯度绝对值。 | +| CALCULATE_GRADIENT_LH_ABS | mid = abs(low - high) | 计算图像在上下的梯度绝对值。 | + +```java +package zhao.algorithmMagic; + +import zhao.algorithmMagic.operands.matrix.ColorMatrix; + +public class MAIN1 { + public static void main(String[] args) { + ColorMatrix calculate1; + ColorMatrix calculate2; + { + // 创建一个图像矩阵 + ColorMatrix parse = ColorMatrix.parseGrayscale("C:\\Users\\Liming\\Desktop\\fsdownload\\test3.bmp"); + // 进行左右的梯度计算 + parse.calculate(ColorMatrix.CALCULATE_GRADIENT_RL, true).show("image1"); + // 进行上下的梯度计算 + parse.calculate(ColorMatrix.CALCULATE_GRADIENT_LH, true).show("image2"); + // 进行左右的绝对值梯度计算 + calculate1 = parse.calculate(ColorMatrix.CALCULATE_GRADIENT_RL_ABS, true); + calculate1.show("image3"); + // 进行上下的绝对值梯度计算 + calculate2 = parse.calculate(ColorMatrix.CALCULATE_GRADIENT_LH_ABS, true); + calculate2.show("image4"); + } + // 进行两幅图像的加法合并 并打印新矩阵 + (calculate1.add(calculate2)).show("image"); + } +} +``` + +* DF 对象的 select * 被支持,使用 * 做查询占位符将不会进行任何运算,而是直接返回数据。 + +```java +package zhao.algorithmMagic; + +import zhao.algorithmMagic.operands.table.*; + +public class MAIN1 { + public static void main(String[] args) { + // 创建一个空的 DataFrame 对象 + FDataFrame dataFrame = FDataFrame.select( + FieldCell.parse("id", "name", "sex", "age"), 1 + ); + // 手动插入数据 + dataFrame.insert( + FinalSeries.parse("1", "zhao", "M", "19"), + FinalSeries.parse("2", "tang", "W", "18"), + FinalSeries.parse("3", "yang", "W", "20"), + FinalSeries.parse("4", "shen", "W", "19") + ); + // 打印出 select * 的数据 + System.out.println( + dataFrame.select("*") + ); + } +} +``` + +* 图像矩阵 agg 函数,新增矩阵坐标相对应坐标的颜色数值相乘的计算逻辑实现。 + +```java +package zhao.algorithmMagic; + +import zhao.algorithmMagic.operands.matrix.ColorMatrix; + +import java.awt.*; + +public class MAIN1 { + public static void main(String[] args) { + Color[] colors = {Color.CYAN, Color.GREEN, Color.PINK, Color.red}; + // 手动创建图像矩阵 + ColorMatrix parse = ColorMatrix.parse( + colors, colors, colors, colors + ); + // 查看图像矩阵 + parse.show("image", 100, 100); + // 将图像中的所有像素与自身矩阵进行规整乘法计算 + ColorMatrix agg1 = parse.agg(parse, ColorMatrix.COLOR_MULTIPLY_REGULATE); + // 将图像中的所有像素与自身矩阵进行取余乘法计算 + ColorMatrix agg2 = parse.agg(parse, ColorMatrix.COLOR_MULTIPLY_REMAINDER); + // 查看两个图像数据 + agg1.show("image1", 100, 100); + agg2.show("image2", 100, 100); + } +} +``` + ### Version update date : xx xx-xx-xx \ No newline at end of file diff --git a/src_code/README.md b/src_code/README.md index 9a47cca..f345bf3 100644 --- a/src_code/README.md +++ b/src_code/README.md @@ -35,6 +35,29 @@ public class MAIN1 { } ``` +* An aggregation implementation logic has been added to the image matrix, where the absolute value of the difference is + returned after the difference is made. + +```java +package zhao.algorithmMagic; + +import zhao.algorithmMagic.operands.matrix.ColorMatrix; + +public class MAIN1 { + public static void main(String[] args) { + // 创建一个图像矩阵 + ColorMatrix parse = ColorMatrix.parseGrayscale("C:\\Users\\Liming\\Desktop\\fsdownload\\test3.bmp"); + // 为图像矩阵进行腐蚀和膨胀操作 + // 黑色作为背景色腐蚀 + ColorMatrix erode1 = parse.erode(3, 3, true); + // 白色作为背景色膨胀 + ColorMatrix erode2 = parse.erode(3, 3, true, ColorMatrix.WHITE); + // 将膨胀的矩阵对象 使用agg函数 减去 腐蚀的矩阵对象 + (erode2.agg(erode1, ColorMatrix.COLOR_DIFF_ABS)).show("image"); + } +} +``` + * When fixing the problem of copying two-dimensional arrays, only deep copies were made at the array level ( one-dimensional arrays in two-dimensional arrays did not undergo deep copies). This has now been redesigned to make the deep copy operation effective again. @@ -55,4 +78,99 @@ public class MAIN1 { } ``` +* 新增矩阵内运算函数,该函数能够实现矩阵中所有坐标元素的计算操作,下面是所有支持的计算操作,并返回一个计算之后的矩阵对象。 + +| Calculating Constants | effect | use | +|---------------------------|-------------------------|---------------------------------------------------------------------------------| +| CALCULATE_GRADIENT_RL | mid = right - left | Calculate the gradient of the image on the left and right. | +| CALCULATE_GRADIENT_LH | mid = low - high | Calculate the gradient of the image up and down. | +| CALCULATE_GRADIENT_RL_ABS | mid = abs(right - left) | Calculate the absolute gradient value of the image at the left and right sides. | +| CALCULATE_GRADIENT_LH_ABS | mid = abs(low - high) | Calculate the absolute gradient value of the image at the top and bottom. | + +```java +package zhao.algorithmMagic; + +import zhao.algorithmMagic.operands.matrix.ColorMatrix; + +public class MAIN1 { + public static void main(String[] args) { + ColorMatrix calculate1; + ColorMatrix calculate2; + { + // 创建一个图像矩阵 + ColorMatrix parse = ColorMatrix.parseGrayscale("C:\\Users\\Liming\\Desktop\\fsdownload\\test3.bmp"); + // 进行左右的梯度计算 + parse.calculate(ColorMatrix.CALCULATE_GRADIENT_RL, true).show("image1"); + // 进行上下的梯度计算 + parse.calculate(ColorMatrix.CALCULATE_GRADIENT_LH, true).show("image2"); + // 进行左右的绝对值梯度计算 + calculate1 = parse.calculate(ColorMatrix.CALCULATE_GRADIENT_RL_ABS, true); + calculate1.show("image3"); + // 进行上下的绝对值梯度计算 + calculate2 = parse.calculate(ColorMatrix.CALCULATE_GRADIENT_LH_ABS, true); + calculate2.show("image4"); + } + // 进行两幅图像的加法合并 并打印新矩阵 + (calculate1.add(calculate2)).show("image"); + } +} +``` + +* The DF object's select * is supported. Using * as a query placeholder will not perform any operations, but will + directly return data. + +```java +package zhao.algorithmMagic; + +import zhao.algorithmMagic.operands.table.*; + +public class MAIN1 { + public static void main(String[] args) { + // 创建一个空的 DataFrame 对象 + FDataFrame dataFrame = FDataFrame.select( + FieldCell.parse("id", "name", "sex", "age"), 1 + ); + // 手动插入数据 + dataFrame.insert( + FinalSeries.parse("1", "zhao", "M", "19"), + FinalSeries.parse("2", "tang", "W", "18"), + FinalSeries.parse("3", "yang", "W", "20"), + FinalSeries.parse("4", "shen", "W", "19") + ); + // 打印出 select * 的数据 + System.out.println( + dataFrame.select("*") + ); + } +} +``` + +* In a color value quadrature calculation scheme, the processing logic is to take the current value of% 256 as the current color value if the value exceeds the range. + +```java +package zhao.algorithmMagic; + +import zhao.algorithmMagic.operands.matrix.ColorMatrix; + +import java.awt.*; + +public class MAIN1 { + public static void main(String[] args) { + Color[] colors = {Color.CYAN, Color.GREEN, Color.PINK, Color.red}; + // 手动创建图像矩阵 + ColorMatrix parse = ColorMatrix.parse( + colors, colors, colors, colors + ); + // 查看图像矩阵 + parse.show("image", 100, 100); + // 将图像中的所有像素与自身矩阵进行规整乘法计算 + ColorMatrix agg1 = parse.agg(parse, ColorMatrix.COLOR_MULTIPLY_REGULATE); + // 将图像中的所有像素与自身矩阵进行取余乘法计算 + ColorMatrix agg2 = parse.agg(parse, ColorMatrix.COLOR_MULTIPLY_REMAINDER); + // 查看两个图像数据 + agg1.show("image1", 100, 100); + agg2.show("image2", 100, 100); + } +} +``` ### Version update date : xx xx-xx-xx \ No newline at end of file diff --git a/src_code/pom.xml b/src_code/pom.xml index d52bc17..6e2f85b 100644 --- a/src_code/pom.xml +++ b/src_code/pom.xml @@ -38,7 +38,7 @@ org.apache.logging.log4j log4j-slf4j-impl 2.20.0 - provided + @@ -46,14 +46,14 @@ org.apache.logging.log4j log4j-api 2.20.0 - provided + org.apache.logging.log4j log4j-core 2.20.0 - provided + diff --git a/src_code/src/main/java/zhao/algorithmMagic/MAIN1.java b/src_code/src/main/java/zhao/algorithmMagic/MAIN1.java index 319cb52..b70f461 100644 --- a/src_code/src/main/java/zhao/algorithmMagic/MAIN1.java +++ b/src_code/src/main/java/zhao/algorithmMagic/MAIN1.java @@ -1,6 +1,24 @@ package zhao.algorithmMagic; +import zhao.algorithmMagic.operands.matrix.ColorMatrix; + +import java.awt.*; + public class MAIN1 { public static void main(String[] args) { + Color[] colors = {Color.CYAN, Color.GREEN, Color.PINK, Color.red}; + // 手动创建图像矩阵 + ColorMatrix parse = ColorMatrix.parse( + colors, colors, colors, colors + ); + // 查看图像矩阵 + System.out.println(parse); + // 将图像中的所有像素与自身矩阵进行规整乘法计算 + ColorMatrix agg1 = parse.agg(parse, ColorMatrix.COLOR_MULTIPLY_REGULATE); + // 将图像中的所有像素与自身矩阵进行取余乘法计算 + ColorMatrix agg2 = parse.agg(parse, ColorMatrix.COLOR_MULTIPLY_REMAINDER); + // 查看两个图像数据 + System.out.println(agg1); + System.out.println(agg2); } -} +} \ No newline at end of file diff --git a/src_code/src/main/java/zhao/algorithmMagic/operands/matrix/ColorMatrix.java b/src_code/src/main/java/zhao/algorithmMagic/operands/matrix/ColorMatrix.java index f772e22..51c3840 100644 --- a/src_code/src/main/java/zhao/algorithmMagic/operands/matrix/ColorMatrix.java +++ b/src_code/src/main/java/zhao/algorithmMagic/operands/matrix/ColorMatrix.java @@ -15,6 +15,7 @@ import java.net.URL; import java.util.Arrays; import java.util.Iterator; +import java.util.function.Consumer; /** * 颜色矩阵对象,其中存储的每一个元素都是一个 Color 对象,适合用来进行图像的绘制等工作。 @@ -102,6 +103,112 @@ public class ColorMatrix extends Matrix + * The processing logic in the color value difference calculation scheme is to take 0 or 255 if the value exceeds the range, and take the value itself if the value does not exceed the range, achieving an efficient calculation scheme for limiting color values. + */ + public final static ManyTrans COLOR_DIFF_ABS = (inputType1, inputType2) -> new Color( + ASMath.absoluteValue(inputType1.getRed() - inputType2.getRed()), + ASMath.absoluteValue(inputType1.getGreen() - inputType2.getGreen()), + ASMath.absoluteValue(inputType1.getBlue() - inputType2.getGreen()) + ); + + /** + * 颜色数值求积计算方案,在该方案中的处理逻辑为如果数值超出范围,则取0或255,如果数值没有超过范围则取数值本身,实现颜色数值的高效限制的计算方案。 + * + * The processing logic in the color value quadrature calculation scheme is to take 0 or 255 if the value exceeds the range, and take the value itself if the value does not exceed the range, achieving an efficient calculation scheme for limiting color values. + */ + public final static ManyTrans COLOR_MULTIPLY_REGULATE = (inputType1, inputType2) -> new Color( + ASMath.regularTricolor(inputType1.getRed() * inputType2.getRed()), + ASMath.regularTricolor(inputType1.getGreen() * inputType2.getGreen()), + ASMath.regularTricolor(inputType1.getBlue() * inputType2.getGreen()) + ); + + /** + * 颜色数值求积计算方案,在该方案中的才处理逻辑为如果数值超出范围,则取当前数值 % 256 的结果数值作为当前颜色数值。 + * + * The processing logic in the color value quadrature calculation scheme is to take 0 or 255 if the value exceeds the range, and take the value itself if the value does not exceed the range, achieving an efficient calculation scheme for limiting color values. + */ + public final static ManyTrans COLOR_MULTIPLY_REMAINDER = (inputType1, inputType2) -> { + int red = inputType1.getRed() + inputType2.getRed(); + int green = inputType1.getGreen() + inputType2.getGreen(); + int blue = inputType1.getBlue() + inputType2.getGreen(); + return new Color( + red - (red >> _G_ << _G_), + green - (green >> _G_ << _G_), + blue - (blue >> _G_ << _G_) + ); + }; + + /** + * 将矩阵中所有坐标点计算为: 当前坐标点 = 右坐标 - 左坐标 + */ + public final static Consumer CALCULATE_GRADIENT_RL = colors -> { + if (colors[0].length <= 2) return; + for (Color[] color : colors) { + Color left = color[0], right = color[2]; + color[0] = color[1]; + for (int i = 1, maxI = color.length - 2; i < maxI; ) { + Color now = right; + color[i] = COLOR_DIFF_REGULATE.function(right, left); + left = now; + right = color[++i]; + } + } + }; + + /** + * 将矩阵中所有坐标点计算为: 当前坐标点 = 下坐标 - 上坐标 + */ + public final static Consumer CALCULATE_GRADIENT_LH = colors -> { + if (colors.length <= 2) return; + Color[] back = colors[0], next = colors[2]; + for (int i = 1, maxI = colors.length - 2; i < maxI; ) { + Color[] now1 = colors[i].clone(); + Color[] now2 = next; + for (int index = 0; index < now2.length; index++) { + now2[index] = COLOR_DIFF_REGULATE.function(next[index], back[index]); + } + back = now1; + next = colors[++i]; + } + }; + + /** + * 将矩阵中所有坐标点计算为: 当前坐标点 = 右坐标 - 左坐标 + */ + public final static Consumer CALCULATE_GRADIENT_RL_ABS = colors -> { + if (colors[0].length <= 2) return; + for (Color[] color : colors) { + Color left = color[0], right = color[2]; + color[0] = color[1]; + for (int i = 1, maxI = color.length - 2; i < maxI; ) { + Color now = right; + color[i] = COLOR_DIFF_ABS.function(right, left); + left = now; + right = color[++i]; + } + } + }; + + /** + * 将矩阵中所有坐标点计算为: 当前坐标点 = 下坐标 - 上坐标 + */ + public final static Consumer CALCULATE_GRADIENT_LH_ABS = colors -> { + if (colors.length <= 2) return; + Color[] back = colors[0], next = colors[2]; + for (int i = 1, maxI = colors.length - 2; i < maxI; ) { + Color[] now1 = colors[i].clone(); + Color[] now2 = next; + for (int index = 0; index < now2.length; index++) { + now2[index] = COLOR_DIFF_ABS.function(next[index], back[index]); + } + back = now1; + next = colors[++i]; + } + }; + private boolean isGrayscale; /** @@ -376,6 +483,32 @@ public ColorMatrix agg(ColorMatrix value, ManyTrans manyTrans) { } } + /** + * 在矩阵内部发生计算,使得矩阵可以满足我们的需求与计算操作。 + *

+ * Computing occurs within the matrix, enabling the matrix to meet our needs and computational operations. + * + * @param action 计算实现逻辑,支持自定义也支持直接使用内置实现。 + *

+ * Computational implementation logic supports customization and direct use of built-in implementations. + * @param isCopy 计算操作是否要在拷贝之后的数组中执行,如果设置为 true 代表计算操作将发生于新数据中,不会影响原矩阵对象。 + *

+ * Whether the calculation operation should be performed in the copied array. If set to true, it means that the calculation operation will occur in the new data and will not affect the original matrix object. + * @return 计算之后的矩阵对象。 + *

+ * The calculated matrix object. + */ + public ColorMatrix calculate(Consumer action, boolean isCopy) { + if (isCopy) { + Color[][] colors = this.copyToNewArrays(); + action.accept(colors); + return ColorMatrix.parse(colors); + } else { + action.accept(this.toArrays()); + return this; + } + } + /** * 获取到矩阵中指定坐标点的数值 * diff --git a/src_code/src/main/java/zhao/algorithmMagic/operands/table/FDataFrame.java b/src_code/src/main/java/zhao/algorithmMagic/operands/table/FDataFrame.java index cbf958c..d057b75 100644 --- a/src_code/src/main/java/zhao/algorithmMagic/operands/table/FDataFrame.java +++ b/src_code/src/main/java/zhao/algorithmMagic/operands/table/FDataFrame.java @@ -197,6 +197,9 @@ public DataFrame desc() { */ @Override public DataFrame select(String... colNames) { + if (colNames.length == 1 && "*".equals(colNames[0])) { + return this; + } ArrayList arrayList = new ArrayList<>(list.size() + 4); int[] index = new int[colNames.length]; { diff --git a/src_code/src/main/java/zhao/algorithmMagic/operands/vector/SparkVector.scala b/src_code/src/main/java/zhao/algorithmMagic/operands/vector/SparkVector.scala index 35b2b00..416767a 100644 --- a/src_code/src/main/java/zhao/algorithmMagic/operands/vector/SparkVector.scala +++ b/src_code/src/main/java/zhao/algorithmMagic/operands/vector/SparkVector.scala @@ -107,21 +107,6 @@ final class SparkVector(sparkContext: SparkContext, vector: org.apache.spark.mll else throw new OperatorOperationException("'DoubleVector1 add DoubleVector2' 时,两个'DoubleVector'的向量所包含的数量不同,DoubleVector1=[" + numberOfDimensions1 + "],DoubleVector2=[" + numberOfDimensions2 + "]\n" + "When 'DoubleVector1 add DoubleVector2', the two vectors of 'DoubleVector' contain different quantities, DoubleVector1=[" + numberOfDimensions1 + "], DoubleVector2=[" + numberOfDimensions2 + "]") } - /** - * - * @return 将本对象中存储的向量序列数组拷贝到一个新数组并将新数组返回,这里返回的是一个新数组,支持修改等操作。 - * - * Copy the vector sequence array stored in this object to a new array and return the new array. Here, a new array is returned, which supports modification and other operations. - */ - override def copyToNewArray(): Array[Double] = vector.toArray - - /** - * @return 向量中包含的维度数量 - *

- * the number of dimensions contained in the vector - */ - override def getNumberOfDimensions: Int = size - /** * 在两个操作数之间做差的方法,具体用法请参阅API说明。 *

@@ -146,6 +131,21 @@ final class SparkVector(sparkContext: SparkContext, vector: org.apache.spark.mll else throw new OperatorOperationException("'DoubleVector1 diff DoubleVector2' 时,两个'DoubleVector'的向量所包含的数量不同,DoubleVector1=[" + numberOfDimensions1 + "],DoubleVector2=[" + numberOfDimensions2 + "]\n" + "When 'DoubleVector1 diff DoubleVector2', the two vectors of 'DoubleVector' contain different quantities, DoubleVector1=[" + numberOfDimensions1 + "], DoubleVector2=[" + numberOfDimensions2 + "]") } + /** + * + * @return 将本对象中存储的向量序列数组拷贝到一个新数组并将新数组返回,这里返回的是一个新数组,支持修改等操作。 + * + * Copy the vector sequence array stored in this object to a new array and return the new array. Here, a new array is returned, which supports modification and other operations. + */ + override def copyToNewArray(): Array[Double] = vector.toArray + + /** + * @return 向量中包含的维度数量 + *

+ * the number of dimensions contained in the vector + */ + override def getNumberOfDimensions: Int = size + /** * * @return 第三方向量中所维护的向量序列,通过此函数您可以直接获取到第三方库中的对象。 From cb38ca9ffe85e01ab533c6c4b4bcf10f6e7e643a Mon Sep 17 00:00:00 2001 From: LingYuZhao Date: Thu, 30 Mar 2023 21:48:35 +0800 Subject: [PATCH 04/17] update MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * 新增边框对象 --- src_code/Case.md | 80 ++++++++++ src_code/README-Chinese.md | 73 +++++++-- src_code/README.md | 78 +++++++--- .../main/java/zhao/algorithmMagic/MAIN1.java | 44 ++++-- .../operands/matrix/ColorMatrix.java | 38 ++++- .../operands/matrix/RectangleMatrix.java | 145 ++++++++++++++++++ .../operands/vector/SparkVector.scala | 30 ++-- 7 files changed, 422 insertions(+), 66 deletions(-) create mode 100644 src_code/src/main/java/zhao/algorithmMagic/operands/matrix/RectangleMatrix.java diff --git a/src_code/Case.md b/src_code/Case.md index 1046b7e..8f513b7 100644 --- a/src_code/Case.md +++ b/src_code/Case.md @@ -30,6 +30,86 @@ public class MAIN1 { } ``` +### 描绘出图像边缘 + +```java +package zhao.algorithmMagic; + +import zhao.algorithmMagic.operands.matrix.ColorMatrix; + +import java.awt.*; + +public class MAIN1 { + public static void main(String[] args) { + // 获取一张图像的像素矩阵 + ColorMatrix colors = ColorMatrix.parse("C:\\Users\\Liming\\Desktop\\fsdownload\\test1.bmp"); + // 将图像拷贝一份出来 + ColorMatrix parse1 = ColorMatrix.parse(colors.copyToNewArrays()); + // 将 parse1 进行二值化 // 请注意阈值 + parse1.globalBinary(ColorMatrix._G_, 60, 0, 0xffffff); + // 将 parse1 矩阵腐蚀,然后将腐蚀的结果获取到 + ColorMatrix parse2 = parse1.erode(2, 2, true, Color.BLACK); + // 将 parse1 矩阵中的白色作为腐蚀背景色(膨胀) + parse1.erode(2, 2, false, Color.WHITE); + // 查看临时结果 + parse1.show("image1"); + parse2.show("image2"); + // 将两个矩阵进行做差运算,并查看做差之后的图像 + ColorMatrix resImage1 = parse1.diff(parse2); + resImage1.show("轮廓图像"); + // 将轮廓绘制到原图像中 并查看绘制之后的图像 + ColorMatrix resImage2 = colors.agg(resImage1, ColorMatrix.COLOR_SUM_REGULATE); + // 查看结果数据 + resImage2.show("绘制之后的结果图像"); + } +} +``` + +### 图像矩阵轮廓检测 + +```java +package zhao.algorithmMagic; + +import zhao.algorithmMagic.operands.matrix.ColorMatrix; +import zhao.algorithmMagic.operands.matrix.RectangleMatrix; + +import java.awt.*; + +public class MAIN1 { + public static void main(String[] args) { + ColorMatrix resImage1, parse1; + { + // 获取一张图像的像素矩阵 + ColorMatrix colors = ColorMatrix.parse("C:\\Users\\Liming\\Desktop\\fsdownload\\test3.bmp"); + // 将图像拷贝一份出来 + parse1 = ColorMatrix.parse(colors.copyToNewArrays()); + // 将结果二值化 + parse1.globalBinary(ColorMatrix._G_, 150, 0xffffff, 0); + // 将 parse1 矩阵腐蚀,然后将腐蚀的结果获取到 + ColorMatrix parse2 = parse1.erode(3, 3, true, Color.BLACK); + // 将 parse1 矩阵中的白色作为腐蚀背景色(膨胀) + parse1.erode(2, 2, false, Color.WHITE); + // 查看临时结果 + parse1.show("image1"); + parse2.show("image2"); + // 将两个矩阵进行做差运算,并查看做差之后的图像 + resImage1 = parse1.diff(parse2); + resImage1.show("轮廓图像"); + } + // 开始提取白色轮廓线内的面积 + System.out.print("轮廓内面积 = "); + System.out.println(resImage1.contourArea(ColorMatrix.WHITE)); + // 将轮廓线转化成矩形轮廓 + ColorMatrix parse = RectangleMatrix.parse(resImage1, Color.GREEN); + parse.show("矩形轮廓"); + System.out.print(parse); + // 将矩形轮廓添加到原矩阵 这里的规则是,如果颜色数值为 0xff000000 就不合并 + ColorMatrix res = parse1.agg(parse, (inputType1, inputType2) -> inputType2.getRGB() == 0xff000000 ? inputType1 : inputType2); + res.show("结果"); + } +} +``` + ## 数据计算类案例 ## 机器学习类案例 diff --git a/src_code/README-Chinese.md b/src_code/README-Chinese.md index 71f0da7..447bd7d 100644 --- a/src_code/README-Chinese.md +++ b/src_code/README-Chinese.md @@ -120,23 +120,23 @@ package zhao.algorithmMagic; import zhao.algorithmMagic.operands.table.*; public class MAIN1 { - public static void main(String[] args) { - // 创建一个空的 DataFrame 对象 - FDataFrame dataFrame = FDataFrame.select( - FieldCell.parse("id", "name", "sex", "age"), 1 - ); - // 手动插入数据 - dataFrame.insert( - FinalSeries.parse("1", "zhao", "M", "19"), - FinalSeries.parse("2", "tang", "W", "18"), - FinalSeries.parse("3", "yang", "W", "20"), - FinalSeries.parse("4", "shen", "W", "19") - ); - // 打印出 select * 的数据 - System.out.println( - dataFrame.select("*") - ); - } + public static void main(String[] args) { + // 创建一个空的 DataFrame 对象 + FDataFrame dataFrame = FDataFrame.select( + FieldCell.parse("id", "name", "sex", "age"), 1 + ); + // 手动插入数据 + dataFrame.insert( + FinalSeries.parse("1", "zhao", "M", "19"), + FinalSeries.parse("2", "tang", "W", "18"), + FinalSeries.parse("3", "yang", "W", "20"), + FinalSeries.parse("4", "shen", "W", "19") + ); + // 打印出 select * 的数据 + System.out.println( + dataFrame.select("*") + ); + } } ``` @@ -169,4 +169,43 @@ public class MAIN1 { } ``` +* 支持图像轮廓的面积计算,在计算的时候以像素作为计算单位,轮廓颜色支持自定义。 + +```java +package zhao.algorithmMagic; + +import zhao.algorithmMagic.operands.matrix.ColorMatrix; + +import java.awt.*; + +public class MAIN1 { + public static void main(String[] args) { + ColorMatrix resImage1; + { + // 获取一张图像的像素矩阵 + ColorMatrix colors = ColorMatrix.parse("C:\\Users\\Liming\\Desktop\\fsdownload\\test1.bmp"); + // 将图像拷贝一份出来 + ColorMatrix parse1 = ColorMatrix.parse(colors.copyToNewArrays()); + // 将 parse1 进行二值化 // 请注意阈值 + parse1.globalBinary(ColorMatrix._G_, 60, 0, 0xffffff); + // 将 parse1 矩阵腐蚀,然后将腐蚀的结果获取到 + ColorMatrix parse2 = parse1.erode(2, 2, true, Color.BLACK); + // 将 parse1 矩阵中的白色作为腐蚀背景色(膨胀) + parse1.erode(2, 2, false, Color.WHITE); + // 查看临时结果 + parse1.show("image1"); + parse2.show("image2"); + // 将两个矩阵进行做差运算,并查看做差之后的图像 + resImage1 = parse1.diff(parse2); + resImage1.show("轮廓图像"); + } + // 查看结果数据 + resImage1.show("绘制之后的结果图像"); + // 开始提取白色轮廓线内的面积 + System.out.print("轮廓内面积 = "); + System.out.println(resImage1.contourArea(ColorMatrix.WHITE)); + } +} +``` + ### Version update date : xx xx-xx-xx \ No newline at end of file diff --git a/src_code/README.md b/src_code/README.md index f345bf3..3fe210e 100644 --- a/src_code/README.md +++ b/src_code/README.md @@ -125,27 +125,28 @@ package zhao.algorithmMagic; import zhao.algorithmMagic.operands.table.*; public class MAIN1 { - public static void main(String[] args) { - // 创建一个空的 DataFrame 对象 - FDataFrame dataFrame = FDataFrame.select( - FieldCell.parse("id", "name", "sex", "age"), 1 - ); - // 手动插入数据 - dataFrame.insert( - FinalSeries.parse("1", "zhao", "M", "19"), - FinalSeries.parse("2", "tang", "W", "18"), - FinalSeries.parse("3", "yang", "W", "20"), - FinalSeries.parse("4", "shen", "W", "19") - ); - // 打印出 select * 的数据 - System.out.println( - dataFrame.select("*") - ); - } + public static void main(String[] args) { + // 创建一个空的 DataFrame 对象 + FDataFrame dataFrame = FDataFrame.select( + FieldCell.parse("id", "name", "sex", "age"), 1 + ); + // 手动插入数据 + dataFrame.insert( + FinalSeries.parse("1", "zhao", "M", "19"), + FinalSeries.parse("2", "tang", "W", "18"), + FinalSeries.parse("3", "yang", "W", "20"), + FinalSeries.parse("4", "shen", "W", "19") + ); + // 打印出 select * 的数据 + System.out.println( + dataFrame.select("*") + ); + } } ``` -* In a color value quadrature calculation scheme, the processing logic is to take the current value of% 256 as the current color value if the value exceeds the range. +* In a color value quadrature calculation scheme, the processing logic is to take the current value of% 256 as the + current color value if the value exceeds the range. ```java package zhao.algorithmMagic; @@ -173,4 +174,45 @@ public class MAIN1 { } } ``` + +* Support area calculation of image contours, using pixels as the calculation unit during calculation, and support + customization of contour colors. + +```java +package zhao.algorithmMagic; + +import zhao.algorithmMagic.operands.matrix.ColorMatrix; + +import java.awt.*; + +public class MAIN1 { + public static void main(String[] args) { + ColorMatrix resImage1; + { + // 获取一张图像的像素矩阵 + ColorMatrix colors = ColorMatrix.parse("C:\\Users\\Liming\\Desktop\\fsdownload\\test1.bmp"); + // 将图像拷贝一份出来 + ColorMatrix parse1 = ColorMatrix.parse(colors.copyToNewArrays()); + // 将 parse1 进行二值化 // 请注意阈值 + parse1.globalBinary(ColorMatrix._G_, 60, 0, 0xffffff); + // 将 parse1 矩阵腐蚀,然后将腐蚀的结果获取到 + ColorMatrix parse2 = parse1.erode(2, 2, true, Color.BLACK); + // 将 parse1 矩阵中的白色作为腐蚀背景色(膨胀) + parse1.erode(2, 2, false, Color.WHITE); + // 查看临时结果 + parse1.show("image1"); + parse2.show("image2"); + // 将两个矩阵进行做差运算,并查看做差之后的图像 + resImage1 = parse1.diff(parse2); + resImage1.show("轮廓图像"); + } + // 查看结果数据 + resImage1.show("绘制之后的结果图像"); + // 开始提取白色轮廓线内的面积 + System.out.print("轮廓内面积 = "); + System.out.println(resImage1.contourArea(ColorMatrix.WHITE)); + } +} +``` + ### Version update date : xx xx-xx-xx \ No newline at end of file diff --git a/src_code/src/main/java/zhao/algorithmMagic/MAIN1.java b/src_code/src/main/java/zhao/algorithmMagic/MAIN1.java index b70f461..e2a7bd5 100644 --- a/src_code/src/main/java/zhao/algorithmMagic/MAIN1.java +++ b/src_code/src/main/java/zhao/algorithmMagic/MAIN1.java @@ -1,24 +1,40 @@ package zhao.algorithmMagic; import zhao.algorithmMagic.operands.matrix.ColorMatrix; +import zhao.algorithmMagic.operands.matrix.RectangleMatrix; import java.awt.*; public class MAIN1 { public static void main(String[] args) { - Color[] colors = {Color.CYAN, Color.GREEN, Color.PINK, Color.red}; - // 手动创建图像矩阵 - ColorMatrix parse = ColorMatrix.parse( - colors, colors, colors, colors - ); - // 查看图像矩阵 - System.out.println(parse); - // 将图像中的所有像素与自身矩阵进行规整乘法计算 - ColorMatrix agg1 = parse.agg(parse, ColorMatrix.COLOR_MULTIPLY_REGULATE); - // 将图像中的所有像素与自身矩阵进行取余乘法计算 - ColorMatrix agg2 = parse.agg(parse, ColorMatrix.COLOR_MULTIPLY_REMAINDER); - // 查看两个图像数据 - System.out.println(agg1); - System.out.println(agg2); + ColorMatrix resImage1, parse1; + { + // 获取一张图像的像素矩阵 + ColorMatrix colors = ColorMatrix.parse("C:\\Users\\Liming\\Desktop\\fsdownload\\test3.bmp"); + // 将图像拷贝一份出来 + parse1 = ColorMatrix.parse(colors.copyToNewArrays()); + // 将结果二值化 + parse1.globalBinary(ColorMatrix._G_, 150, 0xffffff, 0); + // 将 parse1 矩阵腐蚀,然后将腐蚀的结果获取到 + ColorMatrix parse2 = parse1.erode(3, 3, true, Color.BLACK); + // 将 parse1 矩阵中的白色作为腐蚀背景色(膨胀) + parse1.erode(2, 2, false, Color.WHITE); + // 查看临时结果 + parse1.show("image1"); + parse2.show("image2"); + // 将两个矩阵进行做差运算,并查看做差之后的图像 + resImage1 = parse1.diff(parse2); + resImage1.show("轮廓图像"); + } + // 开始提取白色轮廓线内的面积 + System.out.print("轮廓内面积 = "); + System.out.println(resImage1.contourArea(ColorMatrix.WHITE)); + // 将轮廓线转化成矩形轮廓 + ColorMatrix parse = RectangleMatrix.parse(resImage1, Color.GREEN); + parse.show("矩形轮廓"); + System.out.print(parse); + // 将矩形轮廓添加到原矩阵 这里的规则是,如果颜色数值为 0xff000000 就不合并 + ColorMatrix res = parse1.agg(parse, (inputType1, inputType2) -> inputType2.getRGB() == 0xff000000 ? inputType1 : inputType2); + res.show("结果"); } } \ No newline at end of file diff --git a/src_code/src/main/java/zhao/algorithmMagic/operands/matrix/ColorMatrix.java b/src_code/src/main/java/zhao/algorithmMagic/operands/matrix/ColorMatrix.java index 51c3840..8ef27f3 100644 --- a/src_code/src/main/java/zhao/algorithmMagic/operands/matrix/ColorMatrix.java +++ b/src_code/src/main/java/zhao/algorithmMagic/operands/matrix/ColorMatrix.java @@ -116,7 +116,7 @@ public class ColorMatrix extends Matrix * The processing logic in the color value quadrature calculation scheme is to take 0 or 255 if the value exceeds the range, and take the value itself if the value does not exceed the range, achieving an efficient calculation scheme for limiting color values. */ public final static ManyTrans COLOR_MULTIPLY_REGULATE = (inputType1, inputType2) -> new Color( @@ -127,7 +127,7 @@ public class ColorMatrix extends Matrix * The processing logic in the color value quadrature calculation scheme is to take 0 or 255 if the value exceeds the range, and take the value itself if the value does not exceed the range, achieving an efficient calculation scheme for limiting color values. */ public final static ManyTrans COLOR_MULTIPLY_REMAINDER = (inputType1, inputType2) -> { @@ -1323,6 +1323,40 @@ public double avg(byte colorMode) { return sum / (double) (this.getNumberOfDimensions()); } + /** + * 计算出轮廓内的面积大小,以像素为单位。 + *

+ * Calculate the size of the area within the contour, in pixels. + * + * @param contourColor 轮廓线的颜色对象,如果像素的颜色数值与此值一致,则认为其属于边框。 + *

+ * The color object of the outline. If the color value of a pixel matches this value, it is considered to belong to the border. + * @return 轮廓内的所有像素数量。 + *

+ * The number of all pixels within the contour. + */ + public int contourArea(Color contourColor) { + int rgb = contourColor.getRGB(); + int res = 0; + for (Color[] colors : this.toArrays()) { + boolean isOk = false; + for (Color color : colors) { + if (isOk) { + ++res; + if (color.getRGB() == rgb) { + isOk = false; + } + continue; + } + if (color.getRGB() == rgb) { + isOk = true; + ++res; + } + } + } + return res; + } + /** * 图像腐蚀函数,在该函数的处理之下,可以对图像某些不足够的颜色数值进行腐蚀操作, * diff --git a/src_code/src/main/java/zhao/algorithmMagic/operands/matrix/RectangleMatrix.java b/src_code/src/main/java/zhao/algorithmMagic/operands/matrix/RectangleMatrix.java new file mode 100644 index 0000000..d0e64e6 --- /dev/null +++ b/src_code/src/main/java/zhao/algorithmMagic/operands/matrix/RectangleMatrix.java @@ -0,0 +1,145 @@ +package zhao.algorithmMagic.operands.matrix; + +import java.awt.*; +import java.util.Arrays; + +/** + * 图像边缘矩阵对象,该对象专用于针对图像边缘对象进行一些特有的操作,在这里您可以直接执行其中的边缘计算函数,更好的在图像边缘计算中提供良好的效果。 + *

+ * Image edge matrix object, which is specially used to carry out some special operations on image edge objects. Here you can directly execute the edge computing function to better provide good results in image edge computing. + * + * @author 赵凌宇 + * 2023/3/30 16:53 + */ +public class RectangleMatrix extends ColorMatrix { + + private final int outlineWidthL; + private final int outlineHeightR; + private final int outlineWidthR; + private final int outlineHeightL; + + /** + * 构造一个指定行列数据的矩阵对象,其中的行指针最大值将会默认使用行数量。 + *

+ * Construct a matrix object with specified row and column data. The maximum value of row pointer will use the number of rows by default. + * + * @param rowCount 矩阵中的行数量 + *

+ * the number of rows in the matrix + * @param colCount 矩阵中的列数量 + *

+ * the number of cols in the matrix + * @param colors 该矩阵对象中的二维数组对象。 + * @param isGrayscale 如果设置为true 代表此图像是灰度图像 + * @param outlineWidthL 矩形的起始宽度索引 + * @param outlineWidthR 矩形的终止宽度索引 + * @param outlineHeightL 矩形的起始高度索引 + * @param outlineHeightR 矩形的终止高度索引 + */ + protected RectangleMatrix(int rowCount, int colCount, Color[][] colors, boolean isGrayscale, int outlineWidthL, int outlineHeightR, int outlineWidthR, int outlineHeightL) { + super(rowCount, colCount, colors, isGrayscale); + this.outlineWidthL = outlineWidthL; + this.outlineHeightR = outlineHeightR; + this.outlineWidthR = outlineWidthR; + this.outlineHeightL = outlineHeightL; + } + + /** + * 将一个图像矩阵中的轮廓对象获取到 + * + * @param colorMatrix 需要被提取的图像矩阵对象 + * @param rectColor 矩阵对象的轮廓颜色对象 + * @return 提取处理的图像轮廓对象 + */ + public static RectangleMatrix parse(ColorMatrix colorMatrix, Color rectColor) { + // 将轮廓的高度与宽度计算出来 + int outlineWidthL = colorMatrix.getColCount() - 1; + int outlineHeightL = colorMatrix.getRowCount() - 1; + int outlineHeightR = 0; + int outlineWidthR = 0; + // 获取到起始与终止索引 + boolean isH = false; + int rc = -1; + for (Color[] colors : colorMatrix.toArrays()) { + ++rc; + int cc = -1; + boolean isW = false; + boolean isOk = false; + for (Color color : colors) { + ++cc; + // 先判断宽 + if (color.getRGB() != 0xff000000) { + isOk = true; + if (isW) { + if (cc > outlineWidthR) outlineWidthR = cc; + } else if (cc < outlineWidthL) { + outlineWidthL = cc; + outlineWidthR = cc; + isW = true; + } + } + } + // 再判断高 + if (isOk) { + if (isH) { + if (rc > outlineHeightR) outlineHeightR = rc; + } else if (rc < outlineWidthL) { + outlineHeightL = rc; + outlineHeightR = rc; + isH = true; + } + } + } + // 按照宽高生成一个矩阵 + Color[][] colors = new Color[colorMatrix.getRowCount()][colorMatrix.getColCount()]; + for (Color[] color : colors) { + Arrays.fill(color, Color.BLACK); + } + Color[] row = new Color[colorMatrix.getColCount()]; + Arrays.fill(row, Color.BLACK); + for (int i = outlineWidthL; i <= outlineWidthR; i++) { + row[i] = rectColor; + } + colors[outlineHeightL] = row; + colors[outlineHeightR] = row; + for (int i = outlineHeightL, colorsLength = outlineHeightR; i <= colorsLength; i++) { + Color[] color = colors[i]; + color[outlineWidthL] = rectColor; + color[outlineWidthR] = rectColor; + } + return new RectangleMatrix( + colorMatrix.getRowCount(), + colorMatrix.getColCount(), + colors, + false, + outlineWidthL, outlineHeightL, + outlineWidthR, outlineHeightR + ); + } + + public int getOutlineWidthL() { + return outlineWidthL; + } + + public int getOutlineHeightR() { + return outlineHeightR; + } + + public int getOutlineWidthR() { + return outlineWidthR; + } + + public int getOutlineHeightL() { + return outlineHeightL; + } + + @Override + public String toString() { + return "RectangleMatrix{" + + "outlineWidthL=" + getOutlineWidthL() + + ", outlineWidthR=" + getOutlineWidthR() + + ", outlineHeightL=" + getOutlineHeightL() + + ", outlineHeightR=" + getOutlineHeightR() + + '}'; + } +} diff --git a/src_code/src/main/java/zhao/algorithmMagic/operands/vector/SparkVector.scala b/src_code/src/main/java/zhao/algorithmMagic/operands/vector/SparkVector.scala index 416767a..7f0bac4 100644 --- a/src_code/src/main/java/zhao/algorithmMagic/operands/vector/SparkVector.scala +++ b/src_code/src/main/java/zhao/algorithmMagic/operands/vector/SparkVector.scala @@ -107,6 +107,21 @@ final class SparkVector(sparkContext: SparkContext, vector: org.apache.spark.mll else throw new OperatorOperationException("'DoubleVector1 add DoubleVector2' 时,两个'DoubleVector'的向量所包含的数量不同,DoubleVector1=[" + numberOfDimensions1 + "],DoubleVector2=[" + numberOfDimensions2 + "]\n" + "When 'DoubleVector1 add DoubleVector2', the two vectors of 'DoubleVector' contain different quantities, DoubleVector1=[" + numberOfDimensions1 + "], DoubleVector2=[" + numberOfDimensions2 + "]") } + /** + * @return 向量中包含的维度数量 + *

+ * the number of dimensions contained in the vector + */ + override def getNumberOfDimensions: Int = size + + /** + * + * @return 将本对象中存储的向量序列数组拷贝到一个新数组并将新数组返回,这里返回的是一个新数组,支持修改等操作。 + * + * Copy the vector sequence array stored in this object to a new array and return the new array. Here, a new array is returned, which supports modification and other operations. + */ + override def copyToNewArray(): Array[Double] = vector.toArray + /** * 在两个操作数之间做差的方法,具体用法请参阅API说明。 *

@@ -131,21 +146,6 @@ final class SparkVector(sparkContext: SparkContext, vector: org.apache.spark.mll else throw new OperatorOperationException("'DoubleVector1 diff DoubleVector2' 时,两个'DoubleVector'的向量所包含的数量不同,DoubleVector1=[" + numberOfDimensions1 + "],DoubleVector2=[" + numberOfDimensions2 + "]\n" + "When 'DoubleVector1 diff DoubleVector2', the two vectors of 'DoubleVector' contain different quantities, DoubleVector1=[" + numberOfDimensions1 + "], DoubleVector2=[" + numberOfDimensions2 + "]") } - /** - * - * @return 将本对象中存储的向量序列数组拷贝到一个新数组并将新数组返回,这里返回的是一个新数组,支持修改等操作。 - * - * Copy the vector sequence array stored in this object to a new array and return the new array. Here, a new array is returned, which supports modification and other operations. - */ - override def copyToNewArray(): Array[Double] = vector.toArray - - /** - * @return 向量中包含的维度数量 - *

- * the number of dimensions contained in the vector - */ - override def getNumberOfDimensions: Int = size - /** * * @return 第三方向量中所维护的向量序列,通过此函数您可以直接获取到第三方库中的对象。 From 9746a0c853664b18704d14d740bb0a23e292f950 Mon Sep 17 00:00:00 2001 From: LingYuZhao Date: Sat, 1 Apr 2023 15:29:18 +0800 Subject: [PATCH 05/17] update --- src_code/Case.md | 39 +++++ src_code/README-Chinese.md | 142 +++++++++++++++++ src_code/README.md | 146 ++++++++++++++++++ src_code/pom.xml | 6 +- .../main/java/zhao/algorithmMagic/MAIN1.java | 50 +++--- .../distanceAlgorithm/CanberraDistance.java | 58 +++++++ .../distanceAlgorithm/ChebyshevDistance.java | 54 +++++++ .../distanceAlgorithm/CosineDistance.java | 42 +++++ .../distanceAlgorithm/DistanceAlgorithm.java | 63 ++++++++ .../distanceAlgorithm/EuclideanMetric.java | 54 +++++++ .../distanceAlgorithm/HausdorffDistance.java | 73 +++++++++ .../distanceAlgorithm/ManhattanDistance.java | 54 +++++++ .../distanceAlgorithm/MinkowskiDistance.java | 56 +++++++ .../StandardizedEuclideanDistance.java | 103 ++++++------ .../algorithmMagic/core/AlgorithmStar.java | 44 ++++++ .../operands/matrix/ColorMatrix.java | 117 +++++++++++++- .../operands/matrix/DoubleMatrix.java | 45 +++++- .../operands/matrix/IntegerMatrix.java | 38 ++++- .../operands/matrix/NumberMatrix.java | 13 ++ .../operands/matrix/RectangleMatrix.java | 13 +- .../operands/vector/SparkVector.scala | 30 ++-- 21 files changed, 1126 insertions(+), 114 deletions(-) diff --git a/src_code/Case.md b/src_code/Case.md index 8f513b7..a92aed4 100644 --- a/src_code/Case.md +++ b/src_code/Case.md @@ -110,6 +110,45 @@ public class MAIN1 { } ``` +### 图像模板匹配 + +```java +package zhao.algorithmMagic; + +import zhao.algorithmMagic.algorithm.distanceAlgorithm.ManhattanDistance; +import zhao.algorithmMagic.operands.coordinate.IntegerCoordinateTwo; +import zhao.algorithmMagic.operands.matrix.ColorMatrix; + +import java.awt.*; + +public class MAIN1 { + public static void main(String[] args) { + ColorMatrix colorMatrix1, colorMatrix2; + // 将图像与样本读取进来 + colorMatrix1 = ColorMatrix.parse("C:\\Users\\zhao\\Desktop\\fsdownload\\test3.bmp"); + colorMatrix2 = ColorMatrix.parse("C:\\Users\\zhao\\Desktop\\fsdownload\\test3YB.bmp"); + // 使用模板匹配 获取到 colorMat1 中 与 colorMat2 最相近的子矩阵 + IntegerCoordinateTwo topLeft = colorMatrix1.templateMatching( + // 相似度计算组件 + ManhattanDistance.getInstance("MAN"), + // 模板图像 + colorMatrix2, + // 需要被计算的颜色通道 + ColorMatrix._G_, + // 相似度越小 匹配度越大 + false + ); + // 由左上角坐标计算出右下坐标 + IntegerCoordinateTwo bottomRight = new IntegerCoordinateTwo( + topLeft.getX() + colorMatrix2.getColCount(), topLeft.getY() + colorMatrix2.getRowCount() + ); + // 将图像矩阵绘制到原矩阵中,并查看结果 + colorMatrix1.drawRectangle(topLeft, bottomRight, Color.MAGENTA); + colorMatrix1.show("res"); + } +} +``` + ## 数据计算类案例 ## 机器学习类案例 diff --git a/src_code/README-Chinese.md b/src_code/README-Chinese.md index 447bd7d..e53517b 100644 --- a/src_code/README-Chinese.md +++ b/src_code/README-Chinese.md @@ -208,4 +208,146 @@ public class MAIN1 { } ``` +* 支持轮廓外接矩形的操作,在该函数的帮助下,图像中的所有轮廓将被统一的圈中,成为一个大矩阵。 + +```java +package zhao.algorithmMagic; + +import zhao.algorithmMagic.operands.matrix.ColorMatrix; +import zhao.algorithmMagic.operands.matrix.RectangleMatrix; + +import java.awt.*; + +public class MAIN1 { + public static void main(String[] args) { + ColorMatrix parse1; + { + // 获取一张图像的像素矩阵 + ColorMatrix colors = ColorMatrix.parse("C:\\Users\\zhao\\Desktop\\fsdownload\\test3.bmp"); + // 将图像拷贝一份出来 + parse1 = ColorMatrix.parse(colors.copyToNewArrays()); + // 将结果二值化 + parse1.globalBinary(ColorMatrix._G_, 150, 0xffffff, 0); + } + // 将轮廓线转化成矩形轮廓 + ColorMatrix parse = RectangleMatrix.parse(parse1, Color.GREEN); + parse.show("二值化后的图像"); + System.out.print(parse); + // 将矩形轮廓添加到原矩阵 这里的规则是,如果颜色数值为 0xff000000 就不合并 + ColorMatrix res = parse1.agg( + // 需要被进行添加的矩阵对象 + parse, + // 添加的逻辑 这里使用的是将非黑色的颜色像素直接覆盖 因为除了黑色以外的像素都是轮廓所需 + (inputType1, inputType2) -> inputType2.getRGB() == 0xff000000 ? inputType1 : inputType2 + ); + res.show("结果"); + } +} +``` + +* 支持矩阵相似函数的计算,在这里您可以按照如下的示例进行矩形相似度的计算。 + +```java +package zhao.algorithmMagic; + +import zhao.algorithmMagic.algorithm.distanceAlgorithm.ManhattanDistance; +import zhao.algorithmMagic.algorithm.distanceAlgorithm.StandardizedEuclideanDistance; +import zhao.algorithmMagic.core.AlgorithmStar; +import zhao.algorithmMagic.operands.matrix.DoubleMatrix; +import zhao.algorithmMagic.operands.matrix.IntegerMatrix; + +public class MAIN1 { + public static void main(String[] args) { + // 获取到三个图像矩阵的颜色RGB数值 + IntegerMatrix parse1, parse2, parse3; + { + parse1 = IntegerMatrix.parse("C:\\Users\\zhao\\Desktop\\fsdownload\\test3.bmp"); + parse2 = IntegerMatrix.parse("C:\\Users\\zhao\\Desktop\\fsdownload\\test32.bmp"); + parse3 = IntegerMatrix.parse("C:\\Users\\zhao\\Desktop\\fsdownload\\test33.bmp"); + } + // 计算出三个图像的相似度系数 + AlgorithmStar algorithmStar = AlgorithmStar.getInstance(); + System.out.println(algorithmStar.getTrueDistance(ManhattanDistance.getInstance("MAN"), parse1, parse2)); + System.out.println(algorithmStar.getTrueDistance(ManhattanDistance.getInstance("MAN"), parse1, parse3)); + + // 将三个图像矩阵的所有数值转换成为double类型,获取到Double矩阵 + DoubleMatrix parse11, parse22, parse33; + { + parse11 = DoubleMatrix.parse(parse1); + parse22 = DoubleMatrix.parse(parse2); + parse33 = DoubleMatrix.parse(parse3); + } + // 计算出三个图像的相似度系数 + System.out.println(algorithmStar.getTrueDistance(ManhattanDistance.getInstance("MAN"), parse11, parse22)); + System.out.println(algorithmStar.getTrueDistance(ManhattanDistance.getInstance("MAN"), parse11, parse33)); + + // TODO 值得注意的是 标准化欧几里得暂不支持这类操作的计算 这一块会报错 + double se = algorithmStar.getTrueDistance(StandardizedEuclideanDistance.getInstance2("SE"), parse11, parse22); + System.out.println(se); + } +} +``` + +* 支持图像矩阵中的规则图形(矩形)的绘制,绘制过程将不会产生冗余的计算计算数据。 + +```java +package zhao.algorithmMagic; + +import zhao.algorithmMagic.operands.coordinate.IntegerCoordinateTwo; +import zhao.algorithmMagic.operands.matrix.ColorMatrix; + +import java.awt.*; + +public class MAIN1 { + public static void main(String[] args) { + ColorMatrix colorMatrix1; + // 将图像与样本读取进来 + colorMatrix1 = ColorMatrix.parse("C:\\Users\\zhao\\Desktop\\fsdownload\\test3.bmp"); + // 将图像矩阵绘制到原矩阵中,并查看结果 + colorMatrix1.drawRectangle( + // 矩形的左上角坐标 + new IntegerCoordinateTwo(40, 30), + // 矩形的右下角坐标 + new IntegerCoordinateTwo(140, 130), + // 矩形边框的颜色对象 + Color.MAGENTA + ); + colorMatrix1.show("res"); + } +} +``` + +* 支持图像矩阵的模板匹配操作,其中卷积核大小为模板大小,当模板与图像矩形的宽度一致的时候会使用优化算法。 + +```java +package zhao.algorithmMagic; + +import zhao.algorithmMagic.algorithm.distanceAlgorithm.ManhattanDistance; +import zhao.algorithmMagic.operands.coordinate.IntegerCoordinateTwo; +import zhao.algorithmMagic.operands.matrix.ColorMatrix; + +import java.awt.*; + +public class MAIN1 { + public static void main(String[] args) { + ColorMatrix colorMatrix1, colorMatrix2; + // 将图像与样本读取进来 + colorMatrix1 = ColorMatrix.parse("C:\\Users\\zhao\\Desktop\\fsdownload\\test3.bmp"); + colorMatrix2 = ColorMatrix.parse("C:\\Users\\zhao\\Desktop\\fsdownload\\test3YB.bmp"); + // 使用模板匹配 获取到 colorMat1 中 与 colorMat2 最相近的子矩阵 + IntegerCoordinateTwo topLeft = colorMatrix1.templateMatching( + // 相似度计算组件 + ManhattanDistance.getInstance("MAN"), + // 模板图像 + colorMatrix2, + // 需要被计算的颜色通道 + ColorMatrix._G_, + // 相似度越小 匹配度越大 + false + ); + System.out.println(topLeft); + } +} +``` + ### Version update date : xx xx-xx-xx \ No newline at end of file diff --git a/src_code/README.md b/src_code/README.md index 3fe210e..10f727a 100644 --- a/src_code/README.md +++ b/src_code/README.md @@ -215,4 +215,150 @@ public class MAIN1 { } ``` +* Supports the operation of enclosing rectangles with contours. With the help of this function, all contours in the + image will be unified into a circle, forming a large matrix. + +```java +package zhao.algorithmMagic; + +import zhao.algorithmMagic.operands.matrix.ColorMatrix; +import zhao.algorithmMagic.operands.matrix.RectangleMatrix; + +import java.awt.*; + +public class MAIN1 { + public static void main(String[] args) { + ColorMatrix parse1; + { + // 获取一张图像的像素矩阵 + ColorMatrix colors = ColorMatrix.parse("C:\\Users\\zhao\\Desktop\\fsdownload\\test3.bmp"); + // 将图像拷贝一份出来 + parse1 = ColorMatrix.parse(colors.copyToNewArrays()); + // 将结果二值化 + parse1.globalBinary(ColorMatrix._G_, 150, 0xffffff, 0); + } + // 将轮廓线转化成矩形轮廓 + ColorMatrix parse = RectangleMatrix.parse(parse1, Color.GREEN); + parse.show("二值化后的图像"); + System.out.print(parse); + // 将矩形轮廓添加到原矩阵 这里的规则是,如果颜色数值为 0xff000000 就不合并 + ColorMatrix res = parse1.agg( + // 需要被进行添加的矩阵对象 + parse, + // 添加的逻辑 这里使用的是将非黑色的颜色像素直接覆盖 因为除了黑色以外的像素都是轮廓所需 + (inputType1, inputType2) -> inputType2.getRGB() == 0xff000000 ? inputType1 : inputType2 + ); + res.show("结果"); + } +} +``` + +* Support the calculation of matrix similarity functions. Here, you can calculate the rectangular similarity using the + following example. + +```java +package zhao.algorithmMagic; + +import zhao.algorithmMagic.algorithm.distanceAlgorithm.ManhattanDistance; +import zhao.algorithmMagic.algorithm.distanceAlgorithm.StandardizedEuclideanDistance; +import zhao.algorithmMagic.core.AlgorithmStar; +import zhao.algorithmMagic.operands.matrix.DoubleMatrix; +import zhao.algorithmMagic.operands.matrix.IntegerMatrix; + +public class MAIN1 { + public static void main(String[] args) { + // 获取到三个图像矩阵的颜色RGB数值 + IntegerMatrix parse1, parse2, parse3; + { + parse1 = IntegerMatrix.parse("C:\\Users\\zhao\\Desktop\\fsdownload\\test3.bmp"); + parse2 = IntegerMatrix.parse("C:\\Users\\zhao\\Desktop\\fsdownload\\test32.bmp"); + parse3 = IntegerMatrix.parse("C:\\Users\\zhao\\Desktop\\fsdownload\\test33.bmp"); + } + // 计算出三个图像的相似度系数 + AlgorithmStar algorithmStar = AlgorithmStar.getInstance(); + System.out.println(algorithmStar.getTrueDistance(ManhattanDistance.getInstance("MAN"), parse1, parse2)); + System.out.println(algorithmStar.getTrueDistance(ManhattanDistance.getInstance("MAN"), parse1, parse3)); + + // 将三个图像矩阵的所有数值转换成为double类型,获取到Double矩阵 + DoubleMatrix parse11, parse22, parse33; + { + parse11 = DoubleMatrix.parse(parse1); + parse22 = DoubleMatrix.parse(parse2); + parse33 = DoubleMatrix.parse(parse3); + } + // 计算出三个图像的相似度系数 + System.out.println(algorithmStar.getTrueDistance(ManhattanDistance.getInstance("MAN"), parse11, parse22)); + System.out.println(algorithmStar.getTrueDistance(ManhattanDistance.getInstance("MAN"), parse11, parse33)); + + // TODO 值得注意的是 标准化欧几里得暂不支持这类操作的计算 这一块会报错 + double se = algorithmStar.getTrueDistance(StandardizedEuclideanDistance.getInstance2("SE"), parse11, parse22); + System.out.println(se); + } +} +``` + +* Support the rendering of regular graphics (rectangles) in the image matrix, and the rendering process will not + generate redundant computational data. + +```java +package zhao.algorithmMagic; + +import zhao.algorithmMagic.operands.coordinate.IntegerCoordinateTwo; +import zhao.algorithmMagic.operands.matrix.ColorMatrix; + +import java.awt.*; + +public class MAIN1 { + public static void main(String[] args) { + ColorMatrix colorMatrix1; + // 将图像与样本读取进来 + colorMatrix1 = ColorMatrix.parse("C:\\Users\\zhao\\Desktop\\fsdownload\\test3.bmp"); + // 将图像矩阵绘制到原矩阵中,并查看结果 + colorMatrix1.drawRectangle( + // 矩形的左上角坐标 + new IntegerCoordinateTwo(40, 30), + // 矩形的右下角坐标 + new IntegerCoordinateTwo(140, 130), + // 矩形边框的颜色对象 + Color.MAGENTA + ); + colorMatrix1.show("res"); + } +} +``` + +* Support template matching operations for image matrices, where the convolution core size is the template size. When + the width of the template and the image rectangle coincide, an optimization algorithm is used. + +```java +package zhao.algorithmMagic; + +import zhao.algorithmMagic.algorithm.distanceAlgorithm.ManhattanDistance; +import zhao.algorithmMagic.operands.coordinate.IntegerCoordinateTwo; +import zhao.algorithmMagic.operands.matrix.ColorMatrix; + +import java.awt.*; + +public class MAIN1 { + public static void main(String[] args) { + ColorMatrix colorMatrix1, colorMatrix2; + // 将图像与样本读取进来 + colorMatrix1 = ColorMatrix.parse("C:\\Users\\zhao\\Desktop\\fsdownload\\test3.bmp"); + colorMatrix2 = ColorMatrix.parse("C:\\Users\\zhao\\Desktop\\fsdownload\\test3YB.bmp"); + // 使用模板匹配 获取到 colorMat1 中 与 colorMat2 最相近的子矩阵 + IntegerCoordinateTwo topLeft = colorMatrix1.templateMatching( + // 相似度计算组件 + ManhattanDistance.getInstance("MAN"), + // 模板图像 + colorMatrix2, + // 需要被计算的颜色通道 + ColorMatrix._G_, + // 相似度越小 匹配度越大 + false + ); + System.out.println(topLeft); + } +} +``` + ### Version update date : xx xx-xx-xx \ No newline at end of file diff --git a/src_code/pom.xml b/src_code/pom.xml index 6e2f85b..b44116e 100644 --- a/src_code/pom.xml +++ b/src_code/pom.xml @@ -38,7 +38,7 @@ org.apache.logging.log4j log4j-slf4j-impl 2.20.0 - + @@ -46,14 +46,14 @@ org.apache.logging.log4j log4j-api 2.20.0 - + org.apache.logging.log4j log4j-core 2.20.0 - + diff --git a/src_code/src/main/java/zhao/algorithmMagic/MAIN1.java b/src_code/src/main/java/zhao/algorithmMagic/MAIN1.java index e2a7bd5..25f4b3f 100644 --- a/src_code/src/main/java/zhao/algorithmMagic/MAIN1.java +++ b/src_code/src/main/java/zhao/algorithmMagic/MAIN1.java @@ -1,40 +1,26 @@ package zhao.algorithmMagic; +import zhao.algorithmMagic.algorithm.distanceAlgorithm.ManhattanDistance; +import zhao.algorithmMagic.operands.coordinate.IntegerCoordinateTwo; import zhao.algorithmMagic.operands.matrix.ColorMatrix; -import zhao.algorithmMagic.operands.matrix.RectangleMatrix; - -import java.awt.*; public class MAIN1 { public static void main(String[] args) { - ColorMatrix resImage1, parse1; - { - // 获取一张图像的像素矩阵 - ColorMatrix colors = ColorMatrix.parse("C:\\Users\\Liming\\Desktop\\fsdownload\\test3.bmp"); - // 将图像拷贝一份出来 - parse1 = ColorMatrix.parse(colors.copyToNewArrays()); - // 将结果二值化 - parse1.globalBinary(ColorMatrix._G_, 150, 0xffffff, 0); - // 将 parse1 矩阵腐蚀,然后将腐蚀的结果获取到 - ColorMatrix parse2 = parse1.erode(3, 3, true, Color.BLACK); - // 将 parse1 矩阵中的白色作为腐蚀背景色(膨胀) - parse1.erode(2, 2, false, Color.WHITE); - // 查看临时结果 - parse1.show("image1"); - parse2.show("image2"); - // 将两个矩阵进行做差运算,并查看做差之后的图像 - resImage1 = parse1.diff(parse2); - resImage1.show("轮廓图像"); - } - // 开始提取白色轮廓线内的面积 - System.out.print("轮廓内面积 = "); - System.out.println(resImage1.contourArea(ColorMatrix.WHITE)); - // 将轮廓线转化成矩形轮廓 - ColorMatrix parse = RectangleMatrix.parse(resImage1, Color.GREEN); - parse.show("矩形轮廓"); - System.out.print(parse); - // 将矩形轮廓添加到原矩阵 这里的规则是,如果颜色数值为 0xff000000 就不合并 - ColorMatrix res = parse1.agg(parse, (inputType1, inputType2) -> inputType2.getRGB() == 0xff000000 ? inputType1 : inputType2); - res.show("结果"); + ColorMatrix colorMatrix1, colorMatrix2; + // 将图像与样本读取进来 + colorMatrix1 = ColorMatrix.parse("C:\\Users\\zhao\\Desktop\\fsdownload\\test3.bmp"); + colorMatrix2 = ColorMatrix.parse("C:\\Users\\zhao\\Desktop\\fsdownload\\test3YB.bmp"); + // 使用模板匹配 获取到 colorMat1 中 与 colorMat2 最相近的子矩阵 + IntegerCoordinateTwo topLeft = colorMatrix1.templateMatching( + // 相似度计算组件 + ManhattanDistance.getInstance("MAN"), + // 模板图像 + colorMatrix2, + // 需要被计算的颜色通道 + ColorMatrix._G_, + // 相似度越小 匹配度越大 + false + ); + System.out.println(topLeft); } } \ No newline at end of file diff --git a/src_code/src/main/java/zhao/algorithmMagic/algorithm/distanceAlgorithm/CanberraDistance.java b/src_code/src/main/java/zhao/algorithmMagic/algorithm/distanceAlgorithm/CanberraDistance.java index 9514fda..1fd94a3 100644 --- a/src_code/src/main/java/zhao/algorithmMagic/algorithm/distanceAlgorithm/CanberraDistance.java +++ b/src_code/src/main/java/zhao/algorithmMagic/algorithm/distanceAlgorithm/CanberraDistance.java @@ -7,6 +7,8 @@ import zhao.algorithmMagic.exception.OperatorOperationException; import zhao.algorithmMagic.exception.TargetNotRealizedException; import zhao.algorithmMagic.operands.coordinate.*; +import zhao.algorithmMagic.operands.matrix.DoubleMatrix; +import zhao.algorithmMagic.operands.matrix.IntegerMatrix; import zhao.algorithmMagic.operands.route.DoubleConsanguinityRoute; import zhao.algorithmMagic.operands.route.DoubleConsanguinityRoute2D; import zhao.algorithmMagic.operands.route.IntegerConsanguinityRoute; @@ -14,6 +16,8 @@ import zhao.algorithmMagic.utils.ASClass; import zhao.algorithmMagic.utils.ASMath; +import java.util.Iterator; + /** * Java类于 2022/10/14 12:47:02 创建 *

@@ -298,4 +302,58 @@ public double getTrueDistance(IntegerConsanguinityRoute2D integerConsanguinityRo IntegerCoordinateTwo endPointCoordinate = integerConsanguinityRoute2D.getEndPointCoordinate(); return getTrueDistance(startingCoordinate.toArray(), endPointCoordinate.toArray()); } + + /** + * 计算两个矩阵对象之间的距离度量函数,通过该函数可以实现两个矩阵对象度量系数的计算。 + *

+ * Calculates the distance metric function between two matrix objects, through which the metric coefficients of two matrix objects can be calculated. + * + * @param integerMatrix1 需要被进行计算的矩阵对象。 + *

+ * The matrix object that needs to be calculated. + * @param matrix2 需要被进行计算的矩阵对象。 + *

+ * The matrix object that needs to be calculated. + * @return 计算出来的度量结果系数。 + *

+ * The calculated measurement result coefficient. + */ + @Override + public double getTrueDistance(IntegerMatrix integerMatrix1, IntegerMatrix matrix2) { + DistanceAlgorithm.checkMat(integerMatrix1, matrix2); + Iterator iterator1 = integerMatrix1.iterator(); + Iterator iterator2 = matrix2.iterator(); + double res = 0; + while (iterator1.hasNext() && iterator2.hasNext()) { + res += getTrueDistance(iterator1.next(), iterator2.next()); + } + return res; + } + + /** + * 计算两个矩阵对象之间的距离度量函数,通过该函数可以实现两个矩阵对象度量系数的计算。 + *

+ * Calculates the distance metric function between two matrix objects, through which the metric coefficients of two matrix objects can be calculated. + * + * @param matrix1 需要被进行计算的矩阵对象。 + *

+ * The matrix object that needs to be calculated. + * @param matrix2 需要被进行计算的矩阵对象。 + *

+ * The matrix object that needs to be calculated. + * @return 计算出来的度量结果系数。 + *

+ * The calculated measurement result coefficient. + */ + @Override + public double getTrueDistance(DoubleMatrix matrix1, DoubleMatrix matrix2) { + DistanceAlgorithm.checkMat(matrix1, matrix2); + Iterator iterator1 = matrix1.iterator(); + Iterator iterator2 = matrix2.iterator(); + double res = 0; + while (iterator1.hasNext() && iterator2.hasNext()) { + res += getTrueDistance(iterator1.next(), iterator2.next()); + } + return res; + } } diff --git a/src_code/src/main/java/zhao/algorithmMagic/algorithm/distanceAlgorithm/ChebyshevDistance.java b/src_code/src/main/java/zhao/algorithmMagic/algorithm/distanceAlgorithm/ChebyshevDistance.java index 7b1433f..08f551d 100644 --- a/src_code/src/main/java/zhao/algorithmMagic/algorithm/distanceAlgorithm/ChebyshevDistance.java +++ b/src_code/src/main/java/zhao/algorithmMagic/algorithm/distanceAlgorithm/ChebyshevDistance.java @@ -9,6 +9,8 @@ import zhao.algorithmMagic.operands.coordinate.Coordinate; import zhao.algorithmMagic.operands.coordinate.FloatingPointCoordinates; import zhao.algorithmMagic.operands.coordinate.IntegerCoordinates; +import zhao.algorithmMagic.operands.matrix.DoubleMatrix; +import zhao.algorithmMagic.operands.matrix.IntegerMatrix; import zhao.algorithmMagic.operands.route.DoubleConsanguinityRoute; import zhao.algorithmMagic.operands.route.DoubleConsanguinityRoute2D; import zhao.algorithmMagic.operands.route.IntegerConsanguinityRoute; @@ -322,4 +324,56 @@ public double getTrueDistance(IntegerConsanguinityRoute integerConsanguinityRout public double getTrueDistance(IntegerConsanguinityRoute2D integerConsanguinityRoute2D) { return getTrueDistance(integerConsanguinityRoute2D.getStartingCoordinate().toArray(), integerConsanguinityRoute2D.getEndPointCoordinate().toArray()); } + + /** + * 计算两个矩阵对象之间的距离度量函数,通过该函数可以实现两个矩阵对象度量系数的计算。 + *

+ * Calculates the distance metric function between two matrix objects, through which the metric coefficients of two matrix objects can be calculated. + * + * @param integerMatrix1 需要被进行计算的矩阵对象。 + *

+ * The matrix object that needs to be calculated. + * @param matrix2 需要被进行计算的矩阵对象。 + *

+ * The matrix object that needs to be calculated. + * @return 计算出来的度量结果系数。 + *

+ * The calculated measurement result coefficient. + */ + @Override + public double getTrueDistance(IntegerMatrix integerMatrix1, IntegerMatrix matrix2) { + int res = 0; + for (int[] ints : integerMatrix1.diff(matrix2)) { + for (int anInt : ints) { + if (anInt > res) res = anInt; + } + } + return res; + } + + /** + * 计算两个矩阵对象之间的距离度量函数,通过该函数可以实现两个矩阵对象度量系数的计算。 + *

+ * Calculates the distance metric function between two matrix objects, through which the metric coefficients of two matrix objects can be calculated. + * + * @param matrix1 需要被进行计算的矩阵对象。 + *

+ * The matrix object that needs to be calculated. + * @param matrix2 需要被进行计算的矩阵对象。 + *

+ * The matrix object that needs to be calculated. + * @return 计算出来的度量结果系数。 + *

+ * The calculated measurement result coefficient. + */ + @Override + public double getTrueDistance(DoubleMatrix matrix1, DoubleMatrix matrix2) { + double res = 0; + for (double[] ints : matrix1.diff(matrix2)) { + for (double anInt : ints) { + if (anInt > res) res = anInt; + } + } + return res; + } } diff --git a/src_code/src/main/java/zhao/algorithmMagic/algorithm/distanceAlgorithm/CosineDistance.java b/src_code/src/main/java/zhao/algorithmMagic/algorithm/distanceAlgorithm/CosineDistance.java index 563ebd4..a9243bf 100644 --- a/src_code/src/main/java/zhao/algorithmMagic/algorithm/distanceAlgorithm/CosineDistance.java +++ b/src_code/src/main/java/zhao/algorithmMagic/algorithm/distanceAlgorithm/CosineDistance.java @@ -5,6 +5,8 @@ import zhao.algorithmMagic.algorithm.OperationAlgorithm; import zhao.algorithmMagic.algorithm.OperationAlgorithmManager; import zhao.algorithmMagic.exception.TargetNotRealizedException; +import zhao.algorithmMagic.operands.matrix.DoubleMatrix; +import zhao.algorithmMagic.operands.matrix.IntegerMatrix; import zhao.algorithmMagic.operands.route.DoubleConsanguinityRoute; import zhao.algorithmMagic.operands.route.DoubleConsanguinityRoute2D; import zhao.algorithmMagic.operands.route.IntegerConsanguinityRoute; @@ -273,4 +275,44 @@ public double getTrueDistance(IntegerConsanguinityRoute integerConsanguinityRout public double getTrueDistance(IntegerConsanguinityRoute2D integerConsanguinityRoute2D) { return getTrueDistance(integerConsanguinityRoute2D.getStartingCoordinate().toArray(), integerConsanguinityRoute2D.getEndPointCoordinate().toArray()); } + + /** + * 计算两个矩阵对象之间的距离度量函数,通过该函数可以实现两个矩阵对象度量系数的计算。 + *

+ * Calculates the distance metric function between two matrix objects, through which the metric coefficients of two matrix objects can be calculated. + * + * @param integerMatrix1 需要被进行计算的矩阵对象。 + *

+ * The matrix object that needs to be calculated. + * @param matrix2 需要被进行计算的矩阵对象。 + *

+ * The matrix object that needs to be calculated. + * @return 计算出来的度量结果系数。 + *

+ * The calculated measurement result coefficient. + */ + @Override + public double getTrueDistance(IntegerMatrix integerMatrix1, IntegerMatrix matrix2) { + return integerMatrix1.innerProduct(matrix2) / (double) (integerMatrix1.moduleLength() * matrix2.moduleLength()); + } + + /** + * 计算两个矩阵对象之间的距离度量函数,通过该函数可以实现两个矩阵对象度量系数的计算。 + *

+ * Calculates the distance metric function between two matrix objects, through which the metric coefficients of two matrix objects can be calculated. + * + * @param matrix1 需要被进行计算的矩阵对象。 + *

+ * The matrix object that needs to be calculated. + * @param matrix2 需要被进行计算的矩阵对象。 + *

+ * The matrix object that needs to be calculated. + * @return 计算出来的度量结果系数。 + *

+ * The calculated measurement result coefficient. + */ + @Override + public double getTrueDistance(DoubleMatrix matrix1, DoubleMatrix matrix2) { + return matrix1.innerProduct(matrix2) / (matrix1.moduleLength() * matrix2.moduleLength()); + } } diff --git a/src_code/src/main/java/zhao/algorithmMagic/algorithm/distanceAlgorithm/DistanceAlgorithm.java b/src_code/src/main/java/zhao/algorithmMagic/algorithm/distanceAlgorithm/DistanceAlgorithm.java index cd2b46a..e9ed0fe 100644 --- a/src_code/src/main/java/zhao/algorithmMagic/algorithm/distanceAlgorithm/DistanceAlgorithm.java +++ b/src_code/src/main/java/zhao/algorithmMagic/algorithm/distanceAlgorithm/DistanceAlgorithm.java @@ -1,6 +1,10 @@ package zhao.algorithmMagic.algorithm.distanceAlgorithm; import zhao.algorithmMagic.algorithm.OperationAlgorithm; +import zhao.algorithmMagic.exception.OperatorOperationException; +import zhao.algorithmMagic.operands.matrix.DoubleMatrix; +import zhao.algorithmMagic.operands.matrix.IntegerMatrix; +import zhao.algorithmMagic.operands.matrix.Matrix; import zhao.algorithmMagic.operands.route.DoubleConsanguinityRoute; import zhao.algorithmMagic.operands.route.DoubleConsanguinityRoute2D; import zhao.algorithmMagic.operands.route.IntegerConsanguinityRoute; @@ -13,6 +17,31 @@ */ public interface DistanceAlgorithm extends OperationAlgorithm { + /** + * 在度量计算算法组件中,能够支持矩阵对象的运算,但是在运算之前需要先对矩阵中的数据进行一个检查,确保其不会发生错误。 + *

+ * In the metric calculation algorithm component, operations on matrix objects can be supported, but before operations, it is necessary to check the data in the matrix to ensure that no errors occur. + * + * @param Matrix1 需要被计算的矩阵对象,待检查的矩阵对象。 + *

+ * The matrix object to be calculated and the matrix object to be checked. + * @param Matrix2 需要被计算的矩阵对象,待检查的矩阵对象。 + *

+ * The matrix object to be calculated and the matrix object to be checked. + */ + static void checkMat(Matrix Matrix1, Matrix Matrix2) { + check(Matrix1.getRowCount(), Matrix1.getColCount(), Matrix2.getRowCount(), Matrix2.getColCount()); + } + + static void check(int rowCount, int colCount, int rowCount3, int colCount3) { + if (rowCount != rowCount3) + throw new OperatorOperationException("矩阵对象中的行数量不一致,不可计算\nThe number of rows in the matrix object is inconsistent and cannot be calculated.ERROR => row1 = " + + rowCount + "\trow2 = " + rowCount3); + if (colCount != colCount3) + throw new OperatorOperationException("矩阵对象中的列数量不一致,不可计算\nThe number of cols in the matrix object is inconsistent and cannot be calculated.ERROR => col1 = " + + colCount + "\tcol2 = " + colCount3); + } + /** * 计算一个路线的起始点与终止点的真实距离。具体的距离实现,需要您查阅算法实现的文档。 *

@@ -82,4 +111,38 @@ public interface DistanceAlgorithm extends OperationAlgorithm { * @return ... */ double getTrueDistance(IntegerConsanguinityRoute2D integerConsanguinityRoute2D); + + /** + * 计算两个矩阵对象之间的距离度量函数,通过该函数可以实现两个矩阵对象度量系数的计算。 + *

+ * Calculates the distance metric function between two matrix objects, through which the metric coefficients of two matrix objects can be calculated. + * + * @param matrix1 需要被进行计算的矩阵对象。 + *

+ * The matrix object that needs to be calculated. + * @param matrix2 需要被进行计算的矩阵对象。 + *

+ * The matrix object that needs to be calculated. + * @return 计算出来的度量结果系数。 + *

+ * The calculated measurement result coefficient. + */ + double getTrueDistance(IntegerMatrix matrix1, IntegerMatrix matrix2); + + /** + * 计算两个矩阵对象之间的距离度量函数,通过该函数可以实现两个矩阵对象度量系数的计算。 + *

+ * Calculates the distance metric function between two matrix objects, through which the metric coefficients of two matrix objects can be calculated. + * + * @param matrix1 需要被进行计算的矩阵对象。 + *

+ * The matrix object that needs to be calculated. + * @param matrix2 需要被进行计算的矩阵对象。 + *

+ * The matrix object that needs to be calculated. + * @return 计算出来的度量结果系数。 + *

+ * The calculated measurement result coefficient. + */ + double getTrueDistance(DoubleMatrix matrix1, DoubleMatrix matrix2); } diff --git a/src_code/src/main/java/zhao/algorithmMagic/algorithm/distanceAlgorithm/EuclideanMetric.java b/src_code/src/main/java/zhao/algorithmMagic/algorithm/distanceAlgorithm/EuclideanMetric.java index 94f5388..c7bbe1a 100644 --- a/src_code/src/main/java/zhao/algorithmMagic/algorithm/distanceAlgorithm/EuclideanMetric.java +++ b/src_code/src/main/java/zhao/algorithmMagic/algorithm/distanceAlgorithm/EuclideanMetric.java @@ -9,6 +9,8 @@ import zhao.algorithmMagic.operands.coordinate.Coordinate; import zhao.algorithmMagic.operands.coordinate.FloatingPointCoordinates; import zhao.algorithmMagic.operands.coordinate.IntegerCoordinates; +import zhao.algorithmMagic.operands.matrix.DoubleMatrix; +import zhao.algorithmMagic.operands.matrix.IntegerMatrix; import zhao.algorithmMagic.operands.route.DoubleConsanguinityRoute; import zhao.algorithmMagic.operands.route.DoubleConsanguinityRoute2D; import zhao.algorithmMagic.operands.route.IntegerConsanguinityRoute; @@ -332,6 +334,58 @@ public double getTrueDistance(IntegerConsanguinityRoute2D integerConsanguinityRo return getTrueDistance(integerConsanguinityRoute2D.getStartingCoordinate().toArray(), integerConsanguinityRoute2D.getEndPointCoordinate().toArray()); } + /** + * 计算两个矩阵对象之间的距离度量函数,通过该函数可以实现两个矩阵对象度量系数的计算。 + *

+ * Calculates the distance metric function between two matrix objects, through which the metric coefficients of two matrix objects can be calculated. + * + * @param integerMatrix1 需要被进行计算的矩阵对象。 + *

+ * The matrix object that needs to be calculated. + * @param matrix2 需要被进行计算的矩阵对象。 + *

+ * The matrix object that needs to be calculated. + * @return 计算出来的度量结果系数。 + *

+ * The calculated measurement result coefficient. + */ + @Override + public double getTrueDistance(IntegerMatrix integerMatrix1, IntegerMatrix matrix2) { + int res = 0; + for (int[] ints : integerMatrix1.diff(matrix2)) { + for (int anInt : ints) { + res += ASMath.Power2(anInt); + } + } + return Math.sqrt(res); + } + + /** + * 计算两个矩阵对象之间的距离度量函数,通过该函数可以实现两个矩阵对象度量系数的计算。 + *

+ * Calculates the distance metric function between two matrix objects, through which the metric coefficients of two matrix objects can be calculated. + * + * @param matrix1 需要被进行计算的矩阵对象。 + *

+ * The matrix object that needs to be calculated. + * @param matrix2 需要被进行计算的矩阵对象。 + *

+ * The matrix object that needs to be calculated. + * @return 计算出来的度量结果系数。 + *

+ * The calculated measurement result coefficient. + */ + @Override + public double getTrueDistance(DoubleMatrix matrix1, DoubleMatrix matrix2) { + double res = 0; + for (double[] ints : matrix1.diff(matrix2)) { + for (double anInt : ints) { + res += ASMath.Power2(anInt); + } + } + return Math.sqrt(res); + } + /** * 计算向量距离原点的距离。 * diff --git a/src_code/src/main/java/zhao/algorithmMagic/algorithm/distanceAlgorithm/HausdorffDistance.java b/src_code/src/main/java/zhao/algorithmMagic/algorithm/distanceAlgorithm/HausdorffDistance.java index e55ebec..2952d59 100644 --- a/src_code/src/main/java/zhao/algorithmMagic/algorithm/distanceAlgorithm/HausdorffDistance.java +++ b/src_code/src/main/java/zhao/algorithmMagic/algorithm/distanceAlgorithm/HausdorffDistance.java @@ -9,11 +9,14 @@ import zhao.algorithmMagic.operands.coordinate.Coordinate; import zhao.algorithmMagic.operands.coordinate.FloatingPointCoordinates; import zhao.algorithmMagic.operands.coordinate.IntegerCoordinates; +import zhao.algorithmMagic.operands.matrix.DoubleMatrix; +import zhao.algorithmMagic.operands.matrix.IntegerMatrix; import zhao.algorithmMagic.operands.route.DoubleConsanguinityRoute; import zhao.algorithmMagic.operands.route.DoubleConsanguinityRoute2D; import zhao.algorithmMagic.operands.route.IntegerConsanguinityRoute; import zhao.algorithmMagic.operands.route.IntegerConsanguinityRoute2D; import zhao.algorithmMagic.utils.ASClass; +import zhao.algorithmMagic.utils.ASMath; import java.util.HashMap; @@ -230,4 +233,74 @@ public double getTrueDistance(IntegerConsanguinityRoute integerConsanguinityRout public double getTrueDistance(IntegerConsanguinityRoute2D integerConsanguinityRoute2D) { return getTrueDistance(integerConsanguinityRoute2D.getStartingCoordinate().toArray(), integerConsanguinityRoute2D.getEndPointCoordinate().toArray()); } + + /** + * 计算两个矩阵对象之间的距离度量函数,通过该函数可以实现两个矩阵对象度量系数的计算。 + *

+ * Calculates the distance metric function between two matrix objects, through which the metric coefficients of two matrix objects can be calculated. + * + * @param integerMatrix1 需要被进行计算的矩阵对象。 + *

+ * The matrix object that needs to be calculated. + * @param matrix2 需要被进行计算的矩阵对象。 + *

+ * The matrix object that needs to be calculated. + * @return 计算出来的度量结果系数。 + *

+ * The calculated measurement result coefficient. + */ + @Override + public double getTrueDistance(IntegerMatrix integerMatrix1, IntegerMatrix matrix2) { + DistanceAlgorithm.checkMat(integerMatrix1, matrix2); + int[][] mat2 = matrix2.toArrays(); + int[] res = new int[integerMatrix1.getNumberOfDimensions()]; + for (int[] ints : integerMatrix1) { + for (int anInt : ints) { + // 将 1 矩阵中的所有坐标点与 2 矩阵中的所有坐标点的距离的最大值计算出来 + int max = 0; + for (int[] ints1 : mat2) { + for (int i : ints1) { + max = Math.max(anInt - i, max); + } + } + } + } + // 获取到 res 中的最大数值并返回 + return res[ASMath.findMaxIndex(res)]; + } + + /** + * 计算两个矩阵对象之间的距离度量函数,通过该函数可以实现两个矩阵对象度量系数的计算。 + *

+ * Calculates the distance metric function between two matrix objects, through which the metric coefficients of two matrix objects can be calculated. + * + * @param matrix1 需要被进行计算的矩阵对象。 + *

+ * The matrix object that needs to be calculated. + * @param matrix2 需要被进行计算的矩阵对象。 + *

+ * The matrix object that needs to be calculated. + * @return 计算出来的度量结果系数。 + *

+ * The calculated measurement result coefficient. + */ + @Override + public double getTrueDistance(DoubleMatrix matrix1, DoubleMatrix matrix2) { + DistanceAlgorithm.checkMat(matrix1, matrix2); + double[][] mat2 = matrix2.toArrays(); + double[] res = new double[matrix1.getNumberOfDimensions()]; + for (double[] ints : matrix1) { + for (double anInt : ints) { + // 将 1 矩阵中的所有坐标点与 2 矩阵中的所有坐标点的距离的最大值计算出来 + double max = 0; + for (double[] ints1 : mat2) { + for (double i : ints1) { + max = Math.max(anInt - i, max); + } + } + } + } + // 获取到 res 中的最大数值并返回 + return res[ASMath.findMaxIndex(res)]; + } } diff --git a/src_code/src/main/java/zhao/algorithmMagic/algorithm/distanceAlgorithm/ManhattanDistance.java b/src_code/src/main/java/zhao/algorithmMagic/algorithm/distanceAlgorithm/ManhattanDistance.java index 71b8270..6f86cfb 100644 --- a/src_code/src/main/java/zhao/algorithmMagic/algorithm/distanceAlgorithm/ManhattanDistance.java +++ b/src_code/src/main/java/zhao/algorithmMagic/algorithm/distanceAlgorithm/ManhattanDistance.java @@ -6,6 +6,8 @@ import zhao.algorithmMagic.algorithm.OperationAlgorithmManager; import zhao.algorithmMagic.exception.TargetNotRealizedException; import zhao.algorithmMagic.operands.coordinate.*; +import zhao.algorithmMagic.operands.matrix.DoubleMatrix; +import zhao.algorithmMagic.operands.matrix.IntegerMatrix; import zhao.algorithmMagic.operands.route.DoubleConsanguinityRoute; import zhao.algorithmMagic.operands.route.DoubleConsanguinityRoute2D; import zhao.algorithmMagic.operands.route.IntegerConsanguinityRoute; @@ -329,6 +331,58 @@ public double getTrueDistance(IntegerConsanguinityRoute2D integerConsanguinityRo return getTrueDistance(integerConsanguinityRoute2D.getStartingCoordinate().toArray(), integerConsanguinityRoute2D.getEndPointCoordinate().toArray()); } + /** + * 计算两个矩阵对象之间的距离度量函数,通过该函数可以实现两个矩阵对象度量系数的计算。 + *

+ * Calculates the distance metric function between two matrix objects, through which the metric coefficients of two matrix objects can be calculated. + * + * @param integerMatrix1 需要被进行计算的矩阵对象。 + *

+ * The matrix object that needs to be calculated. + * @param matrix2 需要被进行计算的矩阵对象。 + *

+ * The matrix object that needs to be calculated. + * @return 计算出来的度量结果系数。 + *

+ * The calculated measurement result coefficient. + */ + @Override + public double getTrueDistance(IntegerMatrix integerMatrix1, IntegerMatrix matrix2) { + int res = 0; + for (int[] ints : integerMatrix1.diff(matrix2)) { + for (int anInt : ints) { + res += ASMath.absoluteValue(anInt); + } + } + return res; + } + + /** + * 计算两个矩阵对象之间的距离度量函数,通过该函数可以实现两个矩阵对象度量系数的计算。 + *

+ * Calculates the distance metric function between two matrix objects, through which the metric coefficients of two matrix objects can be calculated. + * + * @param matrix1 需要被进行计算的矩阵对象。 + *

+ * The matrix object that needs to be calculated. + * @param matrix2 需要被进行计算的矩阵对象。 + *

+ * The matrix object that needs to be calculated. + * @return 计算出来的度量结果系数。 + *

+ * The calculated measurement result coefficient. + */ + @Override + public double getTrueDistance(DoubleMatrix matrix1, DoubleMatrix matrix2) { + double res = 0; + for (double[] ints : matrix1.diff(matrix2)) { + for (double anInt : ints) { + res += ASMath.absoluteValue(anInt); + } + } + return res; + } + /** * 计算向量距离原点的距离。 * diff --git a/src_code/src/main/java/zhao/algorithmMagic/algorithm/distanceAlgorithm/MinkowskiDistance.java b/src_code/src/main/java/zhao/algorithmMagic/algorithm/distanceAlgorithm/MinkowskiDistance.java index 2491248..7034066 100644 --- a/src_code/src/main/java/zhao/algorithmMagic/algorithm/distanceAlgorithm/MinkowskiDistance.java +++ b/src_code/src/main/java/zhao/algorithmMagic/algorithm/distanceAlgorithm/MinkowskiDistance.java @@ -6,6 +6,8 @@ import zhao.algorithmMagic.algorithm.OperationAlgorithmManager; import zhao.algorithmMagic.exception.TargetNotRealizedException; import zhao.algorithmMagic.operands.coordinate.*; +import zhao.algorithmMagic.operands.matrix.DoubleMatrix; +import zhao.algorithmMagic.operands.matrix.IntegerMatrix; import zhao.algorithmMagic.operands.route.DoubleConsanguinityRoute; import zhao.algorithmMagic.operands.route.DoubleConsanguinityRoute2D; import zhao.algorithmMagic.operands.route.IntegerConsanguinityRoute; @@ -339,6 +341,60 @@ public double getTrueDistance(IntegerConsanguinityRoute2D integerConsanguinityRo return getTrueDistance(integerConsanguinityRoute2D.getStartingCoordinate().toArray(), integerConsanguinityRoute2D.getEndPointCoordinate().toArray()); } + /** + * 计算两个矩阵对象之间的距离度量函数,通过该函数可以实现两个矩阵对象度量系数的计算。 + *

+ * Calculates the distance metric function between two matrix objects, through which the metric coefficients of two matrix objects can be calculated. + * + * @param matrix1 需要被进行计算的矩阵对象。 + *

+ * The matrix object that needs to be calculated. + * @param matrix2 需要被进行计算的矩阵对象。 + *

+ * The matrix object that needs to be calculated. + * @return 计算出来的度量结果系数。 + *

+ * The calculated measurement result coefficient. + */ + @Override + public double getTrueDistance(IntegerMatrix matrix1, IntegerMatrix matrix2) { + int res = 0; + int $P = get$P(); + for (int[] ints : matrix1.diff(matrix2)) { + for (int anInt : ints) { + res += Math.pow(anInt, $P); + } + } + return res; + } + + /** + * 计算两个矩阵对象之间的距离度量函数,通过该函数可以实现两个矩阵对象度量系数的计算。 + *

+ * Calculates the distance metric function between two matrix objects, through which the metric coefficients of two matrix objects can be calculated. + * + * @param matrix1 需要被进行计算的矩阵对象。 + *

+ * The matrix object that needs to be calculated. + * @param matrix2 需要被进行计算的矩阵对象。 + *

+ * The matrix object that needs to be calculated. + * @return 计算出来的度量结果系数。 + *

+ * The calculated measurement result coefficient. + */ + @Override + public double getTrueDistance(DoubleMatrix matrix1, DoubleMatrix matrix2) { + int res = 0; + int $P = get$P(); + for (double[] ints : matrix1.diff(matrix2)) { + for (double anInt : ints) { + res += Math.pow(anInt, $P); + } + } + return res; + } + /** * 计算向量距离原点的距离。 * diff --git a/src_code/src/main/java/zhao/algorithmMagic/algorithm/distanceAlgorithm/StandardizedEuclideanDistance.java b/src_code/src/main/java/zhao/algorithmMagic/algorithm/distanceAlgorithm/StandardizedEuclideanDistance.java index acbe5cf..05f83f0 100644 --- a/src_code/src/main/java/zhao/algorithmMagic/algorithm/distanceAlgorithm/StandardizedEuclideanDistance.java +++ b/src_code/src/main/java/zhao/algorithmMagic/algorithm/distanceAlgorithm/StandardizedEuclideanDistance.java @@ -4,30 +4,22 @@ import zhao.algorithmMagic.algorithm.OperationAlgorithmManager; import zhao.algorithmMagic.algorithm.normalization.Z_ScoreNormalization; import zhao.algorithmMagic.exception.TargetNotRealizedException; -import zhao.algorithmMagic.operands.coordinate.*; +import zhao.algorithmMagic.operands.coordinate.DoubleCoordinateMany; +import zhao.algorithmMagic.operands.coordinate.FloatingPointCoordinates; +import zhao.algorithmMagic.operands.coordinate.IntegerCoordinateMany; +import zhao.algorithmMagic.operands.coordinate.IntegerCoordinates; +import zhao.algorithmMagic.operands.matrix.DoubleMatrix; +import zhao.algorithmMagic.operands.matrix.IntegerMatrix; import zhao.algorithmMagic.utils.ASClass; -import zhao.algorithmMagic.utils.DependentAlgorithmNameLibrary; /** * Java类于 2022/10/13 10:30:50 创建 *

* 标准化欧几里得度量,先将每一个维度的量进行标准化,然后再进行欧几里得计算 * - * @param 本类中参与运算的整数坐标的类型,您需要在此类种指定该类可以运算的整形坐标 - *

- * The type of integer coordinates involved in the operation in this class, you need to specify the integer coordinates that this class can operate on in this class. - * @param 本类中参与运算的浮点坐标的类型,您需要在此类种指定该类可以运算的浮点坐标 - *

- * The type of floating-point coordinates involved in the operation in this class. You need to specify the floating-point coordinates that this class can operate on. * @author LingYuZhao */ -public class StandardizedEuclideanDistance & Coordinate, D extends FloatingPointCoordinates> extends EuclideanMetric { - - private final EuclideanMetric euclideanMetric = EuclideanMetric.getInstance(DependentAlgorithmNameLibrary.EUCLIDEAN_METRIC_NAME); - - protected StandardizedEuclideanDistance() { - super(); - } +public class StandardizedEuclideanDistance extends EuclideanMetric { protected StandardizedEuclideanDistance(String algorithmName) { super(algorithmName); @@ -37,40 +29,25 @@ protected StandardizedEuclideanDistance(String algorithmName) { * 获取到该算法的类对象, * * @param Name 该算法的名称 - * @param 该算法用来处理的整形坐标是什么数据类型 - *

- * What data type is the integer coordinate used by this algorithm? - * @param

该算法用来处理的浮点坐标是什么数据类型 * @return 算法类对象 * @throws TargetNotRealizedException 当您传入的算法名称对应的组件不能被成功提取的时候会抛出异常 */ - public static & Coordinate, DD extends FloatingPointCoordinates> StandardizedEuclideanDistance getInstance2(String Name) { + public static StandardizedEuclideanDistance getInstance2(String Name) { if (OperationAlgorithmManager.containsAlgorithmName(Name)) { OperationAlgorithm operationAlgorithm = OperationAlgorithmManager.getInstance().get(Name); - if (operationAlgorithm instanceof StandardizedEuclideanDistance) { + if (operationAlgorithm instanceof StandardizedEuclideanDistance) { return ASClass.transform(operationAlgorithm); } else { throw new TargetNotRealizedException("您提取的[" + Name + "]算法被找到了,但是它不属于StandardizedEuclideanDistance类型,请您为这个算法重新定义一个名称。\n" + "The [" + Name + "] algorithm you extracted has been found, but it does not belong to the StandardizedEuclideanDistance type. Please redefine a name for this algorithm."); } } else { - StandardizedEuclideanDistance standardizedEuclideanDistance = new StandardizedEuclideanDistance<>(Name); + StandardizedEuclideanDistance standardizedEuclideanDistance = new StandardizedEuclideanDistance(Name); OperationAlgorithmManager.getInstance().register(standardizedEuclideanDistance); return standardizedEuclideanDistance; } } - public EuclideanMetric toEuclideanMetric() { - if (this.euclideanMetric != null) { - return this.euclideanMetric; - } else { - String euclideanMetricName = DependentAlgorithmNameLibrary.EUCLIDEAN_METRIC_NAME; - logger.warn("名称为:[" + euclideanMetricName + "]的算法被删除了,为了避免异常,标准化欧几里德算法模块所需的欧几里德算法依赖,已经直接绑定到OperationAlgorithmManager,如果您想要避免该警告,请您在不删除[EuclideanMetric]算法的前提下重启程序!\n" + - "The algorithm named: [" + euclideanMetricName + "] has been removed. To avoid exceptions, the Euclidean algorithm dependencies required by the standardized Euclidean algorithm module have been directly bound to the Operation Algorithm Manager, if you want to avoid the warning , please restart the program without deleting the [" + euclideanMetricName + "] algorithm!"); - return EuclideanMetric.getInstance(euclideanMetricName); - } - } - /** * 多维空间之中,自身坐标点到原点的距离,这里是多维空间,所以是每一个维度的坐标点都会被进行2次方 *

@@ -82,13 +59,13 @@ public EuclideanMetric toEuclideanM * @return Euclidean distance from its own coordinates to the origin */ @Override - public double getTrueDistance(FloatingPointCoordinates iFloatingPointCoordinates) { + public double getTrueDistance(FloatingPointCoordinates iFloatingPointCoordinates) { double[] doubles1 = iFloatingPointCoordinates.toArray(); double[] doublesZ = Z_ScoreNormalization.StandardizedSequence(doubles1); if (OperationAlgorithmManager.PrintCalculationComponentLog) { logger.info("√ ⁿ∑₁( (iFloatingPointCoordinates(n) / StandardSequence(n))² )"); } - return this.euclideanMetric.getTrueDistance(new DoubleCoordinateMany(DivideByNormalization(doubles1, doublesZ))); + return super.getTrueDistance(new DoubleCoordinateMany(DivideByNormalization(doubles1, doublesZ))); } /** @@ -132,13 +109,13 @@ private int[] DivideByNormalization(int[] NonstandardSequence, int[] StandardSeq * @return Euclidean distance from its own coordinates to the origin */ @Override - public double getTrueDistance(IntegerCoordinates integerCoordinates) { + public double getTrueDistance(IntegerCoordinates integerCoordinates) { int[] doubles1 = integerCoordinates.toArray(); int[] doublesZ = Z_ScoreNormalization.StandardizedSequence(doubles1); if (OperationAlgorithmManager.PrintCalculationComponentLog) { logger.info("√ ⁿ∑₁( (integerCoordinates(n) / StandardSequence(n))² )"); } - return this.euclideanMetric.getTrueDistance(new IntegerCoordinateMany(DivideByNormalization(doubles1, doublesZ))); + return super.getTrueDistance(new IntegerCoordinateMany(DivideByNormalization(doubles1, doublesZ))); } /** @@ -152,13 +129,13 @@ public double getTrueDistance(IntegerCoordinates integerCoordinates) { * @return True Euclidean distance between two points */ @Override - public double getTrueDistance(IntegerCoordinates integerCoordinateMany1, IntegerCoordinates integerCoordinateMany2) { + public double getTrueDistance(IntegerCoordinates integerCoordinateMany1, IntegerCoordinates integerCoordinateMany2) { if (OperationAlgorithmManager.PrintCalculationComponentLog) { logger.info("√ ⁿ∑₁( " + integerCoordinateMany1 + " - " + integerCoordinateMany2 + ").map(i -> (i / StandardSequence)²)"); } int[] doubles1 = integerCoordinateMany1.extend().diff(integerCoordinateMany2.extend()).toArray(); int[] doublesZ = Z_ScoreNormalization.StandardizedSequence(doubles1); - return this.euclideanMetric.getTrueDistance(new IntegerCoordinateMany(DivideByNormalization(doubles1, doublesZ))); + return super.getTrueDistance(new IntegerCoordinateMany(DivideByNormalization(doubles1, doublesZ))); } /** @@ -171,13 +148,13 @@ public double getTrueDistance(IntegerCoordinates integerCoordinateMany1, Inte * @return True Euclidean distance between two points */ @Override - public double getTrueDistance(FloatingPointCoordinates doubleCoordinateMany1, FloatingPointCoordinates doubleCoordinateMany2) { + public double getTrueDistance(FloatingPointCoordinates doubleCoordinateMany1, FloatingPointCoordinates doubleCoordinateMany2) { if (OperationAlgorithmManager.PrintCalculationComponentLog) { logger.info("√ ⁿ∑₁( " + doubleCoordinateMany1 + " - " + doubleCoordinateMany2 + ").map(d -> (d / StandardSequence)²)"); } double[] doubles1 = doubleCoordinateMany1.diff(doubleCoordinateMany2.extend()).toArray(); double[] doublesZ = Z_ScoreNormalization.StandardizedSequence(doubles1); - return this.euclideanMetric.getTrueDistance(new DoubleCoordinateMany(DivideByNormalization(doubles1, doublesZ))); + return super.getTrueDistance(new DoubleCoordinateMany(DivideByNormalization(doubles1, doublesZ))); } @@ -223,7 +200,7 @@ public boolean init() { public double getTrueDistance(double[] doubles1, double[] doubles2) { double[] doubles11 = new DoubleCoordinateMany(doubles1).diff(new DoubleCoordinateMany(doubles2)).toArray(); double[] doubles22 = Z_ScoreNormalization.StandardizedSequence(doubles11); - return this.euclideanMetric.getTrueDistance(new DoubleCoordinateMany(DivideByNormalization(doubles11, doubles22))); + return super.getTrueDistance(new DoubleCoordinateMany(DivideByNormalization(doubles11, doubles22))); } /** @@ -241,6 +218,46 @@ public double getTrueDistance(double[] doubles1, double[] doubles2) { public double getTrueDistance(int[] ints1, int[] ints2) { double[] doubles11 = new DoubleCoordinateMany(ints1).diff(new DoubleCoordinateMany(ints2)).toArray(); double[] doubles22 = Z_ScoreNormalization.StandardizedSequence(doubles11); - return this.euclideanMetric.getTrueDistance(new DoubleCoordinateMany(DivideByNormalization(doubles11, doubles22))); + return super.getTrueDistance(new DoubleCoordinateMany(DivideByNormalization(doubles11, doubles22))); + } + + /** + * 计算两个矩阵对象之间的距离度量函数,通过该函数可以实现两个矩阵对象度量系数的计算。 + *

+ * Calculates the distance metric function between two matrix objects, through which the metric coefficients of two matrix objects can be calculated. + * + * @param integerMatrix1 需要被进行计算的矩阵对象。 + *

+ * The matrix object that needs to be calculated. + * @param matrix2 需要被进行计算的矩阵对象。 + *

+ * The matrix object that needs to be calculated. + * @return 计算出来的度量结果系数。 + *

+ * The calculated measurement result coefficient. + */ + @Override + public double getTrueDistance(IntegerMatrix integerMatrix1, IntegerMatrix matrix2) { + throw new UnsupportedOperationException("The matrix does not currently support serialization operations"); + } + + /** + * 计算两个矩阵对象之间的距离度量函数,通过该函数可以实现两个矩阵对象度量系数的计算。 + *

+ * Calculates the distance metric function between two matrix objects, through which the metric coefficients of two matrix objects can be calculated. + * + * @param matrix1 需要被进行计算的矩阵对象。 + *

+ * The matrix object that needs to be calculated. + * @param matrix2 需要被进行计算的矩阵对象。 + *

+ * The matrix object that needs to be calculated. + * @return 计算出来的度量结果系数。 + *

+ * The calculated measurement result coefficient. + */ + @Override + public double getTrueDistance(DoubleMatrix matrix1, DoubleMatrix matrix2) { + throw new UnsupportedOperationException("The matrix does not currently support serialization operations"); } } diff --git a/src_code/src/main/java/zhao/algorithmMagic/core/AlgorithmStar.java b/src_code/src/main/java/zhao/algorithmMagic/core/AlgorithmStar.java index c047db6..c414861 100644 --- a/src_code/src/main/java/zhao/algorithmMagic/core/AlgorithmStar.java +++ b/src_code/src/main/java/zhao/algorithmMagic/core/AlgorithmStar.java @@ -190,6 +190,50 @@ public double getTrueDistance(DistanceAlgorithm distanceAlgorithm, IntegerConsan return distanceAlgorithm.getTrueDistance(integerConsanguinityRoute2D); } + /** + * 计算两个矩阵对象之间的距离度量函数,通过该函数可以实现两个矩阵对象度量系数的计算。 + *

+ * Calculates the distance metric function between two matrix objects, through which the metric coefficients of two matrix objects can be calculated. + * + * @param distanceAlgorithm 距离计算组件,是您在调用本次函数之后,函数用来计算的距离计算组件对象。 + *

+ * The distance calculation component is the distance calculation component object that the function uses to calculate after you call this function. + * @param matrix1 需要被进行计算的矩阵对象。 + *

+ * The matrix object that needs to be calculated. + * @param matrix2 需要被进行计算的矩阵对象。 + *

+ * The matrix object that needs to be calculated. + * @return 计算出来的度量结果系数。 + *

+ * The calculated measurement result coefficient. + */ + public double getTrueDistance(DistanceAlgorithm distanceAlgorithm, IntegerMatrix matrix1, IntegerMatrix matrix2) { + return distanceAlgorithm.getTrueDistance(matrix1, matrix2); + } + + /** + * 计算两个矩阵对象之间的距离度量函数,通过该函数可以实现两个矩阵对象度量系数的计算。 + *

+ * Calculates the distance metric function between two matrix objects, through which the metric coefficients of two matrix objects can be calculated. + * + * @param distanceAlgorithm 距离计算组件,是您在调用本次函数之后,函数用来计算的距离计算组件对象。 + *

+ * The distance calculation component is the distance calculation component object that the function uses to calculate after you call this function. + * @param matrix1 需要被进行计算的矩阵对象。 + *

+ * The matrix object that needs to be calculated. + * @param matrix2 需要被进行计算的矩阵对象。 + *

+ * The matrix object that needs to be calculated. + * @return 计算出来的度量结果系数。 + *

+ * The calculated measurement result coefficient. + */ + public double getTrueDistance(DistanceAlgorithm distanceAlgorithm, DoubleMatrix matrix1, DoubleMatrix matrix2) { + return distanceAlgorithm.getTrueDistance(matrix1, matrix2); + } + /** * 计算向量距离原点的距离。 * diff --git a/src_code/src/main/java/zhao/algorithmMagic/operands/matrix/ColorMatrix.java b/src_code/src/main/java/zhao/algorithmMagic/operands/matrix/ColorMatrix.java index 8ef27f3..5f8aa28 100644 --- a/src_code/src/main/java/zhao/algorithmMagic/operands/matrix/ColorMatrix.java +++ b/src_code/src/main/java/zhao/algorithmMagic/operands/matrix/ColorMatrix.java @@ -1,7 +1,9 @@ package zhao.algorithmMagic.operands.matrix; +import zhao.algorithmMagic.algorithm.distanceAlgorithm.DistanceAlgorithm; import zhao.algorithmMagic.exception.OperatorOperationException; import zhao.algorithmMagic.integrator.ImageRenderingIntegrator; +import zhao.algorithmMagic.operands.coordinate.IntegerCoordinateTwo; import zhao.algorithmMagic.operands.matrix.block.IntegerMatrixSpace; import zhao.algorithmMagic.utils.ASClass; import zhao.algorithmMagic.utils.ASIO; @@ -1149,7 +1151,7 @@ public final void contrast(int contrastThreshold) { public ColorMatrix extractImage(int x1, int y1, int x2, int y2) { if (x1 >= x2 || y1 >= y2) { throw new OperatorOperationException("图像提取发生错误,您设置的提取坐标点有误!!!\nAn error occurred in image extraction. The extraction coordinate point you set is incorrect!!!\n" + - "ERROR => (" + x1 + ',' + y1 + ") >= (" + y1 + ',' + y2 + ')'); + "ERROR => (" + x1 + ',' + y1 + ") >= (" + x2 + ',' + y2 + ')'); } if (x2 >= this.getColCount() || y2 >= this.getRowCount()) { throw new OperatorOperationException("图像提取发生错误,您不能提取不存在于图像中的坐标点\nAn error occurred in image extraction. You cannot extract coordinate points that do not exist in the image\n" + @@ -1421,6 +1423,119 @@ public ColorMatrix erode(int width, int height, boolean isCopy, Color backColor) return res; } + /** + * 将图像矩阵中的指定通道的颜色整数数值矩阵获取到。 + * + * @param colorMode 需要被获取到颜色通道编码。 + * @return 对应颜色通道的颜色数值组成的颜色矩阵对象。 + */ + public final IntegerMatrix getChannel(int colorMode) { + int[][] res = new int[this.getRowCount()][this.getColCount()]; + int y = -1; + for (Color[] colors : this.toArrays()) { + int[] re = res[++y]; + int x = -1; + for (Color color : colors) { + re[++x] = (color.getRGB() >> colorMode) & 0xFF; + } + } + return IntegerMatrix.parse(res); + } + + /** + * 向矩阵中绘制一个矩阵。 + * + * @param start 矩阵的左上角坐标对象。 + * @param end 矩阵的右下角坐标对象。 + * @param color 绘制的轮廓颜色对象。 + */ + public void drawRectangle(IntegerCoordinateTwo start, IntegerCoordinateTwo end, Color color) { + Color[][] colors = this.toArrays(); + int x1 = start.getX(); + int x2 = end.getX(); + int y1 = start.getY(); + int y2 = end.getY(); + // 首先将起始终止点的 x 轴变更为 color 颜色(使用y轴获取) + Color[] color1 = colors[start.getY()]; + Color[] color2 = colors[end.getY()]; + for (int i = x1; i <= x2; i++) { + color1[i] = color; + color2[i] = color; + } + // 然后将剩余所有点的 y 轴变为 color 颜色(使用x轴获取) + for (int i = y1; i <= y2; i++) { + Color[] colorRow = colors[i]; + colorRow[x1] = color; + colorRow[x2] = color; + } + } + + /** + * 将图像进行模板匹配计算 + * + * @param distanceAlgorithm 需要使用的度量计算组件。 + * @param template 在进行模板匹配时的被匹配模板。 + * @param channel 在进行计算时,需要被计算的颜色通道。 + * @param isMAX 如果设置为 true 代表取相似度系数最大的子图像坐标作为。 + * @return 经过计算之后,与当前矩阵对象最相似的子图像的左上角坐标数值对象。 + */ + public final IntegerCoordinateTwo templateMatching(DistanceAlgorithm distanceAlgorithm, ColorMatrix template, int channel, boolean isMAX) { + // 开始匹配 + IntegerMatrix channel1 = template.getChannel(channel); + // 首先提取出矩阵1中相同大小的子矩阵图像 + int rowCount1 = this.getRowCount(); + int colCount1 = this.getColCount(); + int rowCount2 = template.getRowCount(); + int colCount2 = template.getColCount(); + if (rowCount1 < rowCount2 || colCount1 < colCount2) { + throw new OperatorOperationException("您的模板矩阵尺寸过大!!"); + } + int[] xy = new int[2]; + double value = isMAX ? Integer.MIN_VALUE : Integer.MAX_VALUE; + + if (rowCount1 == rowCount2) { + // 这种情况代表可以使用优化方案 + int ye = rowCount2 - 1; + for (int y = 0; y < rowCount1; y++, ye++) { + double trueDistance = distanceAlgorithm.getTrueDistance(this.extractImageSrc(y, ye).getChannel(channel), channel1); + if (isMAX) { + if (value < trueDistance) { + value = trueDistance; + xy[1] = y; + } + } else { + if (value > trueDistance) { + value = trueDistance; + xy[1] = y; + } + } + } + return new IntegerCoordinateTwo(xy[0], xy[1]); + } + int ye = rowCount2 - 1; + for (int y = 0; ye < rowCount1; y++, ye++) { + int xe = colCount2 - 1; + for (int x = 0; xe < colCount1; x++, xe++) { + // 计算出相似度 + double trueDistance = distanceAlgorithm.getTrueDistance(this.extractImage(x, y, xe, ye).getChannel(channel), channel1); + if (isMAX) { + if (value < trueDistance) { + value = trueDistance; + xy[0] = x; + xy[1] = y; + } + } else { + if (value > trueDistance) { + value = trueDistance; + xy[0] = x; + xy[1] = y; + } + } + } + } + return new IntegerCoordinateTwo(xy[0], xy[1]); + } + /** * 将图像矩阵展示出来,使得在矩阵的图像数据能够被展示出来。 *

diff --git a/src_code/src/main/java/zhao/algorithmMagic/operands/matrix/DoubleMatrix.java b/src_code/src/main/java/zhao/algorithmMagic/operands/matrix/DoubleMatrix.java index ab7e6df..b997219 100644 --- a/src_code/src/main/java/zhao/algorithmMagic/operands/matrix/DoubleMatrix.java +++ b/src_code/src/main/java/zhao/algorithmMagic/operands/matrix/DoubleMatrix.java @@ -185,6 +185,15 @@ public static DoubleMatrix parse(DataFrame dataFrame, int height, int width) { return parse(doubles); } + public static DoubleMatrix parse(IntegerMatrix parse1) { + double[][] doubles = new double[parse1.getRowCount()][]; + int index = -1; + for (int[] ints : parse1.toArrays()) { + doubles[++index] = ASClass.IntArray_To_DoubleArray(ints); + } + return new DoubleMatrix(doubles.length, parse1.getColCount(), doubles); + } + /** * 获取到矩阵中指定坐标点的数值 * @@ -315,15 +324,11 @@ public DoubleMatrix diff(DoubleMatrix value) { @Override public Double moduleLength() { double res = 0; - int rowPointer = this.RowPointer; - PointerReset(); - while (this.MovePointerDown()) { - double[] doubles1 = this.toArray(); - for (double v : doubles1) { - res += ASMath.Power2(v); + for (double[] ints : this.toArrays()) { + for (double anInt : ints) { + res += anInt * anInt; } } - this.RowPointer = rowPointer; return res; } @@ -632,6 +637,32 @@ public DoubleMatrix extractMat(int x1, int y1, int x2, int y2) { return DoubleMatrix.parse(colors); } + /** + * 提取出子矩阵对象 + * + * @param y1 被提取矩阵在原矩阵中的起始坐标点。 + *

+ * The starting coordinate point of the extracted matrix in the original matrix. + * @param y2 被提取矩阵在原矩阵中的终止坐标点。 + *

+ * The end coordinate point of the extracted matrix in the original matrix. + * @return 整形矩阵对象中的子矩阵对象。 + */ + @Override + public DoubleMatrix extractSrcMat(int y1, int y2) { + if (y1 >= y2) { + throw new OperatorOperationException("整形矩阵提取发生错误,您设置的提取坐标点有误!!!\nAn error occurred in mat extraction. The extraction coordinate point you set is incorrect!!!\n" + + "ERROR => (" + y1 + ") >= (" + y2 + ')'); + } + double[][] res_colors = new double[y2 - y1][]; + double[][] colors1 = this.toArrays(); + int index = -1; + for (int i = y1; i < y2; i++) { + res_colors[++index] = colors1[i]; + } + return DoubleMatrix.parse(res_colors); + } + @Override protected void reFresh() { this.PointerReset(); diff --git a/src_code/src/main/java/zhao/algorithmMagic/operands/matrix/IntegerMatrix.java b/src_code/src/main/java/zhao/algorithmMagic/operands/matrix/IntegerMatrix.java index f3eb817..633108a 100644 --- a/src_code/src/main/java/zhao/algorithmMagic/operands/matrix/IntegerMatrix.java +++ b/src_code/src/main/java/zhao/algorithmMagic/operands/matrix/IntegerMatrix.java @@ -338,15 +338,11 @@ protected void reFresh() { @Override public Integer moduleLength() { int res = 0; - int rowPointer = this.RowPointer; - PointerReset(); - while (this.MovePointerDown()) { - int[] ints = toArray(); - for (int v : ints) { - res += ASMath.Power2(v); + for (int[] ints : this.toArrays()) { + for (int anInt : ints) { + res += anInt * anInt; } } - this.RowPointer = rowPointer; return res; } @@ -629,7 +625,7 @@ public IntegerMatrix deleteRelatedDimensions(int index, double thresholdLeft, do public IntegerMatrix extractMat(int x1, int y1, int x2, int y2) { if (x1 >= x2 || y1 >= y2) { throw new OperatorOperationException("整形矩阵提取发生错误,您设置的提取坐标点有误!!!\nAn error occurred in mat extraction. The extraction coordinate point you set is incorrect!!!\n" + - "ERROR => (" + x1 + ',' + x2 + ") >= (" + x2 + ',' + y2 + ')'); + "ERROR => (" + x1 + ',' + y1 + ") >= (" + x2 + ',' + y2 + ')'); } if (x2 >= this.getColCount() || y2 >= this.getRowCount()) { throw new OperatorOperationException("整形矩阵提取发生错误,您不能提取不存在于矩阵中的坐标点\nAn error occurred in mat extraction. You cannot extract coordinate points that do not exist in the image\n" + @@ -644,6 +640,32 @@ public IntegerMatrix extractMat(int x1, int y1, int x2, int y2) { return IntegerMatrix.parse(colors); } + /** + * 提取出子矩阵对象 + * + * @param y1 被提取矩阵在原矩阵中的起始坐标点。 + *

+ * The starting coordinate point of the extracted matrix in the original matrix. + * @param y2 被提取矩阵在原矩阵中的终止坐标点。 + *

+ * The end coordinate point of the extracted matrix in the original matrix. + * @return 整形矩阵对象中的子矩阵对象。 + */ + @Override + public IntegerMatrix extractSrcMat(int y1, int y2) { + if (y1 >= y2) { + throw new OperatorOperationException("整形矩阵提取发生错误,您设置的提取坐标点有误!!!\nAn error occurred in mat extraction. The extraction coordinate point you set is incorrect!!!\n" + + "ERROR => (" + y1 + ") >= (" + y2 + ')'); + } + int[][] res_colors = new int[y2 - y1][]; + int[][] colors1 = this.toArrays(); + int index = -1; + for (int i = y1; i < y2; i++) { + res_colors[++index] = colors1[i]; + } + return IntegerMatrix.parse(res_colors); + } + /** * 将数据所维护的数组左移n个位置,并获取到结果数值 *

diff --git a/src_code/src/main/java/zhao/algorithmMagic/operands/matrix/NumberMatrix.java b/src_code/src/main/java/zhao/algorithmMagic/operands/matrix/NumberMatrix.java index 1c079ef..5ebd82e 100644 --- a/src_code/src/main/java/zhao/algorithmMagic/operands/matrix/NumberMatrix.java +++ b/src_code/src/main/java/zhao/algorithmMagic/operands/matrix/NumberMatrix.java @@ -78,4 +78,17 @@ protected NumberMatrix(int rowCount, int colCount, ArraysType arraysType) { * @return 整形矩阵对象中的子矩阵对象。 */ public abstract ImplementationType extractMat(int x1, int y1, int x2, int y2); + + /** + * 提取出子矩阵对象 + * + * @param y1 被提取矩阵在原矩阵中的起始坐标点。 + *

+ * The starting coordinate point of the extracted matrix in the original matrix. + * @param y2 被提取矩阵在原矩阵中的终止坐标点。 + *

+ * The end coordinate point of the extracted matrix in the original matrix. + * @return 整形矩阵对象中的子矩阵对象。 + */ + public abstract ImplementationType extractSrcMat(int y1, int y2); } diff --git a/src_code/src/main/java/zhao/algorithmMagic/operands/matrix/RectangleMatrix.java b/src_code/src/main/java/zhao/algorithmMagic/operands/matrix/RectangleMatrix.java index d0e64e6..b7a4b54 100644 --- a/src_code/src/main/java/zhao/algorithmMagic/operands/matrix/RectangleMatrix.java +++ b/src_code/src/main/java/zhao/algorithmMagic/operands/matrix/RectangleMatrix.java @@ -59,23 +59,26 @@ public static RectangleMatrix parse(ColorMatrix colorMatrix, Color rectColor) { int outlineWidthR = 0; // 获取到起始与终止索引 boolean isH = false; + boolean isW = false; int rc = -1; for (Color[] colors : colorMatrix.toArrays()) { ++rc; + boolean isRw = false; int cc = -1; - boolean isW = false; boolean isOk = false; for (Color color : colors) { ++cc; // 先判断宽 if (color.getRGB() != 0xff000000) { isOk = true; - if (isW) { - if (cc > outlineWidthR) outlineWidthR = cc; - } else if (cc < outlineWidthL) { + if (!isRw && cc < outlineWidthL) { outlineWidthL = cc; outlineWidthR = cc; isW = true; + isRw = true; + } + if (isW) { + if (cc > outlineWidthR) outlineWidthR = cc; } } } @@ -83,7 +86,7 @@ public static RectangleMatrix parse(ColorMatrix colorMatrix, Color rectColor) { if (isOk) { if (isH) { if (rc > outlineHeightR) outlineHeightR = rc; - } else if (rc < outlineWidthL) { + } else if (rc < outlineHeightL) { outlineHeightL = rc; outlineHeightR = rc; isH = true; diff --git a/src_code/src/main/java/zhao/algorithmMagic/operands/vector/SparkVector.scala b/src_code/src/main/java/zhao/algorithmMagic/operands/vector/SparkVector.scala index 7f0bac4..59c6761 100644 --- a/src_code/src/main/java/zhao/algorithmMagic/operands/vector/SparkVector.scala +++ b/src_code/src/main/java/zhao/algorithmMagic/operands/vector/SparkVector.scala @@ -77,6 +77,14 @@ final class SparkVector(sparkContext: SparkContext, vector: org.apache.spark.mll else throw new OperatorOperationException("'DoubleVector1 innerProduct DoubleVector2' 时,两个'DoubleVector'的向量所包含的数量不同,DoubleVector1=[" + doubles1.length + "],DoubleVector2=[" + doubles2.length + "]\n" + "When 'DoubleVector1 innerProduct DoubleVector2', the two vectors of 'DoubleVector' contain different quantities, DoubleVector1=[" + doubles1.length + "], DoubleVector2=[" + doubles2.length + "]") } + /** + * + * @return 将本对象中存储的向量序列数组拷贝到一个新数组并将新数组返回,这里返回的是一个新数组,支持修改等操作。 + * + * Copy the vector sequence array stored in this object to a new array and return the new array. Here, a new array is returned, which supports modification and other operations. + */ + override def copyToNewArray(): Array[Double] = vector.toArray + /** * @return 该类的实现类对象,用于拓展该接口的子类 */ @@ -107,21 +115,6 @@ final class SparkVector(sparkContext: SparkContext, vector: org.apache.spark.mll else throw new OperatorOperationException("'DoubleVector1 add DoubleVector2' 时,两个'DoubleVector'的向量所包含的数量不同,DoubleVector1=[" + numberOfDimensions1 + "],DoubleVector2=[" + numberOfDimensions2 + "]\n" + "When 'DoubleVector1 add DoubleVector2', the two vectors of 'DoubleVector' contain different quantities, DoubleVector1=[" + numberOfDimensions1 + "], DoubleVector2=[" + numberOfDimensions2 + "]") } - /** - * @return 向量中包含的维度数量 - *

- * the number of dimensions contained in the vector - */ - override def getNumberOfDimensions: Int = size - - /** - * - * @return 将本对象中存储的向量序列数组拷贝到一个新数组并将新数组返回,这里返回的是一个新数组,支持修改等操作。 - * - * Copy the vector sequence array stored in this object to a new array and return the new array. Here, a new array is returned, which supports modification and other operations. - */ - override def copyToNewArray(): Array[Double] = vector.toArray - /** * 在两个操作数之间做差的方法,具体用法请参阅API说明。 *

@@ -146,6 +139,13 @@ final class SparkVector(sparkContext: SparkContext, vector: org.apache.spark.mll else throw new OperatorOperationException("'DoubleVector1 diff DoubleVector2' 时,两个'DoubleVector'的向量所包含的数量不同,DoubleVector1=[" + numberOfDimensions1 + "],DoubleVector2=[" + numberOfDimensions2 + "]\n" + "When 'DoubleVector1 diff DoubleVector2', the two vectors of 'DoubleVector' contain different quantities, DoubleVector1=[" + numberOfDimensions1 + "], DoubleVector2=[" + numberOfDimensions2 + "]") } + /** + * @return 向量中包含的维度数量 + *

+ * the number of dimensions contained in the vector + */ + override def getNumberOfDimensions: Int = size + /** * * @return 第三方向量中所维护的向量序列,通过此函数您可以直接获取到第三方库中的对象。 From 2a1f253dad77a2b6e228f7cd207b66ed605ba2d7 Mon Sep 17 00:00:00 2001 From: LingYuZhao Date: Sat, 1 Apr 2023 19:05:03 +0800 Subject: [PATCH 06/17] update MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * 完善模板匹配函数的实现,增加步长,提升效率。 --- src_code/Case.md | 2 + src_code/README-Chinese.md | 2 + src_code/README.md | 2 + .../main/java/zhao/algorithmMagic/MAIN1.java | 44 ++++++++++++------- .../operands/matrix/ColorMatrix.java | 9 ++-- 5 files changed, 39 insertions(+), 20 deletions(-) diff --git a/src_code/Case.md b/src_code/Case.md index a92aed4..0eee52b 100644 --- a/src_code/Case.md +++ b/src_code/Case.md @@ -135,6 +135,8 @@ public class MAIN1 { colorMatrix2, // 需要被计算的颜色通道 ColorMatrix._G_, + // 卷积时的步长数值 + 10, // 相似度越小 匹配度越大 false ); diff --git a/src_code/README-Chinese.md b/src_code/README-Chinese.md index e53517b..ba455df 100644 --- a/src_code/README-Chinese.md +++ b/src_code/README-Chinese.md @@ -342,6 +342,8 @@ public class MAIN1 { colorMatrix2, // 需要被计算的颜色通道 ColorMatrix._G_, + // 卷积时的步长数值 + 10, // 相似度越小 匹配度越大 false ); diff --git a/src_code/README.md b/src_code/README.md index 10f727a..767763d 100644 --- a/src_code/README.md +++ b/src_code/README.md @@ -353,6 +353,8 @@ public class MAIN1 { colorMatrix2, // 需要被计算的颜色通道 ColorMatrix._G_, + // 卷积时的步长数值 + 10, // 相似度越小 匹配度越大 false ); diff --git a/src_code/src/main/java/zhao/algorithmMagic/MAIN1.java b/src_code/src/main/java/zhao/algorithmMagic/MAIN1.java index 25f4b3f..0a3a8df 100644 --- a/src_code/src/main/java/zhao/algorithmMagic/MAIN1.java +++ b/src_code/src/main/java/zhao/algorithmMagic/MAIN1.java @@ -4,23 +4,35 @@ import zhao.algorithmMagic.operands.coordinate.IntegerCoordinateTwo; import zhao.algorithmMagic.operands.matrix.ColorMatrix; +import java.awt.*; + + public class MAIN1 { - public static void main(String[] args) { + public static void main(String[] args) throws CloneNotSupportedException { ColorMatrix colorMatrix1, colorMatrix2; - // 将图像与样本读取进来 - colorMatrix1 = ColorMatrix.parse("C:\\Users\\zhao\\Desktop\\fsdownload\\test3.bmp"); - colorMatrix2 = ColorMatrix.parse("C:\\Users\\zhao\\Desktop\\fsdownload\\test3YB.bmp"); - // 使用模板匹配 获取到 colorMat1 中 与 colorMat2 最相近的子矩阵 - IntegerCoordinateTwo topLeft = colorMatrix1.templateMatching( - // 相似度计算组件 - ManhattanDistance.getInstance("MAN"), - // 模板图像 - colorMatrix2, - // 需要被计算的颜色通道 - ColorMatrix._G_, - // 相似度越小 匹配度越大 - false - ); - System.out.println(topLeft); + { // 将图像与样本读取进来 + colorMatrix1 = ColorMatrix.parse("C:\\Users\\zhao\\Desktop\\fsdownload\\YB.bmp"); + colorMatrix2 = ColorMatrix.parse("C:\\Users\\zhao\\Desktop\\fsdownload\\test22.jpg"); + ColorMatrix temp = ColorMatrix.parse(colorMatrix2.copyToNewArrays()); + // 开始二值化 + colorMatrix1.localBinary(ColorMatrix._G_, 10, 0xffffff, 0, 1); + temp.localBinary(ColorMatrix._G_, 5, 0xffffff, 0, 0); + // 开始进行模板匹配 + IntegerCoordinateTwo man = temp.templateMatching( + ManhattanDistance.getInstance("MAN"), + colorMatrix1, + ColorMatrix._G_, + 10, + false + ); + // 开始进行绘制 + colorMatrix2.drawRectangle( + man, + new IntegerCoordinateTwo(man.getX() + colorMatrix1.getColCount(), man.getY() + colorMatrix1.getRowCount()), + Color.MAGENTA + ); + } + colorMatrix1.show("人脸样本"); + colorMatrix2.show("识别结果"); } } \ No newline at end of file diff --git a/src_code/src/main/java/zhao/algorithmMagic/operands/matrix/ColorMatrix.java b/src_code/src/main/java/zhao/algorithmMagic/operands/matrix/ColorMatrix.java index 5f8aa28..18c0bb2 100644 --- a/src_code/src/main/java/zhao/algorithmMagic/operands/matrix/ColorMatrix.java +++ b/src_code/src/main/java/zhao/algorithmMagic/operands/matrix/ColorMatrix.java @@ -1476,10 +1476,11 @@ public void drawRectangle(IntegerCoordinateTwo start, IntegerCoordinateTwo end, * @param distanceAlgorithm 需要使用的度量计算组件。 * @param template 在进行模板匹配时的被匹配模板。 * @param channel 在进行计算时,需要被计算的颜色通道。 + * @param sep 图像卷积步长数值。该数值增加,会增强计算的速度与性能,该数值的减小,会增强计算的精确度。 * @param isMAX 如果设置为 true 代表取相似度系数最大的子图像坐标作为。 * @return 经过计算之后,与当前矩阵对象最相似的子图像的左上角坐标数值对象。 */ - public final IntegerCoordinateTwo templateMatching(DistanceAlgorithm distanceAlgorithm, ColorMatrix template, int channel, boolean isMAX) { + public final IntegerCoordinateTwo templateMatching(DistanceAlgorithm distanceAlgorithm, ColorMatrix template, int channel, int sep, boolean isMAX) { // 开始匹配 IntegerMatrix channel1 = template.getChannel(channel); // 首先提取出矩阵1中相同大小的子矩阵图像 @@ -1496,7 +1497,7 @@ public final IntegerCoordinateTwo templateMatching(DistanceAlgorithm distanceAlg if (rowCount1 == rowCount2) { // 这种情况代表可以使用优化方案 int ye = rowCount2 - 1; - for (int y = 0; y < rowCount1; y++, ye++) { + for (int y = 0; y < rowCount1; y+=sep, ye+=sep) { double trueDistance = distanceAlgorithm.getTrueDistance(this.extractImageSrc(y, ye).getChannel(channel), channel1); if (isMAX) { if (value < trueDistance) { @@ -1513,9 +1514,9 @@ public final IntegerCoordinateTwo templateMatching(DistanceAlgorithm distanceAlg return new IntegerCoordinateTwo(xy[0], xy[1]); } int ye = rowCount2 - 1; - for (int y = 0; ye < rowCount1; y++, ye++) { + for (int y = 0; ye < rowCount1; y+=sep, ye+=sep) { int xe = colCount2 - 1; - for (int x = 0; xe < colCount1; x++, xe++) { + for (int x = 0; xe < colCount1; x+=sep, xe+=sep) { // 计算出相似度 double trueDistance = distanceAlgorithm.getTrueDistance(this.extractImage(x, y, xe, ye).getChannel(channel), channel1); if (isMAX) { From dc49d7ac6cb185e53ba47d48d8ffa61185a64bde Mon Sep 17 00:00:00 2001 From: LingYuZhao Date: Sun, 2 Apr 2023 07:31:13 +0800 Subject: [PATCH 07/17] update --- src_code/Case.md | 110 ++++++++++++++---- src_code/README-Chinese.md | 9 +- src_code/README.md | 9 +- .../main/java/zhao/algorithmMagic/MAIN1.java | 39 +++++-- .../operands/matrix/ColorMatrix.java | 86 ++++++++++++-- .../operands/vector/SparkVector.scala | 16 +-- 6 files changed, 207 insertions(+), 62 deletions(-) diff --git a/src_code/Case.md b/src_code/Case.md index 0eee52b..96ff885 100644 --- a/src_code/Case.md +++ b/src_code/Case.md @@ -112,6 +112,8 @@ public class MAIN1 { ### 图像模板匹配 +- 人脸识别 通过人脸模板匹配曼哈顿相似系数 + ```java package zhao.algorithmMagic; @@ -120,33 +122,95 @@ import zhao.algorithmMagic.operands.coordinate.IntegerCoordinateTwo; import zhao.algorithmMagic.operands.matrix.ColorMatrix; import java.awt.*; +import java.util.Map; + public class MAIN1 { public static void main(String[] args) { ColorMatrix colorMatrix1, colorMatrix2; - // 将图像与样本读取进来 - colorMatrix1 = ColorMatrix.parse("C:\\Users\\zhao\\Desktop\\fsdownload\\test3.bmp"); - colorMatrix2 = ColorMatrix.parse("C:\\Users\\zhao\\Desktop\\fsdownload\\test3YB.bmp"); - // 使用模板匹配 获取到 colorMat1 中 与 colorMat2 最相近的子矩阵 - IntegerCoordinateTwo topLeft = colorMatrix1.templateMatching( - // 相似度计算组件 - ManhattanDistance.getInstance("MAN"), - // 模板图像 - colorMatrix2, - // 需要被计算的颜色通道 - ColorMatrix._G_, - // 卷积时的步长数值 - 10, - // 相似度越小 匹配度越大 - false - ); - // 由左上角坐标计算出右下坐标 - IntegerCoordinateTwo bottomRight = new IntegerCoordinateTwo( - topLeft.getX() + colorMatrix2.getColCount(), topLeft.getY() + colorMatrix2.getRowCount() - ); - // 将图像矩阵绘制到原矩阵中,并查看结果 - colorMatrix1.drawRectangle(topLeft, bottomRight, Color.MAGENTA); - colorMatrix1.show("res"); + { // 将图像与样本读取进来 + colorMatrix1 = ColorMatrix.parse("C:\\Users\\zhao\\Desktop\\fsdownload\\YB.bmp"); + colorMatrix2 = ColorMatrix.parse("C:\\Users\\zhao\\Desktop\\fsdownload\\test22.jpg"); + ColorMatrix temp = ColorMatrix.parse(colorMatrix2.copyToNewArrays()); + // 开始二值化 + colorMatrix1.localBinary(ColorMatrix._G_, 10, 0xffffff, 0, 1); + temp.localBinary(ColorMatrix._G_, 5, 0xffffff, 0, 0); + // 开始进行模板匹配 并返回最匹配的结果数值,在这里返回的就是所有匹配的结果数据,key为匹配系数 value为匹配结果 + Map.Entry matching = temp.templateMatching( + ManhattanDistance.getInstance("MAN"), + colorMatrix1, + ColorMatrix._G_, + 10, + false + ); + // 开始进行绘制 在这里首先获取到坐标数据 + IntegerCoordinateTwo coordinateTwo = matching.getValue(); + System.out.print("匹配系数 = "); + System.out.println(matching.getKey()); + colorMatrix2.drawRectangle( + coordinateTwo, + new IntegerCoordinateTwo(coordinateTwo.getX() + colorMatrix1.getColCount(), coordinateTwo.getY() + colorMatrix1.getRowCount()), + Color.MAGENTA + ); + } + colorMatrix1.show("人脸样本"); + colorMatrix2.show("识别结果"); + } +} +``` + +```java +package zhao.algorithmMagic; + +import zhao.algorithmMagic.algorithm.distanceAlgorithm.ManhattanDistance; +import zhao.algorithmMagic.operands.coordinate.IntegerCoordinateTwo; +import zhao.algorithmMagic.operands.matrix.ColorMatrix; + +import java.awt.*; +import java.util.ArrayList; +import java.util.Map; + +public class MAIN1 { + public static void main(String[] args) throws CloneNotSupportedException { + ColorMatrix colorMatrix1, colorMatrix2; + { // 将图像与样本读取进来 + colorMatrix1 = ColorMatrix.parse("C:\\Users\\zhao\\Desktop\\fsdownload\\YB.bmp"); + colorMatrix2 = ColorMatrix.parse("C:\\Users\\zhao\\Desktop\\fsdownload\\test22.jpg"); + ColorMatrix temp = ColorMatrix.parse(colorMatrix2.copyToNewArrays()); + // 开始二值化 + colorMatrix1.localBinary(ColorMatrix._G_, 10, 0xffffff, 0, 1); + temp.localBinary(ColorMatrix._G_, 5, 0xffffff, 0, 15); + temp.erode(2, 2, false); + temp.show("temp"); + // 开始进行模板匹配 并返回最匹配的结果数值,在这里返回的就是所有匹配的结果数据 + // 其中每一个元素都是一个匹配到的符合条件的结果信息对象 key为匹配系数 value为匹配结果 + ArrayList> entries = temp.templateMatching( + // 计算相似度需要使用的计算组件 + ManhattanDistance.getInstance("MAN"), + // 需要被计算的矩阵对象 + colorMatrix1, + // 计算时需要使用的颜色通道 + ColorMatrix._G_, + // 计算时需要使用的卷积步长 + 16, + // 相似度阈值设定条件 + v -> v < 1200000 + ); + // 将所有的边框绘制到原图中 + for (Map.Entry matching : entries) { + // 开始进行绘制 在这里首先获取到坐标数据 + IntegerCoordinateTwo coordinateTwo = matching.getValue(); + System.out.print("匹配系数 = "); + System.out.println(matching.getKey()); + colorMatrix2.drawRectangle( + coordinateTwo, + new IntegerCoordinateTwo(coordinateTwo.getX() + colorMatrix1.getColCount(), coordinateTwo.getY() + colorMatrix1.getRowCount()), + Color.MAGENTA + ); + } + } + colorMatrix1.show("人脸样本"); + colorMatrix2.show("识别结果"); } } ``` diff --git a/src_code/README-Chinese.md b/src_code/README-Chinese.md index ba455df..e7eeb07 100644 --- a/src_code/README-Chinese.md +++ b/src_code/README-Chinese.md @@ -326,7 +326,7 @@ import zhao.algorithmMagic.algorithm.distanceAlgorithm.ManhattanDistance; import zhao.algorithmMagic.operands.coordinate.IntegerCoordinateTwo; import zhao.algorithmMagic.operands.matrix.ColorMatrix; -import java.awt.*; +import java.util.Map; public class MAIN1 { public static void main(String[] args) { @@ -334,8 +334,9 @@ public class MAIN1 { // 将图像与样本读取进来 colorMatrix1 = ColorMatrix.parse("C:\\Users\\zhao\\Desktop\\fsdownload\\test3.bmp"); colorMatrix2 = ColorMatrix.parse("C:\\Users\\zhao\\Desktop\\fsdownload\\test3YB.bmp"); - // 使用模板匹配 获取到 colorMat1 中 与 colorMat2 最相近的子矩阵 - IntegerCoordinateTwo topLeft = colorMatrix1.templateMatching( + // 使用模板匹配 获取到 colorMat1 中 与 colorMat2 最相近的子矩阵信息 + // 其中 key 为最相似子矩阵匹配系数 value 为最相似子矩阵左上角坐标 + Map.Entry matching = colorMatrix1.templateMatching( // 相似度计算组件 ManhattanDistance.getInstance("MAN"), // 模板图像 @@ -347,7 +348,7 @@ public class MAIN1 { // 相似度越小 匹配度越大 false ); - System.out.println(topLeft); + System.out.println(matching); } } ``` diff --git a/src_code/README.md b/src_code/README.md index 767763d..e37387d 100644 --- a/src_code/README.md +++ b/src_code/README.md @@ -337,7 +337,7 @@ import zhao.algorithmMagic.algorithm.distanceAlgorithm.ManhattanDistance; import zhao.algorithmMagic.operands.coordinate.IntegerCoordinateTwo; import zhao.algorithmMagic.operands.matrix.ColorMatrix; -import java.awt.*; +import java.util.Map; public class MAIN1 { public static void main(String[] args) { @@ -345,8 +345,9 @@ public class MAIN1 { // 将图像与样本读取进来 colorMatrix1 = ColorMatrix.parse("C:\\Users\\zhao\\Desktop\\fsdownload\\test3.bmp"); colorMatrix2 = ColorMatrix.parse("C:\\Users\\zhao\\Desktop\\fsdownload\\test3YB.bmp"); - // 使用模板匹配 获取到 colorMat1 中 与 colorMat2 最相近的子矩阵 - IntegerCoordinateTwo topLeft = colorMatrix1.templateMatching( + // 使用模板匹配 获取到 colorMat1 中 与 colorMat2 最相近的子矩阵信息 + // 其中 key 为最相似子矩阵匹配系数 value 为最相似子矩阵左上角坐标 + Map.Entry matching = colorMatrix1.templateMatching( // 相似度计算组件 ManhattanDistance.getInstance("MAN"), // 模板图像 @@ -358,7 +359,7 @@ public class MAIN1 { // 相似度越小 匹配度越大 false ); - System.out.println(topLeft); + System.out.println(matching); } } ``` diff --git a/src_code/src/main/java/zhao/algorithmMagic/MAIN1.java b/src_code/src/main/java/zhao/algorithmMagic/MAIN1.java index 0a3a8df..c1fe627 100644 --- a/src_code/src/main/java/zhao/algorithmMagic/MAIN1.java +++ b/src_code/src/main/java/zhao/algorithmMagic/MAIN1.java @@ -5,7 +5,8 @@ import zhao.algorithmMagic.operands.matrix.ColorMatrix; import java.awt.*; - +import java.util.ArrayList; +import java.util.Map; public class MAIN1 { public static void main(String[] args) throws CloneNotSupportedException { @@ -16,21 +17,35 @@ public static void main(String[] args) throws CloneNotSupportedException { ColorMatrix temp = ColorMatrix.parse(colorMatrix2.copyToNewArrays()); // 开始二值化 colorMatrix1.localBinary(ColorMatrix._G_, 10, 0xffffff, 0, 1); - temp.localBinary(ColorMatrix._G_, 5, 0xffffff, 0, 0); - // 开始进行模板匹配 - IntegerCoordinateTwo man = temp.templateMatching( + temp.localBinary(ColorMatrix._G_, 5, 0xffffff, 0, 15); + temp.erode(2, 2, false); + temp.show("temp"); + // 开始进行模板匹配 并返回最匹配的结果数值,在这里返回的就是所有匹配的结果数据 + // 其中每一个元素都是一个匹配到的符合条件的结果信息对象 key为匹配系数 value为匹配结果 + ArrayList> entries = temp.templateMatching( + // 计算相似度需要使用的计算组件 ManhattanDistance.getInstance("MAN"), + // 需要被计算的矩阵对象 colorMatrix1, + // 计算时需要使用的颜色通道 ColorMatrix._G_, - 10, - false - ); - // 开始进行绘制 - colorMatrix2.drawRectangle( - man, - new IntegerCoordinateTwo(man.getX() + colorMatrix1.getColCount(), man.getY() + colorMatrix1.getRowCount()), - Color.MAGENTA + // 计算时需要使用的卷积步长 + 16, + // 相似度阈值设定条件 + v -> v < 1200000 ); + // 将所有的边框绘制到原图中 + for (Map.Entry matching : entries) { + // 开始进行绘制 在这里首先获取到坐标数据 + IntegerCoordinateTwo coordinateTwo = matching.getValue(); + System.out.print("匹配系数 = "); + System.out.println(matching.getKey()); + colorMatrix2.drawRectangle( + coordinateTwo, + new IntegerCoordinateTwo(coordinateTwo.getX() + colorMatrix1.getColCount(), coordinateTwo.getY() + colorMatrix1.getRowCount()), + Color.MAGENTA + ); + } } colorMatrix1.show("人脸样本"); colorMatrix2.show("识别结果"); diff --git a/src_code/src/main/java/zhao/algorithmMagic/operands/matrix/ColorMatrix.java b/src_code/src/main/java/zhao/algorithmMagic/operands/matrix/ColorMatrix.java index 18c0bb2..8cb02b7 100644 --- a/src_code/src/main/java/zhao/algorithmMagic/operands/matrix/ColorMatrix.java +++ b/src_code/src/main/java/zhao/algorithmMagic/operands/matrix/ColorMatrix.java @@ -8,6 +8,7 @@ import zhao.algorithmMagic.utils.ASClass; import zhao.algorithmMagic.utils.ASIO; import zhao.algorithmMagic.utils.ASMath; +import zhao.algorithmMagic.utils.filter.DoubleFiltering; import zhao.algorithmMagic.utils.transformation.ManyTrans; import javax.swing.*; @@ -15,8 +16,7 @@ import java.io.File; import java.io.IOException; import java.net.URL; -import java.util.Arrays; -import java.util.Iterator; +import java.util.*; import java.util.function.Consumer; /** @@ -1474,15 +1474,26 @@ public void drawRectangle(IntegerCoordinateTwo start, IntegerCoordinateTwo end, * 将图像进行模板匹配计算 * * @param distanceAlgorithm 需要使用的度量计算组件。 + *

+ * The metric calculation component that needs to be used. * @param template 在进行模板匹配时的被匹配模板。 + *

+ * The template to be matched during template matching. * @param channel 在进行计算时,需要被计算的颜色通道。 - * @param sep 图像卷积步长数值。该数值增加,会增强计算的速度与性能,该数值的减小,会增强计算的精确度。 + *

+ * When calculating, the color channel to be calculated is required. + * @param sep 图像卷积步长数值。该数值增加,会增强计算的速度与性能,该数值的减小,会增强计算的精确度。 + *

+ * Image convolution step size value. An increase in this value will enhance the speed and performance of the calculation, while a decrease in this value will enhance the accuracy of the calculation. * @param isMAX 如果设置为 true 代表取相似度系数最大的子图像坐标作为。 + *

+ * If set to true, the coordinate of the sub image with the largest similarity coefficient is taken as the coordinate. * @return 经过计算之后,与当前矩阵对象最相似的子图像的左上角坐标数值对象。 */ - public final IntegerCoordinateTwo templateMatching(DistanceAlgorithm distanceAlgorithm, ColorMatrix template, int channel, int sep, boolean isMAX) { + public final Map.Entry templateMatching(DistanceAlgorithm distanceAlgorithm, ColorMatrix template, int channel, int sep, boolean isMAX) { // 开始匹配 IntegerMatrix channel1 = template.getChannel(channel); + IntegerMatrix channel2 = this.getChannel(channel); // 首先提取出矩阵1中相同大小的子矩阵图像 int rowCount1 = this.getRowCount(); int colCount1 = this.getColCount(); @@ -1497,8 +1508,8 @@ public final IntegerCoordinateTwo templateMatching(DistanceAlgorithm distanceAlg if (rowCount1 == rowCount2) { // 这种情况代表可以使用优化方案 int ye = rowCount2 - 1; - for (int y = 0; y < rowCount1; y+=sep, ye+=sep) { - double trueDistance = distanceAlgorithm.getTrueDistance(this.extractImageSrc(y, ye).getChannel(channel), channel1); + for (int y = 0; y < rowCount1; y += sep, ye += sep) { + double trueDistance = distanceAlgorithm.getTrueDistance(channel2.extractSrcMat(y, ye), channel1); if (isMAX) { if (value < trueDistance) { value = trueDistance; @@ -1511,14 +1522,14 @@ public final IntegerCoordinateTwo templateMatching(DistanceAlgorithm distanceAlg } } } - return new IntegerCoordinateTwo(xy[0], xy[1]); + return new AbstractMap.SimpleEntry<>(value, new IntegerCoordinateTwo(xy[0], xy[1])); } int ye = rowCount2 - 1; - for (int y = 0; ye < rowCount1; y+=sep, ye+=sep) { + for (int y = 0; ye < rowCount1; y += sep, ye += sep) { int xe = colCount2 - 1; - for (int x = 0; xe < colCount1; x+=sep, xe+=sep) { + for (int x = 0; xe < colCount1; x += sep, xe += sep) { // 计算出相似度 - double trueDistance = distanceAlgorithm.getTrueDistance(this.extractImage(x, y, xe, ye).getChannel(channel), channel1); + double trueDistance = distanceAlgorithm.getTrueDistance(channel2.extractMat(x, y, xe, ye), channel1); if (isMAX) { if (value < trueDistance) { value = trueDistance; @@ -1534,7 +1545,60 @@ public final IntegerCoordinateTwo templateMatching(DistanceAlgorithm distanceAlg } } } - return new IntegerCoordinateTwo(xy[0], xy[1]); + return new AbstractMap.SimpleEntry<>(value, new IntegerCoordinateTwo(xy[0], xy[1])); + } + + /** + * @param distanceAlgorithm 需要使用的度量计算组件。 + *

+ * The metric calculation component that needs to be used. + * @param template 在进行模板匹配时的被匹配模板。 + *

+ * The template to be matched during template matching. + * @param channel 在进行计算时,需要被计算的颜色通道。 + *

+ * When calculating, the color channel to be calculated is required. + * @param sep 图像卷积步长数值。该数值增加,会增强计算的速度与性能,该数值的减小,会增强计算的精确度。 + *

+ * Image convolution step size value. An increase in this value will enhance the speed and performance of the calculation, while a decrease in this value will enhance the accuracy of the calculation. + * @param cFilter 相似系数过滤逻辑实现,在这里您将可以自定义的书写相似度系数的过滤操作,符合的系数对应的信息将会被添加到结果数据中。 + * @return 图像中所有符合过滤条件的子矩阵信息数据对象。 + *

+ * All sub matrix information data objects in the image that meet the filtering conditions. + */ + public final ArrayList> templateMatching(DistanceAlgorithm distanceAlgorithm, ColorMatrix template, int channel, int sep, DoubleFiltering cFilter) { + // 开始匹配 + IntegerMatrix channel1 = template.getChannel(channel); + IntegerMatrix channel2 = this.getChannel(channel); + // 首先提取出矩阵1中相同大小的子矩阵图像 + int rowCount1 = this.getRowCount(), colCount1 = this.getColCount(); + int rowCount2 = template.getRowCount(), colCount2 = template.getColCount(); + if (rowCount1 < rowCount2 || colCount1 < colCount2) { + throw new OperatorOperationException("您的模板矩阵尺寸过大!!"); + } + ArrayList> res = new ArrayList<>(); + if (rowCount1 == rowCount2) { + // 这种情况代表可以使用优化方案 + int ye = rowCount2 - 1; + for (int y = 0; y < rowCount1; y += sep, ye += sep) { + double trueDistance = distanceAlgorithm.getTrueDistance(channel2.extractSrcMat(y, ye), channel1); + if (cFilter.isComplianceEvents(trueDistance)) + res.add(new AbstractMap.SimpleEntry<>(trueDistance, new IntegerCoordinateTwo(0, y))); + } + return res; + } + // 这里就代表不能够使用优化方案 + int ye = rowCount2 - 1; + for (int y = 0; ye < rowCount1; y += sep, ye += sep) { + int xe = colCount2 - 1; + for (int x = 0; xe < colCount1; x += sep, xe += sep) { + // 计算出相似度 + double trueDistance = distanceAlgorithm.getTrueDistance(channel2.extractMat(x, y, xe, ye), channel1); + if (cFilter.isComplianceEvents(trueDistance)) + res.add(new AbstractMap.SimpleEntry<>(trueDistance, new IntegerCoordinateTwo(x, y))); + } + } + return res; } /** diff --git a/src_code/src/main/java/zhao/algorithmMagic/operands/vector/SparkVector.scala b/src_code/src/main/java/zhao/algorithmMagic/operands/vector/SparkVector.scala index 59c6761..44216ac 100644 --- a/src_code/src/main/java/zhao/algorithmMagic/operands/vector/SparkVector.scala +++ b/src_code/src/main/java/zhao/algorithmMagic/operands/vector/SparkVector.scala @@ -53,6 +53,14 @@ final class SparkVector(sparkContext: SparkContext, vector: org.apache.spark.mll else throw new OperatorOperationException("'DoubleVector1 multiply DoubleVector2' 时,两个'DoubleVector'的向量所包含的数量不同,DoubleVector1=[" + length1 + "],DoubleVector2=[" + length2 + "]\n" + "When 'DoubleVector1 multiply DoubleVector2', the two vectors of 'DoubleVector' contain different quantities, DoubleVector1=[" + length1 + "], DoubleVector2=[" + length2 + "]") } + /** + * + * @return 将本对象中存储的向量序列数组拷贝到一个新数组并将新数组返回,这里返回的是一个新数组,支持修改等操作。 + * + * Copy the vector sequence array stored in this object to a new array and return the new array. Here, a new array is returned, which supports modification and other operations. + */ + override def copyToNewArray(): Array[Double] = vector.toArray + /** * 计算两个向量的内积,也称之为数量积,具体实现请参阅api说明 *

@@ -77,14 +85,6 @@ final class SparkVector(sparkContext: SparkContext, vector: org.apache.spark.mll else throw new OperatorOperationException("'DoubleVector1 innerProduct DoubleVector2' 时,两个'DoubleVector'的向量所包含的数量不同,DoubleVector1=[" + doubles1.length + "],DoubleVector2=[" + doubles2.length + "]\n" + "When 'DoubleVector1 innerProduct DoubleVector2', the two vectors of 'DoubleVector' contain different quantities, DoubleVector1=[" + doubles1.length + "], DoubleVector2=[" + doubles2.length + "]") } - /** - * - * @return 将本对象中存储的向量序列数组拷贝到一个新数组并将新数组返回,这里返回的是一个新数组,支持修改等操作。 - * - * Copy the vector sequence array stored in this object to a new array and return the new array. Here, a new array is returned, which supports modification and other operations. - */ - override def copyToNewArray(): Array[Double] = vector.toArray - /** * @return 该类的实现类对象,用于拓展该接口的子类 */ From 44616c63e3bf7903b6b0536801b91513695cecd2 Mon Sep 17 00:00:00 2001 From: LingYuZhao Date: Mon, 3 Apr 2023 15:18:29 +0800 Subject: [PATCH 08/17] update MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * 矩阵开始被归一化计算组件支持 --- src_code/Case.md | 73 ++++++++- src_code/README-Chinese.md | 35 +++++ src_code/README.md | 36 +++++ .../main/java/zhao/algorithmMagic/MAIN1.java | 80 +++++----- .../normalization/DataStandardization.java | 26 ++++ .../normalization/LinearNormalization.java | 146 ++++++++++++++++-- .../normalization/Z_ScoreNormalization.java | 32 ++++ .../algorithmMagic/core/AlgorithmStar.java | 28 ++++ .../operands/vector/SparkVector.scala | 30 ++-- .../zhao/algorithmMagic/utils/ASMath.java | 37 +++++ 10 files changed, 460 insertions(+), 63 deletions(-) diff --git a/src_code/Case.md b/src_code/Case.md index 96ff885..8a703bf 100644 --- a/src_code/Case.md +++ b/src_code/Case.md @@ -171,7 +171,7 @@ import java.util.ArrayList; import java.util.Map; public class MAIN1 { - public static void main(String[] args) throws CloneNotSupportedException { + public static void main(String[] args) { ColorMatrix colorMatrix1, colorMatrix2; { // 将图像与样本读取进来 colorMatrix1 = ColorMatrix.parse("C:\\Users\\zhao\\Desktop\\fsdownload\\YB.bmp"); @@ -215,6 +215,77 @@ public class MAIN1 { } ``` +### 指定文字识别 + +这里展示的是通过模板匹配操作,圈出图像中的指定数字的字符,图像数据集与图像的处理代码如下所示。 + +- 图像数据集 + 这里有所有数字的图像数据以及被处理的图像。 + +- 图像识别代码,通过下面的代码实现了图像中数字 4 的圈出操作。 + +```java +package zhao.algorithmMagic; + +import zhao.algorithmMagic.algorithm.distanceAlgorithm.ManhattanDistance; +import zhao.algorithmMagic.operands.coordinate.IntegerCoordinateTwo; +import zhao.algorithmMagic.operands.matrix.ColorMatrix; + +import java.awt.*; +import java.io.File; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.Map; +import java.util.Objects; + +public class MAIN1 { + public static void main(String[] args) { + HashMap hashMap = new HashMap<>(0b1100); + // 将样本读取进来 + { + int count = 0; + for (File file : Objects.requireNonNull(new File("C:\\Users\\Liming\\Desktop\\fsdownload\\number").listFiles())) { + ColorMatrix parse = ColorMatrix.parse(file.getPath()); + parse.globalBinary(ColorMatrix._G_, 200, 0xffffff, 0); + hashMap.put(++count, parse); + } + } + // 将需要被进行数字提取的图像获取到 + ColorMatrix parse = ColorMatrix.parse("C:\\Users\\Liming\\Desktop\\fsdownload\\test4.bmp"); + // 将原矩阵复制一份,稍后用于结果展示 + ColorMatrix parse1 = ColorMatrix.parse(parse.copyToNewArrays()); + // 将需要被处理的图像进行二值化,使其颜色不会干扰匹配 + parse.localBinary(ColorMatrix._G_, 4, 0xffffff, 0, -15); + parse.show("二值化结果"); + // 在这里获取到 4 数值对应的样本 + ColorMatrix temp = hashMap.get(4); + temp.show("模板数据"); + // 开始进行模板匹配操作,该操作将会返回所有度量系数小于 3500 的子图像左上角坐标 + ArrayList> matching = parse.templateMatching( + ManhattanDistance.getInstance("MAN"), + temp, + ColorMatrix._G_, + 1, + v -> { + System.out.println(v); + return v < 3500; + } + ); + // 将所有的轮廓绘制到用于展示的矩阵对象上。 + for (Map.Entry doubleIntegerCoordinateTwoEntry : matching) { + IntegerCoordinateTwo value = doubleIntegerCoordinateTwoEntry.getValue(); + parse1.drawRectangle( + value, + new IntegerCoordinateTwo(value.getX() + temp.getColCount() - 1, value.getY() + temp.getRowCount() - 1), + Color.MAGENTA + ); + } + // 查看结果 + parse1.show("res"); + } +} +``` + ## 数据计算类案例 ## 机器学习类案例 diff --git a/src_code/README-Chinese.md b/src_code/README-Chinese.md index e7eeb07..2c36a1f 100644 --- a/src_code/README-Chinese.md +++ b/src_code/README-Chinese.md @@ -353,4 +353,39 @@ public class MAIN1 { } ``` +* 支持矩阵的归一化操作,使得归一化计算能够用于更加广泛的对象。 + +```java +package zhao.algorithmMagic; + +import zhao.algorithmMagic.algorithm.normalization.LinearNormalization; +import zhao.algorithmMagic.core.AlgorithmStar; +import zhao.algorithmMagic.operands.matrix.DoubleMatrix; +import zhao.algorithmMagic.operands.matrix.IntegerMatrix; + +public class MAIN1 { + public static void main(String[] args) { + IntegerMatrix integerMatrix = IntegerMatrix.parse( + new int[]{1, 2, 3, 4, 5}, + new int[]{5, 4, 3, 4, 5}, + new int[]{5, 4, 3, 2, 1} + ); + // 归一化计算组件 + LinearNormalization line = LinearNormalization.getInstance("line").setMax(3).setMin(-3); + AlgorithmStar algorithmStar = AlgorithmStar.getInstance(); + // 开始进行归一化操作 + IntegerMatrix pretreatment1 = algorithmStar.pretreatment( + line, + // 需要被归一化的矩阵对象 + integerMatrix + ); + System.out.print(pretreatment1); + + DoubleMatrix doubleMatrix = DoubleMatrix.parse(integerMatrix); + DoubleMatrix pretreatment2 = algorithmStar.pretreatment(line, doubleMatrix); + System.out.print(pretreatment2); + } +} +``` + ### Version update date : xx xx-xx-xx \ No newline at end of file diff --git a/src_code/README.md b/src_code/README.md index e37387d..fcb36ca 100644 --- a/src_code/README.md +++ b/src_code/README.md @@ -364,4 +364,40 @@ public class MAIN1 { } ``` +* Support the normalization operation of matrices, making the normalization calculation applicable to a wider range of + objects. + +```java +package zhao.algorithmMagic; + +import zhao.algorithmMagic.algorithm.normalization.LinearNormalization; +import zhao.algorithmMagic.core.AlgorithmStar; +import zhao.algorithmMagic.operands.matrix.DoubleMatrix; +import zhao.algorithmMagic.operands.matrix.IntegerMatrix; + +public class MAIN1 { + public static void main(String[] args) { + IntegerMatrix integerMatrix = IntegerMatrix.parse( + new int[]{1, 2, 3, 4, 5}, + new int[]{5, 4, 3, 4, 5}, + new int[]{5, 4, 3, 2, 1} + ); + // 归一化计算组件 + LinearNormalization line = LinearNormalization.getInstance("line").setMax(3).setMin(-3); + AlgorithmStar algorithmStar = AlgorithmStar.getInstance(); + // 开始进行归一化操作 + IntegerMatrix pretreatment1 = algorithmStar.pretreatment( + line, + // 需要被归一化的矩阵对象 + integerMatrix + ); + System.out.print(pretreatment1); + + DoubleMatrix doubleMatrix = DoubleMatrix.parse(integerMatrix); + DoubleMatrix pretreatment2 = algorithmStar.pretreatment(line, doubleMatrix); + System.out.print(pretreatment2); + } +} +``` + ### Version update date : xx xx-xx-xx \ No newline at end of file diff --git a/src_code/src/main/java/zhao/algorithmMagic/MAIN1.java b/src_code/src/main/java/zhao/algorithmMagic/MAIN1.java index c1fe627..6024f80 100644 --- a/src_code/src/main/java/zhao/algorithmMagic/MAIN1.java +++ b/src_code/src/main/java/zhao/algorithmMagic/MAIN1.java @@ -5,49 +5,55 @@ import zhao.algorithmMagic.operands.matrix.ColorMatrix; import java.awt.*; +import java.io.File; import java.util.ArrayList; +import java.util.HashMap; import java.util.Map; +import java.util.Objects; public class MAIN1 { public static void main(String[] args) throws CloneNotSupportedException { - ColorMatrix colorMatrix1, colorMatrix2; - { // 将图像与样本读取进来 - colorMatrix1 = ColorMatrix.parse("C:\\Users\\zhao\\Desktop\\fsdownload\\YB.bmp"); - colorMatrix2 = ColorMatrix.parse("C:\\Users\\zhao\\Desktop\\fsdownload\\test22.jpg"); - ColorMatrix temp = ColorMatrix.parse(colorMatrix2.copyToNewArrays()); - // 开始二值化 - colorMatrix1.localBinary(ColorMatrix._G_, 10, 0xffffff, 0, 1); - temp.localBinary(ColorMatrix._G_, 5, 0xffffff, 0, 15); - temp.erode(2, 2, false); - temp.show("temp"); - // 开始进行模板匹配 并返回最匹配的结果数值,在这里返回的就是所有匹配的结果数据 - // 其中每一个元素都是一个匹配到的符合条件的结果信息对象 key为匹配系数 value为匹配结果 - ArrayList> entries = temp.templateMatching( - // 计算相似度需要使用的计算组件 - ManhattanDistance.getInstance("MAN"), - // 需要被计算的矩阵对象 - colorMatrix1, - // 计算时需要使用的颜色通道 - ColorMatrix._G_, - // 计算时需要使用的卷积步长 - 16, - // 相似度阈值设定条件 - v -> v < 1200000 - ); - // 将所有的边框绘制到原图中 - for (Map.Entry matching : entries) { - // 开始进行绘制 在这里首先获取到坐标数据 - IntegerCoordinateTwo coordinateTwo = matching.getValue(); - System.out.print("匹配系数 = "); - System.out.println(matching.getKey()); - colorMatrix2.drawRectangle( - coordinateTwo, - new IntegerCoordinateTwo(coordinateTwo.getX() + colorMatrix1.getColCount(), coordinateTwo.getY() + colorMatrix1.getRowCount()), - Color.MAGENTA - ); + HashMap hashMap = new HashMap<>(0b1100); + // 将样本读取进来 + { + int count = 0; + for (File file : Objects.requireNonNull(new File("C:\\Users\\Liming\\Desktop\\fsdownload\\number").listFiles())) { + ColorMatrix parse = ColorMatrix.parse(file.getPath()); + parse.globalBinary(ColorMatrix._G_, 200, 0xffffff, 0); + hashMap.put(++count, parse); } } - colorMatrix1.show("人脸样本"); - colorMatrix2.show("识别结果"); + // 将需要被进行数字提取的图像获取到 + ColorMatrix parse = ColorMatrix.parse("C:\\Users\\Liming\\Desktop\\fsdownload\\test4.bmp"); + // 将原矩阵复制一份,稍后用于结果展示 + ColorMatrix parse1 = ColorMatrix.parse(parse.copyToNewArrays()); + // 将需要被处理的图像进行二值化,使其颜色不会干扰匹配 + parse.localBinary(ColorMatrix._G_, 4, 0xffffff, 0, -15); + parse.show("二值化结果"); + // 在这里获取到 4 数值对应的样本 + ColorMatrix temp = hashMap.get(4); + temp.show("模板数据"); + // 开始进行模板匹配操作,该操作将会返回所有度量系数小于 3500 的子图像左上角坐标 + ArrayList> matching = parse.templateMatching( + ManhattanDistance.getInstance("MAN"), + temp, + ColorMatrix._G_, + 1, + v -> { + System.out.println(v); + return v < 3500; + } + ); + // 将所有的轮廓绘制到用于展示的矩阵对象上。 + for (Map.Entry doubleIntegerCoordinateTwoEntry : matching) { + IntegerCoordinateTwo value = doubleIntegerCoordinateTwoEntry.getValue(); + parse1.drawRectangle( + value, + new IntegerCoordinateTwo(value.getX() + temp.getColCount() - 1, value.getY() + temp.getRowCount() - 1), + Color.MAGENTA + ); + } + // 查看结果 + parse1.show("res"); } } \ No newline at end of file diff --git a/src_code/src/main/java/zhao/algorithmMagic/algorithm/normalization/DataStandardization.java b/src_code/src/main/java/zhao/algorithmMagic/algorithm/normalization/DataStandardization.java index 382a107..611ca70 100644 --- a/src_code/src/main/java/zhao/algorithmMagic/algorithm/normalization/DataStandardization.java +++ b/src_code/src/main/java/zhao/algorithmMagic/algorithm/normalization/DataStandardization.java @@ -8,6 +8,8 @@ import zhao.algorithmMagic.operands.coordinate.FloatingPointCoordinates; import zhao.algorithmMagic.operands.coordinate.IntegerCoordinateMany; import zhao.algorithmMagic.operands.coordinate.IntegerCoordinates; +import zhao.algorithmMagic.operands.matrix.DoubleMatrix; +import zhao.algorithmMagic.operands.matrix.IntegerMatrix; import zhao.algorithmMagic.operands.vector.DoubleVector; import zhao.algorithmMagic.operands.vector.IntegerVector; @@ -93,6 +95,30 @@ public String getAlgorithmName() { */ public abstract IntegerVector pretreatment(IntegerVector integerVector); + /** + * 将一个序列进行标准化,具体的标准化有不同的实现 + * + * @param doubleMatrix 需要被标准化的数值,可以是坐标或向量,更多信息需要查阅实现 + *

+ * The value to be normalized, which can be a coordinate or a vector. For more information, see the implementation + * @return v的标准化样式 + *

+ * Normalized style of v + */ + public abstract DoubleMatrix pretreatment(DoubleMatrix doubleMatrix); + + /** + * 将一个序列进行标准化,具体的标准化有不同的实现 + * + * @param integerMatrix 需要被标准化的数值,可以是坐标或向量,更多信息需要查阅实现 + *

+ * The value to be normalized, which can be a coordinate or a vector. For more information, see the implementation + * @return v的标准化样式 + *

+ * Normalized style of v + */ + public abstract IntegerMatrix pretreatment(IntegerMatrix integerMatrix); + /** * 算法模块的初始化方法,在这里您可以进行组件的初始化方法,当初始化成功之后,该算法就可以处于就绪的状态,一般这里就是将自己添加到算法管理类中 *

diff --git a/src_code/src/main/java/zhao/algorithmMagic/algorithm/normalization/LinearNormalization.java b/src_code/src/main/java/zhao/algorithmMagic/algorithm/normalization/LinearNormalization.java index 2b30f7a..53dbf72 100644 --- a/src_code/src/main/java/zhao/algorithmMagic/algorithm/normalization/LinearNormalization.java +++ b/src_code/src/main/java/zhao/algorithmMagic/algorithm/normalization/LinearNormalization.java @@ -7,6 +7,8 @@ import zhao.algorithmMagic.operands.coordinate.FloatingPointCoordinates; import zhao.algorithmMagic.operands.coordinate.IntegerCoordinateMany; import zhao.algorithmMagic.operands.coordinate.IntegerCoordinates; +import zhao.algorithmMagic.operands.matrix.DoubleMatrix; +import zhao.algorithmMagic.operands.matrix.IntegerMatrix; import zhao.algorithmMagic.operands.vector.DoubleVector; import zhao.algorithmMagic.operands.vector.FastRangeDoubleVector; import zhao.algorithmMagic.operands.vector.FastRangeIntegerVector; @@ -69,13 +71,13 @@ public static LinearNormalization getInstance(String Name) { * @param doubles 需要被标准化的一个序列 * @return 标准化的结果 */ - public static double[] normalization(double[] doubles, double min1, double max1) { + public static double[] pretreatment(double[] doubles, double min1, double max1) { // 计算最大值最小值 double[] doubles1 = ASMath.MaxAndMin(doubles); double max = doubles1[0b0]; double min = doubles1[0b1]; // 开始将序列标准化 - return normalization(doubles, min, max, min1, max1); + return pretreatment(doubles, min, max, min1, max1); } /** @@ -88,7 +90,7 @@ public static double[] normalization(double[] doubles, double min1, double max1) * @param min1 归一化之后的每一个元素的允许的最大值 * @return 归一化之后的新向量数组对象。 */ - public static double[] normalization(double[] doubles, double min, double max, double min1, double max1) { + public static double[] pretreatment(double[] doubles, double min, double max, double min1, double max1) { double[] res = new double[doubles.length]; // 判断是否需要归一化,不需要就原样返回 if (max <= max1 && min >= min1) { @@ -114,12 +116,12 @@ public static double[] normalization(double[] doubles, double min, double max, d * @param ints 需要被标准化的一个序列 * @return 标准化的结果 */ - public static int[] normalization(int[] ints, int min1, int max1) { + public static int[] pretreatment(int[] ints, int min1, int max1) { int[] doubles1 = ASMath.MaxAndMin(ints); int max = doubles1[0b0]; int min = doubles1[0b1]; // 开始将序列标准化 - return normalization(ints, min, max, min1, max1); + return pretreatment(ints, min, max, min1, max1); } /** @@ -132,7 +134,7 @@ public static int[] normalization(int[] ints, int min1, int max1) { * @param min1 归一化之后的每一个元素的允许的最大值 * @return 归一化之后的新向量对象数组 */ - public static int[] normalization(int[] ints, int min, int max, int min1, int max1) { + public static int[] pretreatment(int[] ints, int min, int max, int min1, int max1) { int[] res = new int[ints.length]; // 判断是否需要归一化,不需要就原样返回 if (max <= max1 && min >= min1) { @@ -148,6 +150,100 @@ public static int[] normalization(int[] ints, int min, int max, int min1, int ma return res; } + /** + * 将一个多维序列标准化,标准化手段就是序列v中的每一个数值进行运算:Xn = (Xn - min(x))/ max(x) - min(x) + *

+ * To normalize a multidimensional sequence, the normalization method is to operate on each value in the sequence v: x = (x - min(x)) max(x) - min(x) + * + * @param max1 归一化之后的每一个元素的允许的最小值 + * @param min1 归一化之后的每一个元素的允许的最大值 + * @param ints 需要被标准化的一个序列 + * @return 标准化的结果 + */ + public static int[][] pretreatment(int[][] ints, int min1, int max1) { + int[] doubles1 = ASMath.MaxAndMin(ints); + int max = doubles1[0b0]; + int min = doubles1[0b1]; + // 开始将序列标准化 + return pretreatment(ints, min, max, min1, max1); + } + + /** + * 将一个多维序列归一化,在该函数中可以指定归一化之后的结果区间数值 + * + * @param ints 需要被归一化的序列 + * @param min 向量序列中的最大值 + * @param max 向量序列中的最小值 + * @param max1 归一化之后的每一个元素的允许的最小值 + * @param min1 归一化之后的每一个元素的允许的最大值 + * @return 归一化之后的新向量对象数组 + */ + public static int[][] pretreatment(int[][] ints, int min, int max, int min1, int max1) { + int[][] res1 = new int[ints.length][ints[0].length]; + // 判断是否需要归一化,不需要就原样返回 + if (max <= max1 && min >= min1) { + return ASClass.array2DCopy(ints, res1); + } + // 如果需要就开始归一化 + int MAX_MIN = max - min; + int MAX_MIN1 = max1 - min1; + int y = -1; + for (int[] res : ints) { + int[] ints1 = res1[++y]; + for (int i = 0; i < res.length; i++) { + ints1[i] = (int) (((res[i] - min) / (double) MAX_MIN) * MAX_MIN1 + min1); + } + } + return res1; + } + + + /** + * 将一个多维序列标准化,标准化手段就是序列v中的每一个数值进行运算:Xn = (Xn - min(x))/ max(x) - min(x) + *

+ * To normalize a multidimensional sequence, the normalization method is to operate on each value in the sequence v: x = (x - min(x)) max(x) - min(x) + * + * @param max1 归一化之后的每一个元素的允许的最小值 + * @param min1 归一化之后的每一个元素的允许的最大值 + * @param doubles 需要被标准化的一个序列 + * @return 标准化的结果 + */ + public static double[][] pretreatment(double[][] doubles, double min1, double max1) { + // 计算最大值最小值 + double[] doubles1 = ASMath.MaxAndMin(doubles); + // 开始将序列标准化 + return pretreatment(doubles, doubles1[0b1], doubles1[0b0], min1, max1); + } + + /** + * 将一个多维序列归一化,在该函数中可以指定归一化之后的结果区间数值 + * + * @param ints 需要被归一化的序列 + * @param min 向量序列中的最大值 + * @param max 向量序列中的最小值 + * @param max1 归一化之后的每一个元素的允许的最小值 + * @param min1 归一化之后的每一个元素的允许的最大值 + * @return 归一化之后的新向量对象数组 + */ + public static double[][] pretreatment(double[][] ints, double min, double max, double min1, double max1) { + double[][] res1 = new double[ints.length][ints[0].length]; + // 判断是否需要归一化,不需要就原样返回 + if (max <= max1 && min >= min1) { + return ASClass.array2DCopy(ints, res1); + } + // 如果需要就开始归一化 + double MAX_MIN = max - min; + double MAX_MIN1 = max1 - min1; + int y = -1; + for (double[] res : ints) { + double[] doubles = res1[++y]; + for (int i = 0; i < res.length; i++) { + doubles[i] = ((res[i] - min) / MAX_MIN) * MAX_MIN1 + min1; + } + } + return res1; + } + /** * 设置归一化之后的序列最大值 * @@ -182,7 +278,7 @@ public LinearNormalization setMin(double min) { */ @Override public FloatingPointCoordinates pretreatment(DoubleCoordinateMany v) { - return new DoubleCoordinateMany(normalization(v.toArray(), min, max)); + return new DoubleCoordinateMany(pretreatment(v.toArray(), min, max)); } /** @@ -197,7 +293,7 @@ public FloatingPointCoordinates pretreatment(DoubleCoordin */ @Override public IntegerCoordinates pretreatment(IntegerCoordinateMany v) { - return new IntegerCoordinateMany(normalization(v.toArray(), (int) min, (int) max)); + return new IntegerCoordinateMany(pretreatment(v.toArray(), (int) min, (int) max)); } /** @@ -210,7 +306,7 @@ public IntegerCoordinates pretreatment(IntegerCoordinateM */ @Override public DoubleVector pretreatment(DoubleVector doubleVector) { - return new DoubleVector(normalization(doubleVector.toArray(), min, max)); + return new DoubleVector(pretreatment(doubleVector.toArray(), min, max)); } /** @@ -223,7 +319,37 @@ public DoubleVector pretreatment(DoubleVector doubleVector) { */ @Override public IntegerVector pretreatment(IntegerVector integerVector) { - return new IntegerVector(normalization(integerVector.toArray(), (int) min, (int) max)); + return new IntegerVector(pretreatment(integerVector.toArray(), (int) min, (int) max)); + } + + /** + * 将一个序列进行标准化,具体的标准化有不同的实现 + * + * @param doubleMatrix 需要被标准化的数值,可以是坐标或向量,更多信息需要查阅实现 + *

+ * The value to be normalized, which can be a coordinate or a vector. For more information, see the implementation + * @return v的标准化样式 + *

+ * Normalized style of v + */ + @Override + public DoubleMatrix pretreatment(DoubleMatrix doubleMatrix) { + return DoubleMatrix.parse(pretreatment(doubleMatrix.toArrays(), min, max)); + } + + /** + * 将一个序列进行标准化,具体的标准化有不同的实现 + * + * @param integerMatrix 需要被标准化的数值,可以是坐标或向量,更多信息需要查阅实现 + *

+ * The value to be normalized, which can be a coordinate or a vector. For more information, see the implementation + * @return v的标准化样式 + *

+ * Normalized style of v + */ + @Override + public IntegerMatrix pretreatment(IntegerMatrix integerMatrix) { + return IntegerMatrix.parse(pretreatment(integerMatrix.toArrays(), (int) min, (int) max)); } /** diff --git a/src_code/src/main/java/zhao/algorithmMagic/algorithm/normalization/Z_ScoreNormalization.java b/src_code/src/main/java/zhao/algorithmMagic/algorithm/normalization/Z_ScoreNormalization.java index 9675972..1a8f4d3 100644 --- a/src_code/src/main/java/zhao/algorithmMagic/algorithm/normalization/Z_ScoreNormalization.java +++ b/src_code/src/main/java/zhao/algorithmMagic/algorithm/normalization/Z_ScoreNormalization.java @@ -8,6 +8,8 @@ import zhao.algorithmMagic.operands.coordinate.FloatingPointCoordinates; import zhao.algorithmMagic.operands.coordinate.IntegerCoordinateMany; import zhao.algorithmMagic.operands.coordinate.IntegerCoordinates; +import zhao.algorithmMagic.operands.matrix.DoubleMatrix; +import zhao.algorithmMagic.operands.matrix.IntegerMatrix; import zhao.algorithmMagic.operands.vector.DoubleVector; import zhao.algorithmMagic.operands.vector.FastRangeDoubleVector; import zhao.algorithmMagic.operands.vector.FastRangeIntegerVector; @@ -171,6 +173,36 @@ public IntegerVector pretreatment(IntegerVector integerVector) { return new IntegerVector(StandardizedSequence(integerVector.toArray())); } + /** + * 将一个序列进行标准化,具体的标准化有不同的实现 + * + * @param doubleMatrix 需要被标准化的数值,可以是坐标或向量,更多信息需要查阅实现 + *

+ * The value to be normalized, which can be a coordinate or a vector. For more information, see the implementation + * @return v的标准化样式 + *

+ * Normalized style of v + */ + @Override + public DoubleMatrix pretreatment(DoubleMatrix doubleMatrix) { + throw new UnsupportedOperationException("The matrix currently does not support standardization operations."); + } + + /** + * 将一个序列进行标准化,具体的标准化有不同的实现 + * + * @param integerMatrix 需要被标准化的数值,可以是坐标或向量,更多信息需要查阅实现 + *

+ * The value to be normalized, which can be a coordinate or a vector. For more information, see the implementation + * @return v的标准化样式 + *

+ * Normalized style of v + */ + @Override + public IntegerMatrix pretreatment(IntegerMatrix integerMatrix) { + throw new UnsupportedOperationException("The matrix currently does not support standardization operations."); + } + /** * 将一个序列进行标准化,具体的标准化有不同的实现 * diff --git a/src_code/src/main/java/zhao/algorithmMagic/core/AlgorithmStar.java b/src_code/src/main/java/zhao/algorithmMagic/core/AlgorithmStar.java index c414861..969c171 100644 --- a/src_code/src/main/java/zhao/algorithmMagic/core/AlgorithmStar.java +++ b/src_code/src/main/java/zhao/algorithmMagic/core/AlgorithmStar.java @@ -672,6 +672,34 @@ public IntegerCoordinates pretreatment(DataStandardizatio return dataStandardization.pretreatment(v); } + /** + * 将一个序列进行标准化,具体的标准化有不同的实现 + * + * @param integerMatrix 需要被标准化的数值,可以是坐标或向量,更多信息需要查阅实现 + *

+ * The value to be normalized, which can be a coordinate or a vector. For more information, see the implementation + * @return v的标准化样式 + *

+ * Normalized style of v + */ + public IntegerMatrix pretreatment(DataStandardization dataStandardization, IntegerMatrix integerMatrix) { + return dataStandardization.pretreatment(integerMatrix); + } + + /** + * 将一个序列进行标准化,具体的标准化有不同的实现 + * + * @param doubleMatrix 需要被标准化的数值,可以是坐标或向量,更多信息需要查阅实现 + *

+ * The value to be normalized, which can be a coordinate or a vector. For more information, see the implementation + * @return v的标准化样式 + *

+ * Normalized style of v + */ + public DoubleMatrix pretreatment(DataStandardization dataStandardization, DoubleMatrix doubleMatrix) { + return dataStandardization.pretreatment(doubleMatrix); + } + /** * 将一个序列进行标准化,具体的标准化有不同的实现 * diff --git a/src_code/src/main/java/zhao/algorithmMagic/operands/vector/SparkVector.scala b/src_code/src/main/java/zhao/algorithmMagic/operands/vector/SparkVector.scala index 44216ac..42bfc95 100644 --- a/src_code/src/main/java/zhao/algorithmMagic/operands/vector/SparkVector.scala +++ b/src_code/src/main/java/zhao/algorithmMagic/operands/vector/SparkVector.scala @@ -53,14 +53,6 @@ final class SparkVector(sparkContext: SparkContext, vector: org.apache.spark.mll else throw new OperatorOperationException("'DoubleVector1 multiply DoubleVector2' 时,两个'DoubleVector'的向量所包含的数量不同,DoubleVector1=[" + length1 + "],DoubleVector2=[" + length2 + "]\n" + "When 'DoubleVector1 multiply DoubleVector2', the two vectors of 'DoubleVector' contain different quantities, DoubleVector1=[" + length1 + "], DoubleVector2=[" + length2 + "]") } - /** - * - * @return 将本对象中存储的向量序列数组拷贝到一个新数组并将新数组返回,这里返回的是一个新数组,支持修改等操作。 - * - * Copy the vector sequence array stored in this object to a new array and return the new array. Here, a new array is returned, which supports modification and other operations. - */ - override def copyToNewArray(): Array[Double] = vector.toArray - /** * 计算两个向量的内积,也称之为数量积,具体实现请参阅api说明 *

@@ -115,6 +107,13 @@ final class SparkVector(sparkContext: SparkContext, vector: org.apache.spark.mll else throw new OperatorOperationException("'DoubleVector1 add DoubleVector2' 时,两个'DoubleVector'的向量所包含的数量不同,DoubleVector1=[" + numberOfDimensions1 + "],DoubleVector2=[" + numberOfDimensions2 + "]\n" + "When 'DoubleVector1 add DoubleVector2', the two vectors of 'DoubleVector' contain different quantities, DoubleVector1=[" + numberOfDimensions1 + "], DoubleVector2=[" + numberOfDimensions2 + "]") } + /** + * @return 向量中包含的维度数量 + *

+ * the number of dimensions contained in the vector + */ + override def getNumberOfDimensions: Int = size + /** * 在两个操作数之间做差的方法,具体用法请参阅API说明。 *

@@ -139,13 +138,6 @@ final class SparkVector(sparkContext: SparkContext, vector: org.apache.spark.mll else throw new OperatorOperationException("'DoubleVector1 diff DoubleVector2' 时,两个'DoubleVector'的向量所包含的数量不同,DoubleVector1=[" + numberOfDimensions1 + "],DoubleVector2=[" + numberOfDimensions2 + "]\n" + "When 'DoubleVector1 diff DoubleVector2', the two vectors of 'DoubleVector' contain different quantities, DoubleVector1=[" + numberOfDimensions1 + "], DoubleVector2=[" + numberOfDimensions2 + "]") } - /** - * @return 向量中包含的维度数量 - *

- * the number of dimensions contained in the vector - */ - override def getNumberOfDimensions: Int = size - /** * * @return 第三方向量中所维护的向量序列,通过此函数您可以直接获取到第三方库中的对象。 @@ -167,6 +159,14 @@ final class SparkVector(sparkContext: SparkContext, vector: org.apache.spark.mll * Objects after disruption. */ override def shuffle(seed: Long): SparkVector = SparkVector.parse(sparkContext, ASMath.shuffle(this.copyToNewArray(), seed, false)) + + /** + * + * @return 将本对象中存储的向量序列数组拷贝到一个新数组并将新数组返回,这里返回的是一个新数组,支持修改等操作。 + * + * Copy the vector sequence array stored in this object to a new array and return the new array. Here, a new array is returned, which supports modification and other operations. + */ + override def copyToNewArray(): Array[Double] = vector.toArray } object SparkVector { diff --git a/src_code/src/main/java/zhao/algorithmMagic/utils/ASMath.java b/src_code/src/main/java/zhao/algorithmMagic/utils/ASMath.java index 13e2ca1..c084087 100644 --- a/src_code/src/main/java/zhao/algorithmMagic/utils/ASMath.java +++ b/src_code/src/main/java/zhao/algorithmMagic/utils/ASMath.java @@ -63,6 +63,43 @@ public static int[] MaxAndMin(int[] doubles) { return new int[]{max, min}; } + /** + * 计算一个序列的最大值与最小值 + * + * @param doubles 被计算的序列 + * @return 计算结果,第一个参数是最大值,第二个参数是最小值 + */ + public static double[] MaxAndMin(double[][] doubles) { + // 计算最大值最小值 + double max = doubles[0][0]; + double min = doubles[0][0]; + for (double[] aDouble : doubles) { + for (double v : aDouble) { + if (v > max) max = v; + if (v < min) min = v; + } + } + return new double[]{max, min}; + } + + /** + * 计算一个序列的最大值与最小值 + * + * @param doubles 被计算的序列 + * @return 计算结果,第一个参数是最大值,第二个参数是最小值 + */ + public static int[] MaxAndMin(int[][] doubles) { + // 计算最大值最小值 + int max = doubles[0][0]; + int min = doubles[0][0]; + for (int[] aDouble : doubles) { + for (int v : aDouble) { + if (v > max) max = v; + if (v < min) min = v; + } + } + return new int[]{max, min}; + } /** * 计算一个序列中的最大值或最小值 From 88b94a300952057a671dbe36e95a70ce157a36a2 Mon Sep 17 00:00:00 2001 From: LingYuZhao <113756063+BeardedManZhao@users.noreply.github.com> Date: Mon, 3 Apr 2023 15:34:30 +0800 Subject: [PATCH 09/17] Update Case.md --- src_code/Case.md | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/src_code/Case.md b/src_code/Case.md index 8a703bf..e4485e0 100644 --- a/src_code/Case.md +++ b/src_code/Case.md @@ -221,6 +221,17 @@ public class MAIN1 { - 图像数据集 这里有所有数字的图像数据以及被处理的图像。 + +![1](https://user-images.githubusercontent.com/113756063/229441539-03d9adb6-4883-4c2c-a86d-e008b2a620d6.jpg) +![2](https://user-images.githubusercontent.com/113756063/229441568-a273d88e-a411-4d70-907b-67084ec404a8.jpg) +![3](https://user-images.githubusercontent.com/113756063/229441606-fa1f7195-0bbb-4c86-91dc-c0b7d3267850.jpg) +![4](https://user-images.githubusercontent.com/113756063/229441641-f0e01024-df8a-432f-a028-462327beb3c0.jpg) +![5](https://user-images.githubusercontent.com/113756063/229441669-ae0b3a20-fbf0-447f-bf76-20ea84016751.jpg) +![6](https://user-images.githubusercontent.com/113756063/229441694-c80357da-865e-40ec-815b-7dcebe743897.jpg) +![7](https://user-images.githubusercontent.com/113756063/229441706-5ae85394-ae91-4d77-85d8-5c088693277b.jpg) +![8](https://user-images.githubusercontent.com/113756063/229441723-c51b2a73-c007-436e-9a5c-e841a5312424.jpg) +![9](https://user-images.githubusercontent.com/113756063/229441430-7b9b8ded-b207-4415-b5a0-0b3e594d7846.jpg) +![test4](https://user-images.githubusercontent.com/113756063/229441864-ec1770d5-1154-4e9c-837e-a4acfc5fb259.jpg) - 图像识别代码,通过下面的代码实现了图像中数字 4 的圈出操作。 From 04f28b8fd51d292a0237e6fed87d687fd033ce82 Mon Sep 17 00:00:00 2001 From: LingYuZhao Date: Mon, 3 Apr 2023 19:08:32 +0800 Subject: [PATCH 10/17] update MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * 针对图像矩阵进行了合并功能的更新 * 针对IO数据流进行了优化。 --- src_code/Case.md | 34 +++++----- src_code/README-Chinese.md | 29 ++++++++ src_code/README.md | 29 ++++++++ src_code/pom.xml | 6 +- .../main/java/zhao/algorithmMagic/MAIN1.java | 66 +++++-------------- .../operands/matrix/ColorMatrix.java | 39 +++++++++++ .../zhao/algorithmMagic/utils/ASClass.java | 1 - .../java/zhao/algorithmMagic/utils/ASIO.java | 5 +- 8 files changed, 138 insertions(+), 71 deletions(-) diff --git a/src_code/Case.md b/src_code/Case.md index e4485e0..2c3e8fa 100644 --- a/src_code/Case.md +++ b/src_code/Case.md @@ -219,9 +219,8 @@ public class MAIN1 { 这里展示的是通过模板匹配操作,圈出图像中的指定数字的字符,图像数据集与图像的处理代码如下所示。 -- 图像数据集 - 这里有所有数字的图像数据以及被处理的图像。 - +- 图像数据集 这里有所有数字的图像数据以及被处理的图像。 + ![1](https://user-images.githubusercontent.com/113756063/229441539-03d9adb6-4883-4c2c-a86d-e008b2a620d6.jpg) ![2](https://user-images.githubusercontent.com/113756063/229441568-a273d88e-a411-4d70-907b-67084ec404a8.jpg) ![3](https://user-images.githubusercontent.com/113756063/229441606-fa1f7195-0bbb-4c86-91dc-c0b7d3267850.jpg) @@ -243,26 +242,29 @@ import zhao.algorithmMagic.operands.coordinate.IntegerCoordinateTwo; import zhao.algorithmMagic.operands.matrix.ColorMatrix; import java.awt.*; -import java.io.File; +import java.net.MalformedURLException; +import java.net.URL; import java.util.ArrayList; import java.util.HashMap; import java.util.Map; -import java.util.Objects; public class MAIN1 { - public static void main(String[] args) { + public static void main(String[] args) throws MalformedURLException { HashMap hashMap = new HashMap<>(0b1100); - // 将样本读取进来 + // 将样本通过网络读取进来 { - int count = 0; - for (File file : Objects.requireNonNull(new File("C:\\Users\\Liming\\Desktop\\fsdownload\\number").listFiles())) { - ColorMatrix parse = ColorMatrix.parse(file.getPath()); - parse.globalBinary(ColorMatrix._G_, 200, 0xffffff, 0); - hashMap.put(++count, parse); - } + hashMap.put(1, ColorMatrix.parse(new URL("https://user-images.githubusercontent.com/113756063/229441539-03d9adb6-4883-4c2c-a86d-e008b2a620d6.jpg"))); + hashMap.put(2, ColorMatrix.parse(new URL("https://user-images.githubusercontent.com/113756063/229441568-a273d88e-a411-4d70-907b-67084ec404a8.jpg"))); + hashMap.put(3, ColorMatrix.parse(new URL("https://user-images.githubusercontent.com/113756063/229441606-fa1f7195-0bbb-4c86-91dc-c0b7d3267850.jpg"))); + hashMap.put(4, ColorMatrix.parse(new URL("https://user-images.githubusercontent.com/113756063/229441641-f0e01024-df8a-432f-a028-462327beb3c0.jpg"))); + hashMap.put(5, ColorMatrix.parse(new URL("https://user-images.githubusercontent.com/113756063/229441669-ae0b3a20-fbf0-447f-bf76-20ea84016751.jpg"))); + hashMap.put(6, ColorMatrix.parse(new URL("https://user-images.githubusercontent.com/113756063/229441694-c80357da-865e-40ec-815b-7dcebe743897.jpg"))); + hashMap.put(7, ColorMatrix.parse(new URL("https://user-images.githubusercontent.com/113756063/229441706-5ae85394-ae91-4d77-85d8-5c088693277b.jpg"))); + hashMap.put(8, ColorMatrix.parse(new URL("https://user-images.githubusercontent.com/113756063/229441723-c51b2a73-c007-436e-9a5c-e841a5312424.jpg"))); + hashMap.put(9, ColorMatrix.parse(new URL("https://user-images.githubusercontent.com/113756063/229441430-7b9b8ded-b207-4415-b5a0-0b3e594d7846.jpg"))); } - // 将需要被进行数字提取的图像获取到 - ColorMatrix parse = ColorMatrix.parse("C:\\Users\\Liming\\Desktop\\fsdownload\\test4.bmp"); + // 将需要被进行数字提取的图像通过网络获取到 + ColorMatrix parse = ColorMatrix.parse(new URL("https://user-images.githubusercontent.com/113756063/229441864-ec1770d5-1154-4e9c-837e-a4acfc5fb259.jpg")); // 将原矩阵复制一份,稍后用于结果展示 ColorMatrix parse1 = ColorMatrix.parse(parse.copyToNewArrays()); // 将需要被处理的图像进行二值化,使其颜色不会干扰匹配 @@ -279,7 +281,7 @@ public class MAIN1 { 1, v -> { System.out.println(v); - return v < 3500; + return v < 5000; } ); // 将所有的轮廓绘制到用于展示的矩阵对象上。 diff --git a/src_code/README-Chinese.md b/src_code/README-Chinese.md index 2c36a1f..3bbfcc8 100644 --- a/src_code/README-Chinese.md +++ b/src_code/README-Chinese.md @@ -388,4 +388,33 @@ public class MAIN1 { } ``` +* 支持矩阵的追加合并计算,其可以将两个图像矩阵合并到同一个新的图像矩阵中。 + +```java +package zhao.algorithmMagic; + +import zhao.algorithmMagic.operands.matrix.ColorMatrix; + +import java.net.MalformedURLException; +import java.net.URL; + +public class MAIN1 { + public static void main(String[] args) throws MalformedURLException { + // 获取到一个图像矩形对象 + ColorMatrix parse1 = ColorMatrix.parse( + new URL("https://user-images.githubusercontent.com/113756063/229441864-ec1770d5-1154-4e9c-837e-a4acfc5fb259.jpg") + ); + // 将图像矩阵对象拷贝出一份并进行颜色反转 + ColorMatrix parse2 = ColorMatrix.parse(parse1.copyToNewArrays()).colorReversal(false); + // 将两个图像矩阵进行合并操作,并展示出上下左右合并的结果图像 + // 这里是左右合并 + ColorMatrix colorMatrixLR = parse1.append(parse2, true); + // 这里是上下合并 + ColorMatrix colorMatrixTB = parse1.append(parse2, false); + // 查看结果图像 + colorMatrixLR.show("LR"); + colorMatrixTB.show("TB"); + } +} +``` ### Version update date : xx xx-xx-xx \ No newline at end of file diff --git a/src_code/README.md b/src_code/README.md index fcb36ca..1bcb304 100644 --- a/src_code/README.md +++ b/src_code/README.md @@ -400,4 +400,33 @@ public class MAIN1 { } ``` +* Support the addition and merging calculation of matrices, which can merge two image matrices into the same new image matrix. + +```java +package zhao.algorithmMagic; + +import zhao.algorithmMagic.operands.matrix.ColorMatrix; + +import java.net.MalformedURLException; +import java.net.URL; + +public class MAIN1 { + public static void main(String[] args) throws MalformedURLException { + // 获取到一个图像矩形对象 + ColorMatrix parse1 = ColorMatrix.parse( + new URL("https://user-images.githubusercontent.com/113756063/229441864-ec1770d5-1154-4e9c-837e-a4acfc5fb259.jpg") + ); + // 将图像矩阵对象拷贝出一份并进行颜色反转 + ColorMatrix parse2 = ColorMatrix.parse(parse1.copyToNewArrays()).colorReversal(false); + // 将两个图像矩阵进行合并操作,并展示出上下左右合并的结果图像 + // 这里是左右合并 + ColorMatrix colorMatrixLR = parse1.append(parse2, true); + // 这里是上下合并 + ColorMatrix colorMatrixTB = parse1.append(parse2, false); + // 查看结果图像 + colorMatrixLR.show("LR"); + colorMatrixTB.show("TB"); + } +} +``` ### Version update date : xx xx-xx-xx \ No newline at end of file diff --git a/src_code/pom.xml b/src_code/pom.xml index b44116e..0ba637f 100644 --- a/src_code/pom.xml +++ b/src_code/pom.xml @@ -38,7 +38,7 @@ org.apache.logging.log4j log4j-slf4j-impl 2.20.0 - + @@ -46,14 +46,14 @@ org.apache.logging.log4j log4j-api 2.20.0 - + org.apache.logging.log4j log4j-core 2.20.0 - + diff --git a/src_code/src/main/java/zhao/algorithmMagic/MAIN1.java b/src_code/src/main/java/zhao/algorithmMagic/MAIN1.java index 6024f80..8291f92 100644 --- a/src_code/src/main/java/zhao/algorithmMagic/MAIN1.java +++ b/src_code/src/main/java/zhao/algorithmMagic/MAIN1.java @@ -1,59 +1,25 @@ package zhao.algorithmMagic; -import zhao.algorithmMagic.algorithm.distanceAlgorithm.ManhattanDistance; -import zhao.algorithmMagic.operands.coordinate.IntegerCoordinateTwo; import zhao.algorithmMagic.operands.matrix.ColorMatrix; -import java.awt.*; -import java.io.File; -import java.util.ArrayList; -import java.util.HashMap; -import java.util.Map; -import java.util.Objects; +import java.net.MalformedURLException; +import java.net.URL; public class MAIN1 { - public static void main(String[] args) throws CloneNotSupportedException { - HashMap hashMap = new HashMap<>(0b1100); - // 将样本读取进来 - { - int count = 0; - for (File file : Objects.requireNonNull(new File("C:\\Users\\Liming\\Desktop\\fsdownload\\number").listFiles())) { - ColorMatrix parse = ColorMatrix.parse(file.getPath()); - parse.globalBinary(ColorMatrix._G_, 200, 0xffffff, 0); - hashMap.put(++count, parse); - } - } - // 将需要被进行数字提取的图像获取到 - ColorMatrix parse = ColorMatrix.parse("C:\\Users\\Liming\\Desktop\\fsdownload\\test4.bmp"); - // 将原矩阵复制一份,稍后用于结果展示 - ColorMatrix parse1 = ColorMatrix.parse(parse.copyToNewArrays()); - // 将需要被处理的图像进行二值化,使其颜色不会干扰匹配 - parse.localBinary(ColorMatrix._G_, 4, 0xffffff, 0, -15); - parse.show("二值化结果"); - // 在这里获取到 4 数值对应的样本 - ColorMatrix temp = hashMap.get(4); - temp.show("模板数据"); - // 开始进行模板匹配操作,该操作将会返回所有度量系数小于 3500 的子图像左上角坐标 - ArrayList> matching = parse.templateMatching( - ManhattanDistance.getInstance("MAN"), - temp, - ColorMatrix._G_, - 1, - v -> { - System.out.println(v); - return v < 3500; - } + public static void main(String[] args) throws MalformedURLException { + // 获取到一个图像矩形对象 + ColorMatrix parse1 = ColorMatrix.parse( + new URL("https://user-images.githubusercontent.com/113756063/229441864-ec1770d5-1154-4e9c-837e-a4acfc5fb259.jpg") ); - // 将所有的轮廓绘制到用于展示的矩阵对象上。 - for (Map.Entry doubleIntegerCoordinateTwoEntry : matching) { - IntegerCoordinateTwo value = doubleIntegerCoordinateTwoEntry.getValue(); - parse1.drawRectangle( - value, - new IntegerCoordinateTwo(value.getX() + temp.getColCount() - 1, value.getY() + temp.getRowCount() - 1), - Color.MAGENTA - ); - } - // 查看结果 - parse1.show("res"); + // 将图像矩阵对象拷贝出一份并进行颜色反转 + ColorMatrix parse2 = ColorMatrix.parse(parse1.copyToNewArrays()).colorReversal(false); + // 将两个图像矩阵进行合并操作,并展示出上下左右合并的结果图像 + // 这里是左右合并 + ColorMatrix colorMatrixLR = parse1.append(parse2, true); + // 这里是上下合并 + ColorMatrix colorMatrixTB = parse1.append(parse2, false); + // 查看结果图像 + colorMatrixLR.show("LR"); + colorMatrixTB.show("TB"); } } \ No newline at end of file diff --git a/src_code/src/main/java/zhao/algorithmMagic/operands/matrix/ColorMatrix.java b/src_code/src/main/java/zhao/algorithmMagic/operands/matrix/ColorMatrix.java index 8cb02b7..906e35a 100644 --- a/src_code/src/main/java/zhao/algorithmMagic/operands/matrix/ColorMatrix.java +++ b/src_code/src/main/java/zhao/algorithmMagic/operands/matrix/ColorMatrix.java @@ -1423,6 +1423,45 @@ public ColorMatrix erode(int width, int height, boolean isCopy, Color backColor) return res; } + /** + * 浅拷贝的方式获取到两个矩阵上下或左右合并之后产生的新矩阵对象,需要注意的是,该操作不会链接矩阵,更不会更改调用方的的数据。 + *

+ * The shallow copy method obtains a new matrix object generated by merging two matrices up and down or left and right. Note that this operation does not link the matrix, nor does it change the caller's data. + * + * @param colorMatrix 需要被追加的颜色矩阵对象。 + *

+ * The color matrix object that needs to be appended. + * @param isLR 如果设置为 true 代表使用左右追加的方式进行追加, 如果设置为 false 代表使用上下追加的方式进行追加。 + *

+ * If set to true, it means appending using the left and right appending method. If set to false, it means appending using the up and down appending method. + * @return 追加之后的矩阵对象,该矩阵对象中的行与原矩阵是无关联的。 + *

+ * The appended matrix object has no rows associated with the original matrix. + */ + public ColorMatrix append(ColorMatrix colorMatrix, boolean isLR) { + if (isLR) { + Color[][] colors1 = this.toArrays(); + Color[][] colors2 = colorMatrix.toArrays(); + if (colors1.length != colors2.length) { + throw new OperatorOperationException("左右追加的矩阵对象需要保持行数相同!"); + } + Color[][] res = new Color[colors1.length][this.getColCount() + colorMatrix.getColCount()]; + int index = -1; + for (Color[] re : res) { + ASClass.mergeArray(re, colors1[++index], colors2[index]); + } + return ColorMatrix.parse(res); + } + Color[][] colors1 = this.copyToNewArrays(); + Color[][] colors2 = colorMatrix.copyToNewArrays(); + if (colors1.length != colors2.length) { + throw new OperatorOperationException("上下追加的矩阵对象需要保持列数相同!"); + } + Color[][] res = new Color[colors1.length + colors2.length][]; + ASClass.mergeArray(res, colors1, colors2); + return ColorMatrix.parse(res); + } + /** * 将图像矩阵中的指定通道的颜色整数数值矩阵获取到。 * diff --git a/src_code/src/main/java/zhao/algorithmMagic/utils/ASClass.java b/src_code/src/main/java/zhao/algorithmMagic/utils/ASClass.java index 5ed3308..abbc855 100644 --- a/src_code/src/main/java/zhao/algorithmMagic/utils/ASClass.java +++ b/src_code/src/main/java/zhao/algorithmMagic/utils/ASClass.java @@ -354,5 +354,4 @@ public static void mergeArray(arr[] res, arr[] arr1, arr[] arr2) { } } } - } \ No newline at end of file diff --git a/src_code/src/main/java/zhao/algorithmMagic/utils/ASIO.java b/src_code/src/main/java/zhao/algorithmMagic/utils/ASIO.java index e41f3e1..045893d 100644 --- a/src_code/src/main/java/zhao/algorithmMagic/utils/ASIO.java +++ b/src_code/src/main/java/zhao/algorithmMagic/utils/ASIO.java @@ -148,7 +148,10 @@ public static Color[][] parseImageGetColorArray(String inputString) { */ public static Color[][] parseURLGetColorArray(URL imageUrl) { try { - return parseImageGetColorArray(ImageIO.read(imageUrl.openStream())); + InputStream inputStream = imageUrl.openStream(); + Color[][] colors = parseImageGetColorArray(ImageIO.read(inputStream)); + inputStream.close(); + return colors; } catch (IOException e) { throw new OperatorOperationException(e); } From e0a2703fb7a0f2116813edf16eadf3e4cd7ebfe6 Mon Sep 17 00:00:00 2001 From: LingYuZhao Date: Wed, 5 Apr 2023 15:41:11 +0800 Subject: [PATCH 11/17] update MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * 为AS库添加第三方数据源支持,并允许通过摄像头获取到图像数据 --- KnowledgeDocument/Operands-Chinese.md | 15 +- KnowledgeDocument/Operands.md | 26 +++- README-Chinese.md | 12 ++ README.md | 31 +++- src_code/Case.md | 122 +++++++++++++++ src_code/README-Chinese.md | 73 ++++++++- src_code/README.md | 37 ++++- src_code/pom.xml | 10 +- .../main/java/zhao/algorithmMagic/MAIN1.java | 66 +++++--- .../zhao/algorithmMagic/io/InputBuilder.java | 39 +++++ .../zhao/algorithmMagic/io/InputCamera.java | 144 ++++++++++++++++++ .../algorithmMagic/io/InputCameraBuilder.java | 94 ++++++++++++ .../algorithmMagic/io/InputComponent.java | 73 +++++++++ .../operands/matrix/ColorMatrix.java | 136 +++++++++++++++-- .../operands/vector/SparkVector.scala | 16 +- .../java/zhao/algorithmMagic/utils/ASIO.java | 19 ++- .../utils/transformation/ProTransForm.java | 28 ++++ src_code/src/main/resources/log4j2.xml | 8 +- 18 files changed, 884 insertions(+), 65 deletions(-) create mode 100644 src_code/src/main/java/zhao/algorithmMagic/io/InputBuilder.java create mode 100644 src_code/src/main/java/zhao/algorithmMagic/io/InputCamera.java create mode 100644 src_code/src/main/java/zhao/algorithmMagic/io/InputCameraBuilder.java create mode 100644 src_code/src/main/java/zhao/algorithmMagic/io/InputComponent.java create mode 100644 src_code/src/main/java/zhao/algorithmMagic/utils/transformation/ProTransForm.java diff --git a/KnowledgeDocument/Operands-Chinese.md b/KnowledgeDocument/Operands-Chinese.md index 220db86..663dce1 100644 --- a/KnowledgeDocument/Operands-Chinese.md +++ b/KnowledgeDocument/Operands-Chinese.md @@ -413,14 +413,16 @@ Table 是AS库中用于进行数据分析的数据对象,其表现形式属于 ### DataFrameBuilder 与 DataFrame -DataFrameBuilder 与 DataFrame 分别用于数据的加载与数据的分析操作,在数据的加载过程中可以通过 DataFameBuilder 数据对象快捷同时容易理解的函数构造出一个DataFrame,并使用DataFrame进行数据的处理。 +DataFrameBuilder 与 DataFrame 分别用于数据的加载与数据的分析操作,在数据的加载过程中可以通过 DataFameBuilder +数据对象快捷同时容易理解的函数构造出一个DataFrame,并使用DataFrame进行数据的处理。 DataFrame 简称 "DF" 在数据的处理阶段,诸多函数采用SQL风格设计,能够有效降低学习成本,使得精力专注于更重要的事情上,接下来展示下 DataFrameBuilder 的基本使用。 #### 使用 FDataFrame 加载数据 -- 读取数据库 - 在AS库中您可以将数据加载成为 FDataFrame 数据对象,该对象能够实现基本的数据读取与数据处理功能,能够实现有效的数据管控,您可以将数据库中的数据加载成为一个 FDataFrame ,接下来就是有关数据库数据加载的代码示例。 - - 需要注意的是,在读取数据库的时候请在项目中导入 JDBC 的驱动类。 + +- 读取数据库 在AS库中您可以将数据加载成为 FDataFrame 数据对象,该对象能够实现基本的数据读取与数据处理功能,能够实现有效的数据管控,您可以将数据库中的数据加载成为一个 FDataFrame + ,接下来就是有关数据库数据加载的代码示例。 + - 需要注意的是,在读取数据库的时候请在项目中导入 JDBC 的驱动类。 ```java package zhao.algorithmMagic; @@ -447,8 +449,8 @@ public class MAIN1 { } } ``` -- 读取文件系统 - 针对文件系统的读取,FDataFrame 是可以轻松做到本地文件系统读取的,不需要依赖任何的第三方库就可以实现文件系统的读取,接下来就实现一下具体的步骤! + +- 读取文件系统 针对文件系统的读取,FDataFrame 是可以轻松做到本地文件系统读取的,不需要依赖任何的第三方库就可以实现文件系统的读取,接下来就实现一下具体的步骤! ```java package zhao.algorithmMagic; @@ -481,6 +483,7 @@ public class MAIN1 { } } ``` + #### 综合案例 ```java diff --git a/KnowledgeDocument/Operands.md b/KnowledgeDocument/Operands.md index 4b8bc76..365f422 100644 --- a/KnowledgeDocument/Operands.md +++ b/KnowledgeDocument/Operands.md @@ -427,19 +427,27 @@ public class MAIN1 { } } ``` + ## Table -Table is the data object used for data analysis in the AS database. Its representation is a table with row and column indexes, which can realize convenient data processing tasks. Data can be loaded and processed in the AS database through the DataFrame object. +Table is the data object used for data analysis in the AS database. Its representation is a table with row and column +indexes, which can realize convenient data processing tasks. Data can be loaded and processed in the AS database through +the DataFrame object. ### DataFrameBuilder & DataFrame -DataFrameBuilder and DataFrame are used for data loading and data analysis respectively. In the process of data loading, a DataFrame can be constructed through DataFameBuilder data object fast and easy to understand functions, and the DataFrame can be used for data processing. -DataFrame is called "DF" for short. In the data processing stage, many functions are designed in SQL style, which can effectively reduce learning costs and focus on more important things. Next, we will show the basic use of DataFrameBuilder. +DataFrameBuilder and DataFrame are used for data loading and data analysis respectively. In the process of data loading, +a DataFrame can be constructed through DataFameBuilder data object fast and easy to understand functions, and the +DataFrame can be used for data processing. DataFrame is called "DF" for short. In the data processing stage, many +functions are designed in SQL style, which can effectively reduce learning costs and focus on more important things. +Next, we will show the basic use of DataFrameBuilder. #### Load data using FDataFrame --Read Database - In the AS database, you can load data into an FDataFrame data object, which can realize basic data reading and data processing functions, and effective data control. You can load data in the database into an FDataFrame. Next is the code example about database data loading. - -It should be noted that when reading the database, please import the JDBC driver class in the project. + +-Read Database In the AS database, you can load data into an FDataFrame data object, which can realize basic data +reading and data processing functions, and effective data control. You can load data in the database into an FDataFrame. +Next is the code example about database data loading. -It should be noted that when reading the database, please import +the JDBC driver class in the project. ```java package zhao.algorithmMagic; @@ -466,8 +474,9 @@ public class MAIN1 { } } ``` --Read file system - For the reading of the file system, FDataFrame can easily read the local file system without relying on any third-party library. Next, we will implement the specific steps! + +-Read file system For the reading of the file system, FDataFrame can easily read the local file system without relying +on any third-party library. Next, we will implement the specific steps! ```java package zhao.algorithmMagic; @@ -500,6 +509,7 @@ public class MAIN1 { } } ``` + #### Comprehensive case ```java diff --git a/README-Chinese.md b/README-Chinese.md index 1665a67..592a668 100644 --- a/README-Chinese.md +++ b/README-Chinese.md @@ -22,12 +22,15 @@ ``` + ### AS库的所需依赖 在1.17版本之后,AS库的所有依赖被剥离,更好避免依赖的捆绑问题,减少项目发生冲突的可能性,同时也可以按照开发者的需求使用更加适合的依赖配置项,您可以在这里查看到AS库所依赖的第三方库依赖。 #### 必选依赖项 + AS库在进行诸多计算函数的时候会产生一些日志数据,因此AS库的使用需要导入日志依赖项,这个依赖项是必不可少的,请按照如下的方式导入依赖。 + ```xml @@ -54,9 +57,11 @@ AS库在进行诸多计算函数的时候会产生一些日志数据,因此AS ``` + #### 可选依赖项 AS库在针对数据库,Spark等各种平台对接的时候,需要使用到第三方依赖程序包,这些包是可选的,如果您不需要使用这些功能,您可以不去导入依赖,如果您需要,可以参考下面的配置。 + ```xml @@ -82,6 +87,13 @@ AS库在针对数据库,Spark等各种平台对接的时候,需要使用到 spark-mllib_2.12 3.1.3 + + + + com.github.sarxos + webcam-capture + 0.3.12 + ``` diff --git a/README.md b/README.md index 3faeaf3..af475ad 100644 --- a/README.md +++ b/README.md @@ -28,11 +28,18 @@ can add it to your maven project, or you can download it from Releases and manua ### Required dependencies of the AS library -After version 1.17, all dependencies of the AS library have been stripped to better avoid binding dependencies and reduce the possibility of project conflicts. At the same time, more suitable dependency configuration items can be used according to the needs of developers. You can view third-party library dependencies on which the AS library depends here. +After version 1.17, all dependencies of the AS library have been stripped to better avoid binding dependencies and +reduce the possibility of project conflicts. At the same time, more suitable dependency configuration items can be used +according to the needs of developers. You can view third-party library dependencies on which the AS library depends +here. #### Required Dependencies -The AS library generates some log data when performing many calculation functions. Therefore, the use of the AS library requires importing log dependencies, which are essential. Please import the dependencies as follows. + +The AS library generates some log data when performing many calculation functions. Therefore, the use of the AS library +requires importing log dependencies, which are essential. Please import the dependencies as follows. + ```xml + @@ -58,18 +65,23 @@ The AS library generates some log data when performing many calculation function ``` + #### Optional Dependencies -When interfacing with various platforms such as databases and Sparks, the AS library needs to use third-party dependency packages, which are optional. If you do not need to use these functions, you may not need to import dependencies. If you need to, you can refer to the following configuration. +When interfacing with various platforms such as databases and Sparks, the AS library needs to use third-party dependency +packages, which are optional. If you do not need to use these functions, you may not need to import dependencies. If you +need to, you can refer to the following configuration. + ```xml + - + mysql mysql-connector-java 8.0.30 - + org.apache.spark spark-core_2.12 @@ -80,12 +92,19 @@ When interfacing with various platforms such as databases and Sparks, the AS lib spark-sql_2.12 3.1.3 - + org.apache.spark spark-mllib_2.12 3.1.3 + + + + com.github.sarxos + webcam-capture + 0.3.12 + ``` diff --git a/src_code/Case.md b/src_code/Case.md index 2c3e8fa..03fd6ad 100644 --- a/src_code/Case.md +++ b/src_code/Case.md @@ -215,6 +215,66 @@ public class MAIN1 { } ``` +```java +package zhao.algorithmMagic; + +import zhao.algorithmMagic.algorithm.distanceAlgorithm.ManhattanDistance; +import zhao.algorithmMagic.io.InputCamera; +import zhao.algorithmMagic.io.InputCameraBuilder; +import zhao.algorithmMagic.io.InputComponent; +import zhao.algorithmMagic.operands.coordinate.IntegerCoordinateTwo; +import zhao.algorithmMagic.operands.matrix.ColorMatrix; +import zhao.algorithmMagic.operands.table.FinalCell; + +import java.awt.*; +import java.util.Map; + +public class MAIN1 { + public static void main(String[] args) { + ColorMatrix colorMatrix1, colorMatrix2; + { + // 获取到摄像头输入设备 + InputComponent inputComponent = InputCamera.builder() + // 要使用的摄像头的名字 索引 或def默认,我们这里使用的是 def 代表使用默认摄像头 + .addInputArg(InputCameraBuilder.Camera_Index, new FinalCell<>("def")) + // 要使用的拍摄图像格式 + .addInputArg(InputCameraBuilder.Image_Format, new FinalCell<>("JPG")) + // 图像尺寸 这里的数值是 WebcamResolution 枚举类的属性字段 VGA + .addInputArg(InputCameraBuilder.CUSTOM_VIEW_SIZES, new FinalCell<>("VGA")) + .create(); + // 将图像与样本读取进来 + colorMatrix1 = ColorMatrix.parse("C:\\Users\\liming\\Desktop\\fsdownload\\YB.bmp"); + colorMatrix2 = ColorMatrix.parse(inputComponent); + ColorMatrix temp = ColorMatrix.parse(colorMatrix2.copyToNewArrays()); + // 开始二值化 + colorMatrix1.localBinary(ColorMatrix._G_, 10, 0xffffff, 0, 1); + temp.localBinary(ColorMatrix._G_, 5, 0xffffff, 0, 20); + temp.erode(2, 2, false); + temp.show("temp"); + // 开始进行模板匹配 并返回最匹配的结果数值,在这里返回的就是所有匹配的结果数据,key为匹配系数 value为匹配结果 + Map.Entry matching = temp.templateMatching( + ManhattanDistance.getInstance("MAN"), + colorMatrix1, + ColorMatrix._G_, + 10, + false + ); + // 开始进行绘制 在这里首先获取到坐标数据 + IntegerCoordinateTwo coordinateTwo = matching.getValue(); + System.out.print("匹配系数 = "); + System.out.println(matching.getKey()); + colorMatrix2.drawRectangle( + coordinateTwo, + new IntegerCoordinateTwo(coordinateTwo.getX() + colorMatrix1.getColCount(), coordinateTwo.getY() + colorMatrix1.getRowCount()), + Color.MAGENTA + ); + } + colorMatrix1.show("人脸样本"); + colorMatrix2.show("识别结果"); + } +} +``` + ### 指定文字识别 这里展示的是通过模板匹配操作,圈出图像中的指定数字的字符,图像数据集与图像的处理代码如下所示。 @@ -299,6 +359,68 @@ public class MAIN1 { } ``` +### 图像放大案例 + +```java +package zhao.algorithmMagic; + +import zhao.algorithmMagic.operands.matrix.ColorMatrix; +import zhao.algorithmMagic.operands.table.Cell; +import zhao.algorithmMagic.operands.table.FinalCell; + +import java.util.HashMap; + + +public class MAIN1 { + public static void main(String[] args) { + // 被处理图像路径 + String dataPath = "C:\\Users\\Liming\\Desktop\\fsdownload\\微信图片_33.jpg"; + ColorMatrix colorMatrix1 = ColorMatrix.parse(dataPath); + colorMatrix1.show("src"); + // 将矩阵变换 首先需要创建出不同模式中需要的配置信息 这里是反转和拉伸矩阵的配置 + HashMap> pro = new HashMap<>(); + // 拉伸矩阵时指定拉伸倍数 + pro.put("times", new FinalCell<>(2)); + // 开始左右拉伸 + ColorMatrix converter = colorMatrix1.converter(ColorMatrix.SLIT_LR, pro); + // 将左右拉伸后的结果再进行上下拉伸,然后查看图像 + converter.converter(ColorMatrix.SLIT_BT, pro).show("res"); + } +} +``` + ## 数据计算类案例 +### Series 转 Stream + +```java +package zhao.algorithmMagic; + +import zhao.algorithmMagic.operands.table.*; + +import java.util.stream.Stream; + + +public class MAIN1 { + public static void main(String[] args) { + // 手动创建一个DF对象 + DataFrame dataFrame = FDataFrame.select( + FieldCell.parse("name", "sex", "age"), 0 + ); + // 插入一行数据 + dataFrame.insert( + FinalSeries.parse("zhao1", "男", "19"), + FinalSeries.parse("zhao2", "女", "18"), + FinalSeries.parse("zhao3", "女", "20") + ); + System.out.println(dataFrame); + // 将其中的第二行数据转换成Java的Stream对象 + Series cells = dataFrame.toList().get(1); + Stream> cellStream = cells.toStream(); + // 查看对象 + cellStream.forEach(System.out::println); + } +} +``` + ## 机器学习类案例 diff --git a/src_code/README-Chinese.md b/src_code/README-Chinese.md index 3bbfcc8..469ef14 100644 --- a/src_code/README-Chinese.md +++ b/src_code/README-Chinese.md @@ -375,7 +375,7 @@ public class MAIN1 { AlgorithmStar algorithmStar = AlgorithmStar.getInstance(); // 开始进行归一化操作 IntegerMatrix pretreatment1 = algorithmStar.pretreatment( - line, + line, // 需要被归一化的矩阵对象 integerMatrix ); @@ -417,4 +417,75 @@ public class MAIN1 { } } ``` + +* 支持图像矩阵的通用变换函数操作,在这个函数中,您可以手动的指定矩形变换模式,也可以手动实现图像变换逻辑。 + +```java +package zhao.algorithmMagic; + +import zhao.algorithmMagic.operands.matrix.ColorMatrix; +import zhao.algorithmMagic.operands.table.Cell; +import zhao.algorithmMagic.operands.table.FinalCell; + +import java.util.HashMap; + + +public class MAIN1 { + public static void main(String[] args) { + // 被处理图像路径 + String dataPath = "C:\\Users\\Liming\\Desktop\\fsdownload\\test7.jpg"; + ColorMatrix colorMatrix1 = ColorMatrix.parse(dataPath); + // 将矩阵变换 首先需要创建出不同模式中需要的配置信息 这里是反转和拉伸矩阵的配置 + HashMap> pro = new HashMap<>(); + // 反转矩阵时指定拷贝操作 + pro.put("isCopy", new FinalCell<>(true)); + // 拉伸矩阵时指定拉伸倍数 + pro.put("times", new FinalCell<>(2)); + // 上下 反转 + ColorMatrix converter1 = colorMatrix1.converter(ColorMatrix.REVERSE_BT, pro); + converter1.show("REVERSE_BT"); + // 左右 反转 + converter1 = colorMatrix1.converter(ColorMatrix.REVERSE_LR, pro); + converter1.show("REVERSE_LR"); + // 上下 拉伸 + converter1 = colorMatrix1.converter(ColorMatrix.SLIT_BT, pro); + converter1.show("SLIT_BT"); + // 左右 拉伸 + converter1 = colorMatrix1.converter(ColorMatrix.SLIT_LR, pro); + converter1.show("SLIT_LR"); + } +} +``` + +* 支持通过摄像头获取到图像矩阵。 + +```java +package zhao.algorithmMagic; + +import zhao.algorithmMagic.io.InputCamera; +import zhao.algorithmMagic.io.InputCameraBuilder; +import zhao.algorithmMagic.io.InputComponent; +import zhao.algorithmMagic.operands.matrix.ColorMatrix; +import zhao.algorithmMagic.operands.matrix.DoubleMatrix; +import zhao.algorithmMagic.operands.table.FinalCell; + +public class MAIN1 { + public static void main(String[] args) { + // 获取到摄像头输入设备 + InputComponent inputComponent = InputCamera.builder() + // 要使用的摄像头的名字 索引 或def默认,我们这里使用的是 def 代表使用默认摄像头 + .addInputArg(InputCameraBuilder.Camera_Index, new FinalCell<>("def")) + // 要使用的拍摄图像格式 + .addInputArg(InputCameraBuilder.Image_Format, new FinalCell<>("JPG")) + // 图像尺寸 这里的数值是 WebcamResolution 枚举类的属性字段 VGA + .addInputArg(InputCameraBuilder.CUSTOM_VIEW_SIZES, new FinalCell<>("VGA")) + .create(); + ColorMatrix parse1 = ColorMatrix.parse(inputComponent); + parse1.show("image1"); + double[][] double2Array = inputComponent.getDouble2Array(); + DoubleMatrix parse = DoubleMatrix.parse(double2Array); + } +} +``` + ### Version update date : xx xx-xx-xx \ No newline at end of file diff --git a/src_code/README.md b/src_code/README.md index 1bcb304..af073fb 100644 --- a/src_code/README.md +++ b/src_code/README.md @@ -387,7 +387,7 @@ public class MAIN1 { AlgorithmStar algorithmStar = AlgorithmStar.getInstance(); // 开始进行归一化操作 IntegerMatrix pretreatment1 = algorithmStar.pretreatment( - line, + line, // 需要被归一化的矩阵对象 integerMatrix ); @@ -400,7 +400,8 @@ public class MAIN1 { } ``` -* Support the addition and merging calculation of matrices, which can merge two image matrices into the same new image matrix. +* Support the addition and merging calculation of matrices, which can merge two image matrices into the same new image + matrix. ```java package zhao.algorithmMagic; @@ -429,4 +430,36 @@ public class MAIN1 { } } ``` + +* Support obtaining image matrix through camera. + +```java +package zhao.algorithmMagic; + +import zhao.algorithmMagic.io.InputCamera; +import zhao.algorithmMagic.io.InputCameraBuilder; +import zhao.algorithmMagic.io.InputComponent; +import zhao.algorithmMagic.operands.matrix.ColorMatrix; +import zhao.algorithmMagic.operands.matrix.DoubleMatrix; +import zhao.algorithmMagic.operands.table.FinalCell; + +public class MAIN1 { + public static void main(String[] args) { + // 获取到摄像头输入设备 + InputComponent inputComponent = InputCamera.builder() + // 要使用的摄像头的名字 索引 或def默认,我们这里使用的是 def 代表使用默认摄像头 + .addInputArg(InputCameraBuilder.Camera_Index, new FinalCell<>("def")) + // 要使用的拍摄图像格式 + .addInputArg(InputCameraBuilder.Image_Format, new FinalCell<>("JPG")) + // 图像尺寸 这里的数值是 WebcamResolution 枚举类的属性字段 VGA + .addInputArg(InputCameraBuilder.CUSTOM_VIEW_SIZES, new FinalCell<>("VGA")) + .create(); + ColorMatrix parse1 = ColorMatrix.parse(inputComponent); + parse1.show("image1"); + double[][] double2Array = inputComponent.getDouble2Array(); + DoubleMatrix parse = DoubleMatrix.parse(double2Array); + } +} +``` + ### Version update date : xx xx-xx-xx \ No newline at end of file diff --git a/src_code/pom.xml b/src_code/pom.xml index 0ba637f..69ce282 100644 --- a/src_code/pom.xml +++ b/src_code/pom.xml @@ -1,6 +1,6 @@ + xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 4.0.0 io.github.BeardedManZhao @@ -81,6 +81,14 @@ 3.1.3 provided + + + + com.github.sarxos + webcam-capture + 0.3.12 + provided + diff --git a/src_code/src/main/java/zhao/algorithmMagic/MAIN1.java b/src_code/src/main/java/zhao/algorithmMagic/MAIN1.java index 8291f92..ff88547 100644 --- a/src_code/src/main/java/zhao/algorithmMagic/MAIN1.java +++ b/src_code/src/main/java/zhao/algorithmMagic/MAIN1.java @@ -1,25 +1,57 @@ package zhao.algorithmMagic; +import zhao.algorithmMagic.algorithm.distanceAlgorithm.ManhattanDistance; +import zhao.algorithmMagic.io.InputCamera; +import zhao.algorithmMagic.io.InputCameraBuilder; +import zhao.algorithmMagic.io.InputComponent; +import zhao.algorithmMagic.operands.coordinate.IntegerCoordinateTwo; import zhao.algorithmMagic.operands.matrix.ColorMatrix; +import zhao.algorithmMagic.operands.table.FinalCell; -import java.net.MalformedURLException; -import java.net.URL; +import java.awt.*; +import java.util.Map; public class MAIN1 { - public static void main(String[] args) throws MalformedURLException { - // 获取到一个图像矩形对象 - ColorMatrix parse1 = ColorMatrix.parse( - new URL("https://user-images.githubusercontent.com/113756063/229441864-ec1770d5-1154-4e9c-837e-a4acfc5fb259.jpg") - ); - // 将图像矩阵对象拷贝出一份并进行颜色反转 - ColorMatrix parse2 = ColorMatrix.parse(parse1.copyToNewArrays()).colorReversal(false); - // 将两个图像矩阵进行合并操作,并展示出上下左右合并的结果图像 - // 这里是左右合并 - ColorMatrix colorMatrixLR = parse1.append(parse2, true); - // 这里是上下合并 - ColorMatrix colorMatrixTB = parse1.append(parse2, false); - // 查看结果图像 - colorMatrixLR.show("LR"); - colorMatrixTB.show("TB"); + public static void main(String[] args) { + ColorMatrix colorMatrix1, colorMatrix2; + { + // 获取到摄像头输入设备 + InputComponent inputComponent = InputCamera.builder() + // 要使用的摄像头的名字 索引 或def默认,我们这里使用的是 def 代表使用默认摄像头 + .addInputArg(InputCameraBuilder.Camera_Index, new FinalCell<>("def")) + // 要使用的拍摄图像格式 + .addInputArg(InputCameraBuilder.Image_Format, new FinalCell<>("JPG")) + // 图像尺寸 这里的数值是 WebcamResolution 枚举类的属性字段 VGA + .addInputArg(InputCameraBuilder.CUSTOM_VIEW_SIZES, new FinalCell<>("VGA")) + .create(); + // 将图像与样本读取进来 + colorMatrix1 = ColorMatrix.parse("C:\\Users\\liming\\Desktop\\fsdownload\\YB.bmp"); + colorMatrix2 = ColorMatrix.parse(inputComponent); + ColorMatrix temp = ColorMatrix.parse(colorMatrix2.copyToNewArrays()); + // 开始二值化 + colorMatrix1.localBinary(ColorMatrix._G_, 10, 0xffffff, 0, 1); + temp.localBinary(ColorMatrix._G_, 5, 0xffffff, 0, 20); + temp.erode(2, 2, false); + temp.show("temp"); + // 开始进行模板匹配 并返回最匹配的结果数值,在这里返回的就是所有匹配的结果数据,key为匹配系数 value为匹配结果 + Map.Entry matching = temp.templateMatching( + ManhattanDistance.getInstance("MAN"), + colorMatrix1, + ColorMatrix._G_, + 10, + false + ); + // 开始进行绘制 在这里首先获取到坐标数据 + IntegerCoordinateTwo coordinateTwo = matching.getValue(); + System.out.print("匹配系数 = "); + System.out.println(matching.getKey()); + colorMatrix2.drawRectangle( + coordinateTwo, + new IntegerCoordinateTwo(coordinateTwo.getX() + colorMatrix1.getColCount(), coordinateTwo.getY() + colorMatrix1.getRowCount()), + Color.MAGENTA + ); + } + colorMatrix1.show("人脸样本"); + colorMatrix2.show("识别结果"); } } \ No newline at end of file diff --git a/src_code/src/main/java/zhao/algorithmMagic/io/InputBuilder.java b/src_code/src/main/java/zhao/algorithmMagic/io/InputBuilder.java new file mode 100644 index 0000000..9ce8784 --- /dev/null +++ b/src_code/src/main/java/zhao/algorithmMagic/io/InputBuilder.java @@ -0,0 +1,39 @@ +package zhao.algorithmMagic.io; + +import zhao.algorithmMagic.operands.table.FinalCell; + +/** + * 数据输入组件生成器,其本身作为所有数据输入设备对象的建造者对象。 + *

+ * The data input component generator itself serves as the creator object for all data input device objects. + * + * @author 赵凌宇 + * 2023/4/5 13:54 + */ +public interface InputBuilder { + + /** + * 添加数据输入描述,不同的组件有不同的配置属性,具体可以参阅实现类。 + *

+ * Add data input descriptions, and different components have different configuration properties. Please refer to the implementation class for details. + * + * @param key 属性名称 + *

+ * Attribute Name. + * @param value 属性数值 + *

+ * Attribute Value. + * @return 链式调用,继续构建 + *

+ * Chain call, continue building. + */ + InputBuilder addInputArg(String key, FinalCell value); + + /** + * 将所需的对象构建出来并获取到对应的输入设备对象。 + * + * @return 输入设备对象。 + */ + InputComponent create(); + +} diff --git a/src_code/src/main/java/zhao/algorithmMagic/io/InputCamera.java b/src_code/src/main/java/zhao/algorithmMagic/io/InputCamera.java new file mode 100644 index 0000000..b4ac523 --- /dev/null +++ b/src_code/src/main/java/zhao/algorithmMagic/io/InputCamera.java @@ -0,0 +1,144 @@ +package zhao.algorithmMagic.io; + +import com.github.sarxos.webcam.Webcam; +import com.github.sarxos.webcam.WebcamUtils; +import zhao.algorithmMagic.exception.OperatorOperationException; +import zhao.algorithmMagic.utils.ASClass; +import zhao.algorithmMagic.utils.ASIO; + +import javax.imageio.ImageIO; +import java.awt.image.BufferedImage; +import java.io.ByteArrayInputStream; +import java.io.IOException; +import java.io.InputStream; + +/** + * 摄像头对象,用于从摄像头抓取一张图像。 + * + * @author 赵凌宇 + * 2023/4/5 13:51 + */ +public class InputCamera implements InputComponent { + + private final Webcam webcam; + private final String format; + + /** + * @param webcam 拍照对象 + * @param format 拍照之后的图像格式 + */ + protected InputCamera(Webcam webcam, String format) { + this.webcam = webcam; + this.format = format; + } + + /** + * @return 开始构建本数据组件对象。 + *

+ * Start building this data component object. + */ + public static InputBuilder builder() { + return new InputCameraBuilder(); + } + + /** + * 启动数据输入组件 + * + * @return 如果启动成功返回true + */ + @Override + public boolean open() { + return webcam.open(); + } + + /** + * @return 如果组件已经启动了,在这里返回true + */ + @Override + public boolean isOpen() { + return webcam.isOpen(); + } + + /** + * 关闭数据输入组件 + */ + @Override + public void close() { + webcam.close(); + } + + /** + * 从数据输入组件中提取出 byte 数组的数据,一般情况下,这里返回的都是一些二进制的数据。 + *

+ * Extract the data of the byte array from the data input component. Generally, the returned data here is some binary data. + * + * @return byte[] 的数据对象 + */ + @Override + public byte[] getByteArray() { + return WebcamUtils.getImageBytes(webcam, format); + } + + /** + * 从数据输入组件中提取出 int 矩阵数据,一般情况下,这里返回的是一些矩阵元素数据。 + *

+ * From the data input component, int matrix data is increasingly generated. Generally, some matrix element data is returned here. + * + * @return int[][] + */ + @Override + public int[][] getInt2Array() { + return ASIO.parseImageGetArray(this.getBufferedImage()); + } + + /** + * 从数据输入组件中提取出 int 矩阵数据,一般情况下,这里返回的是一些矩阵元素数据。 + *

+ * From the data input component, double matrix data is increasingly generated. Generally, some matrix element data is returned here. + * + * @return double[][] + */ + @Override + public double[][] getDouble2Array() { + int[][] int2Array = this.getInt2Array(); + double[][] res = new double[int2Array.length][]; + int index = -1; + for (int[] ints : int2Array) { + res[++index] = ASClass.IntArray_To_DoubleArray(ints); + } + return res; + } + + /** + * 从数据输入组件中提取出 数据流 对象。 + *

+ * Extract the data flow object from the data input component. + * + * @return 数据输入流对象 + */ + @Override + public InputStream getInputStream() { + return new ByteArrayInputStream(this.getByteArray()); + } + + /** + * 从数据输入组件中提取出 图像缓存 对象,需要注意的是,该操作在有些情况下可能不被支持。 + *

+ * Extracting image cache objects from the data input component, it should be noted that this operation may not be supported in some cases. + * + * @return 图像缓存对象。 + */ + @Override + public BufferedImage getBufferedImage() { + InputStream inputStream = null; + try { + inputStream = this.getInputStream(); + return ImageIO.read(inputStream); + } catch (IOException e) { + throw new OperatorOperationException("照相机拍照读取失败.Camera photo reading failed.", e); + } finally { + ASIO.close(inputStream); + } + } + +} diff --git a/src_code/src/main/java/zhao/algorithmMagic/io/InputCameraBuilder.java b/src_code/src/main/java/zhao/algorithmMagic/io/InputCameraBuilder.java new file mode 100644 index 0000000..3eebc7a --- /dev/null +++ b/src_code/src/main/java/zhao/algorithmMagic/io/InputCameraBuilder.java @@ -0,0 +1,94 @@ +package zhao.algorithmMagic.io; + +import com.github.sarxos.webcam.Webcam; +import com.github.sarxos.webcam.WebcamResolution; +import zhao.algorithmMagic.exception.OperatorOperationException; +import zhao.algorithmMagic.operands.table.Cell; +import zhao.algorithmMagic.operands.table.FinalCell; + +import java.util.HashMap; + +/** + * 摄像头设备输入建造者对象 + * + * @author 赵凌宇 + * 2023/4/5 14:28 + */ +public class InputCameraBuilder extends HashMap> implements InputBuilder { + + /** + * 拍照时需要使用的摄像头索引,索引对应的value应是一个数值或字符串,如果时数值将当作索引处理,如果时字符串将当作摄像头名称处理。 + */ + public final static String Camera_Index = "Camera"; + + /** + * 拍照时需要使用的图像拍照格式,格式对应的值应是一个字符串。 + */ + public final static String Image_Format = "ImageFormat"; + + /** + * 图像拍照大小尺寸对象,格式对应的值可以直接是 WebcamResolution 类的属性。 + */ + public final static String CUSTOM_VIEW_SIZES = "CustomViewSizes"; + + public WebcamResolution webcamResolution = WebcamResolution.VGA; + + /** + * 添加数据输入描述,不同的组件有不同的配置属性,具体可以参阅实现类。 + *

+ * Add data input descriptions, and different components have different configuration properties. Please refer to the implementation class for details. + * + * @param key 属性名称 + *

+ * Attribute Name. + * @param value 属性数值 + *

+ * Attribute Value. + * @return 链式调用,继续构建 + *

+ * Chain call, continue building. + */ + @Override + public InputBuilder addInputArg(String key, FinalCell value) { + if (CUSTOM_VIEW_SIZES.equals(key)) { + return this.setViewSize(WebcamResolution.valueOf(value.toString())); + } + this.put(key, value); + return this; + } + + /** + * @param WebcamResolution 图像拍照大小尺寸对象 + * @return 链式调用,继续构建 + *

+ * Chain call, continue building. + */ + public InputBuilder setViewSize(WebcamResolution WebcamResolution) { + this.webcamResolution = WebcamResolution; + return this; + } + + /** + * 将所需的对象构建出来并获取到对应的输入设备对象。 + * + * @return 输入设备对象。 + */ + @Override + public InputComponent create() { + Cell cell = this.get(Camera_Index); + Webcam webcam; + if (cell != null) { + if (cell.isNumber()) { + webcam = Webcam.getWebcams().get(cell.getIntValue()); + } else { + String s = cell.toString(); + if ("def".equals(s)) webcam = Webcam.getDefault(); + else webcam = Webcam.getWebcamByName(s); + } + webcam.setViewSize(this.webcamResolution.getSize()); + return new InputCamera(webcam, this.getOrDefault(Image_Format, new FinalCell<>("JPG")).toString()); + } else { + throw new OperatorOperationException("请您配置摄像头的名称或摄像头索引。\nPlease configure the name or camera index of the camera."); + } + } +} diff --git a/src_code/src/main/java/zhao/algorithmMagic/io/InputComponent.java b/src_code/src/main/java/zhao/algorithmMagic/io/InputComponent.java new file mode 100644 index 0000000..9496c03 --- /dev/null +++ b/src_code/src/main/java/zhao/algorithmMagic/io/InputComponent.java @@ -0,0 +1,73 @@ +package zhao.algorithmMagic.io; + +import java.awt.image.BufferedImage; +import java.io.Closeable; +import java.io.InputStream; + +/** + * IO组件对象,该对象是所有数据输入设备的父类接口对象,通过该对象可以实现获取到第三方数据的操作 + *

+ * IO component object, which is the parent interface object of all data input devices, through which the operation of obtaining third-party data can be achieved + * + * @author 赵凌宇 + * 2023/4/5 13:52 + */ +public interface InputComponent extends Closeable { + + /** + * 启动数据输入组件 + * + * @return 如果启动成功返回true + */ + boolean open(); + + /** + * @return 如果组件已经启动了,在这里返回true + */ + boolean isOpen(); + + /** + * 从数据输入组件中提取出 byte 数组的数据,一般情况下,这里返回的都是一些二进制的数据。 + *

+ * Extract the data of the byte array from the data input component. Generally, the returned data here is some binary data. + * + * @return byte[] 的数据对象 + */ + byte[] getByteArray(); + + /** + * 从数据输入组件中提取出 int 矩阵数据,一般情况下,这里返回的是一些矩阵元素数据。 + *

+ * From the data input component, int matrix data is increasingly generated. Generally, some matrix element data is returned here. + * + * @return int[][] + */ + int[][] getInt2Array(); + + /** + * 从数据输入组件中提取出 int 矩阵数据,一般情况下,这里返回的是一些矩阵元素数据。 + *

+ * From the data input component, double matrix data is increasingly generated. Generally, some matrix element data is returned here. + * + * @return double[][] + */ + double[][] getDouble2Array(); + + /** + * 从数据输入组件中提取出 数据流 对象。 + *

+ * Extract the data flow object from the data input component. + * + * @return 数据输入流对象 + */ + InputStream getInputStream(); + + /** + * 从数据输入组件中提取出 图像缓存 对象,需要注意的是,该操作在有些情况下可能不被支持。 + *

+ * Extracting image cache objects from the data input component, it should be noted that this operation may not be supported in some cases. + * + * @return 图像缓存对象。 + */ + BufferedImage getBufferedImage(); +} diff --git a/src_code/src/main/java/zhao/algorithmMagic/operands/matrix/ColorMatrix.java b/src_code/src/main/java/zhao/algorithmMagic/operands/matrix/ColorMatrix.java index 906e35a..851e4a7 100644 --- a/src_code/src/main/java/zhao/algorithmMagic/operands/matrix/ColorMatrix.java +++ b/src_code/src/main/java/zhao/algorithmMagic/operands/matrix/ColorMatrix.java @@ -3,13 +3,16 @@ import zhao.algorithmMagic.algorithm.distanceAlgorithm.DistanceAlgorithm; import zhao.algorithmMagic.exception.OperatorOperationException; import zhao.algorithmMagic.integrator.ImageRenderingIntegrator; +import zhao.algorithmMagic.io.InputComponent; import zhao.algorithmMagic.operands.coordinate.IntegerCoordinateTwo; import zhao.algorithmMagic.operands.matrix.block.IntegerMatrixSpace; +import zhao.algorithmMagic.operands.table.Cell; import zhao.algorithmMagic.utils.ASClass; import zhao.algorithmMagic.utils.ASIO; import zhao.algorithmMagic.utils.ASMath; import zhao.algorithmMagic.utils.filter.DoubleFiltering; import zhao.algorithmMagic.utils.transformation.ManyTrans; +import zhao.algorithmMagic.utils.transformation.ProTransForm; import javax.swing.*; import java.awt.*; @@ -211,6 +214,84 @@ public class ColorMatrix extends Matrix REVERSE_LR = (colorMatrix, value) -> { + if (value != null) { + Cell isCopy = value.get("isCopy"); + if (isCopy != null) { + return colorMatrix.reverseLR(Boolean.parseBoolean(isCopy.toString())); + } + } + return colorMatrix.reverseLR(true); + }; + + /** + * 矩阵左右反转操作,其参数是可选的,如果您想要传递参数,请按照如下方式传递。 + * key = "isCopy" value = boolean 数值 + */ + public final static ProTransForm REVERSE_BT = (colorMatrix, value) -> { + if (value != null) { + Cell isCopy = value.get("isCopy"); + if (isCopy != null) { + return colorMatrix.reverseBT(Boolean.parseBoolean(isCopy.toString())); + } + } + return colorMatrix.reverseBT(true); + }; + + /** + * 矩阵按照左右的方向进行拉伸操作,使得图像变宽,需要传递参数 times。 + */ + public final static ProTransForm SLIT_LR = (colorMatrix, value) -> { + if (value == null || !value.containsKey("times")) { + throw new OperatorOperationException("矩阵拉伸操作需要您传递配置项 times 其代表每一个像素在原矩阵中的横向拉伸倍数。"); + } + // 获取到横向拉伸倍数 + int times = value.get("times").getIntValue(); + // 开始拉伸 + Color[][] res = new Color[colorMatrix.getRowCount()][colorMatrix.getColCount() * times]; + int index = -1; + for (Color[] colors : colorMatrix.toArrays()) { + Color[] re = res[++index]; + int x1 = 0, x2 = times; + for (Color color : colors) { + // 在这里拉伸一个像素为原来的 times 倍宽 + for (int x = x1; x < x2; x++) { + re[x] = color; + } + x1 += times; + x2 += times; + } + } + return ColorMatrix.parse(res); + }; + + /** + * 矩阵按照上下的方向进行拉伸操作,使得图像变宽,需要传递参数 times。 + */ + public final static ProTransForm SLIT_BT = (colorMatrix, value) -> { + if (value == null || !value.containsKey("times")) { + throw new OperatorOperationException("矩阵拉伸操作需要您传递配置项 times 其代表每一个像素在原矩阵中的横向拉伸倍数。"); + } + // 获取到横向拉伸倍数 + int times = value.get("times").getIntValue(); + // 开始拉伸 + Color[][] res = new Color[colorMatrix.getRowCount() * times][colorMatrix.getColCount()]; + int x1 = 0, x2 = times; + for (Color[] colors : colorMatrix.toArrays()) { + // 在这里将行拉伸 times 倍 + for (int i = x1; i < x2; i++) { + res[i] = colors.clone(); + } + x1 += times; + x2 += times; + } + return ColorMatrix.parse(res); + }; + private boolean isGrayscale; /** @@ -306,17 +387,7 @@ public static ColorMatrix parse(String inputString) { */ public static ColorMatrix parseGrayscale(String inputString) { Color[][] colors = ASIO.parseImageGetColorArray(inputString); - return GrayscaleColors(colors); - } - - /** - * 将图像URL解析,并获取对应的图像矩阵 - * - * @param url 需要被解析的URL对象 - * @return URL对象所对应的图像矩阵。 - */ - public static ColorMatrix parseGrayscale(URL url) { - return GrayscaleColors(ASIO.parseURLGetColorArray(url)); + return ColorMatrix.GrayscaleColors(colors); } /** @@ -403,6 +474,38 @@ public static ColorMatrix parse(URL url) { return ColorMatrix.parse(ASIO.parseURLGetColorArray(url)); } + + /** + * 将图像URL解析,并获取对应的图像矩阵 + * + * @param url 需要被解析的URL对象 + * @return URL对象所对应的图像矩阵。 + */ + public static ColorMatrix parseGrayscale(URL url) { + return ColorMatrix.GrayscaleColors(ASIO.parseURLGetColorArray(url)); + } + + /** + * 使用组件将一个图像数据提取,并获取对应的图像矩阵。 + * + * @param inputComponent 能够被提取出图像矩阵的数据组件。 + * @return 从组件中提取出来的图像矩阵对象。 + */ + public static ColorMatrix parse(InputComponent inputComponent) { + boolean isOk; + if (!inputComponent.isOpen()) { + isOk = inputComponent.open(); + } else { + isOk = true; + } + if (isOk) { + // 开始进行数据提取 + ColorMatrix parse = ColorMatrix.parse(ASIO.parseImageGetColorArray(inputComponent.getBufferedImage())); + ASIO.close(inputComponent); + return parse; + } else throw new OperatorOperationException("Unable to open your camera."); + } + /** * 将一个图像矩阵中的所有像素转换成为三原色均值,获取到灰度矩阵。 * @@ -511,6 +614,17 @@ public ColorMatrix calculate(Consumer action, boolean isCopy) { } } + /** + * 矩阵变换操作函数,在此函数中您可以传递多个矩阵变换模式,实现多种不同的矩阵转换效果,同时还可以自定义矩阵变换时需要使用的逻辑。 + * + * @param transformation 矩阵变换逻辑。 + * @param value 矩阵中的变换操作计算时需要的其它参数对象。 + * @return 矩阵变换之后返回的新矩阵对象。 + */ + public ColorMatrix converter(ProTransForm transformation, HashMap> value) { + return transformation.function(this, value); + } + /** * 获取到矩阵中指定坐标点的数值 * diff --git a/src_code/src/main/java/zhao/algorithmMagic/operands/vector/SparkVector.scala b/src_code/src/main/java/zhao/algorithmMagic/operands/vector/SparkVector.scala index 42bfc95..7f0bac4 100644 --- a/src_code/src/main/java/zhao/algorithmMagic/operands/vector/SparkVector.scala +++ b/src_code/src/main/java/zhao/algorithmMagic/operands/vector/SparkVector.scala @@ -114,6 +114,14 @@ final class SparkVector(sparkContext: SparkContext, vector: org.apache.spark.mll */ override def getNumberOfDimensions: Int = size + /** + * + * @return 将本对象中存储的向量序列数组拷贝到一个新数组并将新数组返回,这里返回的是一个新数组,支持修改等操作。 + * + * Copy the vector sequence array stored in this object to a new array and return the new array. Here, a new array is returned, which supports modification and other operations. + */ + override def copyToNewArray(): Array[Double] = vector.toArray + /** * 在两个操作数之间做差的方法,具体用法请参阅API说明。 *

@@ -159,14 +167,6 @@ final class SparkVector(sparkContext: SparkContext, vector: org.apache.spark.mll * Objects after disruption. */ override def shuffle(seed: Long): SparkVector = SparkVector.parse(sparkContext, ASMath.shuffle(this.copyToNewArray(), seed, false)) - - /** - * - * @return 将本对象中存储的向量序列数组拷贝到一个新数组并将新数组返回,这里返回的是一个新数组,支持修改等操作。 - * - * Copy the vector sequence array stored in this object to a new array and return the new array. Here, a new array is returned, which supports modification and other operations. - */ - override def copyToNewArray(): Array[Double] = vector.toArray } object SparkVector { diff --git a/src_code/src/main/java/zhao/algorithmMagic/utils/ASIO.java b/src_code/src/main/java/zhao/algorithmMagic/utils/ASIO.java index 045893d..40971a1 100644 --- a/src_code/src/main/java/zhao/algorithmMagic/utils/ASIO.java +++ b/src_code/src/main/java/zhao/algorithmMagic/utils/ASIO.java @@ -184,7 +184,7 @@ public static Color[][] parseImageGetColorArray(File inputFile) throws IOExcepti * @param image 需要被读取的图像缓冲对象 * @return 读取成功之后返回的整形矩阵 */ - private static Color[][] parseImageGetColorArray(BufferedImage image) { + public static Color[][] parseImageGetColorArray(BufferedImage image) { final byte[] pixels = ((DataBufferByte) image.getRaster() .getDataBuffer()) .getData(); @@ -238,6 +238,10 @@ private static Color[][] parseImageGetColorArray(BufferedImage image) { */ public static int[][] parseImageGetArray(File inputFile) throws IOException { BufferedImage image = ImageIO.read(inputFile); + return parseImageGetArray(image); + } + + public static int[][] parseImageGetArray(BufferedImage image) { final byte[] pixels = ((DataBufferByte) image.getRaster().getDataBuffer()).getData(); final int width = image.getWidth(), height = image.getHeight(); final boolean hasAlphaChannel = image.getAlphaRaster() != null; @@ -319,4 +323,17 @@ public static IntegerMatrix[] parseImageGetArrays(File inputFile) throws IOExcep IntegerMatrix.parse(RMat), IntegerMatrix.parse(GMat), IntegerMatrix.parse(BMat) }; } + + /** + * @param closeable 需要被关闭的对象。 + */ + public static void close(Closeable closeable) { + if (closeable != null) { + try { + closeable.close(); + } catch (IOException ignored) { + + } + } + } } diff --git a/src_code/src/main/java/zhao/algorithmMagic/utils/transformation/ProTransForm.java b/src_code/src/main/java/zhao/algorithmMagic/utils/transformation/ProTransForm.java new file mode 100644 index 0000000..9293ee8 --- /dev/null +++ b/src_code/src/main/java/zhao/algorithmMagic/utils/transformation/ProTransForm.java @@ -0,0 +1,28 @@ +package zhao.algorithmMagic.utils.transformation; + +import zhao.algorithmMagic.operands.table.Cell; + +import java.util.HashMap; + +/** + * 带参数的数据转换接口类,其中在接受一个需要被转换的对象之后还需要传递一个配置参数项。 + *

+ * A data conversion interface class with parameters, in which a configuration parameter item needs to be passed after receiving an object that needs to be converted. + * + * @author 赵凌宇 + * 2023/4/5 9:05 + */ +public interface ProTransForm { + + /** + * @param inputType 来自内部的待转换数据。 + * Data to be converted from inside. + * @param value 转换时需要使用的参数列表,其中key是参数名称,value是参数数值的单元格对象,可以封装任意类型的数据,一般情况下,如果不需要传递参数可以直接传递null。 + *

+ * The list of parameters that need to be used during conversion, where key is the parameter name and value is the cell object of the parameter value, which can encapsulate any type of data. Generally, if no parameters need to be passed, null can be passed directly. + * @return 转换之后的数据。 + *

+ * Data after conversion. + */ + OutputType function(InputType inputType, HashMap> value); +} diff --git a/src_code/src/main/resources/log4j2.xml b/src_code/src/main/resources/log4j2.xml index f839453..f109e0a 100644 --- a/src_code/src/main/resources/log4j2.xml +++ b/src_code/src/main/resources/log4j2.xml @@ -15,7 +15,7 @@ - + @@ -25,7 +25,7 @@ - + @@ -34,8 +34,8 @@ - - + + \ No newline at end of file From 166703f34134bc445caa39d43a088ec623cd5a0b Mon Sep 17 00:00:00 2001 From: LingYuZhao Date: Thu, 6 Apr 2023 21:11:47 +0800 Subject: [PATCH 12/17] update --- src_code/pom.xml | 10 +- .../main/java/zhao/algorithmMagic/MAIN1.java | 80 +++--- .../modelAlgorithm/LinearRegression.java | 10 + .../integrator/ImageRenderingIntegrator.java | 17 +- .../zhao/algorithmMagic/io/InputCamera.java | 15 ++ .../algorithmMagic/io/InputComponent.java | 13 + .../zhao/algorithmMagic/io/InputHDFS.java | 216 ++++++++++++++++ .../algorithmMagic/io/InputHDFSBuilder.java | 87 +++++++ .../zhao/algorithmMagic/io/OutputBuilder.java | 37 +++ .../algorithmMagic/io/OutputComponent.java | 80 ++++++ .../zhao/algorithmMagic/io/OutputHDFS.java | 244 ++++++++++++++++++ .../algorithmMagic/io/OutputHDFSBuidler.java | 77 ++++++ .../operands/matrix/ColorMatrix.java | 20 ++ .../operands/matrix/ColumnDoubleMatrix.java | 20 ++ .../operands/matrix/ColumnIntegerMatrix.java | 20 ++ .../operands/matrix/SaveMatrix.java | 13 + .../operands/table/FDataFrame.java | 11 + .../operands/vector/SparkVector.scala | 16 +- src_code/src/main/resources/log4j2.xml | 8 +- 19 files changed, 940 insertions(+), 54 deletions(-) create mode 100644 src_code/src/main/java/zhao/algorithmMagic/io/InputHDFS.java create mode 100644 src_code/src/main/java/zhao/algorithmMagic/io/InputHDFSBuilder.java create mode 100644 src_code/src/main/java/zhao/algorithmMagic/io/OutputBuilder.java create mode 100644 src_code/src/main/java/zhao/algorithmMagic/io/OutputComponent.java create mode 100644 src_code/src/main/java/zhao/algorithmMagic/io/OutputHDFS.java create mode 100644 src_code/src/main/java/zhao/algorithmMagic/io/OutputHDFSBuidler.java diff --git a/src_code/pom.xml b/src_code/pom.xml index 69ce282..85ca51c 100644 --- a/src_code/pom.xml +++ b/src_code/pom.xml @@ -1,6 +1,6 @@ + xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 4.0.0 io.github.BeardedManZhao @@ -89,6 +89,14 @@ 0.3.12 provided + + + + org.apache.hadoop + hadoop-client + 3.3.1 + provided + diff --git a/src_code/src/main/java/zhao/algorithmMagic/MAIN1.java b/src_code/src/main/java/zhao/algorithmMagic/MAIN1.java index ff88547..f79a286 100644 --- a/src_code/src/main/java/zhao/algorithmMagic/MAIN1.java +++ b/src_code/src/main/java/zhao/algorithmMagic/MAIN1.java @@ -11,47 +11,49 @@ import java.awt.*; import java.util.Map; + public class MAIN1 { - public static void main(String[] args) { + public static void main(String[] args) throws InterruptedException { + // 获取到摄像头输入设备 + InputComponent inputComponent = InputCamera.builder() + // 要使用的摄像头的名字 索引 或def默认,我们这里使用的是 def 代表使用默认摄像头 + .addInputArg(InputCameraBuilder.Camera_Index, new FinalCell<>("def")) + // 要使用的拍摄图像格式 + .addInputArg(InputCameraBuilder.Image_Format, new FinalCell<>("JPG")) + // 图像尺寸 这里的数值是 WebcamResolution 枚举类的属性字段 VGA + .addInputArg(InputCameraBuilder.CUSTOM_VIEW_SIZES, new FinalCell<>("VGA")) + .create(); + ColorMatrix colorMatrix1, colorMatrix2; - { - // 获取到摄像头输入设备 - InputComponent inputComponent = InputCamera.builder() - // 要使用的摄像头的名字 索引 或def默认,我们这里使用的是 def 代表使用默认摄像头 - .addInputArg(InputCameraBuilder.Camera_Index, new FinalCell<>("def")) - // 要使用的拍摄图像格式 - .addInputArg(InputCameraBuilder.Image_Format, new FinalCell<>("JPG")) - // 图像尺寸 这里的数值是 WebcamResolution 枚举类的属性字段 VGA - .addInputArg(InputCameraBuilder.CUSTOM_VIEW_SIZES, new FinalCell<>("VGA")) - .create(); - // 将图像与样本读取进来 - colorMatrix1 = ColorMatrix.parse("C:\\Users\\liming\\Desktop\\fsdownload\\YB.bmp"); - colorMatrix2 = ColorMatrix.parse(inputComponent); - ColorMatrix temp = ColorMatrix.parse(colorMatrix2.copyToNewArrays()); - // 开始二值化 - colorMatrix1.localBinary(ColorMatrix._G_, 10, 0xffffff, 0, 1); - temp.localBinary(ColorMatrix._G_, 5, 0xffffff, 0, 20); - temp.erode(2, 2, false); - temp.show("temp"); - // 开始进行模板匹配 并返回最匹配的结果数值,在这里返回的就是所有匹配的结果数据,key为匹配系数 value为匹配结果 - Map.Entry matching = temp.templateMatching( - ManhattanDistance.getInstance("MAN"), - colorMatrix1, - ColorMatrix._G_, - 10, - false - ); - // 开始进行绘制 在这里首先获取到坐标数据 - IntegerCoordinateTwo coordinateTwo = matching.getValue(); - System.out.print("匹配系数 = "); - System.out.println(matching.getKey()); - colorMatrix2.drawRectangle( - coordinateTwo, - new IntegerCoordinateTwo(coordinateTwo.getX() + colorMatrix1.getColCount(), coordinateTwo.getY() + colorMatrix1.getRowCount()), - Color.MAGENTA - ); + colorMatrix1 = ColorMatrix.parse("C:\\Users\\liming\\Desktop\\fsdownload\\YB.bmp"); + int count = 0; + while (++count < 5) { + { + colorMatrix2 = ColorMatrix.parse(inputComponent); + ColorMatrix temp = ColorMatrix.parse(colorMatrix2.copyToNewArrays()); + // 开始二值化 + colorMatrix1.localBinary(ColorMatrix._G_, 10, 0xffffff, 0, 1); + temp.localBinary(ColorMatrix._G_, 5, 0xffffff, 0, 0); + // 开始进行模板匹配 并返回最匹配的结果数值,在这里返回的就是所有匹配的结果数据,key为匹配系数 value为匹配结果 + Map.Entry matching = temp.templateMatching( + ManhattanDistance.getInstance("MAN"), + colorMatrix1, + ColorMatrix._G_, + 10, + false + ); + // 开始进行绘制 在这里首先获取到坐标数据 + IntegerCoordinateTwo coordinateTwo = matching.getValue(); + System.out.print("匹配系数 = "); + System.out.println(matching.getKey()); + colorMatrix2.drawRectangle( + coordinateTwo, + new IntegerCoordinateTwo(coordinateTwo.getX() + colorMatrix1.getColCount(), coordinateTwo.getY() + colorMatrix1.getRowCount()), + Color.MAGENTA + ); + } + colorMatrix2.show("识别结果"); + Thread.sleep(1024); } - colorMatrix1.show("人脸样本"); - colorMatrix2.show("识别结果"); } } \ No newline at end of file diff --git a/src_code/src/main/java/zhao/algorithmMagic/algorithm/modelAlgorithm/LinearRegression.java b/src_code/src/main/java/zhao/algorithmMagic/algorithm/modelAlgorithm/LinearRegression.java index 93ce0d8..6d20701 100644 --- a/src_code/src/main/java/zhao/algorithmMagic/algorithm/modelAlgorithm/LinearRegression.java +++ b/src_code/src/main/java/zhao/algorithmMagic/algorithm/modelAlgorithm/LinearRegression.java @@ -47,10 +47,20 @@ public static LinearRegression getInstance(String Name) { } } + /** + * 设置自变量的列编号 + * + * @param featureIndex 自变量的列编号 + */ public void setFeatureIndex(int featureIndex) { this.featureIndex = featureIndex; } + /** + * 设置因变量的列编号 + * + * @param targetIndex 因变量的列编号 + */ public void setTargetIndex(int targetIndex) { this.targetIndex = targetIndex; } diff --git a/src_code/src/main/java/zhao/algorithmMagic/integrator/ImageRenderingIntegrator.java b/src_code/src/main/java/zhao/algorithmMagic/integrator/ImageRenderingIntegrator.java index 8559a2f..9f406f8 100644 --- a/src_code/src/main/java/zhao/algorithmMagic/integrator/ImageRenderingIntegrator.java +++ b/src_code/src/main/java/zhao/algorithmMagic/integrator/ImageRenderingIntegrator.java @@ -93,6 +93,20 @@ public static boolean draw(String outFilePath, int[][] ints, int imageWidth, int * @return 如果返回true 代表绘制成功! */ public static boolean draw(String outFilePath, Color[][] colors, int imageWidth, int imageHeight, int pixelMagnification, boolean writeNumber) { + BufferedImage image = drawBufferedImage(colors, imageWidth, imageHeight, pixelMagnification, writeNumber); + // 输出图片 + return ASIO.outImage(image, outFilePath); + } + + /** + * @param colors 需要被用来作为图像输出的像素矩阵对象 + * @param imageWidth 需要绘制图像的宽度 + * @param imageHeight 需要绘制图像的高度 + * @param pixelMagnification 矩阵中一个元素代表的像素块倍率,也是一个像素块大小 + * @param writeNumber 如果设置为true,代表输出图片的时候将颜色编号打上去 + * @return 如果返回true 代表绘制成功! + */ + public static BufferedImage drawBufferedImage(Color[][] colors, int imageWidth, int imageHeight, int pixelMagnification, boolean writeNumber) { // 创建一个图片 int width = imageWidth * pixelMagnification; int height = imageHeight * pixelMagnification; @@ -125,8 +139,7 @@ public static boolean draw(String outFilePath, Color[][] colors, int imageWidth, x1 = 0; } } - // 输出图片 - return ASIO.outImage(image, outFilePath); + return image; } @Override diff --git a/src_code/src/main/java/zhao/algorithmMagic/io/InputCamera.java b/src_code/src/main/java/zhao/algorithmMagic/io/InputCamera.java index b4ac523..d98f7c2 100644 --- a/src_code/src/main/java/zhao/algorithmMagic/io/InputCamera.java +++ b/src_code/src/main/java/zhao/algorithmMagic/io/InputCamera.java @@ -3,6 +3,7 @@ import com.github.sarxos.webcam.Webcam; import com.github.sarxos.webcam.WebcamUtils; import zhao.algorithmMagic.exception.OperatorOperationException; +import zhao.algorithmMagic.operands.table.DataFrame; import zhao.algorithmMagic.utils.ASClass; import zhao.algorithmMagic.utils.ASIO; @@ -109,6 +110,20 @@ public double[][] getDouble2Array() { return res; } + /** + * 从数据输入组件获取到 DataFrame 对象,该函数有些数据输入组件可能不支持。 + *

+ * Retrieve the DataFrame object from the data input component, which may not be supported by some data input components. + * + * @return 从数据输入组件中获取到的DataFrame数据封装对象。 + *

+ * The DataFrame data encapsulation object obtained from the data input component. + */ + @Override + public DataFrame getDataFrame() { + throw new UnsupportedOperationException("相机设备不支持获取到DataFrame对象。\nThe camera device does not support obtaining DataFrame objects."); + } + /** * 从数据输入组件中提取出 数据流 对象。 *

diff --git a/src_code/src/main/java/zhao/algorithmMagic/io/InputComponent.java b/src_code/src/main/java/zhao/algorithmMagic/io/InputComponent.java index 9496c03..97ecf48 100644 --- a/src_code/src/main/java/zhao/algorithmMagic/io/InputComponent.java +++ b/src_code/src/main/java/zhao/algorithmMagic/io/InputComponent.java @@ -1,5 +1,7 @@ package zhao.algorithmMagic.io; +import zhao.algorithmMagic.operands.table.DataFrame; + import java.awt.image.BufferedImage; import java.io.Closeable; import java.io.InputStream; @@ -53,6 +55,17 @@ public interface InputComponent extends Closeable { */ double[][] getDouble2Array(); + /** + * 从数据输入组件获取到 DataFrame 对象,该函数有些数据输入组件可能不支持。 + *

+ * Retrieve the DataFrame object from the data input component, which may not be supported by some data input components. + * + * @return 从数据输入组件中获取到的DataFrame数据封装对象。 + *

+ * The DataFrame data encapsulation object obtained from the data input component. + */ + DataFrame getDataFrame(); + /** * 从数据输入组件中提取出 数据流 对象。 *

diff --git a/src_code/src/main/java/zhao/algorithmMagic/io/InputHDFS.java b/src_code/src/main/java/zhao/algorithmMagic/io/InputHDFS.java new file mode 100644 index 0000000..8f16e76 --- /dev/null +++ b/src_code/src/main/java/zhao/algorithmMagic/io/InputHDFS.java @@ -0,0 +1,216 @@ +package zhao.algorithmMagic.io; + +import org.apache.hadoop.fs.FSDataInputStream; +import org.apache.hadoop.fs.FileSystem; +import org.apache.hadoop.fs.Path; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import zhao.algorithmMagic.exception.OperatorOperationException; +import zhao.algorithmMagic.operands.table.*; +import zhao.algorithmMagic.utils.ASIO; +import zhao.algorithmMagic.utils.ASStr; + +import javax.imageio.ImageIO; +import java.awt.image.BufferedImage; +import java.io.*; + +/** + * HDFS数据输入组件对象,能够从HDFS平台中获取到文件数据。 + *

+ * HDFS data input component object, which can obtain file data from the HDFS platform. + * + * @author 赵凌宇 + * 2023/4/6 8:40 + */ +public class InputHDFS implements InputComponent { + + private final static Logger LOGGER = LoggerFactory.getLogger("InputHDFS"); + private final FileSystem fileSystem; + private final Path inputPath; + private final char sep; + private final String charset; + private final Series field; + private final int pk; + private FSDataInputStream fsDataInputStream; + private BufferedReader bufferedReader; + private boolean isOpen; + + /** + * @param fileSystem HDFS 文件系统对象 + * @param inputPath 需要被读取的文件路径 + * @param sep 读取时需要使用的分隔符 + * @param charset 读取时需要使用字符集 + * @param field 读取时的表字段名称行 + * @param pk 读取时的表主键对象 + */ + protected InputHDFS(FileSystem fileSystem, Path inputPath, char sep, String charset, String[] field, int pk) { + if (fileSystem == null || inputPath == null || field == null) { + throw new OperatorOperationException("The parameter in [FileSystem fileSystem, Path inputPath, String[] field] cannot be null!!!!"); + } + this.fileSystem = fileSystem; + this.inputPath = inputPath; + this.sep = sep; + this.charset = charset; + this.field = FieldCell.parse(field); + this.pk = pk; + this.isOpen = false; + } + + /** + * @return 开始构建本数据组件对象。 + *

+ * Start building this data component object. + */ + public static InputBuilder builder() { + return new InputCameraBuilder(); + } + + /** + * 启动数据输入组件 + * + * @return 如果启动成功返回true + */ + @Override + public boolean open() { + try { + LOGGER.info("InputHDFS.open()"); + this.fsDataInputStream = fileSystem.open(this.inputPath); + this.bufferedReader = new BufferedReader(new InputStreamReader(fsDataInputStream)); + return true; + } catch (IOException e) { + LOGGER.error("InputHDFS.open() error!!!", e); + return false; + } + } + + /** + * @return 如果组件已经启动了,在这里返回true + */ + @Override + public boolean isOpen() { + LOGGER.info("InputHDFS.isOpen()"); + return this.isOpen; + } + + /** + * 从数据输入组件中提取出 byte 数组的数据,一般情况下,这里返回的都是一些二进制的数据。 + *

+ * Extract the data of the byte array from the data input component. Generally, the returned data here is some binary data. + * + * @return byte[] 的数据对象 + */ + @Override + public byte[] getByteArray() { + LOGGER.info("getByteArray()"); + ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); + try { + while (bufferedReader.ready()) { + byteArrayOutputStream.write(bufferedReader.readLine().getBytes(charset)); + } + return byteArrayOutputStream.toByteArray(); + } catch (IOException e) { + throw new OperatorOperationException(e); + } finally { + ASIO.close(byteArrayOutputStream); + } + } + + /** + * 从数据输入组件中提取出 int 矩阵数据,一般情况下,这里返回的是一些矩阵元素数据。 + *

+ * From the data input component, int matrix data is increasingly generated. Generally, some matrix element data is returned here. + * + * @return int[][] + */ + @Override + public int[][] getInt2Array() { + LOGGER.info("getInt2Array()"); + LOGGER.warn("Not currently supported getInt2Array()!!!"); + return new int[0][]; + } + + /** + * 从数据输入组件中提取出 int 矩阵数据,一般情况下,这里返回的是一些矩阵元素数据。 + *

+ * From the data input component, double matrix data is increasingly generated. Generally, some matrix element data is returned here. + * + * @return double[][] + */ + @Override + public double[][] getDouble2Array() { + LOGGER.info("getDouble2Array()"); + LOGGER.warn("Not currently supported getDouble2Array()!!!"); + return new double[0][]; + } + + /** + * 从数据输入组件获取到 DataFrame 对象,该函数有些数据输入组件可能不支持。 + *

+ * Retrieve the DataFrame object from the data input component, which may not be supported by some data input components. + * + * @return 从数据输入组件中获取到的DataFrame数据封装对象。 + *

+ * The DataFrame data encapsulation object obtained from the data input component. + */ + @Override + public DataFrame getDataFrame() { + LOGGER.info("getDataFrame()"); + DataFrame select = FDataFrame.select(this.field, pk); + try { + while (bufferedReader.ready()) { + select.insert(FinalSeries.parse(ASStr.splitByChar(bufferedReader.readLine(), sep))); + } + return select; + } catch (IOException e) { + throw new OperatorOperationException(e); + } + } + + /** + * 从数据输入组件中提取出 数据流 对象。 + *

+ * Extract the data flow object from the data input component. + * + * @return 数据输入流对象 + */ + @Override + public InputStream getInputStream() { + LOGGER.info("getInputStream()"); + return this.fsDataInputStream; + } + + /** + * 从数据输入组件中提取出 图像缓存 对象,需要注意的是,该操作在有些情况下可能不被支持。 + *

+ * Extracting image cache objects from the data input component, it should be noted that this operation may not be supported in some cases. + * + * @return 图像缓存对象。 + */ + @Override + public BufferedImage getBufferedImage() { + LOGGER.info("getBufferedImage()"); + try { + return ImageIO.read(this.fsDataInputStream); + } catch (IOException e) { + throw new OperatorOperationException(e); + } + } + + /** + * Closes this stream and releases any system resources associated + * with it. If the stream is already closed then invoking this + * method has no effect. + * + *

As noted in {@link AutoCloseable#close()}, cases where the + * close may fail require careful attention. It is strongly advised + * to relinquish the underlying resources and to internally + * mark the {@code Closeable} as closed, prior to throwing + * the {@code IOException}. + */ + @Override + public void close() { + LOGGER.info("close()"); + ASIO.close(this.bufferedReader); + isOpen = false; + } +} diff --git a/src_code/src/main/java/zhao/algorithmMagic/io/InputHDFSBuilder.java b/src_code/src/main/java/zhao/algorithmMagic/io/InputHDFSBuilder.java new file mode 100644 index 0000000..9f492f6 --- /dev/null +++ b/src_code/src/main/java/zhao/algorithmMagic/io/InputHDFSBuilder.java @@ -0,0 +1,87 @@ +package zhao.algorithmMagic.io; + +import org.apache.hadoop.fs.FileSystem; +import org.apache.hadoop.fs.Path; +import zhao.algorithmMagic.operands.table.Cell; +import zhao.algorithmMagic.operands.table.FinalCell; + +import java.util.HashMap; + +/** + * HDFS数据输入组件建造者对象,在该对象中能够为HDFS的数据输入组件进行构建。 + *

+ * HDFS data input component object, which can obtain file data from the HDFS platform. + * + * @author 赵凌宇 + * 2023/4/6 9:18 + */ +public class InputHDFSBuilder extends HashMap> implements InputBuilder { + + /** + * HDFS文件系统对象 + */ + public final static String FILE_SYSTEM = "FS"; + + /** + * 数据加载分隔符 + */ + public final static String SEP = "sep"; + + /** + * 数据输入路径 + */ + public final static String IN_PATH = "inPath"; + + /** + * 数据输入字符集 + */ + public final static String CHAR_SET = "cs"; + + /** + * 数据输入主键 + */ + public final static String PK = "pk"; + + /** + * 数据字符名称 + */ + public final static String FIELD = "field"; + + /** + * 添加数据输入描述,不同的组件有不同的配置属性,具体可以参阅实现类。 + *

+ * Add data input descriptions, and different components have different configuration properties. Please refer to the implementation class for details. + * + * @param key 属性名称 + *

+ * Attribute Name. + * @param value 属性数值 + *

+ * Attribute Value. + * @return 链式调用,继续构建 + *

+ * Chain call, continue building. + */ + @Override + public InputBuilder addInputArg(String key, FinalCell value) { + this.put(key, value); + return this; + } + + /** + * 将所需的对象构建出来并获取到对应的输入设备对象。 + * + * @return 输入设备对象。 + */ + @Override + public InputComponent create() { + return new InputHDFS( + (FileSystem) this.get(FILE_SYSTEM).getValue(), + new Path(this.get(IN_PATH).toString()), + (char) this.getOrDefault(SEP, new FinalCell<>(',')).getIntValue(), + this.getOrDefault(CHAR_SET, new FinalCell<>("utf-8")).toString(), + (String[]) this.get(FIELD).getValue(), + this.getOrDefault(PK, new FinalCell<>(0)).getIntValue() + ); + } +} diff --git a/src_code/src/main/java/zhao/algorithmMagic/io/OutputBuilder.java b/src_code/src/main/java/zhao/algorithmMagic/io/OutputBuilder.java new file mode 100644 index 0000000..519cfc1 --- /dev/null +++ b/src_code/src/main/java/zhao/algorithmMagic/io/OutputBuilder.java @@ -0,0 +1,37 @@ +package zhao.algorithmMagic.io; + +import zhao.algorithmMagic.operands.table.FinalCell; + +/** + * 数据输出组件建造者类,由该类建造出有效的数据输出组件对象。 + *

+ * The data output component builder class, which constructs effective data output component objects. + * + * @author 赵凌宇 + * 2023/4/6 19:32 + */ +public interface OutputBuilder { + /** + * 添加数据输入描述,不同的组件有不同的配置属性,具体可以参阅实现类。 + *

+ * Add data input descriptions, and different components have different configuration properties. Please refer to the implementation class for details. + * + * @param key 属性名称 + *

+ * Attribute Name. + * @param value 属性数值 + *

+ * Attribute Value. + * @return 链式调用,继续构建 + *

+ * Chain call, continue building. + */ + OutputBuilder addOutputArg(String key, FinalCell value); + + /** + * 将所需的对象构建出来并获取到对应的输入设备对象。 + * + * @return 输入设备对象。 + */ + OutputComponent create(); +} diff --git a/src_code/src/main/java/zhao/algorithmMagic/io/OutputComponent.java b/src_code/src/main/java/zhao/algorithmMagic/io/OutputComponent.java new file mode 100644 index 0000000..e12dcec --- /dev/null +++ b/src_code/src/main/java/zhao/algorithmMagic/io/OutputComponent.java @@ -0,0 +1,80 @@ +package zhao.algorithmMagic.io; + +import zhao.algorithmMagic.operands.matrix.ColorMatrix; +import zhao.algorithmMagic.operands.matrix.ColumnDoubleMatrix; +import zhao.algorithmMagic.operands.matrix.ColumnIntegerMatrix; +import zhao.algorithmMagic.operands.table.DataFrame; + +import java.io.Closeable; + +/** + * 数据输出设备类,此类中包含数据输出逻辑实现,可以通过此接口直接将数据输出到第三方平台。 + *

+ * Data output device class, which includes data output logic implementation, can directly output data to third-party platforms through this interface. + * + * @author 赵凌宇 + * 2023/4/6 19:35 + */ +public interface OutputComponent extends Closeable { + + /** + * 启动数据输出组件. + *

+ * Start data output component. + * + * @return 如果启动成功返回true + */ + boolean open(); + + /** + * @return 如果组件已经启动了,在这里返回true. + *

+ * If the component has already started, return true here + */ + boolean isOpen(); + + /** + * 将一份二进制数据输出。 + *

+ * Output a binary data. + * + * @param data 需要被输出的二进制数据包。 + *

+ * The binary data package that needs to be output. + */ + void writeByteArray(byte[] data); + + /** + * 输出一个 整形 矩阵对象 + * + * @param matrix 需要被输出的矩阵 + */ + void writeMat(ColumnIntegerMatrix matrix); + + /** + * 输出一个 double类型的 矩阵对象 + * + * @param matrix 需要被输出的矩阵 + */ + void writeMat(ColumnDoubleMatrix matrix); + + /** + * 将图像矩阵所包含的图像直接输出到目标。 + *

+ * Directly output the images contained in the image matrix to the target. + * + * @param colorMatrix 需要被输出的图像矩阵对象。 + *

+ * The image matrix object that needs to be output. + */ + void writeImage(ColorMatrix colorMatrix); + + /** + * 将一个 DataFrame 中的数据按照数据输出组件进行输出. + *

+ * Output the data in a DataFrame according to the data output component. + * + * @param dataFrame 需要被输出的数据对象 + */ + void writeDataFrame(DataFrame dataFrame); +} diff --git a/src_code/src/main/java/zhao/algorithmMagic/io/OutputHDFS.java b/src_code/src/main/java/zhao/algorithmMagic/io/OutputHDFS.java new file mode 100644 index 0000000..bf5e5bd --- /dev/null +++ b/src_code/src/main/java/zhao/algorithmMagic/io/OutputHDFS.java @@ -0,0 +1,244 @@ +package zhao.algorithmMagic.io; + +import org.apache.hadoop.fs.FSDataOutputStream; +import org.apache.hadoop.fs.FileSystem; +import org.apache.hadoop.fs.Path; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import zhao.algorithmMagic.exception.OperatorOperationException; +import zhao.algorithmMagic.integrator.ImageRenderingIntegrator; +import zhao.algorithmMagic.operands.matrix.ColorMatrix; +import zhao.algorithmMagic.operands.matrix.ColumnDoubleMatrix; +import zhao.algorithmMagic.operands.matrix.ColumnIntegerMatrix; +import zhao.algorithmMagic.operands.table.Cell; +import zhao.algorithmMagic.operands.table.DataFrame; +import zhao.algorithmMagic.operands.table.Series; +import zhao.algorithmMagic.utils.ASIO; + +import javax.imageio.ImageIO; +import java.awt.image.BufferedImage; +import java.io.BufferedWriter; +import java.io.IOException; +import java.io.OutputStreamWriter; + +/** + * HDFS 平台数据输出设备类,在该类示例化出来的对象中能够直接将数据输出到HDFS文件系统中。 + *

+ * HDFS platform data output device class, which can directly output data to the HDFS file system in the instantiated objects of this class. + * + * @author 赵凌宇 + * 2023/4/6 20:01 + */ +public class OutputHDFS implements OutputComponent { + + private final static Logger LOGGER = LoggerFactory.getLogger("OutputHDFS"); + private final FileSystem fileSystem; + private final Path outputPath; + private final String format; + private final char sep; + private boolean isOpen = false; + private FSDataOutputStream fsDataOutputStream; + private BufferedWriter bufferedWriter; + + /** + * @param fileSystem HDFS 文件系统对象。 + *

+ * HDFS File System Objects. + * @param outputPath HDFS 文件数据输出目录。 + *

+ * HDFS file data output directory. + * @param format HDFS 文件数据输出格式。 + *

+ * HDFS file data output format. + * @param sep 文件数据输出时需要使用的分隔符。 + *

+ * Delimiters required for file data output. + */ + public OutputHDFS(FileSystem fileSystem, Path outputPath, String format, char sep) { + if (fileSystem == null || outputPath == null || format == null) { + throw new OperatorOperationException("The parameter in [FileSystem fileSystem, Path outputPath, String format] cannot be null!!!!"); + } + this.fileSystem = fileSystem; + this.outputPath = outputPath; + this.format = format; + this.sep = sep; + } + + /** + * 启动数据输出组件. + *

+ * Start data output component. + * + * @return 如果启动成功返回true + */ + @Override + public boolean open() { + try { + LOGGER.info("OutputHDFS.open()"); + fsDataOutputStream = fileSystem.create(outputPath); + bufferedWriter = new BufferedWriter(new OutputStreamWriter(fsDataOutputStream)); + this.isOpen = true; + return true; + } catch (IOException e) { + LOGGER.error("OutputHDFS.open() error!!!", e); + return false; + } + } + + /** + * @return 如果组件已经启动了,在这里返回true. + *

+ * If the component has already started, return true here + */ + @Override + public boolean isOpen() { + LOGGER.info("OutputHDFS.isOpen()"); + return this.isOpen; + } + + /** + * 将一份二进制数据输出。 + *

+ * Output a binary data. + * + * @param data 需要被输出的二进制数据包。 + *

+ * The binary data package that needs to be output. + */ + @Override + public void writeByteArray(byte[] data) { + LOGGER.info("writeByteArray(byte[] data)"); + try { + fsDataOutputStream.write(data); + } catch (IOException e) { + throw new OperatorOperationException(e); + } + } + + /** + * 输出一个 整形 矩阵对象 + * + * @param matrix 需要被输出的矩阵 + */ + @Override + public void writeMat(ColumnIntegerMatrix matrix) { + int rowCount = -1; + try { + // 输出列 + bufferedWriter.write("colName"); + String[] rowFieldNames = matrix.getRowFieldNames(); + for (String colName : matrix.getColFieldNames()) { + bufferedWriter.write(sep); + bufferedWriter.write(colName); + } + bufferedWriter.newLine(); + for (int[] ints : matrix.toArrays()) { + bufferedWriter.write(rowFieldNames[++rowCount]); + for (int aInt : ints) { + bufferedWriter.write(sep); + bufferedWriter.write(String.valueOf(aInt)); + } + } + } catch (IOException e) { + throw new OperatorOperationException("Write data exception!", e); + } + } + + /** + * 输出一个 double类型的 矩阵对象 + * + * @param matrix 需要被输出的矩阵 + */ + @Override + public void writeMat(ColumnDoubleMatrix matrix) { + int rowCount = -1; + try { + // 输出列 + bufferedWriter.write("colName"); + String[] rowFieldNames = matrix.getRowFieldNames(); + for (String colName : matrix.getColFieldNames()) { + bufferedWriter.write(sep); + bufferedWriter.write(colName); + } + bufferedWriter.newLine(); + for (double[] ints : matrix.toArrays()) { + bufferedWriter.write(rowFieldNames[++rowCount]); + for (double aInt : ints) { + bufferedWriter.write(sep); + bufferedWriter.write(String.valueOf(aInt)); + } + } + } catch (IOException e) { + throw new OperatorOperationException("Write data exception!", e); + } + } + + /** + * 将图像矩阵所包含的图像直接输出到目标。 + *

+ * Directly output the images contained in the image matrix to the target. + * + * @param colorMatrix 需要被输出的图像矩阵对象。 + *

+ * The image matrix object that needs to be output. + */ + @Override + public void writeImage(ColorMatrix colorMatrix) { + LOGGER.info("writeImage(ColorMatrix colorMatrix)"); + BufferedImage bufferedImage = ImageRenderingIntegrator.drawBufferedImage( + colorMatrix.toArrays(), colorMatrix.getColCount(), colorMatrix.getRowCount(), 1, false + ); + try { + ImageIO.write(bufferedImage, format, fsDataOutputStream); + } catch (IOException e) { + throw new OperatorOperationException(e); + } + } + + /** + * 将一个 DataFrame 中的数据按照数据输出组件进行输出. + *

+ * Output the data in a DataFrame according to the data output component. + * + * @param dataFrame 需要被输出的数据对象 + */ + @Override + public void writeDataFrame(DataFrame dataFrame) { + LOGGER.info("writeDataFrame(DataFrame dataFrame)"); + try { + bufferedWriter.write("rowNumber"); + for (Cell cell : dataFrame.getFields()) { + bufferedWriter.write(sep); + bufferedWriter.write(cell.getStringValue()); + } + int count = 0; + for (Series cells : dataFrame) { + bufferedWriter.write(String.valueOf(++count)); + for (Cell cell : cells) { + bufferedWriter.write(sep); + bufferedWriter.write(cell.toString()); + } + bufferedWriter.newLine(); + } + } catch (IOException e) { + throw new OperatorOperationException(e); + } + } + + /** + * Closes this stream and releases any system resources associated + * with it. If the stream is already closed then invoking this + * method has no effect. + * + *

As noted in {@link AutoCloseable#close()}, cases where the + * close may fail require careful attention. It is strongly advised + * to relinquish the underlying resources and to internally + * mark the {@code Closeable} as closed, prior to throwing + * the {@code IOException}. + */ + @Override + public void close() { + ASIO.close(this.bufferedWriter); + ASIO.close(this.fsDataOutputStream); + } +} diff --git a/src_code/src/main/java/zhao/algorithmMagic/io/OutputHDFSBuidler.java b/src_code/src/main/java/zhao/algorithmMagic/io/OutputHDFSBuidler.java new file mode 100644 index 0000000..be1b705 --- /dev/null +++ b/src_code/src/main/java/zhao/algorithmMagic/io/OutputHDFSBuidler.java @@ -0,0 +1,77 @@ +package zhao.algorithmMagic.io; + +import org.apache.hadoop.fs.FileSystem; +import org.apache.hadoop.fs.Path; +import zhao.algorithmMagic.operands.table.Cell; +import zhao.algorithmMagic.operands.table.FinalCell; + +import java.util.HashMap; + +/** + * HDFS数据输出设备的建造者类,通过该类建造出来HDFS数据输出示例。 + *

+ * The builder class of HDFS data output devices, through which HDFS data output examples are constructed. + * + * @author 赵凌宇 + * 2023/4/6 20:01 + */ +public class OutputHDFSBuidler extends HashMap> implements OutputBuilder { + + /** + * HDFS文件系统对象 + */ + public final static String FILE_SYSTEM = "FS"; + + /** + * 数据加载分隔符 + */ + public final static String SEP = "sep"; + + /** + * 数据输出路径 + */ + public final static String OUT_PATH = "outPath"; + + /** + * 数据输出时的格式,目前支持列表如下所示 + * 文本数据:csv + * 图像数据:Java支持的所有图像格式 + */ + public final static String FORMAT = "format"; + + /** + * 添加数据输入描述,不同的组件有不同的配置属性,具体可以参阅实现类。 + *

+ * Add data input descriptions, and different components have different configuration properties. Please refer to the implementation class for details. + * + * @param key 属性名称 + *

+ * Attribute Name. + * @param value 属性数值 + *

+ * Attribute Value. + * @return 链式调用,继续构建 + *

+ * Chain call, continue building. + */ + @Override + public OutputBuilder addOutputArg(String key, FinalCell value) { + this.put(key, value); + return this; + } + + /** + * 将所需的对象构建出来并获取到对应的输入设备对象。 + * + * @return 输入设备对象。 + */ + @Override + public OutputComponent create() { + return new OutputHDFS( + (FileSystem) this.get(FILE_SYSTEM).getValue(), + (Path) this.get(OUT_PATH).getValue(), + this.get(FORMAT).toString(), + (char) this.get(SEP).getIntValue() + ); + } +} diff --git a/src_code/src/main/java/zhao/algorithmMagic/operands/matrix/ColorMatrix.java b/src_code/src/main/java/zhao/algorithmMagic/operands/matrix/ColorMatrix.java index 851e4a7..1dd2bf4 100644 --- a/src_code/src/main/java/zhao/algorithmMagic/operands/matrix/ColorMatrix.java +++ b/src_code/src/main/java/zhao/algorithmMagic/operands/matrix/ColorMatrix.java @@ -4,6 +4,7 @@ import zhao.algorithmMagic.exception.OperatorOperationException; import zhao.algorithmMagic.integrator.ImageRenderingIntegrator; import zhao.algorithmMagic.io.InputComponent; +import zhao.algorithmMagic.io.OutputComponent; import zhao.algorithmMagic.operands.coordinate.IntegerCoordinateTwo; import zhao.algorithmMagic.operands.matrix.block.IntegerMatrixSpace; import zhao.algorithmMagic.operands.table.Cell; @@ -1827,6 +1828,25 @@ public void save(File path, char sep) { }); } + /** + * 将对象交由第三方数据输出组件进行数据的输出。 + *

+ * Submit the object to a third-party data output component for data output. + * + * @param outputComponent 第三方数据输出设备对象实现。 + *

+ * Implementation of third-party data output device objects. + */ + @Override + public void save(OutputComponent outputComponent) { + if (!outputComponent.isOpen()) { + if (!outputComponent.open()) + throw new OperatorOperationException("您的数据输出组件打开失败。\nYour data output component failed to open."); + } + outputComponent.writeImage(this); + ASIO.close(outputComponent); + } + /** * 将图像的 ASCII 图像输出到指定的目录中。 * diff --git a/src_code/src/main/java/zhao/algorithmMagic/operands/matrix/ColumnDoubleMatrix.java b/src_code/src/main/java/zhao/algorithmMagic/operands/matrix/ColumnDoubleMatrix.java index 285c828..6e11a16 100644 --- a/src_code/src/main/java/zhao/algorithmMagic/operands/matrix/ColumnDoubleMatrix.java +++ b/src_code/src/main/java/zhao/algorithmMagic/operands/matrix/ColumnDoubleMatrix.java @@ -2,6 +2,7 @@ import zhao.algorithmMagic.core.ASDynamicLibrary; import zhao.algorithmMagic.exception.OperatorOperationException; +import zhao.algorithmMagic.io.OutputComponent; import zhao.algorithmMagic.operands.RCNOperands; import zhao.algorithmMagic.operands.vector.DoubleVector; import zhao.algorithmMagic.utils.ASClass; @@ -677,4 +678,23 @@ public void save(File path, char sep) { } }); } + + /** + * 将对象交由第三方数据输出组件进行数据的输出。 + *

+ * Submit the object to a third-party data output component for data output. + * + * @param outputComponent 第三方数据输出设备对象实现。 + *

+ * Implementation of third-party data output device objects. + */ + @Override + public void save(OutputComponent outputComponent) { + if (!outputComponent.isOpen()) { + if (!outputComponent.open()) + throw new OperatorOperationException("您的数据输出组件打开失败。\nYour data output component failed to open."); + } + outputComponent.writeMat(this); + ASIO.close(outputComponent); + } } diff --git a/src_code/src/main/java/zhao/algorithmMagic/operands/matrix/ColumnIntegerMatrix.java b/src_code/src/main/java/zhao/algorithmMagic/operands/matrix/ColumnIntegerMatrix.java index 7549490..b863083 100644 --- a/src_code/src/main/java/zhao/algorithmMagic/operands/matrix/ColumnIntegerMatrix.java +++ b/src_code/src/main/java/zhao/algorithmMagic/operands/matrix/ColumnIntegerMatrix.java @@ -2,6 +2,7 @@ import zhao.algorithmMagic.core.ASDynamicLibrary; import zhao.algorithmMagic.exception.OperatorOperationException; +import zhao.algorithmMagic.io.OutputComponent; import zhao.algorithmMagic.operands.RCNOperands; import zhao.algorithmMagic.operands.vector.IntegerVector; import zhao.algorithmMagic.utils.ASClass; @@ -672,4 +673,23 @@ public void save(File path, char sep) { } }); } + + /** + * 将对象交由第三方数据输出组件进行数据的输出。 + *

+ * Submit the object to a third-party data output component for data output. + * + * @param outputComponent 第三方数据输出设备对象实现。 + *

+ * Implementation of third-party data output device objects. + */ + @Override + public void save(OutputComponent outputComponent) { + if (!outputComponent.isOpen()) { + if (!outputComponent.open()) + throw new OperatorOperationException("您的数据输出组件打开失败。\nYour data output component failed to open."); + } + outputComponent.writeMat(this); + ASIO.close(outputComponent); + } } diff --git a/src_code/src/main/java/zhao/algorithmMagic/operands/matrix/SaveMatrix.java b/src_code/src/main/java/zhao/algorithmMagic/operands/matrix/SaveMatrix.java index 8de93d2..cf9c386 100644 --- a/src_code/src/main/java/zhao/algorithmMagic/operands/matrix/SaveMatrix.java +++ b/src_code/src/main/java/zhao/algorithmMagic/operands/matrix/SaveMatrix.java @@ -1,5 +1,7 @@ package zhao.algorithmMagic.operands.matrix; +import zhao.algorithmMagic.io.OutputComponent; + import java.io.File; /** @@ -50,4 +52,15 @@ public interface SaveMatrix { * Separator used when saving. */ void save(File path, char sep); + + /** + * 将对象交由第三方数据输出组件进行数据的输出。 + *

+ * Submit the object to a third-party data output component for data output. + * + * @param outputComponent 第三方数据输出设备对象实现。 + *

+ * Implementation of third-party data output device objects. + */ + void save(OutputComponent outputComponent); } diff --git a/src_code/src/main/java/zhao/algorithmMagic/operands/table/FDataFrame.java b/src_code/src/main/java/zhao/algorithmMagic/operands/table/FDataFrame.java index d057b75..6ab574c 100644 --- a/src_code/src/main/java/zhao/algorithmMagic/operands/table/FDataFrame.java +++ b/src_code/src/main/java/zhao/algorithmMagic/operands/table/FDataFrame.java @@ -1,6 +1,7 @@ package zhao.algorithmMagic.operands.table; import zhao.algorithmMagic.exception.OperatorOperationException; +import zhao.algorithmMagic.io.InputComponent; import zhao.algorithmMagic.utils.ASIO; import zhao.algorithmMagic.utils.ASMath; import zhao.algorithmMagic.utils.transformation.Transformation; @@ -91,6 +92,16 @@ public static FDataFrame select(Series colNameRow, int pk) { ).refreshField(false, true); } + /** + * 使用第三方数据源输入组件进行数据的加载,并获取到对应的DataFrame对象。 + * + * @param inputComponent 需要使用的第三方数据输入组件对象 + * @return 获取到的DataFrame对象。 + */ + public static DataFrame builder(InputComponent inputComponent) { + return inputComponent.getDataFrame(); + } + /** * 刷新字段数据,在数据集中包含针对行列字段名称构建的索引Hash表,在hash表中的字段时可以进行刷新的,经过刷新之后,原先的字段将不会消失,而是与新字段共同指向同一个数据行,一般来说,不更改行字段的情况下将不会调用该函数。 *

diff --git a/src_code/src/main/java/zhao/algorithmMagic/operands/vector/SparkVector.scala b/src_code/src/main/java/zhao/algorithmMagic/operands/vector/SparkVector.scala index 7f0bac4..1d277f7 100644 --- a/src_code/src/main/java/zhao/algorithmMagic/operands/vector/SparkVector.scala +++ b/src_code/src/main/java/zhao/algorithmMagic/operands/vector/SparkVector.scala @@ -53,6 +53,14 @@ final class SparkVector(sparkContext: SparkContext, vector: org.apache.spark.mll else throw new OperatorOperationException("'DoubleVector1 multiply DoubleVector2' 时,两个'DoubleVector'的向量所包含的数量不同,DoubleVector1=[" + length1 + "],DoubleVector2=[" + length2 + "]\n" + "When 'DoubleVector1 multiply DoubleVector2', the two vectors of 'DoubleVector' contain different quantities, DoubleVector1=[" + length1 + "], DoubleVector2=[" + length2 + "]") } + /** + * + * @return 将本对象中存储的向量序列数组拷贝到一个新数组并将新数组返回,这里返回的是一个新数组,支持修改等操作。 + * + * Copy the vector sequence array stored in this object to a new array and return the new array. Here, a new array is returned, which supports modification and other operations. + */ + override def copyToNewArray(): Array[Double] = vector.toArray + /** * 计算两个向量的内积,也称之为数量积,具体实现请参阅api说明 *

@@ -114,14 +122,6 @@ final class SparkVector(sparkContext: SparkContext, vector: org.apache.spark.mll */ override def getNumberOfDimensions: Int = size - /** - * - * @return 将本对象中存储的向量序列数组拷贝到一个新数组并将新数组返回,这里返回的是一个新数组,支持修改等操作。 - * - * Copy the vector sequence array stored in this object to a new array and return the new array. Here, a new array is returned, which supports modification and other operations. - */ - override def copyToNewArray(): Array[Double] = vector.toArray - /** * 在两个操作数之间做差的方法,具体用法请参阅API说明。 *

diff --git a/src_code/src/main/resources/log4j2.xml b/src_code/src/main/resources/log4j2.xml index f109e0a..f839453 100644 --- a/src_code/src/main/resources/log4j2.xml +++ b/src_code/src/main/resources/log4j2.xml @@ -15,7 +15,7 @@ - + @@ -25,7 +25,7 @@ - + @@ -34,8 +34,8 @@ - - + + \ No newline at end of file From a96e1847f6225062915908b77b24eb7bfb903020 Mon Sep 17 00:00:00 2001 From: LingYuZhao Date: Fri, 7 Apr 2023 15:42:16 +0800 Subject: [PATCH 13/17] update --- README-Chinese.md | 8 ++++ README.md | 10 +++- src_code/README-Chinese.md | 8 +++- src_code/README.md | 8 +++- .../main/java/zhao/algorithmMagic/MAIN1.java | 48 +++++++++---------- .../algorithm/OperationAlgorithmManager.java | 2 +- .../operands/vector/SparkVector.scala | 30 ++++++------ 7 files changed, 70 insertions(+), 44 deletions(-) diff --git a/README-Chinese.md b/README-Chinese.md index 592a668..aa89f66 100644 --- a/README-Chinese.md +++ b/README-Chinese.md @@ -94,6 +94,14 @@ AS库在针对数据库,Spark等各种平台对接的时候,需要使用到 webcam-capture 0.3.12 + + + + org.apache.hadoop + hadoop-client + 3.3.1 + + ``` diff --git a/README.md b/README.md index af475ad..d4cd55f 100644 --- a/README.md +++ b/README.md @@ -99,12 +99,20 @@ need to, you can refer to the following configuration. 3.1.3 - + com.github.sarxos webcam-capture 0.3.12 + + + + org.apache.hadoop + hadoop-client + 3.3.1 + + ``` diff --git a/src_code/README-Chinese.md b/src_code/README-Chinese.md index 469ef14..2d7acf0 100644 --- a/src_code/README-Chinese.md +++ b/src_code/README-Chinese.md @@ -8,7 +8,7 @@ ### 更新日志 -* 框架版本:1.17 - x.xx +* 框架版本:1.17 - 1.18 * 为图像矩阵添加diff函数的实现支持,同时为该矩阵对象添加强大的求差聚合计算功能。 ```java @@ -488,4 +488,10 @@ public class MAIN1 { } ``` +* 支持通过 HDFS 获取到操作数对象以及数据的保存操作。 + +```java +// TODO 等待书写 +``` + ### Version update date : xx xx-xx-xx \ No newline at end of file diff --git a/src_code/README.md b/src_code/README.md index af073fb..4669462 100644 --- a/src_code/README.md +++ b/src_code/README.md @@ -8,7 +8,7 @@ ### Update log: -* Framework version: 1.17 - x.xx +* Framework version: 1.17 - 1.18 * Add implementation support for the diff function to the image matrix, and add powerful subtractive aggregation calculation functions to the matrix object. @@ -462,4 +462,10 @@ public class MAIN1 { } ``` +* Support the saving operation of obtaining operand objects and data through HDFS. + +```java +// TODO 等待书写 +``` + ### Version update date : xx xx-xx-xx \ No newline at end of file diff --git a/src_code/src/main/java/zhao/algorithmMagic/MAIN1.java b/src_code/src/main/java/zhao/algorithmMagic/MAIN1.java index f79a286..304ca3e 100644 --- a/src_code/src/main/java/zhao/algorithmMagic/MAIN1.java +++ b/src_code/src/main/java/zhao/algorithmMagic/MAIN1.java @@ -17,7 +17,7 @@ public static void main(String[] args) throws InterruptedException { // 获取到摄像头输入设备 InputComponent inputComponent = InputCamera.builder() // 要使用的摄像头的名字 索引 或def默认,我们这里使用的是 def 代表使用默认摄像头 - .addInputArg(InputCameraBuilder.Camera_Index, new FinalCell<>("def")) + .addInputArg(InputCameraBuilder.Camera_Index, new FinalCell<>("0")) // 要使用的拍摄图像格式 .addInputArg(InputCameraBuilder.Image_Format, new FinalCell<>("JPG")) // 图像尺寸 这里的数值是 WebcamResolution 枚举类的属性字段 VGA @@ -28,30 +28,28 @@ public static void main(String[] args) throws InterruptedException { colorMatrix1 = ColorMatrix.parse("C:\\Users\\liming\\Desktop\\fsdownload\\YB.bmp"); int count = 0; while (++count < 5) { - { - colorMatrix2 = ColorMatrix.parse(inputComponent); - ColorMatrix temp = ColorMatrix.parse(colorMatrix2.copyToNewArrays()); - // 开始二值化 - colorMatrix1.localBinary(ColorMatrix._G_, 10, 0xffffff, 0, 1); - temp.localBinary(ColorMatrix._G_, 5, 0xffffff, 0, 0); - // 开始进行模板匹配 并返回最匹配的结果数值,在这里返回的就是所有匹配的结果数据,key为匹配系数 value为匹配结果 - Map.Entry matching = temp.templateMatching( - ManhattanDistance.getInstance("MAN"), - colorMatrix1, - ColorMatrix._G_, - 10, - false - ); - // 开始进行绘制 在这里首先获取到坐标数据 - IntegerCoordinateTwo coordinateTwo = matching.getValue(); - System.out.print("匹配系数 = "); - System.out.println(matching.getKey()); - colorMatrix2.drawRectangle( - coordinateTwo, - new IntegerCoordinateTwo(coordinateTwo.getX() + colorMatrix1.getColCount(), coordinateTwo.getY() + colorMatrix1.getRowCount()), - Color.MAGENTA - ); - } + colorMatrix2 = ColorMatrix.parse(inputComponent); + ColorMatrix temp = ColorMatrix.parse(colorMatrix2.copyToNewArrays()); + // 开始二值化 + colorMatrix1.localBinary(ColorMatrix._G_, 10, 0xffffff, 0, 1); + temp.localBinary(ColorMatrix._G_, 5, 0xffffff, 0, 0); + // 开始进行模板匹配 并返回最匹配的结果数值,在这里返回的就是所有匹配的结果数据,key为匹配系数 value为匹配结果 + Map.Entry matching = temp.templateMatching( + ManhattanDistance.getInstance("MAN"), + colorMatrix1, + ColorMatrix._G_, + 10, + false + ); + // 开始进行绘制 在这里首先获取到坐标数据 + IntegerCoordinateTwo coordinateTwo = matching.getValue(); + System.out.print("匹配系数 = "); + System.out.println(matching.getKey()); + colorMatrix2.drawRectangle( + coordinateTwo, + new IntegerCoordinateTwo(coordinateTwo.getX() + colorMatrix1.getColCount(), coordinateTwo.getY() + colorMatrix1.getRowCount()), + Color.MAGENTA + ); colorMatrix2.show("识别结果"); Thread.sleep(1024); } diff --git a/src_code/src/main/java/zhao/algorithmMagic/algorithm/OperationAlgorithmManager.java b/src_code/src/main/java/zhao/algorithmMagic/algorithm/OperationAlgorithmManager.java index 767f1a4..a9f879a 100644 --- a/src_code/src/main/java/zhao/algorithmMagic/algorithm/OperationAlgorithmManager.java +++ b/src_code/src/main/java/zhao/algorithmMagic/algorithm/OperationAlgorithmManager.java @@ -18,7 +18,7 @@ * @author LingYuZhao */ public final class OperationAlgorithmManager implements OperationAlgorithm { - public final static float VERSION = 1.17f; + public final static float VERSION = 1.18f; /** * 计算组件的日志打印开关,当此处值为false的时候,计算组件中的日志将不会被打印,logger也不会被调用,一般来说,这里为了减少冗余的字符串实例化操作,会设置为false,当需要调试的时候才需要打开此处的数值。 *

diff --git a/src_code/src/main/java/zhao/algorithmMagic/operands/vector/SparkVector.scala b/src_code/src/main/java/zhao/algorithmMagic/operands/vector/SparkVector.scala index 1d277f7..646e8bf 100644 --- a/src_code/src/main/java/zhao/algorithmMagic/operands/vector/SparkVector.scala +++ b/src_code/src/main/java/zhao/algorithmMagic/operands/vector/SparkVector.scala @@ -53,14 +53,6 @@ final class SparkVector(sparkContext: SparkContext, vector: org.apache.spark.mll else throw new OperatorOperationException("'DoubleVector1 multiply DoubleVector2' 时,两个'DoubleVector'的向量所包含的数量不同,DoubleVector1=[" + length1 + "],DoubleVector2=[" + length2 + "]\n" + "When 'DoubleVector1 multiply DoubleVector2', the two vectors of 'DoubleVector' contain different quantities, DoubleVector1=[" + length1 + "], DoubleVector2=[" + length2 + "]") } - /** - * - * @return 将本对象中存储的向量序列数组拷贝到一个新数组并将新数组返回,这里返回的是一个新数组,支持修改等操作。 - * - * Copy the vector sequence array stored in this object to a new array and return the new array. Here, a new array is returned, which supports modification and other operations. - */ - override def copyToNewArray(): Array[Double] = vector.toArray - /** * 计算两个向量的内积,也称之为数量积,具体实现请参阅api说明 *

@@ -115,13 +107,6 @@ final class SparkVector(sparkContext: SparkContext, vector: org.apache.spark.mll else throw new OperatorOperationException("'DoubleVector1 add DoubleVector2' 时,两个'DoubleVector'的向量所包含的数量不同,DoubleVector1=[" + numberOfDimensions1 + "],DoubleVector2=[" + numberOfDimensions2 + "]\n" + "When 'DoubleVector1 add DoubleVector2', the two vectors of 'DoubleVector' contain different quantities, DoubleVector1=[" + numberOfDimensions1 + "], DoubleVector2=[" + numberOfDimensions2 + "]") } - /** - * @return 向量中包含的维度数量 - *

- * the number of dimensions contained in the vector - */ - override def getNumberOfDimensions: Int = size - /** * 在两个操作数之间做差的方法,具体用法请参阅API说明。 *

@@ -146,6 +131,13 @@ final class SparkVector(sparkContext: SparkContext, vector: org.apache.spark.mll else throw new OperatorOperationException("'DoubleVector1 diff DoubleVector2' 时,两个'DoubleVector'的向量所包含的数量不同,DoubleVector1=[" + numberOfDimensions1 + "],DoubleVector2=[" + numberOfDimensions2 + "]\n" + "When 'DoubleVector1 diff DoubleVector2', the two vectors of 'DoubleVector' contain different quantities, DoubleVector1=[" + numberOfDimensions1 + "], DoubleVector2=[" + numberOfDimensions2 + "]") } + /** + * @return 向量中包含的维度数量 + *

+ * the number of dimensions contained in the vector + */ + override def getNumberOfDimensions: Int = size + /** * * @return 第三方向量中所维护的向量序列,通过此函数您可以直接获取到第三方库中的对象。 @@ -167,6 +159,14 @@ final class SparkVector(sparkContext: SparkContext, vector: org.apache.spark.mll * Objects after disruption. */ override def shuffle(seed: Long): SparkVector = SparkVector.parse(sparkContext, ASMath.shuffle(this.copyToNewArray(), seed, false)) + + /** + * + * @return 将本对象中存储的向量序列数组拷贝到一个新数组并将新数组返回,这里返回的是一个新数组,支持修改等操作。 + * + * Copy the vector sequence array stored in this object to a new array and return the new array. Here, a new array is returned, which supports modification and other operations. + */ + override def copyToNewArray(): Array[Double] = vector.toArray } object SparkVector { From d5e54f7a0c391c69ede5f49c6b907b5859013143 Mon Sep 17 00:00:00 2001 From: LingYuZhao Date: Fri, 7 Apr 2023 20:04:09 +0800 Subject: [PATCH 14/17] update MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * 更新HDFS数据IO组件 --- src_code/README-Chinese.md | 68 ++++++++++++++++++- src_code/README.md | 68 ++++++++++++++++++- src_code/pom.xml | 13 +++- .../main/java/zhao/algorithmMagic/MAIN1.java | 2 +- .../zhao/algorithmMagic/io/InputHDFS.java | 2 +- .../algorithmMagic/io/InputHDFSBuilder.java | 3 +- .../zhao/algorithmMagic/io/OutputHDFS.java | 5 ++ .../algorithmMagic/io/OutputHDFSBuidler.java | 3 +- .../operands/table/DataFrame.java | 11 +++ .../operands/table/FDataFrame.java | 25 ++++++- .../operands/vector/SparkVector.scala | 14 ++-- 11 files changed, 197 insertions(+), 17 deletions(-) diff --git a/src_code/README-Chinese.md b/src_code/README-Chinese.md index 2d7acf0..1957b9c 100644 --- a/src_code/README-Chinese.md +++ b/src_code/README-Chinese.md @@ -491,7 +491,73 @@ public class MAIN1 { * 支持通过 HDFS 获取到操作数对象以及数据的保存操作。 ```java -// TODO 等待书写 +package zhao.algorithmMagic; + +import org.apache.hadoop.conf.Configuration; +import org.apache.hadoop.fs.FileSystem; +import org.apache.hadoop.fs.Path; +import zhao.algorithmMagic.io.*; +import zhao.algorithmMagic.operands.table.DataFrame; +import zhao.algorithmMagic.operands.table.FDataFrame; +import zhao.algorithmMagic.operands.table.FinalCell; + +import java.io.IOException; + + +public class MAIN1 { + public static void main(String[] args) throws IOException { + // 获取到 HDFS 数据输入输出设备对象 首先将两者需要的参数创建出来 + FileSystem fileSystem; + String inPath; + String[] field; + { + Path path = new Path("hdfs://192.168.0.141:8020"); + // 构建HDFS文件系统对象 + fileSystem = path.getFileSystem(new Configuration()); + // 设置需要被读取的文件对象 + inPath = "hdfs://192.168.0.141:8020/data/test.txt"; + // 设置字段头部 + field = new String[]{"name", "sex", "age"}; + } + + /* ***************************************************** + * TODO 从HDFS中加载数据 + * *****************************************************/ + // 开始将所有的参数配置到设备对象中,构建出数据输入设备 + InputComponent inputComponent = InputHDFS.builder() + .addInputArg(InputHDFSBuilder.FILE_SYSTEM, new FinalCell<>(fileSystem)) + .addInputArg(InputHDFSBuilder.IN_PATH, new FinalCell<>(inPath)) + .addInputArg(InputHDFSBuilder.FIELD, new FinalCell<>(field)) + // 设置分隔符 + .addInputArg(InputHDFSBuilder.SEP, new FinalCell<>('\t')) + .create(); + + /* ***************************************************** + * TODO 处理加载出来的 DataFrame 对象,这里是按照 age 正序排序 + * *****************************************************/ + // 开始将 HDFS 中的数据加载成为一个 DataFrame对象 + DataFrame dataFrame = FDataFrame + .builder(inputComponent) + .sort("age"); + + /* ***************************************************** + * TODO 将处理好的数据输出到 HDFS 的 /data/res.csv 中 + * *****************************************************/ + // 实例化出来目标路径 + Path path = new Path("hdfs://192.168.0.141:8020/data/res.csv"); + // 开始构建数据输出组件 + OutputComponent outputComponent = OutputHDFS.builder() + .addOutputArg(OutputHDFSBuidler.FILE_SYSTEM, new FinalCell<>(fileSystem)) + .addOutputArg(OutputHDFSBuidler.SEP, new FinalCell<>(',')) + .addOutputArg(OutputHDFSBuidler.OUT_PATH, new FinalCell<>(path)) + .addOutputArg(OutputHDFSBuidler.FORMAT, new FinalCell<>("csv")) + .create(); + // 开始通过组件将数据输出 同时打印出结果 + System.out.println( + dataFrame.into_outComponent(outputComponent) + ); + } +} ``` ### Version update date : xx xx-xx-xx \ No newline at end of file diff --git a/src_code/README.md b/src_code/README.md index 4669462..47d6770 100644 --- a/src_code/README.md +++ b/src_code/README.md @@ -465,7 +465,73 @@ public class MAIN1 { * Support the saving operation of obtaining operand objects and data through HDFS. ```java -// TODO 等待书写 +package zhao.algorithmMagic; + +import org.apache.hadoop.conf.Configuration; +import org.apache.hadoop.fs.FileSystem; +import org.apache.hadoop.fs.Path; +import zhao.algorithmMagic.io.*; +import zhao.algorithmMagic.operands.table.DataFrame; +import zhao.algorithmMagic.operands.table.FDataFrame; +import zhao.algorithmMagic.operands.table.FinalCell; + +import java.io.IOException; + + +public class MAIN1 { + public static void main(String[] args) throws IOException { + // 获取到 HDFS 数据输入输出设备对象 首先将两者需要的参数创建出来 + FileSystem fileSystem; + String inPath; + String[] field; + { + Path path = new Path("hdfs://192.168.0.141:8020"); + // 构建HDFS文件系统对象 + fileSystem = path.getFileSystem(new Configuration()); + // 设置需要被读取的文件对象 + inPath = "hdfs://192.168.0.141:8020/data/test.txt"; + // 设置字段头部 + field = new String[]{"name", "sex", "age"}; + } + + /* ***************************************************** + * TODO 从HDFS中加载数据 + * *****************************************************/ + // 开始将所有的参数配置到设备对象中,构建出数据输入设备 + InputComponent inputComponent = InputHDFS.builder() + .addInputArg(InputHDFSBuilder.FILE_SYSTEM, new FinalCell<>(fileSystem)) + .addInputArg(InputHDFSBuilder.IN_PATH, new FinalCell<>(inPath)) + .addInputArg(InputHDFSBuilder.FIELD, new FinalCell<>(field)) + // 设置分隔符 + .addInputArg(InputHDFSBuilder.SEP, new FinalCell<>('\t')) + .create(); + + /* ***************************************************** + * TODO 处理加载出来的 DataFrame 对象,这里是按照 age 正序排序 + * *****************************************************/ + // 开始将 HDFS 中的数据加载成为一个 DataFrame对象 + DataFrame dataFrame = FDataFrame + .builder(inputComponent) + .sort("age"); + + /* ***************************************************** + * TODO 将处理好的数据输出到 HDFS 的 /data/res.csv 中 + * *****************************************************/ + // 实例化出来目标路径 + Path path = new Path("hdfs://192.168.0.141:8020/data/res.csv"); + // 开始构建数据输出组件 + OutputComponent outputComponent = OutputHDFS.builder() + .addOutputArg(OutputHDFSBuidler.FILE_SYSTEM, new FinalCell<>(fileSystem)) + .addOutputArg(OutputHDFSBuidler.SEP, new FinalCell<>(',')) + .addOutputArg(OutputHDFSBuidler.OUT_PATH, new FinalCell<>(path)) + .addOutputArg(OutputHDFSBuidler.FORMAT, new FinalCell<>("csv")) + .create(); + // 开始通过组件将数据输出 同时打印出结果 + System.out.println( + dataFrame.into_outComponent(outputComponent) + ); + } +} ``` ### Version update date : xx xx-xx-xx \ No newline at end of file diff --git a/src_code/pom.xml b/src_code/pom.xml index 85ca51c..ea2d19a 100644 --- a/src_code/pom.xml +++ b/src_code/pom.xml @@ -87,15 +87,22 @@ com.github.sarxos webcam-capture 0.3.12 - provided + org.apache.hadoop hadoop-client - 3.3.1 - provided + 3.3.2 + + + + + org.apache.hadoop + hadoop-hdfs + 3.3.2 + diff --git a/src_code/src/main/java/zhao/algorithmMagic/MAIN1.java b/src_code/src/main/java/zhao/algorithmMagic/MAIN1.java index 304ca3e..d406903 100644 --- a/src_code/src/main/java/zhao/algorithmMagic/MAIN1.java +++ b/src_code/src/main/java/zhao/algorithmMagic/MAIN1.java @@ -25,7 +25,7 @@ public static void main(String[] args) throws InterruptedException { .create(); ColorMatrix colorMatrix1, colorMatrix2; - colorMatrix1 = ColorMatrix.parse("C:\\Users\\liming\\Desktop\\fsdownload\\YB.bmp"); + colorMatrix1 = ColorMatrix.parse("C:\\Users\\zhao\\Desktop\\fsdownload\\YB.bmp"); int count = 0; while (++count < 5) { colorMatrix2 = ColorMatrix.parse(inputComponent); diff --git a/src_code/src/main/java/zhao/algorithmMagic/io/InputHDFS.java b/src_code/src/main/java/zhao/algorithmMagic/io/InputHDFS.java index 8f16e76..49f702c 100644 --- a/src_code/src/main/java/zhao/algorithmMagic/io/InputHDFS.java +++ b/src_code/src/main/java/zhao/algorithmMagic/io/InputHDFS.java @@ -62,7 +62,7 @@ protected InputHDFS(FileSystem fileSystem, Path inputPath, char sep, String char * Start building this data component object. */ public static InputBuilder builder() { - return new InputCameraBuilder(); + return new InputHDFSBuilder(); } /** diff --git a/src_code/src/main/java/zhao/algorithmMagic/io/InputHDFSBuilder.java b/src_code/src/main/java/zhao/algorithmMagic/io/InputHDFSBuilder.java index 9f492f6..7b85335 100644 --- a/src_code/src/main/java/zhao/algorithmMagic/io/InputHDFSBuilder.java +++ b/src_code/src/main/java/zhao/algorithmMagic/io/InputHDFSBuilder.java @@ -4,6 +4,7 @@ import org.apache.hadoop.fs.Path; import zhao.algorithmMagic.operands.table.Cell; import zhao.algorithmMagic.operands.table.FinalCell; +import zhao.algorithmMagic.utils.ASClass; import java.util.HashMap; @@ -78,7 +79,7 @@ public InputComponent create() { return new InputHDFS( (FileSystem) this.get(FILE_SYSTEM).getValue(), new Path(this.get(IN_PATH).toString()), - (char) this.getOrDefault(SEP, new FinalCell<>(',')).getIntValue(), + ASClass., Cell>transform(this.getOrDefault(SEP, new FinalCell<>(','))).getValue(), this.getOrDefault(CHAR_SET, new FinalCell<>("utf-8")).toString(), (String[]) this.get(FIELD).getValue(), this.getOrDefault(PK, new FinalCell<>(0)).getIntValue() diff --git a/src_code/src/main/java/zhao/algorithmMagic/io/OutputHDFS.java b/src_code/src/main/java/zhao/algorithmMagic/io/OutputHDFS.java index bf5e5bd..9ab8fab 100644 --- a/src_code/src/main/java/zhao/algorithmMagic/io/OutputHDFS.java +++ b/src_code/src/main/java/zhao/algorithmMagic/io/OutputHDFS.java @@ -64,6 +64,10 @@ public OutputHDFS(FileSystem fileSystem, Path outputPath, String format, char se this.sep = sep; } + public static OutputBuilder builder() { + return new OutputHDFSBuidler(); + } + /** * 启动数据输出组件. *

@@ -211,6 +215,7 @@ public void writeDataFrame(DataFrame dataFrame) { bufferedWriter.write(sep); bufferedWriter.write(cell.getStringValue()); } + bufferedWriter.newLine(); int count = 0; for (Series cells : dataFrame) { bufferedWriter.write(String.valueOf(++count)); diff --git a/src_code/src/main/java/zhao/algorithmMagic/io/OutputHDFSBuidler.java b/src_code/src/main/java/zhao/algorithmMagic/io/OutputHDFSBuidler.java index be1b705..faf93fc 100644 --- a/src_code/src/main/java/zhao/algorithmMagic/io/OutputHDFSBuidler.java +++ b/src_code/src/main/java/zhao/algorithmMagic/io/OutputHDFSBuidler.java @@ -4,6 +4,7 @@ import org.apache.hadoop.fs.Path; import zhao.algorithmMagic.operands.table.Cell; import zhao.algorithmMagic.operands.table.FinalCell; +import zhao.algorithmMagic.utils.ASClass; import java.util.HashMap; @@ -71,7 +72,7 @@ public OutputComponent create() { (FileSystem) this.get(FILE_SYSTEM).getValue(), (Path) this.get(OUT_PATH).getValue(), this.get(FORMAT).toString(), - (char) this.get(SEP).getIntValue() + ASClass., Cell>transform(this.getOrDefault(SEP, new FinalCell<>(','))).getValue() ); } } diff --git a/src_code/src/main/java/zhao/algorithmMagic/operands/table/DataFrame.java b/src_code/src/main/java/zhao/algorithmMagic/operands/table/DataFrame.java index 8ee0f37..dae7fc8 100644 --- a/src_code/src/main/java/zhao/algorithmMagic/operands/table/DataFrame.java +++ b/src_code/src/main/java/zhao/algorithmMagic/operands/table/DataFrame.java @@ -1,5 +1,6 @@ package zhao.algorithmMagic.operands.table; +import zhao.algorithmMagic.io.OutputComponent; import zhao.algorithmMagic.operands.Operands; import zhao.algorithmMagic.utils.transformation.Transformation; @@ -255,6 +256,16 @@ public interface DataFrame extends AggDataFrameData, Iterable, Serializa */ DataFrame into_outHtml(String outPath, String tableName); + /** + * 将计算结果提供给指定的数据输出组件进行数据的输出操作, + * + * @param outputComponent 输出数据需要使用的数据输出组件 + * @return 输出之后会返回数据集本身,不会终止调用。 + *

+ * After output, the data set itself will be returned and the call will not be terminated. + */ + DataFrame into_outComponent(OutputComponent outputComponent); + /** * 将DF对象中的所有数据转换成为一个list容器。 *

diff --git a/src_code/src/main/java/zhao/algorithmMagic/operands/table/FDataFrame.java b/src_code/src/main/java/zhao/algorithmMagic/operands/table/FDataFrame.java index 6ab574c..a0ee892 100644 --- a/src_code/src/main/java/zhao/algorithmMagic/operands/table/FDataFrame.java +++ b/src_code/src/main/java/zhao/algorithmMagic/operands/table/FDataFrame.java @@ -2,6 +2,7 @@ import zhao.algorithmMagic.exception.OperatorOperationException; import zhao.algorithmMagic.io.InputComponent; +import zhao.algorithmMagic.io.OutputComponent; import zhao.algorithmMagic.utils.ASIO; import zhao.algorithmMagic.utils.ASMath; import zhao.algorithmMagic.utils.transformation.Transformation; @@ -99,7 +100,12 @@ public static FDataFrame select(Series colNameRow, int pk) { * @return 获取到的DataFrame对象。 */ public static DataFrame builder(InputComponent inputComponent) { - return inputComponent.getDataFrame(); + if (inputComponent.open()) { + DataFrame dataFrame = inputComponent.getDataFrame(); + ASIO.close(inputComponent); + return dataFrame; + } + throw new OperatorOperationException("inputComponent open error!!!"); } /** @@ -657,6 +663,23 @@ public DataFrame into_outHtml(String outPath, String tableName) { return this; } + /** + * 将计算结果提供给指定的数据输出组件进行数据的输出操作, + * + * @param outputComponent 输出数据需要使用的数据输出组件 + * @return 输出之后会返回数据集本身,不会终止调用。 + *

+ * After output, the data set itself will be returned and the call will not be terminated. + */ + @Override + public DataFrame into_outComponent(OutputComponent outputComponent) { + if (outputComponent.open()) { + outputComponent.writeDataFrame(this); + ASIO.close(outputComponent); + } else throw new OperatorOperationException("into_outComponent(OutputComponent outputComponent) error!!!"); + return this; + } + /** * 将DF对象中的所有数据转换成为一个list容器。 *

diff --git a/src_code/src/main/java/zhao/algorithmMagic/operands/vector/SparkVector.scala b/src_code/src/main/java/zhao/algorithmMagic/operands/vector/SparkVector.scala index 646e8bf..42bfc95 100644 --- a/src_code/src/main/java/zhao/algorithmMagic/operands/vector/SparkVector.scala +++ b/src_code/src/main/java/zhao/algorithmMagic/operands/vector/SparkVector.scala @@ -107,6 +107,13 @@ final class SparkVector(sparkContext: SparkContext, vector: org.apache.spark.mll else throw new OperatorOperationException("'DoubleVector1 add DoubleVector2' 时,两个'DoubleVector'的向量所包含的数量不同,DoubleVector1=[" + numberOfDimensions1 + "],DoubleVector2=[" + numberOfDimensions2 + "]\n" + "When 'DoubleVector1 add DoubleVector2', the two vectors of 'DoubleVector' contain different quantities, DoubleVector1=[" + numberOfDimensions1 + "], DoubleVector2=[" + numberOfDimensions2 + "]") } + /** + * @return 向量中包含的维度数量 + *

+ * the number of dimensions contained in the vector + */ + override def getNumberOfDimensions: Int = size + /** * 在两个操作数之间做差的方法,具体用法请参阅API说明。 *

@@ -131,13 +138,6 @@ final class SparkVector(sparkContext: SparkContext, vector: org.apache.spark.mll else throw new OperatorOperationException("'DoubleVector1 diff DoubleVector2' 时,两个'DoubleVector'的向量所包含的数量不同,DoubleVector1=[" + numberOfDimensions1 + "],DoubleVector2=[" + numberOfDimensions2 + "]\n" + "When 'DoubleVector1 diff DoubleVector2', the two vectors of 'DoubleVector' contain different quantities, DoubleVector1=[" + numberOfDimensions1 + "], DoubleVector2=[" + numberOfDimensions2 + "]") } - /** - * @return 向量中包含的维度数量 - *

- * the number of dimensions contained in the vector - */ - override def getNumberOfDimensions: Int = size - /** * * @return 第三方向量中所维护的向量序列,通过此函数您可以直接获取到第三方库中的对象。 From 695b48b36af323615b3dcc418fded05f4049e86e Mon Sep 17 00:00:00 2001 From: LingYuZhao Date: Sat, 8 Apr 2023 21:24:01 +0800 Subject: [PATCH 15/17] update --- src_code/README-Chinese.md | 212 +++++++++---- src_code/README.md | 283 ++++++++++++++---- .../main/java/zhao/algorithmMagic/MAIN1.java | 72 ++--- .../zhao/algorithmMagic/io/InputByStream.java | 220 ++++++++++++++ .../io/InputByStreamBuilder.java | 77 +++++ .../algorithmMagic/io/InputComponent.java | 4 +- .../zhao/algorithmMagic/io/InputHDFS.java | 4 +- .../algorithmMagic/io/InputHDFSBuilder.java | 2 +- .../zhao/algorithmMagic/io/OutputHDFS.java | 3 + .../operands/matrix/ColorMatrix.java | 100 +++++-- .../algorithmMagic/operands/table/Cell.java | 15 + .../operands/table/DataFrame.java | 20 ++ .../operands/table/FDataFrame.java | 105 +++++++ .../operands/table/FinalCell.java | 32 ++ .../java/zhao/algorithmMagic/utils/ASIO.java | 12 + 15 files changed, 976 insertions(+), 185 deletions(-) create mode 100644 src_code/src/main/java/zhao/algorithmMagic/io/InputByStream.java create mode 100644 src_code/src/main/java/zhao/algorithmMagic/io/InputByStreamBuilder.java diff --git a/src_code/README-Chinese.md b/src_code/README-Chinese.md index 1957b9c..50b9a8b 100644 --- a/src_code/README-Chinese.md +++ b/src_code/README-Chinese.md @@ -488,7 +488,71 @@ public class MAIN1 { } ``` -* 支持通过 HDFS 获取到操作数对象以及数据的保存操作。 +* 支持通过 HDFS 获取到 ColorMatrix 操作数对象以及 ColorMatrix 数据对象的保存操作。 + +```java +package zhao.algorithmMagic; + +import org.apache.hadoop.conf.Configuration; +import org.apache.hadoop.fs.FileSystem; +import org.apache.hadoop.fs.Path; +import zhao.algorithmMagic.io.*; +import zhao.algorithmMagic.operands.matrix.ColorMatrix; +import zhao.algorithmMagic.operands.table.FinalCell; + +import java.io.IOException; + + +public class MAIN1 { + public static void main(String[] args) throws IOException { + // 获取到 HDFS 数据输入输出设备对象 首先将两者需要的参数创建出来 + FileSystem fileSystem; + String inPath; + { + Path path = new Path("hdfs://192.168.0.141:8020"); + // 构建HDFS文件系统对象 + fileSystem = path.getFileSystem(new Configuration()); + // 设置需要被读取的文件对象 + inPath = "hdfs://192.168.0.141:8020/data/test.png"; + } + + /* ***************************************************** + * TODO 从HDFS中加载数据 + * *****************************************************/ + // 开始将所有的参数配置到设备对象中,构建出数据输入设备 + InputComponent inputComponent = InputHDFS.builder() + .addInputArg(InputHDFSBuilder.FILE_SYSTEM, new FinalCell<>(fileSystem)) + .addInputArg(InputHDFSBuilder.IN_PATH, new FinalCell<>(inPath)) + // 由于图像不需要字段名称,但是内部有“防止空值校验”,因此这里随意传递一个字符串组即可 + .addInputArg(InputHDFSBuilder.FIELD, new FinalCell<>(new String[]{"null"})) + .create(); + + /* ***************************************************** + * TODO 处理加载出来的 Color 对象,这里是将颜色进行反转 + * *****************************************************/ + // 开始将 HDFS 中的数据加载成为一个 图像矩阵 对象 并将其进行颜色反转操作 + ColorMatrix colorMatrix = ColorMatrix.parse(inputComponent); + colorMatrix.colorReversal(false); + + /* ***************************************************** + * TODO 将处理好的数据输出到 HDFS 的 /data/res.jpg 中 + * *****************************************************/ + // 实例化出来目标路径 + Path path = new Path("hdfs://192.168.0.141:8020/data/res.jpg"); + // 开始构建数据输出组件 + OutputComponent outputComponent = OutputHDFS.builder() + .addOutputArg(OutputHDFSBuidler.FILE_SYSTEM, new FinalCell<>(fileSystem)) + .addOutputArg(OutputHDFSBuidler.OUT_PATH, new FinalCell<>(path)) + .addOutputArg(OutputHDFSBuidler.FORMAT, new FinalCell<>("JPG")) + .create(); + // 开始通过组件将数据输出 同时打印出结果 + colorMatrix.show("res"); + colorMatrix.save(outputComponent); + } +} +``` + +* 支持通过 HDFS 获取到 DataFrame 操作数对象,同时还支持该对象的数据输出。 ```java package zhao.algorithmMagic; @@ -501,62 +565,108 @@ import zhao.algorithmMagic.operands.table.DataFrame; import zhao.algorithmMagic.operands.table.FDataFrame; import zhao.algorithmMagic.operands.table.FinalCell; +import java.io.BufferedWriter; import java.io.IOException; +import java.io.OutputStreamWriter; public class MAIN1 { - public static void main(String[] args) throws IOException { - // 获取到 HDFS 数据输入输出设备对象 首先将两者需要的参数创建出来 - FileSystem fileSystem; - String inPath; - String[] field; - { - Path path = new Path("hdfs://192.168.0.141:8020"); - // 构建HDFS文件系统对象 - fileSystem = path.getFileSystem(new Configuration()); - // 设置需要被读取的文件对象 - inPath = "hdfs://192.168.0.141:8020/data/test.txt"; - // 设置字段头部 - field = new String[]{"name", "sex", "age"}; + public static void main(String[] args) throws IOException { + // 获取到 HDFS 数据输入输出设备对象 首先将两者需要的参数创建出来 + FileSystem fileSystem; + String inPath; + { + Path path = new Path("hdfs://192.168.0.141:8020"); + // 构建HDFS文件系统对象 + fileSystem = path.getFileSystem(new Configuration()); + // 设置需要被读取的文件对象 + inPath = "hdfs://192.168.0.141:8020/data/test.txt"; + } + + /* ***************************************************** + * TODO 从HDFS中加载数据 + * *****************************************************/ + // 开始将所有的参数配置到设备对象中,构建出数据输入设备 + InputComponent inputComponent = InputHDFS.builder() + .addInputArg(InputHDFSBuilder.FILE_SYSTEM, new FinalCell<>(fileSystem)) + .addInputArg(InputHDFSBuilder.IN_PATH, new FinalCell<>(inPath)) + // 设置文件中内容的字段名称 + .addInputArg(InputHDFSBuilder.FIELD, new FinalCell<>(new String[]{"name", "sex", "age"})) + // 设置文件中每列分隔符 + .addInputArg(InputHDFSBuilder.SEP, new FinalCell<>('\t')) + // 设置文件中读取的字符集 + .addInputArg(InputHDFSBuilder.CHAR_SET, new FinalCell<>("utf-8")) + .create(); + + /* ***************************************************** + * TODO 处理加载出来的 DataFrame 对象,这里是按照 age 正序排序 + * *****************************************************/ + // 开始将 HDFS 中的数据加载成为一个 DataFrame 对象 并将其进行age正序排序操作 + DataFrame builder = FDataFrame + .builder(inputComponent) + .sort("age"); + + /* ***************************************************** + * TODO 将处理好的数据输出到 HDFS 的 /data/res.csv 中 + * *****************************************************/ + // 实例化出来目标路径 + Path path = new Path("hdfs://192.168.0.141:8020/data/res.csv"); + // 开始构建数据输出组件 + OutputComponent outputComponent = OutputHDFS.builder() + // 设置输出时的HDFS文件系统以及其路径 + .addOutputArg(OutputHDFSBuidler.FILE_SYSTEM, new FinalCell<>(fileSystem)) + .addOutputArg(OutputHDFSBuidler.OUT_PATH, new FinalCell<>(path)) + // 设置输出格式,这里是输出一个CSV的文件 + .addOutputArg(OutputHDFSBuidler.FORMAT, new FinalCell<>("csv")) + // 输出时的分隔符 + .addOutputArg(OutputHDFSBuidler.SEP, new FinalCell<>(',')) + .create(); + // 开始通过组件将数据输出 同时使用 show 函数在控制台打印出结果 值得注意的是,show 函数不会关闭数据流对象 + BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(System.out)); + builder.show(bufferedWriter); + System.out.println("ok!!!!"); + builder.into_outComponent(outputComponent); } +} +``` + +* 支持通过数据流获取到数据对象,在这里我们从系统输入流中获取到 DataFrame 对象,做演示。 + +```java +package zhao.algorithmMagic; - /* ***************************************************** - * TODO 从HDFS中加载数据 - * *****************************************************/ - // 开始将所有的参数配置到设备对象中,构建出数据输入设备 - InputComponent inputComponent = InputHDFS.builder() - .addInputArg(InputHDFSBuilder.FILE_SYSTEM, new FinalCell<>(fileSystem)) - .addInputArg(InputHDFSBuilder.IN_PATH, new FinalCell<>(inPath)) - .addInputArg(InputHDFSBuilder.FIELD, new FinalCell<>(field)) - // 设置分隔符 - .addInputArg(InputHDFSBuilder.SEP, new FinalCell<>('\t')) - .create(); - - /* ***************************************************** - * TODO 处理加载出来的 DataFrame 对象,这里是按照 age 正序排序 - * *****************************************************/ - // 开始将 HDFS 中的数据加载成为一个 DataFrame对象 - DataFrame dataFrame = FDataFrame - .builder(inputComponent) - .sort("age"); - - /* ***************************************************** - * TODO 将处理好的数据输出到 HDFS 的 /data/res.csv 中 - * *****************************************************/ - // 实例化出来目标路径 - Path path = new Path("hdfs://192.168.0.141:8020/data/res.csv"); - // 开始构建数据输出组件 - OutputComponent outputComponent = OutputHDFS.builder() - .addOutputArg(OutputHDFSBuidler.FILE_SYSTEM, new FinalCell<>(fileSystem)) - .addOutputArg(OutputHDFSBuidler.SEP, new FinalCell<>(',')) - .addOutputArg(OutputHDFSBuidler.OUT_PATH, new FinalCell<>(path)) - .addOutputArg(OutputHDFSBuidler.FORMAT, new FinalCell<>("csv")) - .create(); - // 开始通过组件将数据输出 同时打印出结果 - System.out.println( - dataFrame.into_outComponent(outputComponent) - ); - } +import zhao.algorithmMagic.io.InputByStream; +import zhao.algorithmMagic.io.InputByStreamBuilder; +import zhao.algorithmMagic.io.InputComponent; +import zhao.algorithmMagic.operands.table.DataFrame; +import zhao.algorithmMagic.operands.table.FDataFrame; +import zhao.algorithmMagic.operands.table.FinalCell; + +import java.io.IOException; + + +public class MAIN1 { + public static void main(String[] args) { + /* ***************************************************** + * TODO 从数据流中读取到数据 并构建 DF 对象 + * *****************************************************/ + // 开始将所有的参数配置到设备对象中,构建出数据输入设备 + InputComponent inputComponent = InputByStream.builder() + // 准备数据输入流,在这里准备的是终端数据输入流 + .addInputArg(InputByStreamBuilder.INPUT_STREAM, new FinalCell<>(System.in)) + // 设置数据输入的字符集 + .addInputArg(InputByStreamBuilder.CHARSET, new FinalCell<>("utf-8")) + // 由于 DF 数据加载是结构化模式加载,因此需要设置数据输入的分隔符 + .addInputArg(InputByStreamBuilder.SEP, new FinalCell<>(',')) + // 由于 DF 数据对象有主键功能,因此需要指定主键索引编号 这里是从0开始的索引 + .addInputArg(InputByStreamBuilder.PK, new FinalCell<>(1)) + // 设置本次数据要输入的行数量 代表我们要输入 3 行数据 + .addInputArg(InputByStreamBuilder.ROW_LEN, new FinalCell<>(3)) + .create(); + // 开始进行数据的加载 需要注意的是,由于我们使用的是终端数据流,因此不需要框架来关闭数据流,需要指定isOC为false + DataFrame builder = FDataFrame.builder(inputComponent, true); + builder.show(); + } } ``` diff --git a/src_code/README.md b/src_code/README.md index 47d6770..c7d65ba 100644 --- a/src_code/README.md +++ b/src_code/README.md @@ -462,7 +462,7 @@ public class MAIN1 { } ``` -* Support the saving operation of obtaining operand objects and data through HDFS. +* Supports obtaining DataFrame operand objects and saving DataFrame data objects through HDFS. ```java package zhao.algorithmMagic; @@ -479,58 +479,239 @@ import java.io.IOException; public class MAIN1 { - public static void main(String[] args) throws IOException { - // 获取到 HDFS 数据输入输出设备对象 首先将两者需要的参数创建出来 - FileSystem fileSystem; - String inPath; - String[] field; - { - Path path = new Path("hdfs://192.168.0.141:8020"); - // 构建HDFS文件系统对象 - fileSystem = path.getFileSystem(new Configuration()); - // 设置需要被读取的文件对象 - inPath = "hdfs://192.168.0.141:8020/data/test.txt"; - // 设置字段头部 - field = new String[]{"name", "sex", "age"}; + public static void main(String[] args) throws IOException { + // 获取到 HDFS 数据输入输出设备对象 首先将两者需要的参数创建出来 + FileSystem fileSystem; + String inPath; + String[] field; + { + Path path = new Path("hdfs://192.168.0.141:8020"); + // 构建HDFS文件系统对象 + fileSystem = path.getFileSystem(new Configuration()); + // 设置需要被读取的文件对象 + inPath = "hdfs://192.168.0.141:8020/data/test.txt"; + // 设置字段头部 + field = new String[]{"name", "sex", "age"}; + } + + /* ***************************************************** + * TODO 从HDFS中加载数据 + * *****************************************************/ + // 开始将所有的参数配置到设备对象中,构建出数据输入设备 + InputComponent inputComponent = InputHDFS.builder() + .addInputArg(InputHDFSBuilder.FILE_SYSTEM, new FinalCell<>(fileSystem)) + .addInputArg(InputHDFSBuilder.IN_PATH, new FinalCell<>(inPath)) + .addInputArg(InputHDFSBuilder.FIELD, new FinalCell<>(field)) + // 设置分隔符 + .addInputArg(InputHDFSBuilder.SEP, new FinalCell<>('\t')) + .create(); + + /* ***************************************************** + * TODO 处理加载出来的 DataFrame 对象,这里是按照 age 正序排序 + * *****************************************************/ + // 开始将 HDFS 中的数据加载成为一个 DataFrame对象 + DataFrame dataFrame = FDataFrame + .builder(inputComponent) + .sort("age"); + + /* ***************************************************** + * TODO 将处理好的数据输出到 HDFS 的 /data/res.csv 中 + * *****************************************************/ + // 实例化出来目标路径 + Path path = new Path("hdfs://192.168.0.141:8020/data/res.csv"); + // 开始构建数据输出组件 + OutputComponent outputComponent = OutputHDFS.builder() + .addOutputArg(OutputHDFSBuidler.FILE_SYSTEM, new FinalCell<>(fileSystem)) + .addOutputArg(OutputHDFSBuidler.SEP, new FinalCell<>(',')) + .addOutputArg(OutputHDFSBuidler.OUT_PATH, new FinalCell<>(path)) + .addOutputArg(OutputHDFSBuidler.FORMAT, new FinalCell<>("csv")) + .create(); + // 开始通过组件将数据输出 同时打印出结果 + System.out.println( + dataFrame.into_outComponent(outputComponent) + ); } +} +``` + +* Support the saving operation of obtaining ColorMatrix operand objects and ColorMatrix data objects through HDFS. + +```java +package zhao.algorithmMagic; + +import org.apache.hadoop.conf.Configuration; +import org.apache.hadoop.fs.FileSystem; +import org.apache.hadoop.fs.Path; +import zhao.algorithmMagic.io.*; +import zhao.algorithmMagic.operands.matrix.ColorMatrix; +import zhao.algorithmMagic.operands.table.FinalCell; + +import java.io.IOException; + + +public class MAIN1 { + public static void main(String[] args) throws IOException { + // 获取到 HDFS 数据输入输出设备对象 首先将两者需要的参数创建出来 + FileSystem fileSystem; + String inPath; + { + Path path = new Path("hdfs://192.168.0.141:8020"); + // 构建HDFS文件系统对象 + fileSystem = path.getFileSystem(new Configuration()); + // 设置需要被读取的文件对象 + inPath = "hdfs://192.168.0.141:8020/data/test.jpg"; + } + + /* ***************************************************** + * TODO 从HDFS中加载数据 + * *****************************************************/ + // 开始将所有的参数配置到设备对象中,构建出数据输入设备 + InputComponent inputComponent = InputHDFS.builder() + .addInputArg(InputHDFSBuilder.FILE_SYSTEM, new FinalCell<>(fileSystem)) + .addInputArg(InputHDFSBuilder.IN_PATH, new FinalCell<>(inPath)) + .create(); + + /* ***************************************************** + * TODO 处理加载出来的 DataFrame 对象,这里是按照 age 正序排序 + * *****************************************************/ + // 开始将 HDFS 中的数据加载成为一个 图像矩阵 对象 并将其进行颜色反转操作 + ColorMatrix colorMatrix = ColorMatrix.parse(inputComponent); + colorMatrix.colorReversal(false); + + /* ***************************************************** + * TODO 将处理好的数据输出到 HDFS 的 /data/res.jpg 中 + * *****************************************************/ + // 实例化出来目标路径 + Path path = new Path("hdfs://192.168.0.141:8020/data/res.jpg"); + // 开始构建数据输出组件 + OutputComponent outputComponent = OutputHDFS.builder() + .addOutputArg(OutputHDFSBuidler.FILE_SYSTEM, new FinalCell<>(fileSystem)) + .addOutputArg(OutputHDFSBuidler.OUT_PATH, new FinalCell<>(path)) + .addOutputArg(OutputHDFSBuidler.FORMAT, new FinalCell<>("JPG")) + .create(); + // 开始通过组件将数据输出 同时打印出结果 + colorMatrix.show("res"); + colorMatrix.save(outputComponent); + } +} +``` + +* Supports obtaining DataFrame operand objects through HDFS, while also supporting data output for this object. + +```java +package zhao.algorithmMagic; - /* ***************************************************** - * TODO 从HDFS中加载数据 - * *****************************************************/ - // 开始将所有的参数配置到设备对象中,构建出数据输入设备 - InputComponent inputComponent = InputHDFS.builder() - .addInputArg(InputHDFSBuilder.FILE_SYSTEM, new FinalCell<>(fileSystem)) - .addInputArg(InputHDFSBuilder.IN_PATH, new FinalCell<>(inPath)) - .addInputArg(InputHDFSBuilder.FIELD, new FinalCell<>(field)) - // 设置分隔符 - .addInputArg(InputHDFSBuilder.SEP, new FinalCell<>('\t')) - .create(); - - /* ***************************************************** - * TODO 处理加载出来的 DataFrame 对象,这里是按照 age 正序排序 - * *****************************************************/ - // 开始将 HDFS 中的数据加载成为一个 DataFrame对象 - DataFrame dataFrame = FDataFrame - .builder(inputComponent) - .sort("age"); - - /* ***************************************************** - * TODO 将处理好的数据输出到 HDFS 的 /data/res.csv 中 - * *****************************************************/ - // 实例化出来目标路径 - Path path = new Path("hdfs://192.168.0.141:8020/data/res.csv"); - // 开始构建数据输出组件 - OutputComponent outputComponent = OutputHDFS.builder() - .addOutputArg(OutputHDFSBuidler.FILE_SYSTEM, new FinalCell<>(fileSystem)) - .addOutputArg(OutputHDFSBuidler.SEP, new FinalCell<>(',')) - .addOutputArg(OutputHDFSBuidler.OUT_PATH, new FinalCell<>(path)) - .addOutputArg(OutputHDFSBuidler.FORMAT, new FinalCell<>("csv")) - .create(); - // 开始通过组件将数据输出 同时打印出结果 - System.out.println( - dataFrame.into_outComponent(outputComponent) - ); - } +import org.apache.hadoop.conf.Configuration; +import org.apache.hadoop.fs.FileSystem; +import org.apache.hadoop.fs.Path; +import zhao.algorithmMagic.io.*; +import zhao.algorithmMagic.operands.table.DataFrame; +import zhao.algorithmMagic.operands.table.FDataFrame; +import zhao.algorithmMagic.operands.table.FinalCell; + +import java.io.BufferedWriter; +import java.io.IOException; +import java.io.OutputStreamWriter; + + +public class MAIN1 { + public static void main(String[] args) throws IOException { + // 获取到 HDFS 数据输入输出设备对象 首先将两者需要的参数创建出来 + FileSystem fileSystem; + String inPath; + { + Path path = new Path("hdfs://192.168.0.141:8020"); + // 构建HDFS文件系统对象 + fileSystem = path.getFileSystem(new Configuration()); + // 设置需要被读取的文件对象 + inPath = "hdfs://192.168.0.141:8020/data/test.txt"; + } + + /* ***************************************************** + * TODO 从HDFS中加载数据 + * *****************************************************/ + // 开始将所有的参数配置到设备对象中,构建出数据输入设备 + InputComponent inputComponent = InputHDFS.builder() + .addInputArg(InputHDFSBuilder.FILE_SYSTEM, new FinalCell<>(fileSystem)) + .addInputArg(InputHDFSBuilder.IN_PATH, new FinalCell<>(inPath)) + // 设置文件中内容的字段名称 + .addInputArg(InputHDFSBuilder.FIELD, new FinalCell<>(new String[]{"name", "sex", "age"})) + // 设置文件中每列分隔符 + .addInputArg(InputHDFSBuilder.SEP, new FinalCell<>('\t')) + // 设置文件中读取的字符集 + .addInputArg(InputHDFSBuilder.CHAR_SET, new FinalCell<>("utf-8")) + .create(); + + /* ***************************************************** + * TODO 处理加载出来的 DataFrame 对象,这里是按照 age 正序排序 + * *****************************************************/ + // 开始将 HDFS 中的数据加载成为一个 DataFrame 对象 并将其进行age正序排序操作 + DataFrame builder = FDataFrame + .builder(inputComponent) + .sort("age"); + + /* ***************************************************** + * TODO 将处理好的数据输出到 HDFS 的 /data/res.csv 中 + * *****************************************************/ + // 实例化出来目标路径 + Path path = new Path("hdfs://192.168.0.141:8020/data/res.csv"); + // 开始构建数据输出组件 + OutputComponent outputComponent = OutputHDFS.builder() + // 设置输出时的HDFS文件系统以及其路径 + .addOutputArg(OutputHDFSBuidler.FILE_SYSTEM, new FinalCell<>(fileSystem)) + .addOutputArg(OutputHDFSBuidler.OUT_PATH, new FinalCell<>(path)) + // 设置输出格式,这里是输出一个CSV的文件 + .addOutputArg(OutputHDFSBuidler.FORMAT, new FinalCell<>("csv")) + // 输出时的分隔符 + .addOutputArg(OutputHDFSBuidler.SEP, new FinalCell<>(',')) + .create(); + // 开始通过组件将数据输出 同时使用 show 函数在控制台打印出结果 值得注意的是,show 函数不会关闭数据流对象 + BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(System.out)); + builder.show(bufferedWriter); + System.out.println("ok!!!!"); + builder.into_outComponent(outputComponent); + } +} +``` + +* Support obtaining data objects through data streams. Here, we obtain DataFrame objects from the system input stream + for demonstration. + +```java +package zhao.algorithmMagic; + +import zhao.algorithmMagic.io.InputByStream; +import zhao.algorithmMagic.io.InputByStreamBuilder; +import zhao.algorithmMagic.io.InputComponent; +import zhao.algorithmMagic.operands.table.DataFrame; +import zhao.algorithmMagic.operands.table.FDataFrame; +import zhao.algorithmMagic.operands.table.FinalCell; + +import java.io.IOException; + + +public class MAIN1 { + public static void main(String[] args) { + /* ***************************************************** + * TODO 从数据流中读取到数据 并构建 DF 对象 + * *****************************************************/ + // 开始将所有的参数配置到设备对象中,构建出数据输入设备 + InputComponent inputComponent = InputByStream.builder() + // 准备数据输入流,在这里准备的是终端数据输入流 + .addInputArg(InputByStreamBuilder.INPUT_STREAM, new FinalCell<>(System.in)) + // 设置数据输入的字符集 + .addInputArg(InputByStreamBuilder.CHARSET, new FinalCell<>("utf-8")) + // 由于 DF 数据加载是结构化模式加载,因此需要设置数据输入的分隔符 + .addInputArg(InputByStreamBuilder.SEP, new FinalCell<>(',')) + // 由于 DF 数据对象有主键功能,因此需要指定主键索引编号 这里是从0开始的索引 + .addInputArg(InputByStreamBuilder.PK, new FinalCell<>(1)) + // 设置本次数据要输入的行数量 代表我们要输入 3 行数据 + .addInputArg(InputByStreamBuilder.ROW_LEN, new FinalCell<>(3)) + .create(); + // 开始进行数据的加载 需要注意的是,由于我们使用的是终端数据流,因此不需要框架来关闭数据流,需要指定isOC为false + DataFrame builder = FDataFrame.builder(inputComponent, true); + builder.show(); + } } ``` diff --git a/src_code/src/main/java/zhao/algorithmMagic/MAIN1.java b/src_code/src/main/java/zhao/algorithmMagic/MAIN1.java index d406903..1a3de06 100644 --- a/src_code/src/main/java/zhao/algorithmMagic/MAIN1.java +++ b/src_code/src/main/java/zhao/algorithmMagic/MAIN1.java @@ -1,57 +1,35 @@ package zhao.algorithmMagic; -import zhao.algorithmMagic.algorithm.distanceAlgorithm.ManhattanDistance; -import zhao.algorithmMagic.io.InputCamera; -import zhao.algorithmMagic.io.InputCameraBuilder; +import zhao.algorithmMagic.io.InputByStream; +import zhao.algorithmMagic.io.InputByStreamBuilder; import zhao.algorithmMagic.io.InputComponent; -import zhao.algorithmMagic.operands.coordinate.IntegerCoordinateTwo; -import zhao.algorithmMagic.operands.matrix.ColorMatrix; +import zhao.algorithmMagic.operands.table.DataFrame; +import zhao.algorithmMagic.operands.table.FDataFrame; import zhao.algorithmMagic.operands.table.FinalCell; -import java.awt.*; -import java.util.Map; +import java.io.IOException; public class MAIN1 { - public static void main(String[] args) throws InterruptedException { - // 获取到摄像头输入设备 - InputComponent inputComponent = InputCamera.builder() - // 要使用的摄像头的名字 索引 或def默认,我们这里使用的是 def 代表使用默认摄像头 - .addInputArg(InputCameraBuilder.Camera_Index, new FinalCell<>("0")) - // 要使用的拍摄图像格式 - .addInputArg(InputCameraBuilder.Image_Format, new FinalCell<>("JPG")) - // 图像尺寸 这里的数值是 WebcamResolution 枚举类的属性字段 VGA - .addInputArg(InputCameraBuilder.CUSTOM_VIEW_SIZES, new FinalCell<>("VGA")) + public static void main(String[] args) throws IOException { + /* ***************************************************** + * TODO 从数据流中读取到数据 并构建 DF 对象 + * *****************************************************/ + // 开始将所有的参数配置到设备对象中,构建出数据输入设备 + InputComponent inputComponent = InputByStream.builder() + // 准备数据输入流,在这里准备的是终端数据输入流 + .addInputArg(InputByStreamBuilder.INPUT_STREAM, new FinalCell<>(System.in)) + // 设置数据输入的字符集 + .addInputArg(InputByStreamBuilder.CHARSET, new FinalCell<>("utf-8")) + // 由于 DF 数据加载是结构化模式加载,因此需要设置数据输入的分隔符 + .addInputArg(InputByStreamBuilder.SEP, new FinalCell<>(',')) + // 由于 DF 数据对象有主键功能,因此需要指定主键索引编号 这里是从0开始的索引 + .addInputArg(InputByStreamBuilder.PK, new FinalCell<>(1)) + // 设置本次数据要输入的行数量 代表我们要输入 3 行数据 + .addInputArg(InputByStreamBuilder.ROW_LEN, new FinalCell<>(3)) .create(); - - ColorMatrix colorMatrix1, colorMatrix2; - colorMatrix1 = ColorMatrix.parse("C:\\Users\\zhao\\Desktop\\fsdownload\\YB.bmp"); - int count = 0; - while (++count < 5) { - colorMatrix2 = ColorMatrix.parse(inputComponent); - ColorMatrix temp = ColorMatrix.parse(colorMatrix2.copyToNewArrays()); - // 开始二值化 - colorMatrix1.localBinary(ColorMatrix._G_, 10, 0xffffff, 0, 1); - temp.localBinary(ColorMatrix._G_, 5, 0xffffff, 0, 0); - // 开始进行模板匹配 并返回最匹配的结果数值,在这里返回的就是所有匹配的结果数据,key为匹配系数 value为匹配结果 - Map.Entry matching = temp.templateMatching( - ManhattanDistance.getInstance("MAN"), - colorMatrix1, - ColorMatrix._G_, - 10, - false - ); - // 开始进行绘制 在这里首先获取到坐标数据 - IntegerCoordinateTwo coordinateTwo = matching.getValue(); - System.out.print("匹配系数 = "); - System.out.println(matching.getKey()); - colorMatrix2.drawRectangle( - coordinateTwo, - new IntegerCoordinateTwo(coordinateTwo.getX() + colorMatrix1.getColCount(), coordinateTwo.getY() + colorMatrix1.getRowCount()), - Color.MAGENTA - ); - colorMatrix2.show("识别结果"); - Thread.sleep(1024); - } + // 开始进行数据的加载 需要注意的是,由于我们使用的是终端数据流,因此不需要框架来关闭数据流,需要指定isOC为false + DataFrame builder = FDataFrame.builder(inputComponent, true); + builder.show(); } -} \ No newline at end of file +} diff --git a/src_code/src/main/java/zhao/algorithmMagic/io/InputByStream.java b/src_code/src/main/java/zhao/algorithmMagic/io/InputByStream.java new file mode 100644 index 0000000..d36c9ec --- /dev/null +++ b/src_code/src/main/java/zhao/algorithmMagic/io/InputByStream.java @@ -0,0 +1,220 @@ +package zhao.algorithmMagic.io; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import zhao.algorithmMagic.exception.OperatorOperationException; +import zhao.algorithmMagic.operands.table.*; +import zhao.algorithmMagic.utils.ASIO; +import zhao.algorithmMagic.utils.ASStr; + +import javax.imageio.ImageIO; +import java.awt.image.BufferedImage; +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.InputStream; +import java.nio.charset.Charset; +import java.util.Scanner; + +/** + * 从一个数据输入流中获取数据的实现类,该类中封装了一系列通过数据输入流获取到数据的操作。 + *

+ * An implementation class that obtains data from a data input stream, encapsulating a series of operations to obtain data through the data input stream. + * + * @author zhao + */ +public class InputByStream implements InputComponent { + + private final static Logger LOGGER = LoggerFactory.getLogger("InputByStream"); + private final InputStream inputStream; + private final Scanner scanner; + private final Charset charSet; + private final int rowLength; + private final int pk; + private final char sep; + + /** + * @param inputStream 需要被读取的数据输入流对象 + * @param charSet 数据输入分隔符,结构化数据需要使用此参数 + * @param rowLength 数据输入行数,结构化数据需要使用此参数 + * @param pk 数据主键,DF数据输入需要使用此参数 + * @param sep 数据输入分隔符,结构化数据输入需要此参数 + */ + public InputByStream(Cell inputStream, Cell charSet, int rowLength, int pk, char sep) { + if (inputStream == null || charSet == null) { + throw new OperatorOperationException("The parameter in [Cell inputStream, Cell charSet] cannot be null!!!!"); + } + this.inputStream = inputStream.getValue(); + scanner = new Scanner(this.inputStream); + this.charSet = Charset.forName(charSet.toString()); + this.rowLength = rowLength; + this.pk = pk; + this.sep = sep; + } + + public static InputBuilder builder() { + return new InputByStreamBuilder(); + } + + /** + * 启动数据输入组件 + * + * @return 如果启动成功返回true + */ + @Override + public boolean open() { + LOGGER.info("open()"); + LOGGER.info("The data input stream component is ready!!!!"); + return isOpen(); + } + + /** + * @return 如果组件已经启动了,在这里返回true。 + *

+ * If the component has already started, return true here. + */ + @Override + public boolean isOpen() { + LOGGER.info("isOpen()"); + return true; + } + + /** + * 从数据输入组件中提取出 byte 数组的数据,一般情况下,这里返回的都是一些二进制的数据。 + *

+ * Extract the data of the byte array from the data input component. Generally, the returned data here is some binary data. + * + * @return byte[] 的数据对象 + */ + @Override + public byte[] getByteArray() { + LOGGER.info("getByteArray()"); + ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); + try { + while (scanner.hasNext()) { + byteArrayOutputStream.write(scanner.nextLine().getBytes(charSet)); + } + return byteArrayOutputStream.toByteArray(); + } catch (IOException e) { + throw new OperatorOperationException(e); + } finally { + ASIO.close(byteArrayOutputStream); + } + } + + /** + * 从数据输入组件中提取出 int 矩阵数据,一般情况下,这里返回的是一些矩阵元素数据。 + *

+ * From the data input component, int matrix data is increasingly generated. Generally, some matrix element data is returned here. + * + * @return int[][] + */ + @Override + public int[][] getInt2Array() { + LOGGER.info("getInt2Array()"); + int[][] res = new int[this.rowLength][]; + int index = -1; + while (++index < this.rowLength) { + String[] strings = ASStr.splitByChar(scanner.nextLine(), sep); + int[] row = new int[strings.length]; + int index_2 = -1; + for (String string : strings) { + row[++index_2] = Integer.parseInt(string); + } + res[index] = row; + } + return res; + } + + /** + * 从数据输入组件中提取出 int 矩阵数据,一般情况下,这里返回的是一些矩阵元素数据。 + *

+ * From the data input component, double matrix data is increasingly generated. Generally, some matrix element data is returned here. + * + * @return double[][] + */ + @Override + public double[][] getDouble2Array() { + LOGGER.info("getDouble2Array()"); + double[][] res = new double[this.rowLength][]; + int index = -1; + while (++index < this.rowLength) { + String[] strings = ASStr.splitByChar(scanner.nextLine(), sep); + double[] row = new double[strings.length]; + int index_2 = -1; + for (String string : strings) { + row[++index_2] = Double.parseDouble(string); + } + res[index] = row; + } + return res; + } + + /** + * 从数据输入组件获取到 DataFrame 对象,该函数有些数据输入组件可能不支持。 + *

+ * Retrieve the DataFrame object from the data input component, which may not be supported by some data input components. + * + * @return 从数据输入组件中获取到的DataFrame数据封装对象。 + *

+ * The DataFrame data encapsulation object obtained from the data input component. + */ + @Override + public DataFrame getDataFrame() { + LOGGER.info("getDataFrame()"); + DataFrame select = FDataFrame.select( + FieldCell.parse(ASStr.splitByChar(scanner.nextLine(), sep)), pk + ); + int index = -1; + while (++index < this.rowLength) { + select.insert(FinalSeries.parse(ASStr.splitByChar(scanner.nextLine(), sep))); + } + return select; + } + + /** + * 从数据输入组件中提取出 数据流 对象。 + *

+ * Extract the data flow object from the data input component. + * + * @return 数据输入流对象 + */ + @Override + public InputStream getInputStream() { + LOGGER.info("getInputStream()"); + return inputStream; + } + + /** + * 从数据输入组件中提取出 图像缓存 对象,需要注意的是,该操作在有些情况下可能不被支持。 + *

+ * Extracting image cache objects from the data input component, it should be noted that this operation may not be supported in some cases. + * + * @return 图像缓存对象。 + */ + @Override + public BufferedImage getBufferedImage() { + LOGGER.info("getBufferedImage()"); + try { + return ImageIO.read(inputStream); + } catch (IOException e) { + throw new OperatorOperationException(e); + } + } + + /** + * Closes this stream and releases any system resources associated + * with it. If the stream is already closed then invoking this + * method has no effect. + * + *

As noted in {@link AutoCloseable#close()}, cases where the + * close may fail require careful attention. It is strongly advised + * to relinquish the underlying resources and to internally + * mark the {@code Closeable} as closed, prior to throwing + * the {@code IOException}. + */ + @Override + public void close() { + LOGGER.info("close()"); + ASIO.close(this.scanner); + } +} diff --git a/src_code/src/main/java/zhao/algorithmMagic/io/InputByStreamBuilder.java b/src_code/src/main/java/zhao/algorithmMagic/io/InputByStreamBuilder.java new file mode 100644 index 0000000..d719be2 --- /dev/null +++ b/src_code/src/main/java/zhao/algorithmMagic/io/InputByStreamBuilder.java @@ -0,0 +1,77 @@ +package zhao.algorithmMagic.io; + +import zhao.algorithmMagic.operands.table.Cell; +import zhao.algorithmMagic.operands.table.FinalCell; +import zhao.algorithmMagic.utils.ASClass; + +import java.util.HashMap; + +/** + * 数据流输入设备对象建造者类,在该类中您可以针对数据流的数据输入操作进行一些配置,以获取到数据流中的数据对象。 + *

+ * The data flow input device object creator class allows you to configure the data input operations of the data flow to obtain the data objects in the data flow. + */ +public class InputByStreamBuilder extends HashMap> implements InputBuilder { + + /** + * 数据输入流 + */ + public final static String INPUT_STREAM = "ism"; + + /** + * 结构化数据下需要被读取的行 + */ + public final static String ROW_LEN = "rc"; + + /** + * DF对象读取中需要读取的行 + */ + public final static String PK = "pk"; + + /** + * 数据输入编码 + */ + public final static String CHARSET = "cs"; + + /** + * 结构化数据下需要使用的分隔符 + */ + public final static String SEP = "sep"; + + /** + * 添加数据输入描述,不同的组件有不同的配置属性,具体可以参阅实现类。 + *

+ * Add data input descriptions, and different components have different configuration properties. Please refer to the implementation class for details. + * + * @param key 属性名称 + *

+ * Attribute Name. + * @param value 属性数值 + *

+ * Attribute Value. + * @return 链式调用,继续构建 + *

+ * Chain call, continue building. + */ + @Override + public InputBuilder addInputArg(String key, FinalCell value) { + this.put(key, value); + return this; + } + + /** + * 将所需的对象构建出来并获取到对应的输入设备对象。 + * + * @return 输入设备对象。 + */ + @Override + public InputComponent create() { + return new InputByStream( + ASClass.transform(this.get(INPUT_STREAM)), + ASClass.transform(this.get(CHARSET)), + this.getOrDefault(ROW_LEN, new FinalCell<>(10)).getIntValue(), + this.getOrDefault(PK, new FinalCell<>(0)).getIntValue(), + ASClass., Cell>transform(this.getOrDefault(SEP, new FinalCell<>(','))).getValue() + ); + } +} diff --git a/src_code/src/main/java/zhao/algorithmMagic/io/InputComponent.java b/src_code/src/main/java/zhao/algorithmMagic/io/InputComponent.java index 97ecf48..90c7069 100644 --- a/src_code/src/main/java/zhao/algorithmMagic/io/InputComponent.java +++ b/src_code/src/main/java/zhao/algorithmMagic/io/InputComponent.java @@ -24,7 +24,9 @@ public interface InputComponent extends Closeable { boolean open(); /** - * @return 如果组件已经启动了,在这里返回true + * @return 如果组件已经启动了,在这里返回true。 + *

+ * If the component has already started, return true here. */ boolean isOpen(); diff --git a/src_code/src/main/java/zhao/algorithmMagic/io/InputHDFS.java b/src_code/src/main/java/zhao/algorithmMagic/io/InputHDFS.java index 49f702c..7f793d8 100644 --- a/src_code/src/main/java/zhao/algorithmMagic/io/InputHDFS.java +++ b/src_code/src/main/java/zhao/algorithmMagic/io/InputHDFS.java @@ -43,7 +43,7 @@ public class InputHDFS implements InputComponent { * @param field 读取时的表字段名称行 * @param pk 读取时的表主键对象 */ - protected InputHDFS(FileSystem fileSystem, Path inputPath, char sep, String charset, String[] field, int pk) { + protected InputHDFS(FileSystem fileSystem, Path inputPath, char sep, String charset, Cell field, int pk) { if (fileSystem == null || inputPath == null || field == null) { throw new OperatorOperationException("The parameter in [FileSystem fileSystem, Path inputPath, String[] field] cannot be null!!!!"); } @@ -51,7 +51,7 @@ protected InputHDFS(FileSystem fileSystem, Path inputPath, char sep, String char this.inputPath = inputPath; this.sep = sep; this.charset = charset; - this.field = FieldCell.parse(field); + this.field = FieldCell.parse(field.getValue()); this.pk = pk; this.isOpen = false; } diff --git a/src_code/src/main/java/zhao/algorithmMagic/io/InputHDFSBuilder.java b/src_code/src/main/java/zhao/algorithmMagic/io/InputHDFSBuilder.java index 7b85335..9c9a325 100644 --- a/src_code/src/main/java/zhao/algorithmMagic/io/InputHDFSBuilder.java +++ b/src_code/src/main/java/zhao/algorithmMagic/io/InputHDFSBuilder.java @@ -81,7 +81,7 @@ public InputComponent create() { new Path(this.get(IN_PATH).toString()), ASClass., Cell>transform(this.getOrDefault(SEP, new FinalCell<>(','))).getValue(), this.getOrDefault(CHAR_SET, new FinalCell<>("utf-8")).toString(), - (String[]) this.get(FIELD).getValue(), + ASClass., Cell>transform(this.get(FIELD)), this.getOrDefault(PK, new FinalCell<>(0)).getIntValue() ); } diff --git a/src_code/src/main/java/zhao/algorithmMagic/io/OutputHDFS.java b/src_code/src/main/java/zhao/algorithmMagic/io/OutputHDFS.java index 9ab8fab..eb06c87 100644 --- a/src_code/src/main/java/zhao/algorithmMagic/io/OutputHDFS.java +++ b/src_code/src/main/java/zhao/algorithmMagic/io/OutputHDFS.java @@ -32,6 +32,8 @@ public class OutputHDFS implements OutputComponent { private final static Logger LOGGER = LoggerFactory.getLogger("OutputHDFS"); + + private final FileSystem fileSystem; private final Path outputPath; private final String format; @@ -243,6 +245,7 @@ public void writeDataFrame(DataFrame dataFrame) { */ @Override public void close() { + LOGGER.info("close()"); ASIO.close(this.bufferedWriter); ASIO.close(this.fsDataOutputStream); } diff --git a/src_code/src/main/java/zhao/algorithmMagic/operands/matrix/ColorMatrix.java b/src_code/src/main/java/zhao/algorithmMagic/operands/matrix/ColorMatrix.java index 1dd2bf4..22f8b01 100644 --- a/src_code/src/main/java/zhao/algorithmMagic/operands/matrix/ColorMatrix.java +++ b/src_code/src/main/java/zhao/algorithmMagic/operands/matrix/ColorMatrix.java @@ -17,6 +17,7 @@ import javax.swing.*; import java.awt.*; +import java.io.BufferedWriter; import java.io.File; import java.io.IOException; import java.net.URL; @@ -493,8 +494,18 @@ public static ColorMatrix parseGrayscale(URL url) { * @return 从组件中提取出来的图像矩阵对象。 */ public static ColorMatrix parse(InputComponent inputComponent) { + return parse(inputComponent, true); + } + + /** + * 使用组件将一个图像数据提取,并获取对应的图像矩阵。 + * + * @param inputComponent 能够被提取出图像矩阵的数据组件。 + * @return 从组件中提取出来的图像矩阵对象。 + */ + public static ColorMatrix parse(InputComponent inputComponent, boolean isOC) { boolean isOk; - if (!inputComponent.isOpen()) { + if (!inputComponent.isOpen() && isOC) { isOk = inputComponent.open(); } else { isOk = true; @@ -502,9 +513,9 @@ public static ColorMatrix parse(InputComponent inputComponent) { if (isOk) { // 开始进行数据提取 ColorMatrix parse = ColorMatrix.parse(ASIO.parseImageGetColorArray(inputComponent.getBufferedImage())); - ASIO.close(inputComponent); + if (isOC) ASIO.close(inputComponent); return parse; - } else throw new OperatorOperationException("Unable to open your camera."); + } else throw new OperatorOperationException("Unable to open your inputComponent."); } /** @@ -524,6 +535,58 @@ private static ColorMatrix GrayscaleColors(Color[][] colors) { return new ColorMatrix(colors.length, colors[0].length, colors, true); } + /** + * 获取到文本数据输出逻辑实现对象 + * + * @param colorMatrix 需要被输出的图像矩形 + * @param sep 输出时需要使用的分隔符 + * @return 一个专用于将图像中的颜色数值按照CSV的格式输出到指定文件中的实现逻辑。 + */ + public static Consumer getSAVE_TEXT(ColorMatrix colorMatrix, char sep) { + return (stream) -> { + try { + int rowCount = 0; + for (Color[] colors : colorMatrix.toArrays()) { + stream.write(String.valueOf(++rowCount)); + for (Color color : colors) { + stream.write(sep); + stream.write(String.valueOf(color.getRGB())); + } + stream.newLine(); + } + } catch (IOException e) { + throw new OperatorOperationException("Write data exception!", e); + } + }; + } + + /** + * 将图像以ASCII的形式输出到指定文件中。 + * + * @param colorMatrix 需要被输出的图像对象 + * @param Mode 需要被输出的颜色通道 + * @param colorBoundary 输出时的颜色边界数值 + * @param imageAscii1 输出时的ASCII + * @param imageAscii2 输出时的ASCII + * @return 一个ASCII符号组成的文本图像 + */ + public static Consumer getSAVE_ASCII(ColorMatrix colorMatrix, byte Mode, int colorBoundary, char imageAscii1, char imageAscii2) { + return bufferedWriter -> { + try { + for (Color[] colors : colorMatrix.toArrays()) { + for (Color color : colors) { + if (((color.getRGB() >> Mode) & 0xFF) > colorBoundary) { + bufferedWriter.write(imageAscii1); + } else bufferedWriter.write(imageAscii2); + } + bufferedWriter.newLine(); + } + } catch (IOException e) { + throw new OperatorOperationException("Write data exception!", e); + } + }; + } + /** * 将两个操作数进行求和的方法,具体用法请参阅API说明。 *

@@ -1811,21 +1874,7 @@ public void save(String path, char sep) { */ @Override public void save(File path, char sep) { - ASIO.writer(path, (stream) -> { - try { - int rowCount = 0; - for (Color[] colors : this.toArrays()) { - stream.write(String.valueOf(++rowCount)); - for (Color color : colors) { - stream.write(sep); - stream.write(String.valueOf(color.getRGB())); - } - stream.newLine(); - } - } catch (IOException e) { - throw new OperatorOperationException("Write data exception!", e); - } - }); + ASIO.writer(path, getSAVE_TEXT(this, sep)); } /** @@ -1867,20 +1916,7 @@ public void save(OutputComponent outputComponent) { * ASCII Symbol In the output ASCII image file, the ASCII value corresponding to the constituent characters of the image for coordinates that are less than the threshold value. */ public void save(File path, byte Mode, int colorBoundary, char imageAscii1, char imageAscii2) { - ASIO.writer(path, bufferedWriter -> { - try { - for (Color[] colors : this.toArrays()) { - for (Color color : colors) { - if (((color.getRGB() >> Mode) & 0xFF) > colorBoundary) { - bufferedWriter.write(imageAscii1); - } else bufferedWriter.write(imageAscii2); - } - bufferedWriter.newLine(); - } - } catch (IOException e) { - throw new OperatorOperationException("Write data exception!", e); - } - }); + ASIO.writer(path, getSAVE_ASCII(this, Mode, colorBoundary, imageAscii1, imageAscii2)); } /** diff --git a/src_code/src/main/java/zhao/algorithmMagic/operands/table/Cell.java b/src_code/src/main/java/zhao/algorithmMagic/operands/table/Cell.java index bb6c596..d996085 100644 --- a/src_code/src/main/java/zhao/algorithmMagic/operands/table/Cell.java +++ b/src_code/src/main/java/zhao/algorithmMagic/operands/table/Cell.java @@ -3,6 +3,7 @@ import zhao.algorithmMagic.operands.Operands; import java.io.Serializable; +import java.util.Date; /** * 单元格数据类型,该数据类型被 Multi-type 矩阵所使用,并进行相对应的处理与操作。 @@ -33,6 +34,13 @@ public interface Cell extends Serializable, Operands> { */ int getIntValue(); + /** + * @return 当前单元格中存储的整数数值类型,该操作返回的将不是原先的对象。 + *

+ * The integer numeric type stored in the current cell. This operation will not return the original object. + */ + long getLongValue(); + /** * @return 当前单元格中存储的Double数值类型,该操作返回的将不是原先的对象。 *

@@ -40,6 +48,13 @@ public interface Cell extends Serializable, Operands> { */ double getDoubleValue(); + /** + * @return 当前单元格中存储的 Date 数值类型,该操作返回的将不是原先的对象。 + *

+ * The Date numeric type stored in the current cell. This operation will not return the original object. + */ + Date getDate(); + /** * @return 当前单元格中存储的字符串数值类型,该操作返回的将不是原先的对象。 *

diff --git a/src_code/src/main/java/zhao/algorithmMagic/operands/table/DataFrame.java b/src_code/src/main/java/zhao/algorithmMagic/operands/table/DataFrame.java index dae7fc8..98d8ce4 100644 --- a/src_code/src/main/java/zhao/algorithmMagic/operands/table/DataFrame.java +++ b/src_code/src/main/java/zhao/algorithmMagic/operands/table/DataFrame.java @@ -4,6 +4,8 @@ import zhao.algorithmMagic.operands.Operands; import zhao.algorithmMagic.utils.transformation.Transformation; +import java.io.BufferedWriter; +import java.io.PrintStream; import java.io.Serializable; import java.util.List; @@ -266,6 +268,24 @@ public interface DataFrame extends AggDataFrameData, Iterable, Serializa */ DataFrame into_outComponent(OutputComponent outputComponent); + /** + * 默认方式查看 DF 数据对象中的数据。 + *

+ * The default method is to view data in DF data objects. + */ + void show(); + + /** + * 指定数据流的方式查看 DF 数据对象中的数据,该操作将会使得 DF 数据被输出到数据流中。 + *

+ * Specify the method of viewing data in the DF data object through a data stream, which will cause the DF data to be output into the data stream. + * + * @param bufferedWriter 需要传递数据的数据流对象。 + */ + void show(BufferedWriter bufferedWriter); + + void show(PrintStream printStream); + /** * 将DF对象中的所有数据转换成为一个list容器。 *

diff --git a/src_code/src/main/java/zhao/algorithmMagic/operands/table/FDataFrame.java b/src_code/src/main/java/zhao/algorithmMagic/operands/table/FDataFrame.java index a0ee892..644c3dd 100644 --- a/src_code/src/main/java/zhao/algorithmMagic/operands/table/FDataFrame.java +++ b/src_code/src/main/java/zhao/algorithmMagic/operands/table/FDataFrame.java @@ -7,8 +7,10 @@ import zhao.algorithmMagic.utils.ASMath; import zhao.algorithmMagic.utils.transformation.Transformation; +import java.io.BufferedWriter; import java.io.File; import java.io.IOException; +import java.io.PrintStream; import java.sql.Connection; import java.util.*; @@ -108,6 +110,23 @@ public static DataFrame builder(InputComponent inputComponent) { throw new OperatorOperationException("inputComponent open error!!!"); } + /** + * 使用第三方数据源输入组件进行数据的加载,并获取到对应的DataFrame对象。 + * + * @param inputComponent 需要使用的第三方数据输入组件对象 + * @return 获取到的DataFrame对象。 + */ + public static DataFrame builder(InputComponent inputComponent, boolean isOC) { + if (isOC) { + if (inputComponent.open()) { + DataFrame dataFrame = inputComponent.getDataFrame(); + ASIO.close(inputComponent); + return dataFrame; + } + throw new OperatorOperationException("inputComponent open error!!!"); + } else return inputComponent.getDataFrame(); + } + /** * 刷新字段数据,在数据集中包含针对行列字段名称构建的索引Hash表,在hash表中的字段时可以进行刷新的,经过刷新之后,原先的字段将不会消失,而是与新字段共同指向同一个数据行,一般来说,不更改行字段的情况下将不会调用该函数。 *

@@ -680,6 +699,92 @@ public DataFrame into_outComponent(OutputComponent outputComponent) { return this; } + /** + * 默认方式查看 DF 数据对象中的数据。 + *

+ * The default method is to view data in DF data objects. + */ + @Override + public void show() { + this.show(System.out); + } + + /** + * 直接在输出流中将图表中的数据展示出来,相较于 toString 该函数性能更加优秀。 + * + * @param bufferedWriter 需要使用的数据输出流。 + */ + @Override + public void show(BufferedWriter bufferedWriter) { + StringBuilder split = new StringBuilder(); + StringBuilder field = new StringBuilder(); + String s; + try { + s = getFieldRowStr(split, field); + bufferedWriter.append('\n') + .append(s) + .append(field) + .append('\n') + .write(s); + for (Series cells : this.list) { + bufferedWriter.write("│\t\t"); + for (Cell cell : cells) { + bufferedWriter + .append(cell.getValue().toString()) + .write("\t│\t"); + } + bufferedWriter.write('\n'); + } + bufferedWriter.append(s); + } catch (IOException e) { + throw new OperatorOperationException(e); + } + } + + public final String getFieldRowStr(StringBuilder split, StringBuilder field) { + String s; + split.append('├'); + field.append("│\t\t"); + for (Cell cell : this.getFields()) { + field.append(cell.toString()); + field.append("\t│\t"); + split.append("─────────────────"); + } + split.append('┤').append('\n'); + s = split.toString(); + return s; + } + + /** + * 直接在输出流中将图表中的数据展示出来,相较于 toString 该函数性能更加优秀。 + * + * @param printStream 需要使用的数据输出流。 + */ + @Override + public void show(PrintStream printStream) { + StringBuilder split = new StringBuilder(); + StringBuilder field = new StringBuilder(); + String s; + { + s = getFieldRowStr(split, field); + printStream.append('\n') + .append(s) + .append(field) + .append('\n') + .append(s); + } + for (Series cells : this.list) { + printStream.append("│\t\t"); + for (Cell cell : cells) { + printStream + .append(cell.getValue().toString()) + .append("\t│\t"); + } + printStream.append('\n'); + } + printStream.append(s); + } + /** * 将DF对象中的所有数据转换成为一个list容器。 *

diff --git a/src_code/src/main/java/zhao/algorithmMagic/operands/table/FinalCell.java b/src_code/src/main/java/zhao/algorithmMagic/operands/table/FinalCell.java index 1d98e28..1d6b074 100644 --- a/src_code/src/main/java/zhao/algorithmMagic/operands/table/FinalCell.java +++ b/src_code/src/main/java/zhao/algorithmMagic/operands/table/FinalCell.java @@ -3,6 +3,8 @@ import zhao.algorithmMagic.exception.OperatorOperationException; import zhao.algorithmMagic.utils.ASStr; +import java.util.Date; + /** * 不可变数值的单元格对象,该数据类型将可存储一个单元格,并允许其类型是多样的。 *

@@ -76,6 +78,20 @@ public final int getIntValue() { } } + /** + * @return 当前单元格中存储的整数数值类型,该操作返回的将不是原先的对象。 + *

+ * The integer numeric type stored in the current cell. This operation will not return the original object. + */ + @Override + public final long getLongValue() { + if (isNumber()) { + return ((Number) this.valueType).longValue(); + } else { + throw new OperatorOperationException("您在尝试将一个非数值的单元格按照整数的方式提取,这是不允许的。\nYou are trying to extract a non-numeric cell as an integer, which is not allowed."); + } + } + /** * @return 当前单元格中存储的Double数值类型,该操作返回的将不是原先的对象。 *

@@ -90,6 +106,22 @@ public final double getDoubleValue() { } } + /** + * @return 当前单元格中存储的 Date 数值类型,该操作返回的将不是原先的对象。 + *

+ * The Date numeric type stored in the current cell. This operation will not return the original object. + */ + @Override + public Date getDate() { + if (isNumber()) { + return new Date(this.getLongValue()); + } else { + if (this.valueType instanceof Date) return (Date) this.valueType; + else + throw new OperatorOperationException("您无法从单元格中提取数据,因为其不属于毫秒数值也不属于日期对象。\nYou cannot extract data from a cell because it does not belong to a millisecond value or a date object."); + } + } + /** * @return 当前单元格中存储的字符串数值类型,该操作返回的将不是原先的对象。 *

diff --git a/src_code/src/main/java/zhao/algorithmMagic/utils/ASIO.java b/src_code/src/main/java/zhao/algorithmMagic/utils/ASIO.java index 40971a1..6c993e4 100644 --- a/src_code/src/main/java/zhao/algorithmMagic/utils/ASIO.java +++ b/src_code/src/main/java/zhao/algorithmMagic/utils/ASIO.java @@ -65,6 +65,18 @@ public static void writer(File path, Consumer voidTrans) { } } + /** + * 写数据的工具函数,该函数将有效的管理数据流。 + * + * @param outputStream 文本数据输出流 + * @param voidTrans 文本数据输出使用输出流 + */ + public static void writer(OutputStream outputStream, Consumer voidTrans) { + BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(outputStream)); + voidTrans.accept(bufferedWriter); + ASIO.close(bufferedWriter); + } + /** * 将一个byte数组输出到指定的路径 * From 9415ec1ac79cb3c70ae25bdda7496f4270cc12da Mon Sep 17 00:00:00 2001 From: LingYuZhao Date: Sun, 9 Apr 2023 16:47:16 +0800 Subject: [PATCH 16/17] =?UTF-8?q?1.18=E7=89=88=E6=9C=AC=E5=8F=91=E5=B8=83?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * 1.18版本发布,新版本已发布至maven --- AsLib/LibSrc/.idea/workspace.xml | 1 + .../Testing/Temporary/LastTest.log | 4 +- AsLib/libBeardedManZhao.dll | Bin 74826 -> 74826 bytes README-Chinese.md | 2 +- README.md | 2 +- src_code/README-Chinese.md | 110 +++++++- src_code/README.md | 103 ++++++++ src_code/pom.xml | 18 +- .../main/java/zhao/algorithmMagic/MAIN1.java | 39 +-- .../algorithmMagic/core/AlgorithmStar.java | 241 +++++++++++++----- .../operands/matrix/ColorMatrix.java | 3 + .../operands/table/DataFrameBuilder.java | 8 + .../operands/table/FDataFrame.java | 3 + .../operands/table/FDataFrameBuilder.java | 1 + .../operands/vector/SparkVector.scala | 30 +-- 15 files changed, 448 insertions(+), 117 deletions(-) diff --git a/AsLib/LibSrc/.idea/workspace.xml b/AsLib/LibSrc/.idea/workspace.xml index 2eb27a2..7583df5 100644 --- a/AsLib/LibSrc/.idea/workspace.xml +++ b/AsLib/LibSrc/.idea/workspace.xml @@ -110,6 +110,7 @@ + diff --git a/AsLib/LibSrc/cmake-build-debug/Testing/Temporary/LastTest.log b/AsLib/LibSrc/cmake-build-debug/Testing/Temporary/LastTest.log index 0022b89..b4e6485 100644 --- a/AsLib/LibSrc/cmake-build-debug/Testing/Temporary/LastTest.log +++ b/AsLib/LibSrc/cmake-build-debug/Testing/Temporary/LastTest.log @@ -1,3 +1,3 @@ -Start testing: Mar 26 11:37 ?D1??? +Start testing: Apr 09 14:04 ?D1??? ---------------------------------------------------------- -End testing: Mar 26 11:37 ?D1??? +End testing: Apr 09 14:04 ?D1??? diff --git a/AsLib/libBeardedManZhao.dll b/AsLib/libBeardedManZhao.dll index 1bf4d30e2e09ef4e9a0558886da11951d4e25fff..7d66fcef7ae50045a10fc96b883ac6e937d09645 100644 GIT binary patch delta 57 zcmX?gg5}f+mI)oq6`@8GyM38#?{B=-tt?`ppO}-LUzAyrksDl+Sfp#HXK1mxQl*;{ Lq;502p7S>Vrf(JB delta 57 zcmX?gg5}f+mI)oq9Q))acKb4!uitp9TUo?hKQSjgzbLaLBR9Atu}IfY&(M5xrAjv^ LNZn?3J?C!#my#88 diff --git a/README-Chinese.md b/README-Chinese.md index aa89f66..c8ab5ec 100644 --- a/README-Chinese.md +++ b/README-Chinese.md @@ -18,7 +18,7 @@ io.github.BeardedManZhao algorithmStar - 1.17 + 1.18 ``` diff --git a/README.md b/README.md index d4cd55f..40c283c 100644 --- a/README.md +++ b/README.md @@ -21,7 +21,7 @@ can add it to your maven project, or you can download it from Releases and manua io.github.BeardedManZhao algorithmStar - 1.17 + 1.18 ``` diff --git a/src_code/README-Chinese.md b/src_code/README-Chinese.md index 50b9a8b..cd3c098 100644 --- a/src_code/README-Chinese.md +++ b/src_code/README-Chinese.md @@ -14,6 +14,7 @@ ```java package zhao.algorithmMagic; +import zhao.algorithmMagic.core.AlgorithmStar; import zhao.algorithmMagic.operands.matrix.ColorMatrix; import java.awt.*; @@ -21,7 +22,7 @@ import java.awt.*; public class MAIN1 { public static void main(String[] args) { // 获取一张图像的像素矩阵 - ColorMatrix parse1 = ColorMatrix.parseGrayscale("C:\\Users\\Liming\\Desktop\\fsdownload\\test1.bmp"); + ColorMatrix parse1 = AlgorithmStar.parseGrayscaleImage("C:\\Users\\Liming\\Desktop\\fsdownload\\test1.bmp"); // 将 parse1 进行二值化 parse1.localBinary(ColorMatrix._G_, 30, 0, 0xffffff, 1); // 将 parse1 矩阵腐蚀,然后将腐蚀的结果获取到 @@ -44,7 +45,7 @@ import zhao.algorithmMagic.operands.matrix.ColorMatrix; public class MAIN1 { public static void main(String[] args) { // 创建一个图像矩阵 - ColorMatrix parse = ColorMatrix.parseGrayscale("C:\\Users\\Liming\\Desktop\\fsdownload\\test3.bmp"); + ColorMatrix parse = AlgorithmStar.parseGrayscaleImage("C:\\Users\\Liming\\Desktop\\fsdownload\\test3.bmp"); // 为图像矩阵进行腐蚀和膨胀操作 // 黑色作为背景色腐蚀 ColorMatrix erode1 = parse.erode(3, 3, true); @@ -68,7 +69,7 @@ import java.awt.*; public class MAIN1 { public static void main(String[] args) { // 获取一张图像的像素矩阵 - ColorMatrix parse1 = ColorMatrix.parseGrayscale("C:\\Users\\Liming\\Desktop\\fsdownload\\test1.bmp"); + ColorMatrix parse1 = AlgorithmStar.parseGrayscaleImage("C:\\Users\\Liming\\Desktop\\fsdownload\\test1.bmp"); Color[][] colors = parse1.copyToNewArrays(); } } @@ -94,7 +95,7 @@ public class MAIN1 { ColorMatrix calculate2; { // 创建一个图像矩阵 - ColorMatrix parse = ColorMatrix.parseGrayscale("C:\\Users\\Liming\\Desktop\\fsdownload\\test3.bmp"); + ColorMatrix parse = AlgorithmStar.parseGrayscaleImage("C:\\Users\\Liming\\Desktop\\fsdownload\\test3.bmp"); // 进行左右的梯度计算 parse.calculate(ColorMatrix.CALCULATE_GRADIENT_RL, true).show("image1"); // 进行上下的梯度计算 @@ -670,4 +671,105 @@ public class MAIN1 { } ``` +* 支持通过 AlgorithmStar 读取到数值矩阵与结构化 DF 数据对象,这种方式创建与通过 实现类静态的parse函数创建的效果一样。 + +```java +package zhao.algorithmMagic; + +import zhao.algorithmMagic.core.AlgorithmStar; +import zhao.algorithmMagic.io.InputByStream; +import zhao.algorithmMagic.io.InputByStreamBuilder; +import zhao.algorithmMagic.io.InputComponent; +import zhao.algorithmMagic.operands.matrix.DoubleMatrix; +import zhao.algorithmMagic.operands.matrix.IntegerMatrix; +import zhao.algorithmMagic.operands.table.DataFrame; +import zhao.algorithmMagic.operands.table.FinalCell; + +import java.io.File; +import java.io.IOException; +import java.sql.Connection; +import java.sql.DriverManager; +import java.sql.SQLException; + + +public class MAIN1 { + public static void main(String[] args) throws SQLException { + /* ***************************************************** + * TODO 从数据流中读取到数据 + * *****************************************************/ + // 开始将所有的参数配置到设备对象中,构建出数据输入设备 + InputComponent inputComponent = InputByStream.builder() + .addInputArg(InputByStreamBuilder.INPUT_STREAM, new FinalCell<>(System.in)) + .addInputArg(InputByStreamBuilder.CHARSET, new FinalCell<>("utf-8")) + .addInputArg(InputByStreamBuilder.SEP, new FinalCell<>(',')) + .addInputArg(InputByStreamBuilder.PK, new FinalCell<>(1)) + .addInputArg(InputByStreamBuilder.ROW_LEN, new FinalCell<>(3)) + .create(); + + // TODO 开始通过 algorithmStar 构建出数值矩阵 这里是整数与浮点数两种类型 + IntegerMatrix integerMatrix = AlgorithmStar.parseIntMat( + new int[]{1, 2, 3}, new int[]{5, 6, 7}, new int[]{8, 9, 0} + ); + DoubleMatrix doubleMatrix = AlgorithmStar.parseDoubleMat( + new double[]{1, 2, 3}, new double[]{5, 6, 7}, new double[]{8, 9, 0} + ); + System.out.println(integerMatrix); + System.out.println(doubleMatrix); + + // TODO 开始通过 algorithmStar 构建出DataFrame对象 这里是通过文件 数据库 数据输入组件 来进行构建 + File file = new File("C:\\Users\\zhao\\Downloads\\test.csv"); + DataFrame dataFrame1 = AlgorithmStar.parseDF(file).setSep(',') + .create("year", "month", "day", "week", "temp_2", "temp_1", "average", "actual", "friend") + .execute(); + dataFrame1.show(); + + DataFrame dataFrame2 = AlgorithmStar.parseDF(inputComponent, false); + dataFrame2.show(); + + Connection connection = DriverManager.getConnection(""); + DataFrame dataFrame3 = AlgorithmStar.parseDF(connection) + .from("xxx") + .execute(); + dataFrame3.show(); + } +} +``` + +* 支持通过 AlgorithmStar静态 读取到图像矩阵,能够从文件,URL,以及数据输入组件中获取到图像对象,这种方式创建与通过 实现类静态的parse函数创建的效果一样。 + +```java +package zhao.algorithmMagic; + +import zhao.algorithmMagic.core.AlgorithmStar; +import zhao.algorithmMagic.io.InputCamera; +import zhao.algorithmMagic.io.InputCameraBuilder; +import zhao.algorithmMagic.io.InputComponent; +import zhao.algorithmMagic.operands.matrix.ColorMatrix; +import zhao.algorithmMagic.operands.table.FinalCell; + +import java.io.IOException; +import java.net.URL; +import java.sql.SQLException; + + +public class MAIN1 { + public static void main(String[] args) throws IOException { + InputComponent inputComponent = InputCamera.builder() + .addInputArg(InputCameraBuilder.Camera_Index, new FinalCell<>(0)) + .addInputArg(InputCameraBuilder.Image_Format, new FinalCell<>("JPG")) + .create(); + // 通过数据输入设备对象获取到图像矩阵 + ColorMatrix colorMatrix = AlgorithmStar.parseImage(inputComponent, true); + colorMatrix.show("image"); + // 通过 URL 获取到图像矩阵 (为了简洁书写在这里没有进行赋值动作) + URL url = new URL("https://user-images.githubusercontent.com/113756063/194830221-abe24fcc-484b-4769-b3b7-ec6d8138f436.png"); + AlgorithmStar.parseImage(url).show("image"); + AlgorithmStar.parseGrayscaleImage(url).show("image"); + // 通过 文件 获取到图像矩阵 + AlgorithmStar.parseImage("C:\\Users\\zhao\\Desktop\\fsdownload\\test.bmp").show("image"); + AlgorithmStar.parseGrayscaleImage("C:\\Users\\zhao\\Desktop\\fsdownload\\test.bmp").show("image"); + } +} +``` + ### Version update date : xx xx-xx-xx \ No newline at end of file diff --git a/src_code/README.md b/src_code/README.md index c7d65ba..3e83c79 100644 --- a/src_code/README.md +++ b/src_code/README.md @@ -715,4 +715,107 @@ public class MAIN1 { } ``` +* Supports reading numerical matrices and structured DF data objects through AlgorithmStar, creating the same effect as + creating class static parse functions. + +```java +package zhao.algorithmMagic; + +import zhao.algorithmMagic.core.AlgorithmStar; +import zhao.algorithmMagic.io.InputByStream; +import zhao.algorithmMagic.io.InputByStreamBuilder; +import zhao.algorithmMagic.io.InputComponent; +import zhao.algorithmMagic.operands.matrix.DoubleMatrix; +import zhao.algorithmMagic.operands.matrix.IntegerMatrix; +import zhao.algorithmMagic.operands.table.DataFrame; +import zhao.algorithmMagic.operands.table.FinalCell; + +import java.io.File; +import java.io.IOException; +import java.sql.Connection; +import java.sql.DriverManager; +import java.sql.SQLException; + + +public class MAIN1 { + public static void main(String[] args) throws SQLException { + /* ***************************************************** + * TODO 从数据流中读取到数据 + * *****************************************************/ + // 开始将所有的参数配置到设备对象中,构建出数据输入设备 + InputComponent inputComponent = InputByStream.builder() + .addInputArg(InputByStreamBuilder.INPUT_STREAM, new FinalCell<>(System.in)) + .addInputArg(InputByStreamBuilder.CHARSET, new FinalCell<>("utf-8")) + .addInputArg(InputByStreamBuilder.SEP, new FinalCell<>(',')) + .addInputArg(InputByStreamBuilder.PK, new FinalCell<>(1)) + .addInputArg(InputByStreamBuilder.ROW_LEN, new FinalCell<>(3)) + .create(); + + // TODO 开始通过 algorithmStar 构建出数值矩阵 这里是整数与浮点数两种类型 + IntegerMatrix integerMatrix = AlgorithmStar.parseIntMat( + new int[]{1, 2, 3}, new int[]{5, 6, 7}, new int[]{8, 9, 0} + ); + DoubleMatrix doubleMatrix = AlgorithmStar.parseDoubleMat( + new double[]{1, 2, 3}, new double[]{5, 6, 7}, new double[]{8, 9, 0} + ); + System.out.println(integerMatrix); + System.out.println(doubleMatrix); + + // TODO 开始通过 algorithmStar 构建出DataFrame对象 这里是通过文件 数据库 数据输入组件 来进行构建 + File file = new File("C:\\Users\\zhao\\Downloads\\test.csv"); + DataFrame dataFrame1 = AlgorithmStar.parseDF(file).setSep(',') + .create("year", "month", "day", "week", "temp_2", "temp_1", "average", "actual", "friend") + .execute(); + dataFrame1.show(); + + DataFrame dataFrame2 = AlgorithmStar.parseDF(inputComponent, false); + dataFrame2.show(); + + Connection connection = DriverManager.getConnection(""); + DataFrame dataFrame3 = AlgorithmStar.parseDF(connection) + .from("xxx") + .execute(); + dataFrame3.show(); + } +} +``` + +* Support static reading of image matrices through AlgorithmStar, and the ability to obtain image objects from files, + URLs, and data input components. This method of creation has the same effect as creating class static parse functions. + +```java +package zhao.algorithmMagic; + +import zhao.algorithmMagic.core.AlgorithmStar; +import zhao.algorithmMagic.io.InputCamera; +import zhao.algorithmMagic.io.InputCameraBuilder; +import zhao.algorithmMagic.io.InputComponent; +import zhao.algorithmMagic.operands.matrix.ColorMatrix; +import zhao.algorithmMagic.operands.table.FinalCell; + +import java.io.IOException; +import java.net.URL; +import java.sql.SQLException; + + +public class MAIN1 { + public static void main(String[] args) throws IOException { + InputComponent inputComponent = InputCamera.builder() + .addInputArg(InputCameraBuilder.Camera_Index, new FinalCell<>(0)) + .addInputArg(InputCameraBuilder.Image_Format, new FinalCell<>("JPG")) + .create(); + // 通过数据输入设备对象获取到图像矩阵 + ColorMatrix colorMatrix = AlgorithmStar.parseImage(inputComponent, true); + colorMatrix.show("image"); + // 通过 URL 获取到图像矩阵 (为了简洁书写在这里没有进行赋值动作) + URL url = new URL("https://user-images.githubusercontent.com/113756063/194830221-abe24fcc-484b-4769-b3b7-ec6d8138f436.png"); + AlgorithmStar.parseImage(url).show("image"); + AlgorithmStar.parseGrayscaleImage(url).show("image"); + // 通过 文件 获取到图像矩阵 + AlgorithmStar.parseImage("C:\\Users\\zhao\\Desktop\\fsdownload\\test.bmp").show("image"); + AlgorithmStar.parseGrayscaleImage("C:\\Users\\zhao\\Desktop\\fsdownload\\test.bmp").show("image"); + } +} +``` + ### Version update date : xx xx-xx-xx \ No newline at end of file diff --git a/src_code/pom.xml b/src_code/pom.xml index ea2d19a..93b0da2 100644 --- a/src_code/pom.xml +++ b/src_code/pom.xml @@ -5,7 +5,7 @@ io.github.BeardedManZhao algorithmStar - 1.17 + 1.18 jar algorithmStar algorithmStar-java @@ -38,7 +38,7 @@ org.apache.logging.log4j log4j-slf4j-impl 2.20.0 - + provided @@ -46,14 +46,14 @@ org.apache.logging.log4j log4j-api 2.20.0 - + provided org.apache.logging.log4j log4j-core 2.20.0 - + provided @@ -82,27 +82,27 @@ provided - + com.github.sarxos webcam-capture 0.3.12 - + provided - + org.apache.hadoop hadoop-client 3.3.2 - + provided org.apache.hadoop hadoop-hdfs 3.3.2 - + provided diff --git a/src_code/src/main/java/zhao/algorithmMagic/MAIN1.java b/src_code/src/main/java/zhao/algorithmMagic/MAIN1.java index 1a3de06..761e70b 100644 --- a/src_code/src/main/java/zhao/algorithmMagic/MAIN1.java +++ b/src_code/src/main/java/zhao/algorithmMagic/MAIN1.java @@ -1,35 +1,18 @@ package zhao.algorithmMagic; -import zhao.algorithmMagic.io.InputByStream; -import zhao.algorithmMagic.io.InputByStreamBuilder; -import zhao.algorithmMagic.io.InputComponent; -import zhao.algorithmMagic.operands.table.DataFrame; -import zhao.algorithmMagic.operands.table.FDataFrame; -import zhao.algorithmMagic.operands.table.FinalCell; - -import java.io.IOException; +import zhao.algorithmMagic.algorithm.OperationAlgorithmManager; +import zhao.algorithmMagic.core.ASDynamicLibrary; +import java.io.File; public class MAIN1 { - public static void main(String[] args) throws IOException { - /* ***************************************************** - * TODO 从数据流中读取到数据 并构建 DF 对象 - * *****************************************************/ - // 开始将所有的参数配置到设备对象中,构建出数据输入设备 - InputComponent inputComponent = InputByStream.builder() - // 准备数据输入流,在这里准备的是终端数据输入流 - .addInputArg(InputByStreamBuilder.INPUT_STREAM, new FinalCell<>(System.in)) - // 设置数据输入的字符集 - .addInputArg(InputByStreamBuilder.CHARSET, new FinalCell<>("utf-8")) - // 由于 DF 数据加载是结构化模式加载,因此需要设置数据输入的分隔符 - .addInputArg(InputByStreamBuilder.SEP, new FinalCell<>(',')) - // 由于 DF 数据对象有主键功能,因此需要指定主键索引编号 这里是从0开始的索引 - .addInputArg(InputByStreamBuilder.PK, new FinalCell<>(1)) - // 设置本次数据要输入的行数量 代表我们要输入 3 行数据 - .addInputArg(InputByStreamBuilder.ROW_LEN, new FinalCell<>(3)) - .create(); - // 开始进行数据的加载 需要注意的是,由于我们使用的是终端数据流,因此不需要框架来关闭数据流,需要指定isOC为false - DataFrame builder = FDataFrame.builder(inputComponent, true); - builder.show(); + public static void main(String[] args) { + System.out.println(OperationAlgorithmManager.VERSION); + if (args.length > 0) { + ASDynamicLibrary.addDllDir(new File(args[0])); + System.out.println(OperationAlgorithmManager.getAlgorithmStarUrl()); + } else { + System.out.println("感谢您的使用。"); + } } } diff --git a/src_code/src/main/java/zhao/algorithmMagic/core/AlgorithmStar.java b/src_code/src/main/java/zhao/algorithmMagic/core/AlgorithmStar.java index 969c171..583cd19 100644 --- a/src_code/src/main/java/zhao/algorithmMagic/core/AlgorithmStar.java +++ b/src_code/src/main/java/zhao/algorithmMagic/core/AlgorithmStar.java @@ -13,22 +13,26 @@ import zhao.algorithmMagic.algorithm.normalization.RangeDataStandardization; import zhao.algorithmMagic.algorithm.probabilisticAlgorithm.ProbabilisticAlgorithm; import zhao.algorithmMagic.algorithm.schemeAlgorithm.SchemeAlgorithm; +import zhao.algorithmMagic.io.InputComponent; import zhao.algorithmMagic.operands.coordinate.DoubleCoordinateMany; import zhao.algorithmMagic.operands.coordinate.FloatingPointCoordinates; import zhao.algorithmMagic.operands.coordinate.IntegerCoordinateMany; import zhao.algorithmMagic.operands.coordinate.IntegerCoordinates; -import zhao.algorithmMagic.operands.matrix.ColumnDoubleMatrix; -import zhao.algorithmMagic.operands.matrix.ColumnIntegerMatrix; -import zhao.algorithmMagic.operands.matrix.DoubleMatrix; -import zhao.algorithmMagic.operands.matrix.IntegerMatrix; +import zhao.algorithmMagic.operands.matrix.*; import zhao.algorithmMagic.operands.route.DoubleConsanguinityRoute; import zhao.algorithmMagic.operands.route.DoubleConsanguinityRoute2D; import zhao.algorithmMagic.operands.route.IntegerConsanguinityRoute; import zhao.algorithmMagic.operands.route.IntegerConsanguinityRoute2D; +import zhao.algorithmMagic.operands.table.DataFrame; +import zhao.algorithmMagic.operands.table.DataFrameBuilder; +import zhao.algorithmMagic.operands.table.FDataFrame; import zhao.algorithmMagic.operands.vector.*; import zhao.algorithmMagic.utils.filter.ArrayDoubleFiltering; import zhao.algorithmMagic.utils.filter.ArrayIntegerFiltering; +import java.io.File; +import java.net.URL; +import java.sql.Connection; import java.util.ArrayList; import java.util.HashMap; import java.util.Map; @@ -90,6 +94,132 @@ public static void clear() { ******************************************************************** */ + /** + * 构造一个矩阵,矩阵的列数量以矩阵的第一行为准! + *

+ * Construct a matrix, the number of columns of the matrix is based on the first row of the matrix! + * + * @param v 用于构造矩阵的二维数组 + *

+ * 2D array for constructing the matrix + * @return matrix object + */ + public static IntegerMatrix parseIntMat(int[]... v) { + return IntegerMatrix.parse(v); + } + + /** + * 构造一个矩阵,矩阵的列数量以矩阵的第一行为准! + *

+ * Construct a matrix, the number of columns of the matrix is based on the first row of the matrix! + * + * @param v 用于构造矩阵的二维数组 + *

+ * 2D array for constructing the matrix + * @return matrix object + */ + public static DoubleMatrix parseDoubleMat(double[]... v) { + return DoubleMatrix.parse(v); + } + + /** + * 从本地文件系统中读取一个数据对象,并返回对应数据对象的建造者类。 + * + * @param file 需要被读取的文件对象。 + * @return 读取之后会返回该数据集对应的一个建造者对象,在该对象中可以对读取操作进行更加详细的设置, + */ + public static DataFrameBuilder parseDF(File file) { + return FDataFrame.builder(file); + } + + /** + * 从远程数据库中读取一个数据对象,并返回数据对象对应的建造者类。 + * + * @param DBC 在连接数据库时需要使用的数据库连接对象。 + * @return 数据库连接设置完毕将会返回一个建造者对象,在该对象中可以对读取数据库操作进行更加详细的设置。 + */ + public static DataFrameBuilder parseDF(Connection DBC) { + return FDataFrame.builder(DBC); + } + + /** + * 使用第三方数据源输入组件进行数据的加载,并获取到对应的DataFrame对象。 + * + * @param inputComponent 需要使用的第三方数据输入组件对象 + * @param isOC 如果设置为 true 代表数据输入设备对象的打开与关闭交由框架管理,在外界将不需要对组件进行打开或关闭操作,反之则代表框架只使用组件,但不会打开与关闭组件对象。 + *

+ * If set to true, it means that the opening and closing of data input device objects are managed by the framework, and there will be no need to open or close components externally. Conversely, it means that the framework only uses components, but will not open or close component objects. + * @return 获取到的DataFrame对象。 + */ + public static DataFrame parseDF(InputComponent inputComponent, boolean isOC) { + return FDataFrame.builder(inputComponent, isOC); + } + + /** + * 使用组件将一个图像数据提取,并获取对应的图像矩阵。 + * + * @param inputComponent 能够被提取出图像矩阵的数据组件。 + * @param isOC 如果设置为 true 代表数据输入设备对象的打开与关闭交由框架管理,在外界将不需要对组件进行打开或关闭操作,反之则代表框架只使用组件,但不会打开与关闭组件对象。 + *

+ * If set to true, it means that the opening and closing of data input device objects are managed by the framework, and there will be no need to open or close components externally. Conversely, it means that the framework only uses components, but will not open or close component objects. + * @return 从组件中提取出来的图像矩阵对象。 + */ + public static ColorMatrix parseImage(InputComponent inputComponent, boolean isOC) { + return ColorMatrix.parse(inputComponent, isOC); + } + + /** + * 将图像URL解析,并获取对应的图像矩阵 + * + * @param url 需要被解析的URL对象 + * @return URL对象所对应的图像矩阵。 + */ + public static ColorMatrix parseImage(URL url) { + return ColorMatrix.parse(url); + } + + /** + * 将图像URL解析,并获取对应的图像矩阵 + * + * @param url 需要被解析的URL对象 + * @return URL对象所对应的图像矩阵。 + */ + public static ColorMatrix parseGrayscaleImage(URL url) { + return ColorMatrix.parseGrayscale(url); + } + + /** + * 根据图像文件获取到整形矩阵对象,在整形矩阵对象中会包含该图像的每一个像素点对应的灰度整形数值。 + *

+ * The reshaping matrix object is obtained from the image file, and the reshaping value corresponding to each pixel of the image will be included in the reshaping matrix object. + * + * @param imagePath 要读取的目标图像文件路径。 + *

+ * The target image file path to read. + * @return 根据图像获取到的矩阵对象。 + *

+ * The matrix object obtained from the image. + */ + public static ColorMatrix parseImage(String imagePath) { + return ColorMatrix.parse(imagePath); + } + + /** + * 根据图像文件获取到整形矩阵对象,在整形矩阵对象中会包含该图像的每一个像素点对应的灰度整形数值。 + *

+ * The reshaping matrix object is obtained from the image file, and the reshaping value corresponding to each pixel of the image will be included in the reshaping matrix object. + * + * @param imagePath 要读取的目标图像文件路径。 + *

+ * The target image file path to read. + * @return 根据图像获取到的矩阵对象。 + *

+ * The matrix object obtained from the image. + */ + public static ColorMatrix parseGrayscaleImage(String imagePath) { + return ColorMatrix.parseGrayscale(imagePath); + } + /** * 计算一个路线的起始点与终止点的真实距离。具体的距离实现,需要您查阅算法实现的文档。 *

@@ -139,6 +269,13 @@ public double getTrueDistance(DistanceAlgorithm distanceAlgorithm, int[] ints1, return distanceAlgorithm.getTrueDistance(ints1, ints2); } + /* + ******************************************************************** + * 聚合算法组件计算函数开始分界线 + * Aggregation algorithm component calculation function start boundary + ******************************************************************** + */ + /** * 计算一个路线的起始点与终止点的真实距离。具体的距离实现,需要您查阅算法实现的文档。 *

@@ -234,6 +371,14 @@ public double getTrueDistance(DistanceAlgorithm distanceAlgorithm, DoubleMatrix return distanceAlgorithm.getTrueDistance(matrix1, matrix2); } + + /* + ******************************************************************** + * 分类算法组件计算函数开始分界线 + * Classification algorithm component calculation function start boundary + ******************************************************************** + */ + /** * 计算向量距离原点的距离。 * @@ -247,13 +392,6 @@ public double getTrueDistance(RangeDistance rangeDistance, RangeVector @@ -271,7 +409,6 @@ public double calculation(AggregationAlgorithm aggregationAlgorithm, double... d return aggregationAlgorithm.calculation(doubles); } - /** * 计算函数,将某个数组中的所有元素按照某个规则进行聚合 *

@@ -350,14 +487,6 @@ public double calculation(RangeAggregation rangeAggregation, RangeVector @@ -444,6 +573,13 @@ public HashMap> classification(NoSampleClassific return noSampleClassification.classification(keys, doubles); } + /* + ******************************************************************** + * 差异算法组件计算函数开始分界线 * + * diff algorithm component calculation function start boundary * + ******************************************************************** + */ + /** * 无样本的距离计算,您在此进行分类,不需要传递很多的数据样本,只需要由实现类按照自己的算法进行类别推断即可。 *

@@ -466,6 +602,13 @@ public HashMap> classification(NoSampleCl return noSampleClassification.classification(keys, columnIntegerMatrix); } + /* + ******************************************************************** + * 特征提取算法组件计算函数开始分界线 * + * FeatureExtraction algorithm component calculation function start boundary * + ******************************************************************** + */ + /** * 无样本的距离计算,您在此进行分类,不需要传递很多的数据样本,只需要由实现类按照自己的算法进行类别推断即可。 *

@@ -488,6 +631,12 @@ public HashMap> classification(NoSampleCla return noSampleClassification.classification(keys, columnDoubleMatrix); } + /* + ******************************************************************** + * 数据预处理算法组件计算函数开始分界线 * + * Data preprocessing algorithm component calculation function start boundary * + ******************************************************************** + */ /** * 计算一个矩阵中所有行或列的数据类别,并将计算之后的数据类别样本返回出去。 @@ -593,14 +742,6 @@ public HashMap> classification(SampleClassifica return sampleClassification.classification(data, categorySample); } - /* - ******************************************************************** - * 差异算法组件计算函数开始分界线 * - * diff algorithm component calculation function start boundary * - ******************************************************************** - */ - - /** * 计算两个事物之间从差异系数百分比 *

@@ -615,13 +756,6 @@ public double getDifferenceRatio(DifferenceAlgorithm differenceAlgori return differenceAlgorithm.getDifferenceRatio(value1, value2); } - /* - ******************************************************************** - * 特征提取算法组件计算函数开始分界线 * - * FeatureExtraction algorithm component calculation function start boundary * - ******************************************************************** - */ - /** * 将很多字符串组合起来进行特征向量的提取,其中的每一个词都是一个特征值。 *

@@ -635,13 +769,6 @@ public featureReturn extract(StringArrayFeature stringArrayFeatur return stringArrayFeature.extract(data); } - /* - ******************************************************************** - * 数据预处理算法组件计算函数开始分界线 * - * Data preprocessing algorithm component calculation function start boundary * - ******************************************************************** - */ - /** * 将一个序列进行标准化,具体的标准化有不同的实现 * @@ -672,12 +799,20 @@ public IntegerCoordinates pretreatment(DataStandardizatio return dataStandardization.pretreatment(v); } + /* + ******************************************************************** + * 概率算法组件计算函数开始分界线 + * Probability algorithm component calculation function start boundary + ******************************************************************** + */ + /** * 将一个序列进行标准化,具体的标准化有不同的实现 * - * @param integerMatrix 需要被标准化的数值,可以是坐标或向量,更多信息需要查阅实现 - *

- * The value to be normalized, which can be a coordinate or a vector. For more information, see the implementation + * @param dataStandardization 需要使用的数据预处理算法组件对象。 + * @param integerMatrix 需要被标准化的数值,可以是坐标或向量,更多信息需要查阅实现 + *

+ * The value to be normalized, which can be a coordinate or a vector. For more information, see the implementation * @return v的标准化样式 *

* Normalized style of v @@ -689,9 +824,10 @@ public IntegerMatrix pretreatment(DataStandardization dataStandardization, Integ /** * 将一个序列进行标准化,具体的标准化有不同的实现 * - * @param doubleMatrix 需要被标准化的数值,可以是坐标或向量,更多信息需要查阅实现 - *

- * The value to be normalized, which can be a coordinate or a vector. For more information, see the implementation + * @param dataStandardization 需要使用的数据预处理算法组件对象。 + * @param doubleMatrix 需要被标准化的数值,可以是坐标或向量,更多信息需要查阅实现 + *

+ * The value to be normalized, which can be a coordinate or a vector. For more information, see the implementation * @return v的标准化样式 *

* Normalized style of v @@ -764,13 +900,6 @@ public DoubleVector pretreatment(RangeDataStandardization rangeDataStandardizati return rangeDataStandardization.pretreatment(fastRangeDoubleVector); } - /* - ******************************************************************** - * 概率算法组件计算函数开始分界线 - * Probability algorithm component calculation function start boundary - ******************************************************************** - */ - /** * 计算一个矩阵中的某些条件限制下的联合概率结果 P(A|B) 其中的分子与分母值! * @@ -905,7 +1034,6 @@ public ArrayList decision(SchemeAlgorithm schemeAlgorithm, return decision(schemeAlgorithm, doubleMatrix.toArrays(), 2, arrayDoubleFiltering); } - /** * 通过决策树,对传进进来的决策序列重新排列,使其成为最优解。 *

@@ -950,7 +1078,6 @@ public ArrayList decision(SchemeAlgorithm schemeAlgorithm, return decision(schemeAlgorithm, ints, 2, arrayDoubleFiltering); } - /** * 通过决策树,对传进进来的决策序列重新排列,使其成为最优解。 *

diff --git a/src_code/src/main/java/zhao/algorithmMagic/operands/matrix/ColorMatrix.java b/src_code/src/main/java/zhao/algorithmMagic/operands/matrix/ColorMatrix.java index 22f8b01..8532b36 100644 --- a/src_code/src/main/java/zhao/algorithmMagic/operands/matrix/ColorMatrix.java +++ b/src_code/src/main/java/zhao/algorithmMagic/operands/matrix/ColorMatrix.java @@ -501,6 +501,9 @@ public static ColorMatrix parse(InputComponent inputComponent) { * 使用组件将一个图像数据提取,并获取对应的图像矩阵。 * * @param inputComponent 能够被提取出图像矩阵的数据组件。 + * @param isOC 如果设置为 true 代表数据输入设备对象的打开与关闭交由框架管理,在外界将不需要对组件进行打开或关闭操作,反之则代表框架只使用组件,但不会打开与关闭组件对象。 + *

+ * If set to true, it means that the opening and closing of data input device objects are managed by the framework, and there will be no need to open or close components externally. Conversely, it means that the framework only uses components, but will not open or close component objects. * @return 从组件中提取出来的图像矩阵对象。 */ public static ColorMatrix parse(InputComponent inputComponent, boolean isOC) { diff --git a/src_code/src/main/java/zhao/algorithmMagic/operands/table/DataFrameBuilder.java b/src_code/src/main/java/zhao/algorithmMagic/operands/table/DataFrameBuilder.java index c3d4371..043f3a9 100644 --- a/src_code/src/main/java/zhao/algorithmMagic/operands/table/DataFrameBuilder.java +++ b/src_code/src/main/java/zhao/algorithmMagic/operands/table/DataFrameBuilder.java @@ -15,6 +15,14 @@ public interface DataFrameBuilder { */ Mode getInMode(); + /** + * 设置读取数据时需要使用的分隔符。在读取一个文件对象的时候会有效果。 + * + * @param sep 指定的数据分隔符,该分隔符将会在读取的时候被调用与获取,最终计算出符合条件的数据行对象。 + * @return 链式调用 + */ + FDataFrameBuilder setSep(char sep); + /** * 将指定的数据查询出来 * diff --git a/src_code/src/main/java/zhao/algorithmMagic/operands/table/FDataFrame.java b/src_code/src/main/java/zhao/algorithmMagic/operands/table/FDataFrame.java index 644c3dd..154d45e 100644 --- a/src_code/src/main/java/zhao/algorithmMagic/operands/table/FDataFrame.java +++ b/src_code/src/main/java/zhao/algorithmMagic/operands/table/FDataFrame.java @@ -114,6 +114,9 @@ public static DataFrame builder(InputComponent inputComponent) { * 使用第三方数据源输入组件进行数据的加载,并获取到对应的DataFrame对象。 * * @param inputComponent 需要使用的第三方数据输入组件对象 + * @param isOC 如果设置为 true 代表数据输入设备对象的打开与关闭交由框架管理,在外界将不需要对组件进行打开或关闭操作,反之则代表框架只使用组件,但不会打开与关闭组件对象。 + *

+ * If set to true, it means that the opening and closing of data input device objects are managed by the framework, and there will be no need to open or close components externally. Conversely, it means that the framework only uses components, but will not open or close component objects. * @return 获取到的DataFrame对象。 */ public static DataFrame builder(InputComponent inputComponent, boolean isOC) { diff --git a/src_code/src/main/java/zhao/algorithmMagic/operands/table/FDataFrameBuilder.java b/src_code/src/main/java/zhao/algorithmMagic/operands/table/FDataFrameBuilder.java index 5627f15..72576ad 100644 --- a/src_code/src/main/java/zhao/algorithmMagic/operands/table/FDataFrameBuilder.java +++ b/src_code/src/main/java/zhao/algorithmMagic/operands/table/FDataFrameBuilder.java @@ -74,6 +74,7 @@ public FDataFrameBuilder setColNames(String[] colNames) { * @param sep 指定的数据分隔符,该分隔符将会在读取的时候被调用与获取,最终计算出符合条件的数据行对象。 * @return 链式调用 */ + @Override public FDataFrameBuilder setSep(char sep) { this.sep = sep; return this; diff --git a/src_code/src/main/java/zhao/algorithmMagic/operands/vector/SparkVector.scala b/src_code/src/main/java/zhao/algorithmMagic/operands/vector/SparkVector.scala index 42bfc95..416767a 100644 --- a/src_code/src/main/java/zhao/algorithmMagic/operands/vector/SparkVector.scala +++ b/src_code/src/main/java/zhao/algorithmMagic/operands/vector/SparkVector.scala @@ -107,13 +107,6 @@ final class SparkVector(sparkContext: SparkContext, vector: org.apache.spark.mll else throw new OperatorOperationException("'DoubleVector1 add DoubleVector2' 时,两个'DoubleVector'的向量所包含的数量不同,DoubleVector1=[" + numberOfDimensions1 + "],DoubleVector2=[" + numberOfDimensions2 + "]\n" + "When 'DoubleVector1 add DoubleVector2', the two vectors of 'DoubleVector' contain different quantities, DoubleVector1=[" + numberOfDimensions1 + "], DoubleVector2=[" + numberOfDimensions2 + "]") } - /** - * @return 向量中包含的维度数量 - *

- * the number of dimensions contained in the vector - */ - override def getNumberOfDimensions: Int = size - /** * 在两个操作数之间做差的方法,具体用法请参阅API说明。 *

@@ -138,6 +131,21 @@ final class SparkVector(sparkContext: SparkContext, vector: org.apache.spark.mll else throw new OperatorOperationException("'DoubleVector1 diff DoubleVector2' 时,两个'DoubleVector'的向量所包含的数量不同,DoubleVector1=[" + numberOfDimensions1 + "],DoubleVector2=[" + numberOfDimensions2 + "]\n" + "When 'DoubleVector1 diff DoubleVector2', the two vectors of 'DoubleVector' contain different quantities, DoubleVector1=[" + numberOfDimensions1 + "], DoubleVector2=[" + numberOfDimensions2 + "]") } + /** + * + * @return 将本对象中存储的向量序列数组拷贝到一个新数组并将新数组返回,这里返回的是一个新数组,支持修改等操作。 + * + * Copy the vector sequence array stored in this object to a new array and return the new array. Here, a new array is returned, which supports modification and other operations. + */ + override def copyToNewArray(): Array[Double] = vector.toArray + + /** + * @return 向量中包含的维度数量 + *

+ * the number of dimensions contained in the vector + */ + override def getNumberOfDimensions: Int = size + /** * * @return 第三方向量中所维护的向量序列,通过此函数您可以直接获取到第三方库中的对象。 @@ -159,14 +167,6 @@ final class SparkVector(sparkContext: SparkContext, vector: org.apache.spark.mll * Objects after disruption. */ override def shuffle(seed: Long): SparkVector = SparkVector.parse(sparkContext, ASMath.shuffle(this.copyToNewArray(), seed, false)) - - /** - * - * @return 将本对象中存储的向量序列数组拷贝到一个新数组并将新数组返回,这里返回的是一个新数组,支持修改等操作。 - * - * Copy the vector sequence array stored in this object to a new array and return the new array. Here, a new array is returned, which supports modification and other operations. - */ - override def copyToNewArray(): Array[Double] = vector.toArray } object SparkVector { From 8870ce2725c35f827b05b4b97a75c33cf795bc5e Mon Sep 17 00:00:00 2001 From: LingYuZhao Date: Sun, 9 Apr 2023 17:05:10 +0800 Subject: [PATCH 17/17] update log MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * 更新日志中英文文档链接 --- src_code/README-Chinese.md | 2 +- src_code/README.md | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src_code/README-Chinese.md b/src_code/README-Chinese.md index cd3c098..c867b86 100644 --- a/src_code/README-Chinese.md +++ b/src_code/README-Chinese.md @@ -1,6 +1,6 @@ # ![image](https://user-images.githubusercontent.com/113756063/194830221-abe24fcc-484b-4769-b3b7-ec6d8138f436.png) 算法之星-机器大脑 -- Switch to [English Document](https://github.com/BeardedManZhao/algorithmStar/blob/main/src_code/README.md) +- Switch to [English Document](https://github.com/BeardedManZhao/algorithmStar/blob/Zhao-develop/src_code/README.md) - knowledge base diff --git a/src_code/README.md b/src_code/README.md index 3e83c79..6e6d4a4 100644 --- a/src_code/README.md +++ b/src_code/README.md @@ -1,6 +1,6 @@ # ![image](https://user-images.githubusercontent.com/113756063/194830221-abe24fcc-484b-4769-b3b7-ec6d8138f436.png) Algorithm Star-MachineBrain -- 切换到 [中文文档](https://github.com/BeardedManZhao/algorithmStar/blob/main/src_code/README-Chinese.md) +- 切换到 [中文文档](https://github.com/BeardedManZhao/algorithmStar/blob/Zhao-develop/src_code/README-Chinese.md) - knowledge base