如何将图像转换为纹身?

时间:2013-03-25 08:36:54

标签: android image

我正在使用应用程序进行纹身,因为我需要将图像(从相机中取出或从图库中选择)转换为纹身......

我的要求如下:

enter image description here    我从github找到了一个示例代码 https://github.com/DrewDahlman/ImageFilter/tree/master/Android/project

用于图像过滤..

我不知道这是将图像转换为纹身的正确过程

如果有人知道这个纹身在android中请建议我,我google了很多

提前致谢..

2 个答案:

答案 0 :(得分:2)

你需要一个差异过滤器:

1)您计算水平差异(这里您将有垂直线段)

2)你计算了垂直差异(这里是水平线段)

3)你或两张地图,找到轮廓

4)如果你愿意,重新创建一个Bitmap对象

类似(EDITED):

int[] pixels;
int width = yourbitmap.getWidth();
int height = yourbitmap.getHeight();
yourbitmap.getPixels(pixels, 0, width, 0, 0, width, height);

// transform grayscale
int[] image = new int[width*height];
for (int y=0; y<height; y++)
    for (int x=0; x<width; x++)
    {
        int pixel = image[y*width + x];
        image[y*width + x] = (Color.red(pixel) + Color.green(pixel) + Color.blue(pixel))/3;
    }

// calculate diff_x (vertical segments)
int[] dx = new int[width*height];

for (int y=0; y<height; y++)
    for (int x=0; x<width; x++)
        dx[y*width + x] = (x==0 || y== 0 ? 0 : Math.abs(image[y*width + x] - image[y*width + x-1]));

// calculate diff_y (horizontal segments)
int[] dy = new int[width*height];

for (int y=0; y<height; y++)
    for (int x=0; x<width; x++)
        dy[y*width + x] = (x==0 || y== 0 ? 0 : Math.abs(image[y*width+x] - image[(y-1)*width+x])); 


// when the color intensity is higher than THRESHOLD, accept segment
// you'll want a slider to change THRESHOLD values
bool[] result = new bool[width*height];
const int THRESHOLD = 60; // adjust this value

for (int y=0; y<height; y++)
    for (int x=0; x<width; x++)
        result[y*width + x] = (dx[y*width + x] > THRESHOLD || dy[y*width + x] > THRESHOLD);

Bitmap result = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
for (int y=0; y<height; y++)
    for (int x=0; x<width; x++)
               result.setPixel(x, y, result[y*width+x]? Color.Black : Color.White);

答案 1 :(得分:0)

您可以使用:

 public static Bitmap ConvertToBlackAndWhite(Bitmap sampleBitmap) {
        ColorMatrix bwMatrix = new ColorMatrix();
        bwMatrix.setSaturation(0);
        final ColorMatrixColorFilter colorFilter = new ColorMatrixColorFilter(
                bwMatrix);
        Bitmap rBitmap = sampleBitmap.copy(Bitmap.Config.ARGB_8888, true);
        Paint paint = new Paint();
        paint.setColorFilter(colorFilter);
        Canvas myCanvas = new Canvas(rBitmap);
        myCanvas.drawBitmap(rBitmap, 0, 0, paint);
        return doBlackWhiteImage(rBitmap);
    }

public static BlackWhiteImage doBlackWhiteImage(Bitmap myBitmap) {
        int[] allpixels = new int[myBitmap.getHeight() * myBitmap.getWidth()];
        myBitmap.getPixels(allpixels, 0, myBitmap.getWidth(), 0, 0,
                myBitmap.getWidth(), myBitmap.getHeight());
        for (int i = 0; i < myBitmap.getHeight() * myBitmap.getWidth(); i++) {
            if (allpixels[i] == Color.BLACK)
                // do something
            else if (allpixels[i] == Color.TRANSPARENT)
                // do something
            else if (allpixels[i] == Color.WHITE)
                // do something
            else {
                allpixels[i] = Color.WHITE;
            }
        }
        myBitmap.setPixels(allpixels, 0, myBitmap.getWidth(), 0, 0,
                myBitmap.getWidth(), myBitmap.getHeight());
        return myBitmap;
    }