【问题标题】:How to convert an image into tattoo? [closed]如何将图像转换为纹身? [关闭]
【发布时间】:2023-03-25 15:48:01
【问题描述】:

我正在为纹身工作应用程序,因为我需要将图像(从相机拍摄或从画廊中选择)转换为纹身......

我的要求如下

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

它用于图像过滤..

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

如果有人知道这个纹身在 android 中,请建议我我用谷歌搜索了很多

提前谢谢..

【问题讨论】:

标签: android image


【解决方案1】:

你需要一个差分过滤器:

1) 你计算水平差(这里你会有垂直段)

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

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

4) 重新创建 Bitmap 对象,如果您愿意的话

类似(已编辑):

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);

【讨论】:

  • 如何计算位图的水平差和垂直差
  • 发布的代码应该计算差异和最终的OR,唯一缺少的是转换位图->字节[][]矩阵。我不太了解 android,但我认为 Bitmap.getPixels(...) 应该可以解决问题
  • 抱歉,它在 android 中不起作用,位图仅转换为 byte[]matrix,它没有转换为 byte[][]matrix,而且这里的 image.x 和 image.y 也无法识别...你能给出明确的解释吗
  • 代码已编辑,应该更容易实现
  • 那么最终如何设置为转换后的位图图像...您将值存储在 bool 知道它在 bitmap.setpixels() 方法中无法识别..
【解决方案2】:

你可以用这个:

 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;
    }

【讨论】:

  • 我用你的代码试过它在将我的位图传递给 ConvertToBlackAndWhite() 这个方法后显示白色图像
  • 在 doBlackWhiteImage() 中,您正在使用白色、透明、黑色检查像素​​,否则分配默认颜色白色(如何检查剩余颜色),所以我认为这就是问题所在......请你能解决这个问题吗
猜你喜欢
  • 2023-03-04
  • 2010-12-24
  • 2017-01-02
  • 2012-12-10
  • 2021-12-15
  • 1970-01-01
  • 2015-05-05
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多