Unify matrix modification methods to applyMatrix
This commit is contained in:
parent
faa0bfe987
commit
4a1bfcd6f8
|
@ -1,8 +1,11 @@
|
||||||
<?xml version="1.0" encoding="UTF-8"?>
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
<module type="JAVA_MODULE" version="4">
|
<module external.linked.project.id="ImgJava" external.linked.project.path="$MODULE_DIR$" external.root.project.path="$MODULE_DIR$" external.system.id="GRADLE" external.system.module.group="io.gitlab.jfronny" external.system.module.version="1.0" type="JAVA_MODULE" version="4">
|
||||||
<component name="NewModuleRootManager" inherit-compiler-output="true">
|
<component name="NewModuleRootManager" inherit-compiler-output="true">
|
||||||
<exclude-output />
|
<exclude-output />
|
||||||
<content url="file://$MODULE_DIR$" />
|
<content url="file://$MODULE_DIR$">
|
||||||
|
<excludeFolder url="file://$MODULE_DIR$/.gradle" />
|
||||||
|
<excludeFolder url="file://$MODULE_DIR$/build" />
|
||||||
|
</content>
|
||||||
<orderEntry type="inheritedJdk" />
|
<orderEntry type="inheritedJdk" />
|
||||||
<orderEntry type="sourceFolder" forTests="false" />
|
<orderEntry type="sourceFolder" forTests="false" />
|
||||||
</component>
|
</component>
|
||||||
|
|
|
@ -1,5 +1,8 @@
|
||||||
<?xml version="1.0" encoding="UTF-8"?>
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
<project version="4">
|
<project version="4">
|
||||||
<component name="ExternalStorageConfigurationManager" enabled="true" />
|
<component name="ExternalStorageConfigurationManager" enabled="true" />
|
||||||
|
<component name="FrameworkDetectionExcludesConfiguration">
|
||||||
|
<file type="web" url="file://$PROJECT_DIR$" />
|
||||||
|
</component>
|
||||||
<component name="ProjectRootManager" version="2" languageLevel="JDK_16" default="true" project-jdk-name="16" project-jdk-type="JavaSDK" />
|
<component name="ProjectRootManager" version="2" languageLevel="JDK_16" default="true" project-jdk-name="16" project-jdk-type="JavaSDK" />
|
||||||
</project>
|
</project>
|
BIN
run/moscov.png
BIN
run/moscov.png
Binary file not shown.
Before Width: | Height: | Size: 242 KiB After Width: | Height: | Size: 680 KiB |
|
@ -25,7 +25,7 @@ public class Main {
|
||||||
pict = stackViewer.push(ImageUtil.rotate(pict, ImageUtil.RotateMode.Right));
|
pict = stackViewer.push(ImageUtil.rotate(pict, ImageUtil.RotateMode.Right));
|
||||||
pict = stackViewer.push(ImageUtil.mirror(pict, ImageUtil.MirrorMode.Vertical));
|
pict = stackViewer.push(ImageUtil.mirror(pict, ImageUtil.MirrorMode.Vertical));
|
||||||
//pict = stackViewer.push(ImageUtil.blurBox(pict, 10));
|
//pict = stackViewer.push(ImageUtil.blurBox(pict, 10));
|
||||||
pict = stackViewer.push(ImageUtil.blurGauss(pict, 3));
|
pict = stackViewer.push(ImageUtil.blurGauss(pict, 3, 2));
|
||||||
pict = stackViewer.push(ImageUtil.sharpen(pict, 3));
|
pict = stackViewer.push(ImageUtil.sharpen(pict, 3));
|
||||||
pict.save(run.resolve("moscov.png"));
|
pict.save(run.resolve("moscov.png"));
|
||||||
stackViewer.repaint();
|
stackViewer.repaint();
|
||||||
|
|
|
@ -77,78 +77,25 @@ public class ImageUtil {
|
||||||
|
|
||||||
|
|
||||||
public static Picture blurBox(Picture picture, int amount) {
|
public static Picture blurBox(Picture picture, int amount) {
|
||||||
int w = picture.getWidth();
|
return applyMatrix(picture, new double[][]{
|
||||||
int h = picture.getHeight();
|
new double[] {1, 1, 1},
|
||||||
|
new double[] {1, 1, 1},
|
||||||
Color[][] pixel;
|
new double[] {1, 1, 1}
|
||||||
Color[][] pixelNeu = picture.getPixelArray();
|
}, amount);
|
||||||
|
|
||||||
for (int i = 0; i < amount; i++) {
|
|
||||||
pixel = pixelNeu;
|
|
||||||
pixelNeu = new Color[w][h];
|
|
||||||
for (int x = 0; x < w; x++) {
|
|
||||||
for (int y = 0; y < h; y++) {
|
|
||||||
if (x < 1 || y < 1 || x + 1 >= w || y + 1 >= h) {
|
|
||||||
pixelNeu[x][y] = pixel[x][y];
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
pixelNeu[x][y] = pixel[x][y];
|
|
||||||
//p p p
|
|
||||||
//p 0 p
|
|
||||||
//p p p
|
|
||||||
MColor sum = new MColor(pixel[x - 1][y + 1])
|
|
||||||
.add(pixel[x + 0][y + 1])
|
|
||||||
.add(pixel[x + 1][y + 1])
|
|
||||||
.add(pixel[x - 1][y + 0])
|
|
||||||
.add(pixel[x + 0][y + 0])
|
|
||||||
.add(pixel[x + 1][y + 0])
|
|
||||||
.add(pixel[x - 1][y - 1])
|
|
||||||
.add(pixel[x + 0][y - 1])
|
|
||||||
.add(pixel[x + 1][y - 1]);
|
|
||||||
|
|
||||||
pixelNeu[x][y] = sum.div(9).get();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
picture.setPixelArray(pixelNeu);
|
|
||||||
return picture;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public static Picture blurGauss(Picture picture, int radius) {
|
public static Picture blurGauss(Picture picture, int radius, int amount) {
|
||||||
int w = picture.getWidth();
|
|
||||||
int h = picture.getHeight();
|
|
||||||
|
|
||||||
Color[][] pixel = picture.getPixelArray();
|
|
||||||
Color[][] pixelNeu = new Color[w][h];
|
|
||||||
|
|
||||||
double sigma = radius / 2d;
|
double sigma = radius / 2d;
|
||||||
|
int bl = radius * 2 + 1;
|
||||||
for (int x = 0; x < w; x++) {
|
double[][] matrix = new double[bl][bl];
|
||||||
for (int y = 0; y < h; y++) {
|
|
||||||
|
for (int x = 0; x < bl; x++) {
|
||||||
pixelNeu[x][y] = pixel[x][y];
|
for (int y = 0; y < bl; y++) {
|
||||||
|
matrix[x][y] = gauss(x - radius, y - radius, sigma);
|
||||||
MColor sum = new MColor();
|
|
||||||
|
|
||||||
double gSum = 0;
|
|
||||||
|
|
||||||
for (int dx = -radius; dx <= radius; dx++) {
|
|
||||||
for (int dy = -radius; dy <= radius; dy++) {
|
|
||||||
if (x + dx >= 0 && y + dy >= 0 && x + dx < w && y + dy < h) {
|
|
||||||
double g = gauss(dx, dy, sigma);
|
|
||||||
gSum += g;
|
|
||||||
sum.addM(pixel[x + dx][y + dy], g);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pixelNeu[x][y] = sum.div(gSum).get();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
picture.setPixelArray(pixelNeu);
|
return applyMatrix(picture, matrix, amount);
|
||||||
return picture;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private static double gauss(double dX, double dY, double stDev) {
|
private static double gauss(double dX, double dY, double stDev) {
|
||||||
|
@ -156,32 +103,52 @@ public class ImageUtil {
|
||||||
}
|
}
|
||||||
|
|
||||||
public static Picture sharpen(Picture picture, int amount) {
|
public static Picture sharpen(Picture picture, int amount) {
|
||||||
|
return applyMatrix(picture, new double[][]{
|
||||||
|
new double[] {0, -1, 0},
|
||||||
|
new double[] {-1, 5, -1},
|
||||||
|
new double[] {0, -1, 0}
|
||||||
|
}, amount);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static Picture applyMatrix(Picture picture, double[][] matrix, int amount) {
|
||||||
int w = picture.getWidth();
|
int w = picture.getWidth();
|
||||||
int h = picture.getHeight();
|
int h = picture.getHeight();
|
||||||
|
|
||||||
|
int mW = matrix.length;
|
||||||
|
int mH = matrix[0].length;
|
||||||
|
if ((mW % 2 != 1) || (mH % 2 != 1))
|
||||||
|
throw new IndexOutOfBoundsException("matrix must not have round length");
|
||||||
|
|
||||||
|
mW /= 2;
|
||||||
|
mH /= 2;
|
||||||
|
|
||||||
Color[][] pixel;
|
Color[][] pixel;
|
||||||
Color[][] pixelNeu = picture.getPixelArray();
|
Color[][] pixelNeu = picture.getPixelArray();
|
||||||
|
|
||||||
for (int i = 0; i < amount; i++) {
|
for (int i = 0; i < amount; i++) {
|
||||||
pixel = pixelNeu;
|
pixel = pixelNeu;
|
||||||
pixelNeu = new Color[w][h];
|
pixelNeu = new Color[w][h];
|
||||||
|
|
||||||
for (int x = 0; x < w; x++) {
|
for (int x = 0; x < w; x++) {
|
||||||
for (int y = 0; y < h; y++) {
|
for (int y = 0; y < h; y++) {
|
||||||
if (x < 1 || y < 1 || x + 1 >= w || y + 1 >= h) {
|
|
||||||
pixelNeu[x][y] = pixel[x][y];
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
pixelNeu[x][y] = pixel[x][y];
|
|
||||||
//p p p
|
|
||||||
//p 0 p
|
|
||||||
//p p p
|
|
||||||
MColor sum = new MColor(pixel[x + 0][y + 1]).mult(-1)
|
|
||||||
.addM(pixel[x - 1][y + 0], -1)
|
|
||||||
.addM(pixel[x + 0][y + 0], 5)
|
|
||||||
.addM(pixel[x + 1][y + 0], -1)
|
|
||||||
.addM(pixel[x + 0][y - 1], -1);
|
|
||||||
|
|
||||||
pixelNeu[x][y] = sum.get();
|
pixelNeu[x][y] = pixel[x][y];
|
||||||
|
|
||||||
|
MColor sum = new MColor();
|
||||||
|
|
||||||
|
double gSum = 0;
|
||||||
|
|
||||||
|
for (int dx = -mW; dx <= mW; dx++) {
|
||||||
|
for (int dy = -mH; dy <= mH; dy++) {
|
||||||
|
if (x + dx >= 0 && y + dy >= 0 && x + dx < w && y + dy < h) {
|
||||||
|
double g = matrix[mW + dx][mH + dy];
|
||||||
|
gSum += g;
|
||||||
|
sum.addM(pixel[x + dx][y + dy], g);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pixelNeu[x][y] = sum.div(gSum).get();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -3,10 +3,10 @@ package io.gitlab.jfronny.ImgJava.util;
|
||||||
import java.awt.*;
|
import java.awt.*;
|
||||||
|
|
||||||
public class MColor {
|
public class MColor {
|
||||||
public int r;
|
public double r;
|
||||||
public int g;
|
public double g;
|
||||||
public int b;
|
public double b;
|
||||||
public int a;
|
public double a;
|
||||||
|
|
||||||
public MColor(Color c) {
|
public MColor(Color c) {
|
||||||
this(c.getRed(), c.getGreen(), c.getBlue(), c.getAlpha());
|
this(c.getRed(), c.getGreen(), c.getBlue(), c.getAlpha());
|
||||||
|
@ -24,7 +24,11 @@ public class MColor {
|
||||||
}
|
}
|
||||||
|
|
||||||
public Color get() {
|
public Color get() {
|
||||||
return new Color(Math.max(r, 0), Math.max(g, 0), Math.max(b, 0), Math.max(a, 0));
|
return new Color(clamp(r), clamp(g), clamp(b), clamp(a));
|
||||||
|
}
|
||||||
|
|
||||||
|
private static int clamp(double d) {
|
||||||
|
return (int)Math.round(Math.max(Math.min(d, 255), 0));
|
||||||
}
|
}
|
||||||
|
|
||||||
public MColor add(Color c) {
|
public MColor add(Color c) {
|
||||||
|
|
Reference in New Issue