-
Notifications
You must be signed in to change notification settings - Fork 1
/
ComicFilter.java
73 lines (52 loc) · 2.68 KB
/
ComicFilter.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
package filters;
import java.awt.*;
import java.awt.geom.AffineTransform;
import java.awt.image.BufferedImage;
import filters.convolution.*;
import pixels.RGBAPixel;
import javax.swing.*;
public class ComicFilter implements FilterInterface{
@Override
public Image runFilter(BufferedImage img) {
float[][] verticalKernel = {{1,2,1},{0,0,0},{-1,-2,-1}};
//float[][] verticalKernel = {{3,2,1,0,-1,-2,-3},{4,3,2,0,-2,-3,-4},{5,4,3,0,-3,-4,-5},{6,5,4,0,-4,-5,-6},{5,4,3,0,-3,-4,-5},{4,3,2,0,-2,-3,-4},{3,2,1,0,-1,-2,-3}};
float[][] horizontalKernel = {{1,0,-1},{2,0,-2},{1,0,-1}};
BufferedImage comicColor = new BufferedImage(img.getWidth(), img.getHeight(), BufferedImage.TYPE_INT_RGB);
for(int x = 0; x < img.getWidth(); x++) {
for(int y = 0; y < img.getHeight(); y++) {
comicColor.setRGB(x, y, RGBAPixel.generateRGBAPixel(
((RGBAPixel.getRed(img.getRGB(x, y)))/64)*64,
((RGBAPixel.getGreen(img.getRGB(x, y)))/64)*64,
((RGBAPixel.getBlue(img.getRGB(x, y)))/64)*64,
255));
}
}
BufferedImage grayImg = (BufferedImage)(new GreyScaleFilter()).runFilter(img);
BufferedImage verticalEdges = (BufferedImage)(new ConvolutionFilterGray(new ConvolutonAbsFunction(verticalKernel))).runFilter(grayImg);
BufferedImage horizontalEdges = (BufferedImage)(new ConvolutionFilterGray(new ConvolutonAbsFunction(horizontalKernel))).runFilter(grayImg);
BufferedImage edges = ImageBlend.blendImages(verticalEdges,horizontalEdges, ImageBlend.BlendType.ADD);
BufferedImage edgesTh = (BufferedImage) (new ThresholdFilter(150).runFilter(edges));
float[][] errosionKernel = {{0,1,0},{1,1,1},{0,1,0}};
BufferedImage erodedEdges = (BufferedImage)(new ConvolutionFilterGray(new ErosionFunction(errosionKernel))).runFilter(edgesTh);
float[][] dilationKernel = {{0,1,0},{1,1,1},{0,1,0}};
BufferedImage dilateddEdges = (BufferedImage)(new ConvolutionFilterGray(new DilationFunction(dilationKernel))).runFilter(erodedEdges);
//dilateddEdges = (BufferedImage)(new ConvolutionFilterGray(new DilationFunction(dilationKernel))).runFilter(dilateddEdges);
return ImageBlend.blendImages(comicColor,dilateddEdges, ImageBlend.BlendType.SUBTRACT);
}
public static BufferedImage scale(BufferedImage sbi, int imageType, double fWidth, double fHeight) {
BufferedImage dbi = null;
int dWidth = (int)(sbi.getWidth()*fWidth);
int dHeight = (int) (sbi.getHeight()*fHeight);
if(sbi != null) {
dbi = new BufferedImage(dWidth, dHeight, imageType);
Graphics2D g = dbi.createGraphics();
AffineTransform at = AffineTransform.getScaleInstance(fWidth, fHeight);
g.drawRenderedImage(sbi, at);
}
return dbi;
}
@Override
public String toString() {
return "Comic Filter";
}
}//class