Last active
December 17, 2015 02:39
-
-
Save tonussi/5537258 to your computer and use it in GitHub Desktop.
Java code for image border detection
This file contains hidden or 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
before sobel | |
0 0 0 0 0 0 0 0 0 0 | |
0 0 0 0 0 0 0 0 0 0 | |
0 0 0 0 1 1 1 0 0 0 | |
0 0 0 1 1 0 0 0 0 0 | |
0 0 1 1 0 0 0 0 0 0 | |
0 0 1 0 0 0 0 0 0 0 | |
0 0 1 0 0 0 0 0 0 0 | |
0 0 1 0 0 0 0 0 0 0 | |
0 0 1 0 0 0 0 0 0 0 | |
0 0 1 0 0 0 0 0 0 0 | |
after sobel | |
0 0 0 0 0 0 0 0 0 0 | |
0 0 0 2 4 4 4 2 0 0 | |
0 0 2 6 4 2 2 2 0 0 | |
0 2 6 4 4 6 4 2 0 0 | |
0 4 4 4 6 2 0 0 0 0 | |
0 4 2 6 2 0 0 0 0 0 | |
0 4 0 4 0 0 0 0 0 0 | |
0 4 0 4 0 0 0 0 0 0 | |
0 4 0 4 0 0 0 0 0 0 | |
0 0 0 0 0 0 0 0 0 0 | |
before convolution | |
0 0 0 0 0 0 0 0 0 0 | |
0 0 0 0 0 0 0 0 0 0 | |
0 0 0 0 1 1 1 0 0 0 | |
0 0 0 1 1 0 0 0 0 0 | |
0 0 1 1 0 0 0 0 0 0 | |
0 0 1 0 0 0 0 0 0 0 | |
0 0 1 0 0 0 0 0 0 0 | |
0 0 1 0 0 0 0 0 0 0 | |
0 0 1 0 0 0 0 0 0 0 | |
0 0 1 0 0 0 0 0 0 0 | |
after convolution gx | |
0 0 1 1 0 -1 -1 0 | |
0 1 3 1 -1 -2 -2 0 | |
1 3 2 -2 -2 -1 -1 0 | |
3 3 -2 -3 -1 0 0 0 | |
4 1 -4 -1 0 0 0 0 | |
4 0 -4 0 0 0 0 0 | |
4 0 -4 0 0 0 0 0 | |
4 0 -4 0 0 0 0 0 | |
after convolution gy | |
0 0 1 3 4 3 1 0 | |
0 1 3 3 1 0 0 0 | |
1 3 2 -2 -4 -3 -1 0 | |
1 1 -2 -3 -1 0 0 0 | |
0 -1 -2 -1 0 0 0 0 | |
0 0 0 0 0 0 0 0 | |
0 0 0 0 0 0 0 0 | |
0 0 0 0 0 0 0 0 |
This file contains hidden or 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
before sobel | |
1 0 0 0 0 0 0 0 0 0 | |
0 1 0 0 0 0 0 0 0 0 | |
0 0 1 0 0 0 0 0 0 0 | |
0 0 0 1 0 0 0 0 0 0 | |
0 0 0 0 1 0 0 0 0 0 | |
0 0 0 0 0 1 0 0 0 0 | |
0 0 0 0 0 0 1 0 0 0 | |
0 0 0 0 0 0 0 1 0 0 | |
0 0 0 0 0 0 0 0 1 0 | |
0 0 0 0 0 0 0 0 0 1 | |
after sobel | |
0 0 0 0 0 0 0 0 0 0 | |
0 0 4 2 0 0 0 0 0 0 | |
0 4 0 4 2 0 0 0 0 0 | |
0 2 4 0 4 2 0 0 0 0 | |
0 0 2 4 0 4 2 0 0 0 | |
0 0 0 2 4 0 4 2 0 0 | |
0 0 0 0 2 4 0 4 2 0 | |
0 0 0 0 0 2 4 0 4 0 | |
0 0 0 0 0 0 2 4 0 0 | |
0 0 0 0 0 0 0 0 0 0 | |
before convolution | |
1 0 0 0 0 0 0 0 0 0 | |
0 1 0 0 0 0 0 0 0 0 | |
0 0 1 0 0 0 0 0 0 0 | |
0 0 0 1 0 0 0 0 0 0 | |
0 0 0 0 1 0 0 0 0 0 | |
0 0 0 0 0 1 0 0 0 0 | |
0 0 0 0 0 0 1 0 0 0 | |
0 0 0 0 0 0 0 1 0 0 | |
0 0 0 0 0 0 0 0 1 0 | |
0 0 0 0 0 0 0 0 0 1 | |
after convolution gx | |
0 -2 -1 0 0 0 0 0 | |
2 0 -2 -1 0 0 0 0 | |
1 2 0 -2 -1 0 0 0 | |
0 1 2 0 -2 -1 0 0 | |
0 0 1 2 0 -2 -1 0 | |
0 0 0 1 2 0 -2 -1 | |
0 0 0 0 1 2 0 -2 | |
0 0 0 0 0 1 2 0 | |
after convolution gy | |
0 2 1 0 0 0 0 0 | |
-2 0 2 1 0 0 0 0 | |
-1 -2 0 2 1 0 0 0 | |
0 -1 -2 0 2 1 0 0 | |
0 0 -1 -2 0 2 1 0 | |
0 0 0 -1 -2 0 2 1 | |
0 0 0 0 -1 -2 0 2 | |
0 0 0 0 0 -1 -2 0 |
This file contains hidden or 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
import static java.lang.Math.abs; | |
import static java.lang.System.out; | |
public class Sobel { | |
public static void main(String[] args) { | |
int nroLinhas = 10, nroColunas = 10; | |
int[][] iRef, g, gx, gy; | |
iRef = new int[nroLinhas][nroColunas]; | |
g = new int[nroLinhas][nroColunas]; | |
gx = new int[3][3]; | |
gy = new int[3][3]; | |
Sobel.fill(iRef); | |
Sobel.fill(g); | |
Sobel.kernel(gx, new int[][] { { +1, 0, -1 }, { +2, 0, -2 }, | |
{ +1, 0, -1 } }); | |
Sobel.kernel(gy, new int[][] { { +1, +2, +1 }, { 0, 0, 0 }, | |
{ -1, -2, -1 } }); | |
int[][] foo = Sobel.convolution2DPadded(g, nroLinhas, nroColunas, gx, | |
3, 3); | |
int[][] bar = Sobel.convolution2DPadded(g, nroLinhas, nroColunas, gy, | |
3, 3); | |
Sobel.display(iRef); | |
out.println(); | |
Sobel.display(g); | |
out.println(); | |
Sobel.display(gx); | |
out.println(); | |
Sobel.display(gy); | |
out.println(); | |
Sobel.display(foo); | |
out.println(); | |
Sobel.display(bar); | |
out.println(); | |
Sobel.display(Sobel.g(foo, bar)); | |
} | |
public static int[][] convolutionG(int[][] d_reg_matriz_conv_gx, | |
int[][] d_reg_matriz_conv_gy, int reg_input_n) { | |
int[][] d_reg_convolucao = new int[reg_input_n][reg_input_n]; | |
for (int i = 0; i < reg_input_n; i++) | |
for (int j = 0; j < reg_input_n; j++) | |
d_reg_convolucao[i][j] = Math.abs(d_reg_matriz_conv_gx[i][j]) | |
+ Math.abs(d_reg_matriz_conv_gy[i][j]); | |
return d_reg_convolucao; | |
} | |
public static void kernel(int[][] mask, int[][] operator) { | |
assert (mask.length == mask[0].length || mask == null) : "InvalidMaskException" | |
+ (mask.length == mask[0].length); | |
assert (operator.length == operator[0].length || operator == null) : "BadOperatorException" | |
+ (mask.length == mask[0].length); | |
for (int i = 0; i < operator.length; i++) | |
for (int j = 0; j < operator[i].length; j++) | |
mask[i][j] = operator[i][j]; | |
} | |
public static void display(int[] inputArray1D) { | |
assert (inputArray1D.length == inputArray1D.length || inputArray1D == null) : "BadInputArrayException" | |
+ (inputArray1D.length == inputArray1D.length); | |
for (int i = 0; i < inputArray1D.length; i++) | |
out.println(inputArray1D[i]); | |
} | |
public static void display(int[][] inputArray2D) { | |
assert (inputArray2D.length == inputArray2D[0].length || inputArray2D == null) : "BadInputArrayException" | |
+ (inputArray2D.length == inputArray2D[0].length); | |
for (int i = 0; i < inputArray2D.length; i++) { | |
for (int j = 0; j < inputArray2D[i].length; j++) { | |
if (inputArray2D[i][j] < 0) | |
out.print(" " + inputArray2D[i][j] + " "); | |
else | |
out.print(" " + inputArray2D[i][j] + " "); | |
} | |
out.println(); | |
} | |
} | |
public static int[][] convolution2D(int[][] inputArray2D, int width, | |
int height, int[][] kernel, int kernelWidth, int kernelHeight) { | |
assert (inputArray2D.length == inputArray2D[0].length || inputArray2D == null) : "BadInputArrayException" | |
+ (inputArray2D.length - inputArray2D[0].length == 0); | |
assert (kernel.length == kernel[0].length || kernel != null) : "BadInputArrayException" | |
+ (kernel.length - kernel[0].length == 0); | |
int smallWidth = width - kernelWidth + 1; | |
int smallHeight = height - kernelHeight + 1; | |
int[][] output = new int[smallWidth][smallHeight]; | |
for (int i = 0; i < smallWidth; ++i) | |
for (int j = 0; j < smallHeight; ++j) | |
output[i][j] = singlePixelConvolution(inputArray2D, i, j, | |
kernel, kernelWidth, kernelHeight); | |
return output; | |
} | |
public static int[][] g(int[][] inputArray2DA, int[][] inputArray2DB) { | |
int[][] output = new int[inputArray2DA.length][inputArray2DA.length]; | |
for (int i = 0; i < inputArray2DA.length; ++i) | |
for (int j = 0; j < inputArray2DA.length; ++j) | |
output[i][j] = abs(inputArray2DA[i][j]) | |
+ abs(inputArray2DB[i][j]); | |
return output; | |
} | |
public static int[] convolutionDoublePadded(int[][] inputArray2D, | |
int width, int height, int[][] kernel, int kernelWidth, | |
int kernelHeight) { | |
int[][] result2D = new int[width][height]; | |
result2D = convolution2DPadded(inputArray2D, width, height, kernel, | |
kernelWidth, kernelHeight); | |
int[] result = new int[width * height]; | |
for (int j = 0; j < height; ++j) { | |
for (int i = 0; i < width; ++i) { | |
result[j * width + i] = result2D[i][j]; | |
} | |
} | |
return result; | |
} | |
public static int[][] convolution2DPadded(int[][] inputArray2D, int width, | |
int height, int[][] kernel, int kernelWidth, int kernelHeight) { | |
int smallWidth = width - kernelWidth + 1; | |
int smallHeight = height - kernelHeight + 1; | |
int top = kernelHeight / 2; | |
int left = kernelWidth / 2; | |
int small[][] = new int[smallWidth][smallHeight]; | |
small = convolution2D(inputArray2D, width, height, kernel, kernelWidth, | |
kernelHeight); | |
int large[][] = new int[width][height]; | |
for (int j = 0; j < smallHeight; ++j) { | |
for (int i = 0; i < smallWidth; ++i) { | |
large[i + left][j + top] = small[i][j]; | |
} | |
} | |
return large; | |
} | |
public static int singlePixelConvolution(int[][] inputArray2D, int x, | |
int y, int[][] k, int kernelWidth, int kernelHeight) { | |
assert (inputArray2D.length == inputArray2D[0].length || inputArray2D == null) : "BadInputArrayException" | |
+ (inputArray2D.length - inputArray2D[0].length == 0); | |
int output = 0; | |
for (int i = 0; i < kernelWidth; ++i) | |
for (int j = 0; j < kernelHeight; ++j) | |
output = output + (inputArray2D[x + i][y + j] * k[i][j]); | |
return output; | |
} | |
public static void fill(int[][] inputArray2D) { | |
assert (inputArray2D.length == inputArray2D[0].length || inputArray2D == null) : "BadInputArrayException" | |
+ (inputArray2D.length - inputArray2D[0].length == 0); | |
for (int i = 0; i < inputArray2D.length; i++) | |
inputArray2D[i][i] = 1; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment