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"?>
|
||||
<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">
|
||||
<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="sourceFolder" forTests="false" />
|
||||
</component>
|
||||
|
|
|
@ -1,5 +1,8 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<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" />
|
||||
</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.mirror(pict, ImageUtil.MirrorMode.Vertical));
|
||||
//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.save(run.resolve("moscov.png"));
|
||||
stackViewer.repaint();
|
||||
|
|
|
@ -77,78 +77,25 @@ public class ImageUtil {
|
|||
|
||||
|
||||
public static Picture blurBox(Picture picture, int amount) {
|
||||
int w = picture.getWidth();
|
||||
int h = picture.getHeight();
|
||||
|
||||
Color[][] pixel;
|
||||
Color[][] pixelNeu = picture.getPixelArray();
|
||||
|
||||
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;
|
||||
return applyMatrix(picture, new double[][]{
|
||||
new double[] {1, 1, 1},
|
||||
new double[] {1, 1, 1},
|
||||
new double[] {1, 1, 1}
|
||||
}, amount);
|
||||
}
|
||||
|
||||
public static Picture blurGauss(Picture picture, int radius) {
|
||||
int w = picture.getWidth();
|
||||
int h = picture.getHeight();
|
||||
|
||||
Color[][] pixel = picture.getPixelArray();
|
||||
Color[][] pixelNeu = new Color[w][h];
|
||||
|
||||
public static Picture blurGauss(Picture picture, int radius, int amount) {
|
||||
double sigma = radius / 2d;
|
||||
|
||||
for (int x = 0; x < w; x++) {
|
||||
for (int y = 0; y < h; y++) {
|
||||
|
||||
pixelNeu[x][y] = pixel[x][y];
|
||||
|
||||
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();
|
||||
int bl = radius * 2 + 1;
|
||||
double[][] matrix = new double[bl][bl];
|
||||
|
||||
for (int x = 0; x < bl; x++) {
|
||||
for (int y = 0; y < bl; y++) {
|
||||
matrix[x][y] = gauss(x - radius, y - radius, sigma);
|
||||
}
|
||||
}
|
||||
|
||||
picture.setPixelArray(pixelNeu);
|
||||
return picture;
|
||||
return applyMatrix(picture, matrix, amount);
|
||||
}
|
||||
|
||||
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) {
|
||||
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 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[][] pixelNeu = picture.getPixelArray();
|
||||
|
||||
|
||||
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 + 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.*;
|
||||
|
||||
public class MColor {
|
||||
public int r;
|
||||
public int g;
|
||||
public int b;
|
||||
public int a;
|
||||
public double r;
|
||||
public double g;
|
||||
public double b;
|
||||
public double a;
|
||||
|
||||
public MColor(Color c) {
|
||||
this(c.getRed(), c.getGreen(), c.getBlue(), c.getAlpha());
|
||||
|
@ -24,7 +24,11 @@ public class MColor {
|
|||
}
|
||||
|
||||
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) {
|
||||
|
|
Reference in New Issue