Skip to content

Commit 100ce52

Browse files
update
* 支持通过URL获取到图像数据
1 parent 7c92e92 commit 100ce52

File tree

7 files changed

+147
-65
lines changed

7 files changed

+147
-65
lines changed

src_code/README-Chinese.md

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -385,4 +385,26 @@ public class MAIN1 {
385385
}
386386
```
387387

388+
* 支持通过一个网络 URL 对象获取到有关图像的数据,并将其转换成为一个图像矩阵。
389+
390+
```java
391+
package zhao.algorithmMagic;
392+
393+
import zhao.algorithmMagic.operands.matrix.ColorMatrix;
394+
395+
import java.net.MalformedURLException;
396+
import java.net.URL;
397+
398+
public class MAIN1 {
399+
public static void main(String[] args) throws MalformedURLException {
400+
// 准备图像的URL对象
401+
URL url = new URL("https://user-images.githubusercontent.com/113756063/194830221-abe24fcc-484b-4769-b3b7-ec6d8138f436.png");
402+
// 解析URL获取到矩阵
403+
ColorMatrix parse = ColorMatrix.parse(url);
404+
// 查看图像
405+
parse.show("image");
406+
}
407+
}
408+
```
409+
388410
### Version update date : xx xx-xx-xx

src_code/README.md

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -383,4 +383,26 @@ public class MAIN1 {
383383
}
384384
```
385385

386+
* 支持通过一个网络 URL 对象获取到有关图像的数据,并将其转换成为一个图像矩阵。
387+
388+
```java
389+
package zhao.algorithmMagic;
390+
391+
import zhao.algorithmMagic.operands.matrix.ColorMatrix;
392+
393+
import java.net.MalformedURLException;
394+
import java.net.URL;
395+
396+
public class MAIN1 {
397+
public static void main(String[] args) throws MalformedURLException {
398+
// 准备图像的URL对象
399+
URL url = new URL("https://user-images.githubusercontent.com/113756063/194830221-abe24fcc-484b-4769-b3b7-ec6d8138f436.png");
400+
// 解析URL获取到矩阵
401+
ColorMatrix parse = ColorMatrix.parse(url);
402+
// 查看图像
403+
parse.show("image");
404+
}
405+
}
406+
```
407+
386408
### Version update date : xx xx-xx-xx
Lines changed: 10 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,17 @@
11
package zhao.algorithmMagic;
22

3-
import zhao.algorithmMagic.operands.matrix.IntegerMatrix;
4-
import zhao.algorithmMagic.operands.table.DataFrame;
5-
import zhao.algorithmMagic.operands.table.FDataFrame;
6-
import zhao.algorithmMagic.operands.table.FieldCell;
7-
import zhao.algorithmMagic.operands.table.FinalCell;
8-
import zhao.algorithmMagic.operands.vector.IntegerVector;
3+
import zhao.algorithmMagic.operands.matrix.ColorMatrix;
94

10-
import java.io.File;
5+
import java.net.MalformedURLException;
6+
import java.net.URL;
117

