0

I'm using this algorithm to filter images in andriod.

http://xjaphx.wordpress.com/2011/06/22/image-processing-convolution-matrix/

But the images are not as expected, where I can find other ways to do this. You see that applications already do this, makes it fast, this algorithm is way too slow.

regards

tebitoq
  • 165
  • 1
  • 13

2 Answers2

2

I recently posted there a faster version of the code you tried, you should give it a try.

By the way, what do you mean with the sentence images are not as expected ? Maybe you're just using a wrong matrix; you can find some matrix example here.

Here is the sample you requested. If you don't need to scale / offset pixel colors, you should add different implementations of convolute without those parameters and the related unnecessary computations.

class Convolution {

    private static Bitmap convolute(Bitmap bmp, Matrix mat, float factor, int offset) {
    /* ... */
    }

    private static Matrix getEdgeEnhanceMatrix() {
        Matrix m = new Matrix();
        m.setValues(new float[] {
                0, 0, 0,
                -1, 1, 0,
                0, 0, 0
        });
        return m;
    }

    // the simple way
    public static Bitmap edgeEnhance1(Bitmap bmp) {
        return convolute(bmp, getEdgeEnhanceMatrix(), 1f, 0);
    }

    // if you want to apply filter to border pixels
    // warning: really memory consuming
    public static Bitmap edgeEnhance2(Bitmap bmp, int bgColor) {
        // create a bigger canvas
        Bitmap bigger = Bitmap.createBitmap(bmp.getWidth() + 2, bmp.getHeight() + 2, bmp.getConfig());
        Canvas cBigger = new Canvas(bigger);
        // paint background
        cBigger.drawColor(bgColor);
        // draw the bmp you want to manipulate from (1,1)
        cBigger.drawBitmap(bmp, 1, 1, null);
        // compute convolution
        bigger = convolute(bigger, getEdgeEnhanceMatrix(), 1f, 0);

        // create the result and project the convolution at (-1,-1)
        Bitmap rt = Bitmap.createBitmap(bmp.getWidth(), bmp.getHeight(), bmp.getConfig());
        Canvas cRt = new Canvas(rt);
        cRt.drawBitmap(bigger, -1, -1, null);

        return rt;
    }
}
ubik
  • 595
  • 5
  • 16
  • Thanks for the reply, when I apply the array, then display the image a few pixels away. I'll try your code. – tebitoq Apr 22 '12 at 02:52
  • the pixel displace you notice is because, to get a pixel transformed, you need the eight pixels surrounding it: so you will "loose" a rectangle of one pixel width around the image - or in any case, if you clone the original pixels as i'm doing, those won't be transformed. – ubik Apr 22 '12 at 18:24
  • so would have to create the matrix? float src[] = {0,0,0, -1,-1,0 ,0,0,0}; Matrix mx = new Matrix(); mx.setValues(src); – tebitoq Apr 22 '12 at 19:00
  • http://imageshack.us/photo/my-images/705/device20120422175353.png/ Figure 16.155. Emboss m.setValues(new float[] { -2, -1, 0, -1, 1, 1, 0, 1, 2 }); – tebitoq Apr 22 '12 at 22:59
  • white pixels, how could I fix that? – tebitoq Apr 22 '12 at 23:06
  • i don't think that just one convolution matrix could be suitable for every image. i use emboss to obtain a 3d effect on some shaped bitmaps but using a black and white bitmap representing the shape, a matrix of {-1,0,0, 0,1,0, 0,0,1} and an offset of 127 - this for instance generates a gray bmp with black and white contours. – ubik Apr 23 '12 at 00:02
  • ok you have the right, the problem is that images are taken from the camera. – tebitoq Apr 23 '12 at 13:11
-3

I am using this formula to filter images as per their extension

class FileExtensionFilter implements FilenameFilter {
        public boolean accept(File dir, String name) {
            return (name.endsWith(".png") || name.endsWith(".PNG"));
        }

If you are fetching it from the sd card let me know about this .I had code for it.

NovusMobile
  • 1,813
  • 2
  • 21
  • 48