-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSobel.java
More file actions
91 lines (88 loc) · 3.68 KB
/
Sobel.java
File metadata and controls
91 lines (88 loc) · 3.68 KB
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.FileWriter;
public class Sobel {
public void saveToTXT(int[][] matrix, String fileName) throws IOException {
FileWriter txWriter = new FileWriter(fileName);
for (int y = 0; y < matrix[0].length; y++) {
for (int x = 0; x < matrix.length; x++) {
txWriter.append(String.valueOf(matrix[x][y]));
if (x < matrix.length - 1) {
txWriter.append(",");
}
}
txWriter.append("\n");
}
txWriter.flush();
txWriter.close();
}
public BufferedImage convertToGrayscale(BufferedImage image) {
int width = image.getWidth();
int height = image.getHeight();
BufferedImage grascaleImage = new BufferedImage(width, height, BufferedImage.TYPE_BYTE_GRAY);
for (int y = 0; y < height; y++) {
for (int x = 0; x < width; x++) {
int rgb = image.getRGB(x, y);
int red = (rgb >> 16) & 0xFF;
int green = (rgb >> 8) & 0xFF;
int blue = (rgb) & 0xFF;
int intensity = (int) (red + green + blue) / 3;
int grayScaleIntensity = (intensity << 16) | (intensity << 8) | intensity;
grascaleImage.setRGB(x, y, grayScaleIntensity);
}
}
return grascaleImage;
}
public BufferedImage sobelAlgo(BufferedImage image) {
int width = image.getWidth();
int height = image.getHeight();
BufferedImage processedImage = new BufferedImage(width, height, BufferedImage.TYPE_BYTE_GRAY);
int[][] sobelHorizontal = { { -1, 0, 1 }, { -2, 0, 2 }, { -1, 0, 1 } };
int[][] sobelVertical = { { -1, -2, -1 }, { 0, 0, 0 }, { 1, 2, 1 } };
for (int y = 1; y < height - 1; y++) {
for (int x = 1; x < width - 1; x++) {
int pixelX = filterApply(image, x, y, sobelHorizontal);
int pixelY = filterApply(image, x, y, sobelVertical);
int gradient = (int) Math.sqrt(pixelX * pixelX + pixelY * pixelY);
gradient = Math.min(gradient, 255);
Color color = new Color(gradient, gradient, gradient);
processedImage.setRGB(x, y, color.getRGB());
}
}
return processedImage;
}
public int filterApply(BufferedImage image, int x, int y, int[][] operator) {
int answer = 0;
for (int i = -1; i <= 1; i++) {
for (int j = -1; j <= 1; j++) {
int pixel = image.getRGB(x + i, y + j) & 0xFF;
answer += pixel * operator[i + 1][j + 1];
}
}
return answer;
}
public int[][] convertMatrix(BufferedImage img,int val){
int width = img.getWidth();
int height = img.getHeight();
int[][] mat = new int[width][height];
for(int y=0;y<height;y++){
for(int x=0;x<width;x++){
int pixel = img.getRGB(x, y) & 0xFF;
if(pixel>val) mat[x][y]=1;
else mat[x][y]=0;
}
}
return mat;
}
public BufferedImage resizeImage(BufferedImage originalImage, int targetWidth, int targetHeight) {
Image resultingImage = originalImage.getScaledInstance(targetWidth, targetHeight, Image.SCALE_DEFAULT);
BufferedImage resizedImage = new BufferedImage(targetWidth, targetHeight, BufferedImage.TYPE_INT_RGB);
Graphics2D g2d = resizedImage.createGraphics();
g2d.drawImage(resultingImage, 0, 0, null);
g2d.dispose();
return resizedImage;
}
}