图像卷积计算
m,m为图像像素
k,l为内核大小
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Scanner;
public class Main {
public static void main(String[] args) throws IOException {
Scanner scanner = new Scanner(System.in);
int m = scanner.nextInt();
int n = scanner.nextInt();
int k = scanner.nextInt();
int l = scanner.nextInt();
int[][] matric = new int[m][n];
int[][] core = new int[k][l];
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++){
matric[i][j] = scanner.nextInt();
}
}
for (int i = 0; i < k; i++) {
for (int j = 0; j < l; j++){
core[i][j] = scanner.nextInt();
}
}
System.out.println("-----------展示矩阵--------");
display(matric);
System.out.println("-----------展示矩阵--------");
System.out.println("-----------展示kernel--------");
display(core);
System.out.println("-----------展示kernel--------");
int half_k = k / 2;
int half_l = l / 2;
//给原矩阵补0
int[][] matric_one = add_zeros(matric, half_k, half_l);
System.out.println("-----------展示补0后矩阵--------");
display(matric_one);
System.out.println("-----------展示补0后矩阵--------");
int[][] res = new int[m][n];
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
//计算卷积和
for (int o = 0; o < k; o++) {
for (int p = 0; p < l; p++) {
res[i][j] += core[o][p] * matric_one[i + o][j + p];
}
res[i][j] = camp(res[i][j]);
}
}
}
System.out.println("卷积结果---------------卷积结果");
display(res);
System.out.println("卷积结果---------------res");
}
public static int camp(int n){
if (n < 0){
return 0;
}else if (n > 255){
return 255;
}else {
return n;
}
}
public static void display(int[][] m){
for (int i = 0; i < m.length; i++) {
for (int j = 0; j < m[0].length; j++) {
System.out.print(m[i][j]+"\t");
}
System.out.println();
}
}
public static int[][] add_zeros(int[][] matric, int half_k, int half_l){
int m = matric.length;
int n = matric[0].length;
int[][] res = new int[m + 2 * half_k][n + 2 * half_l];
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
res[i + half_k][j + half_l] = matric[i][j];
}
}
// display(res);
return res;
}
}
//5 6 3 3
// 1 1 1 1 1 1
// 1 2 2 2 2 1
// 2 3 3 1 2 2
// 1 2 2 3 4 5
// 5 6 6 6 3 4
// 1 10 1
// 1 20 1
// 1 8 1
"D:\Program Files (x86)\jdk8\bin\java.exe" "-javaagent:D:\Program Files (x86)\idea_pro\IntelliJ IDEA 2020.2\lib\idea_rt.jar=5152:D:\Program Files (x86)\idea_pro\IntelliJ IDEA 2020.2\bin" -Dfile.encoding=UTF-8 -classpath "D:\Program Files (x86)\jdk8\jre\lib\charsets.jar;D:\Program Files (x86)\jdk8\jre\lib\deploy.jar;D:\Program Files (x86)\jdk8\jre\lib\ext\access-bridge-64.jar;D:\Program Files (x86)\jdk8\jre\lib\ext\cldrdata.jar;D:\Program Files (x86)\jdk8\jre\lib\ext\dnsns.jar;D:\Program Files (x86)\jdk8\jre\lib\ext\jaccess.jar;D:\Program Files (x86)\jdk8\jre\lib\ext\jfxrt.jar;D:\Program Files (x86)\jdk8\jre\lib\ext\localedata.jar;D:\Program Files (x86)\jdk8\jre\lib\ext\nashorn.jar;D:\Program Files (x86)\jdk8\jre\lib\ext\sunec.jar;D:\Program Files (x86)\jdk8\jre\lib\ext\sunjce_provider.jar;D:\Program Files (x86)\jdk8\jre\lib\ext\sunmscapi.jar;D:\Program Files (x86)\jdk8\jre\lib\ext\sunpkcs11.jar;D:\Program Files (x86)\jdk8\jre\lib\ext\zipfs.jar;D:\Program Files (x86)\jdk8\jre\lib\javaws.jar;D:\Program Files (x86)\jdk8\jre\lib\jce.jar;D:\Program Files (x86)\jdk8\jre\lib\jfr.jar;D:\Program Files (x86)\jdk8\jre\lib\jfxswt.jar;D:\Program Files (x86)\jdk8\jre\lib\jsse.jar;D:\Program Files (x86)\jdk8\jre\lib\management-agent.jar;D:\Program Files (x86)\jdk8\jre\lib\plugin.jar;D:\Program Files (x86)\jdk8\jre\lib\resources.jar;D:\Program Files (x86)\jdk8\jre\lib\rt.jar;G:\idea_java\leetcode_TOP_hot100\out\production\leetcode_TOP_hot100;C:\Users\Administrator\.m2\repository\junit\junit\4.12\junit-4.12.jar;C:\Users\Administrator\.m2\repository\org\hamcrest\hamcrest-core\1.3\hamcrest-core-1.3.jar" Main
5 6 3 3
1 1 1 1 1 1
1 2 2 2 2 1
2 3 1 1 2 3
2 2 2 2 2 2
1 1 1 1 1 1
-1 -1 -1
-2 -5 -1
-5 -5 -5
-----------展示矩阵--------
1 1 1 1 1 1
1 2 2 2 2 1
2 3 1 1 2 3
2 2 2 2 2 2
1 1 1 1 1 1
-----------展示矩阵--------
-----------展示kernel--------
-1 -1 -1
-2 -5 -1
-5 -5 -5
-----------展示kernel--------
-----------展示补0后矩阵--------
0 0 0 0 0 0 0 0
0 1 1 1 1 1 1 0
0 1 2 2 2 2 1 0
0 2 3 1 1 2 3 0
0 2 2 2 2 2 2 0
0 1 1 1 1 1 1 0
0 0 0 0 0 0 0 0
-----------展示补0后矩阵--------
卷积结果---------------卷积结果
0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
卷积结果---------------res
Process finished with exit code 0