128
public class MAIN1 {
13-
public static void main(String[] args) {
14-
// 准备文件对象
15-
File file = new File("C:\\Users\\zhao\\Desktop\\out\\res1.csv");
16-
// 使用 FDF 加载文件
17-
DataFrame execute1 = FDataFrame.builder(file)
18-
// 文件对象的读取需要指定文本分隔符
19-
.setSep(',')
20-
// 文件对象需要指定好列名称,不能使用 * 这里代表的不是查询,而是创建一个DF的列字段
21-
.create("id", "name", "sex")
22-
// 文件对象的主键指定允许使用列名称
23-
.primaryKey("name")
24-
// 执行查询
25-
.execute()
26-
// 为列起别名
27-
.select(
28-
FieldCell.$("id"),
29-
FieldCell.$("name").as("名称"),
30-
FieldCell.$("sex").as("性别")
31-
)
32-
// 将性别列进行转换,男生为1 女生为0
33-
.updateCol(FieldCell.$("性别"), cell -> new FinalCell<>(cell.getStringValue().equals("男") ? 1 : 0))
34-
// 将行主键数值为ZLY的数据行中的所有单元格替换成为数据 405
35-
.updateRow("ZLY", cell -> new FinalCell<>(405));
36-
37-
// 打印出表中的行主键名称为 405 的数据行
38-
System.out.println(execute1.selectRow("405"));
39-
long start = System.currentTimeMillis();
40-
// 将表转换成为一个整形矩阵对象,该操作会将DF对象中的所有数值试图转换成为 col.count()*3 的矩阵对象
41-
IntegerMatrix parse = IntegerMatrix.parse(execute1, execute1.count().getIntValue(), 3);
42-
System.out.println(IntegerVector.parse(parse.getArrayByColIndex(2)));
43-
System.out.print("处理耗时(MS):");
44-
System.out.println(System.currentTimeMillis() - start);
9+
public static void main(String[] args) throws MalformedURLException {
10+
// 准备图像的URL对象
11+
URL url = new URL("https://user-images.githubusercontent.com/113756063/194830221-abe24fcc-484b-4769-b3b7-ec6d8138f436.png");
12+
// 解析URL获取到矩阵
13+
ColorMatrix parse = ColorMatrix.parse(url);
14+
// 查看图像
15+
parse.show("image");
4516
}
4617
}

src_code/src/main/java/zhao/algorithmMagic/operands/matrix/ColorMatrix.java

Lines changed: 39 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import java.awt.*;
1111
import java.io.File;
1212
import java.io.IOException;
13+
import java.net.URL;
1314
import java.util.Arrays;
1415
import java.util.Iterator;
1516

@@ -120,14 +121,17 @@ public static ColorMatrix parse(String inputString) {
120121
*/
121122
public static ColorMatrix parseGrayscale(String inputString) {
122123
Color[][] colors = ASIO.parseImageGetColorArray(inputString);
123-
for (Color[] color : colors) {
124-
int count = -1;
125-
for (Color color1 : color) {
126-
int avg = (int) ASMath.avg(color1.getRed(), color1.getGreen(), color1.getBlue());
127-
color[++count] = new Color(avg, avg, avg, color1.getAlpha());
128-
}
129-
}
130-
return new ColorMatrix(colors.length, colors[0].length, colors, true);
124+
return GrayscaleColors(colors);
125+
}
126+
127+
/**
128+
* 将图像URL解析,并获取对应的图像矩阵
129+
*
130+
* @param url 需要被解析的URL对象
131+
* @return URL对象所对应的图像矩阵。
132+
*/
133+
public static ColorMatrix parseGrayscale(URL url) {
134+
return GrayscaleColors(ASIO.parseURLGetColorArray(url));
131135
}
132136

133137
/**
@@ -204,6 +208,33 @@ public static ColorMatrix parse(IntegerMatrix integerMatrix, boolean isGrayscale
204208
return ColorMatrix.parse(colors);
205209
}
206210

211+
/**
212+
* 将图像URL解析,并获取对应的图像矩阵
213+
*
214+
* @param url 需要被解析的URL对象
215+
* @return URL对象所对应的图像矩阵。
216+
*/
217+
public static ColorMatrix parse(URL url) {
218+
return ColorMatrix.parse(ASIO.parseURLGetColorArray(url));
219+
}
220+
221+
/**
222+
* 将一个图像矩阵中的所有像素转换成为三原色均值,获取到灰度矩阵。
223+
*
224+
* @param colors 需要被转换的颜色矩阵(注意,该矩阵将会被修改)
225+
* @return 转换之后的颜色矩阵
226+
*/
227+
private static ColorMatrix GrayscaleColors(Color[][] colors) {
228+
for (Color[] color : colors) {
229+
int count = -1;
230+
for (Color color1 : color) {
231+
int avg = (int) ASMath.avg(color1.getRed(), color1.getGreen(), color1.getBlue());
232+
color[++count] = new Color(avg, avg, avg, color1.getAlpha());
233+
}
234+
}
235+
return new ColorMatrix(colors.length, colors[0].length, colors, true);
236+
}
237+
207238
/**
208239
* 将两个操作数进行求和的方法,具体用法请参阅API说明。
209240
* <p>

src_code/src/main/java/zhao/algorithmMagic/operands/table/FDataFrame.java

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,16 @@ public static FDataFrameBuilder builder(Connection DBC) {
7070
return new FDataFrameBuilder(DBC);
7171
}
7272

73+
/**
74+
* 创建出一个空的DF对象
75+
*
76+
* @param colNameRow DF中的字段序列。
77+
* @return 空的DF对象。
78+
*/
79+
public static FDataFrame craete(Series colNameRow) {
80+
return new FDataFrame(colNameRow, 0, new ArrayList<>());
81+
}
82+
7383
/**
7484
* 刷新字段数据,在数据集中包含针对行列字段名称构建的索引Hash表,在hash表中的字段时可以进行刷新的,经过刷新之后,原先的字段将不会消失,而是与新字段共同指向同一个数据行,一般来说,不更改行字段的情况下将不会调用该函数。
7585
* <p>
@@ -266,7 +276,7 @@ public DataFrame selectRow(String... rowNames) {
266276
}
267277

268278
/**
269-
* 将指定的符合条件的数据获取到。
279+
* 将指定的符合条件的数据获取到,使用全表扫描
270280
*
271281
* @param whereClause 条件判断逻辑实现。
272282
* @return 符合条件的数据系列组合成的新数据表。

src_code/src/main/java/zhao/algorithmMagic/operands/vector/SparkVector.scala

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,14 @@ final class SparkVector(sparkContext: SparkContext, vector: org.apache.spark.mll
5151
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 + "]")
5252
}
5353

54+
/**
55+
*
56+
* @return 将本对象中存储的向量序列数组拷贝到一个新数组并将新数组返回,这里返回的是一个新数组,支持修改等操作。
57+
*
58+
* 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.
59+
*/
60+
override def copyToNewArray(): Array[Double] = vector.toArray
61+
5462
/**
5563
* 计算两个向量的内积,也称之为数量积,具体实现请参阅api说明
5664
* <p>
@@ -105,21 +113,6 @@ final class SparkVector(sparkContext: SparkContext, vector: org.apache.spark.mll
105113
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 + "]")
106114
}
107115

108-
/**
109-
*
110-
* @return 将本对象中存储的向量序列数组拷贝到一个新数组并将新数组返回,这里返回的是一个新数组,支持修改等操作。
111-
*
112-
* 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.
113-
*/
114-
override def copyToNewArray(): Array[Double] = vector.toArray
115-
116-
/**
117-
* @return 向量中包含的维度数量
118-
* <p>
119-
* the number of dimensions contained in the vector
120-
*/
121-
override def getNumberOfDimensions: Int = size
122-
123116
/**
124117
* 在两个操作数之间做差的方法,具体用法请参阅API说明。
125118
* <p>
@@ -144,6 +137,13 @@ final class SparkVector(sparkContext: SparkContext, vector: org.apache.spark.mll
144137
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 + "]")
145138
}
146139

140+
/**
141+
* @return 向量中包含的维度数量
142+
* <p>
143+
* the number of dimensions contained in the vector
144+
*/
145+
override def getNumberOfDimensions: Int = size
146+
147147
/**
148148
*
149149
* @return 第三方向量中所维护的向量序列,通过此函数您可以直接获取到第三方库中的对象。

src_code/src/main/java/zhao/algorithmMagic/utils/ASIO.java

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import java.awt.image.BufferedImage;
1111
import java.awt.image.DataBufferByte;
1212
import java.io.*;
13+
import java.net.URL;
1314
import java.util.function.Consumer;
1415

1516
/**
@@ -139,6 +140,20 @@ public static Color[][] parseImageGetColorArray(String inputString) {
139140
}
140141
}
141142

143+
/**
144+
* 通过一个URL获取到图像数据,并将其解析成为矩阵对象。
145+
*
146+
* @param imageUrl 需要被获取的图像对象,请注意,确保此URL是一个图像文件的URL
147+
* @return 解析之后的结果数据。
148+
*/
149+
public static Color[][] parseURLGetColorArray(URL imageUrl) {
150+
try {
151+
return parseImageGetColorArray(ImageIO.read(imageUrl.openStream()));
152+
} catch (IOException e) {
153+
throw new OperatorOperationException(e);
154+
}
155+
}
156+
142157
public static IntegerMatrix[] parseImageGetArrays(String inputString) {
143158
try {
144159
return parseImageGetArrays(new File(inputString));
@@ -157,8 +172,19 @@ public static IntegerMatrix[] parseImageGetArrays(String inputString) {
157172
* @throws IOException 解析图像矩阵发生错误的时候抛出该异常
158173
*/
159174
public static Color[][] parseImageGetColorArray(File inputFile) throws IOException {
160-
BufferedImage image = ImageIO.read(inputFile);
161-
final byte[] pixels = ((DataBufferByte) image.getRaster().getDataBuffer()).getData();
175+
return parseImageGetColorArray(ImageIO.read(inputFile));
176+
}
177+
178+
/**
179+
* 将一个图片中的所有像素RGB值所构成的整形二维矩阵获取到!
180+
*
181+
* @param image 需要被读取的图像缓冲对象
182+
* @return 读取成功之后返回的整形矩阵
183+
*/
184+
private static Color[][] parseImageGetColorArray(BufferedImage image) {
185+
final byte[] pixels = ((DataBufferByte) image.getRaster()
186+
.getDataBuffer())
187+
.getData();
162188
if (pixels.length == 0) {
163189
throw new OperatorOperationException("您不能读取一个不包含任何像素的图像,请您重新切换被读取的图像文件!!!\nYou cannot read an image that does not contain any pixels. Please switch the read image file again!!!");
164190
}

0 commit comments

Comments
 (0)