I want to implement Sobel Filter by myself (actual no beautiful implementation). But after doing the convolution I have no idea how to calculate the rgb values.
Assumption: grey scaled image
double [][] sobel_x = { { -1, 0, 1}, { -2, 0, 2}, { -1, 0, 1} }; double [][] sobel_y = { { 1, 2, 1}, { 0, 0, 0}, {-1, -2, 1} }; for(int y=1; y<image.getHeight()-1; y++) { for(int x=1; x<image.getWidth()-1; x++) { Color a = new Color(image.getRGB(x-1, y-1)); Color b = new Color(image.getRGB(x, y-1)); Color c = new Color(image.getRGB(x+1, y-1)); Color d = new Color(image.getRGB(x-1, y)); Color e = new Color(image.getRGB(x, y)); Color f = new Color(image.getRGB(x+1, y)); Color g = new Color(image.getRGB(x-1, y+1)); Color h = new Color(image.getRGB(x, y+1)); Color i = new Color(image.getRGB(x+1, y+1)); double pixel_x = (sobel_x[0][0] * a.getRed()) + (sobel_x[0][1] * b.getRed()) + (sobel_x[0][2] * c.getRed()) + (sobel_x[1][0] * d.getRed()) + (sobel_x[1][1] * e.getRed()) + (sobel_x[1][2] * f.getRed()) + (sobel_x[2][0] * g.getRed()) + (sobel_x[2][1] * h.getRed()) + (sobel_x[2][2] * i.getRed()); double pixel_y = (sobel_y[0][0] * a.getRed()) + (sobel_x[0][1] * b.getRed()) + (sobel_x[0][2] * c.getRed()) + (sobel_y[1][0] * d.getRed()) + (sobel_x[1][1] * e.getRed()) + (sobel_x[1][2] * f.getRed()) + (sobel_y[2][0] * g.getRed()) + (sobel_x[2][1] * h.getRed()) + (sobel_x[2][2] * i.getRed()); //Here it is possible to get values between [-1020, 1020] //How to going on //int rgb = (int) Math.sqrt(pixel_x*pixel_x+pixel_y*pixel_y); //int rgbAsInt = (int)(65536 * rgb + 256 * rgb + rgb); } }