2015-10-18 116 views
2

Android中的getPixels()在上下左右或左右上下讀取像素。基本上它是按行或列讀取的。如果我想告訴圖片中紅色值越高,我可以這樣做(我認爲它是按列讀取的)。如何檢測位圖中的紅色像素

 Bitmap thumbnail = (Bitmap) data.getExtras().get("data"); 

     imgTakenPhoto.setImageBitmap(thumbnail); 
     int[] pixels = new int[thumbnail.getHeight()*thumbnail.getWidth()]; 

     thumbnail.getPixels(pixels, 0, thumbnail.getWidth(), 0, 0, thumbnail.getWidth(), thumbnail.getHeight()); 
     Colered[] color = new Colered[pixels.length]; 
     int length = pixels.length; 
     int width = thumbnail.getWidth(); 
     int height = thumbnail.getHeight(); 
     for(int i=0; i<pixels.length;i++){ 
      color[i] = new Colered(getRed(pixels[i]),getBlue(pixels[i]),getGreen(pixels[i])); 
      System.out.print(color[i].red + " "); 
     } 
     int indice=-1; 
     int greatest = -1; 
     for(int i=0;i<length-1;i++){ 
      if(color[i].red> greatest){ 
       greatest = color[i].red; 
       indice = i; 
      } 


     } 

     public static int getRed(int color){ 
    return (color >> 16) & 0xFF; 
    } 
+2

我用c#編寫了一個類似於你正在做的事情的程序,我注意到的一件事是紅色在圖片中很少是紅色的,你必須給它一個可能的紅色範圍,或許使用RGB,其中R大於X且G和B小於Y,例如。 – Eliseo

+0

將位圖轉換爲像素數組,然後遍歷它並找到紅色像素(如果有的話)! –

+0

'Android中的getPixels()在左上角,左右上下讀取像素。基本上它是按行或列** ** NO **讀取的。它讀取'特定的x,y座標'。 –

回答

3

您的代碼查找「紅色」像素,紅色值最高的像素。我不認爲這實際上是你需要的,但如果我錯了,請糾正我。

你也過於複雜的事情。讓我們先從一個Bitmap和兩個循環:

int redThreshold = 200; // adjust this to your needs 
List<int[]> redPixels = new ArrayList<>(); // redPixels.get(int)[0] = x, redPixels.get(int)[1] = y 
Bitmap thumbnail = (Bitmap) data.getExtras().get("data"); 
for(int x = 0; x < thumbnail.getWidth(); x++) // x is row 
    for(int y = 0; y < thumbnail.getHeight(); y++) // y is col 
     if(Color.red(thumbnail.getPixel(x, y)) > redThreshold) { 
      redPixels.add(new int[]{x, y}); 
      System.out.println(String.format("Pixel at (%d, %d) has a red value exceeding our threshold of %d!", x, y, redThreshold); 
     } 

由於@Eliseo暗示在他的評論中,根據您的實際需要,你可能需要檢查其他兩個顏色是低於某一閾值,以及,如這將拾取諸如白色的顏色(r = 255,g = 255,b = 255是白色!)。

那麼,我們如何尋找視覺衝擊?你和我會看到並同意的只是「紅色」。那麼,讓我們確保我們的紅色值與綠色或藍色中的較大值相適應;藍色和綠色在彼此的範圍內,否則我們會變成粉紅色和橙色。類似這樣的:

int thresholdRatio = 2; 
int threshold = 15; 
List<int[]> redPixels = new ArrayList<>(); 
Bitmap thumbnail = (Bitmap) data.getExtras().get("data"); 
for(int x = 0; x < thumbnail.getWidth(); x++) 
    for(int y = 0; y < thumbnail.getHeight(); y++) { 
     Color color = thumbnail.getPixel(x, y); 
     int r = Color.red(color); 
     int g = Color.green(color); 
     int b = Color.blue(color); 
     if((r > (Math.max(g, b) * thresholdRatio)) && (Math.abs(g - b) < threshold)) { 
      redPixels.add(new int[]{x, y}); 
      System.out.println(String.format("Pixel at (%d, %d) has a red value more than %d times the max(green, blue) value, and the green and blue value are also within %d points!", x, y, thresholdRatio, threshold); 
     } 
    } 

這將捕獲非常淺和非常深的紅色陰影。如果需要捕捉非常特定的紅色陰影,請添加天花板和地板閾值。

想要想象哪些像素被捕獲?如果位圖是可變的,我們可以。

Random rand = new Random(); 
Color getRandomColor() { 
    int[] rgb = new int[3]; 
    for(int i = 0; i < rgb.length; i++) { 
     rgb[i] = rand.nextInt(256); 
    } 
    return Color.rgb(rgb[0], rgb[1], rgb[2]); 
} 

boolean transformRedPixels(Bitmap bm, List<int[]> redPixels) { 
    for(int[] coords : redPixels) { 
     bm.setPixel(coords[0], coords[1], getRandomColor()); // This will set each pixel caught to a new random color 
    } 
    return bm.isMutable(); 
} 

讓我知道這是否適合您的需求。快樂編碼:)

相關問題