-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #118 from Samyssmile/feat/augmentation
Feat/augmentation
- Loading branch information
Showing
30 changed files
with
744 additions
and
215 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
...ux/augmentation/AbstractAugmentation.java → ...gmentation/core/AbstractAugmentation.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
3 changes: 1 addition & 2 deletions
3
...dux/augmentation/AugmentationBuilder.java → ...ugmentation/core/AugmentationBuilder.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
...ux/augmentation/AugmentationSequence.java → ...gmentation/core/AugmentationSequence.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
package de.edux.augmentation; | ||
package de.edux.augmentation.core; | ||
|
||
import java.awt.image.BufferedImage; | ||
|
||
|
6 changes: 1 addition & 5 deletions
6
...posite/CompositeAugmentationSequence.java → ...n/core/CompositeAugmentationSequence.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
22 changes: 16 additions & 6 deletions
22
lib/src/main/java/de/edux/augmentation/effects/BlurAugmentation.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
63 changes: 63 additions & 0 deletions
63
lib/src/main/java/de/edux/augmentation/effects/ColorEqualizationAugmentation.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
package de.edux.augmentation.effects; | ||
|
||
import de.edux.augmentation.core.AbstractAugmentation; | ||
import java.awt.image.BufferedImage; | ||
import java.awt.image.WritableRaster; | ||
|
||
/** | ||
* This class implements an augmentation that applies color equalization to an image. It enhances | ||
* the contrast of the image by equalizing the histogram of each color channel. | ||
*/ | ||
public class ColorEqualizationAugmentation extends AbstractAugmentation { | ||
|
||
/** | ||
* Applies color equalization to the provided image. | ||
* | ||
* @param image The BufferedImage to be augmented. | ||
* @return The augmented BufferedImage with equalized color channels. | ||
*/ | ||
@Override | ||
public BufferedImage apply(BufferedImage image) { | ||
BufferedImage equalizedImage = | ||
new BufferedImage(image.getWidth(), image.getHeight(), image.getType()); | ||
|
||
WritableRaster sourceRaster = image.getRaster(); | ||
WritableRaster equalizedRaster = equalizedImage.getRaster(); | ||
|
||
for (int b = 0; b < sourceRaster.getNumBands(); b++) { | ||
equalizeHistogram(sourceRaster, equalizedRaster, b); | ||
} | ||
|
||
return equalizedImage; | ||
} | ||
|
||
private void equalizeHistogram( | ||
WritableRaster sourceRaster, WritableRaster equalizedRaster, int band) { | ||
int w = sourceRaster.getWidth(); | ||
int h = sourceRaster.getHeight(); | ||
int[] histogram = new int[256]; | ||
float[] cdf = new float[256]; | ||
|
||
// Calculate the histogram | ||
for (int y = 0; y < h; y++) { | ||
for (int x = 0; x < w; x++) { | ||
histogram[sourceRaster.getSample(x, y, band) & 0xFF]++; | ||
} | ||
} | ||
|
||
int total = w * h; | ||
float sum = 0f; | ||
for (int i = 0; i < histogram.length; i++) { | ||
sum += (float) histogram[i] / total; | ||
cdf[i] = sum; | ||
} | ||
|
||
for (int y = 0; y < h; y++) { | ||
for (int x = 0; x < w; x++) { | ||
int value = sourceRaster.getSample(x, y, band) & 0xFF; | ||
int newValue = Math.round(cdf[value] * 255f); | ||
equalizedRaster.setSample(x, y, band, newValue); | ||
} | ||
} | ||
} | ||
} |
2 changes: 1 addition & 1 deletion
2
lib/src/main/java/de/edux/augmentation/effects/ContrastAugmentation.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
lib/src/main/java/de/edux/augmentation/effects/CroppingAugmentation.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
68 changes: 43 additions & 25 deletions
68
lib/src/main/java/de/edux/augmentation/effects/ElasticTransformationAugmentation.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,62 +1,80 @@ | ||
package de.edux.augmentation.effects; | ||
|
||
import de.edux.augmentation.AbstractAugmentation; | ||
import de.edux.augmentation.core.AbstractAugmentation; | ||
import java.awt.image.BufferedImage; | ||
import java.util.Random; | ||
|
||
/** This class provides an augmentation that applies elastic transformations to an image. */ | ||
/** Applies an elastic transformation to an image, simulating natural distortions. */ | ||
public class ElasticTransformationAugmentation extends AbstractAugmentation { | ||
|
||
private final double alpha; | ||
private final double sigma; | ||
|
||
/** | ||
* Initializes a new instance of the ElasticTransformationAugmentation class with specified | ||
* parameters. | ||
* Constructs an ElasticTransformationAugmentation instance with the given parameters. | ||
* | ||
* @param alpha Controls the intensity of the deformation. Start with a low value and increase it | ||
* @param sigma Controls the smoothness of the deformation. Start with a low value and increase it | ||
* @param alpha The intensity of the transformation. Typically between 1 and 10. | ||
* @param sigma The elasticity coefficient. Typically between 0.1 and 0.5. | ||
*/ | ||
public ElasticTransformationAugmentation(double alpha, double sigma) { | ||
this.alpha = alpha; | ||
this.sigma = sigma; | ||
} | ||
|
||
/** | ||
* Applies an elastic transformation to the provided image. | ||
* Applies the elastic transformation to the provided image. | ||
* | ||
* @param image The BufferedImage to which the elastic transformation will be applied. | ||
* @return A new BufferedImage with the elastic transformation applied. | ||
* @param image The BufferedImage to transform. | ||
* @return A new BufferedImage object with the applied elastic transformation. | ||
*/ | ||
@Override | ||
public BufferedImage apply(BufferedImage image) { | ||
int width = image.getWidth(); | ||
int height = image.getHeight(); | ||
BufferedImage transformedImage = new BufferedImage(width, height, image.getType()); | ||
BufferedImage result = new BufferedImage(width, height, image.getType()); | ||
|
||
// Generate displacement fields | ||
Random random = new Random(); | ||
double[] dx = new double[width * height]; | ||
double[] dy = new double[width * height]; | ||
for (int i = 0; i < dx.length; i++) { | ||
dx[i] = random.nextGaussian() * sigma; | ||
dy[i] = random.nextGaussian() * sigma; | ||
} | ||
double[] dx = generateDisplacementField(width, height, sigma); | ||
double[] dy = generateDisplacementField(width, height, sigma); | ||
|
||
// Apply displacement fields to the image | ||
// Apply the elastic transformation | ||
for (int y = 0; y < height; y++) { | ||
for (int x = 0; x < width; x++) { | ||
int newX = (int) (x + dx[y * width + x] * alpha); | ||
int newY = (int) (y + dy[y * width + x] * alpha); | ||
int xx = (int) (x + alpha * dx[y * width + x]); | ||
int yy = (int) (y + alpha * dy[y * width + x]); | ||
|
||
// Bound checking | ||
newX = Math.min(Math.max(newX, 0), width - 1); | ||
newY = Math.min(Math.max(newY, 0), height - 1); | ||
// Boundary condition check | ||
if (xx < 0) xx = 0; | ||
if (xx >= width) xx = width - 1; | ||
if (yy < 0) yy = 0; | ||
if (yy >= height) yy = height - 1; | ||
|
||
transformedImage.setRGB(x, y, image.getRGB(newX, newY)); | ||
result.setRGB(x, y, image.getRGB(xx, yy)); | ||
} | ||
} | ||
|
||
return transformedImage; | ||
return result; | ||
} | ||
|
||
/** | ||
* Generates a displacement field for the elastic transformation. | ||
* | ||
* @param width The width of the displacement field. | ||
* @param height The height of the displacement field. | ||
* @param sigma The elasticity coefficient for the displacement field. | ||
* @return A displacement field represented as a double array. | ||
*/ | ||
private double[] generateDisplacementField(int width, int height, double sigma) { | ||
double[] field = new double[width * height]; | ||
Random random = new Random(); | ||
|
||
for (int i = 0; i < field.length; i++) { | ||
field[i] = random.nextGaussian() * sigma; | ||
} | ||
|
||
// Here you might want to apply a Gaussian blur to the field | ||
// to ensure smoothness of the displacement. | ||
|
||
return field; | ||
} | ||
} |
2 changes: 1 addition & 1 deletion
2
lib/src/main/java/de/edux/augmentation/effects/FlippingAugmentation.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
lib/src/main/java/de/edux/augmentation/effects/MonochromeAugmentation.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
lib/src/main/java/de/edux/augmentation/effects/NoiseInjectionAugmentation.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.