2013-03-25 28 views
4

上午工作在tattoolater應用我需要的圖像(從相機拍攝或從相冊中選擇)到紋身轉換...如何將圖像轉換爲紋身?

我的要求是像下面

enter image description here 我發現從GitHub https://github.com/DrewDahlman/ImageFilter/tree/master/Android/project

其imagefiltering ..

一個示例代碼,我不知道這是正確的處理將圖像轉換到紋身或不

如果有人知道這個紋身在android系統,請建議我我GOOGLE了很多

在此先感謝..

+0

無用的觀察:圖像是從iPhone? – 2013-03-25 08:40:55

+0

[邊緣檢測](http://en.wikipedia.org/wiki/Edge_detection) – 2013-03-25 08:52:41

回答

2

你需要一個差分濾波器:

1)你calc下水平差(在這裏,你將有垂直段)

2)你calc下的垂直差(這裏,水平段)

3)您或兩個映射,發現第Ë概述

4)重新創建一個位圖對象,如果你願意的話

像(編者):

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

如何計算位圖的水平差異和垂直差異 – Santosh 2013-03-25 09:50:27

+0

發佈的代碼應計算差異和最終OR,唯一缺少的是轉換位圖 - >字節[] []矩陣。我不知道很多的android,但我認爲Bitmap.getPixels(...)應該做的技巧 – Exceptyon 2013-03-25 11:14:53

+0

對不起,它不工作在android位圖轉換爲字節[]矩陣只有和它不轉換爲字節[ ] []矩陣,也在這裏image.x和image.y不被識別...你可以給明確的explonation – Santosh 2013-03-25 13:11:14

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

我試着用你的代碼,它顯示了在將我的位圖傳遞給ConvertToBlackAndWhite()這個方法後的白色圖像 – Santosh 2013-03-25 09:38:19

+0

在doBlackWhiteImage()中,你檢查像素是否有白色,透明,黑色和其他默認顏色白色是分配如何檢查剩餘的顏色),所以我認爲這是問題...請你能解決這個問題 – Santosh 2013-03-25 13:16:47