2012-03-29 46 views
0

我得到一個小問題,我得到一個代碼,它讀取所有像素並獲取像素的RGB顏色。但在此之前,我將圖片分塊,所以我也可以計算出更大的圖像,而不會超過內存。 這裏是代碼:獲取特定像素的x,y位置

Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED); 

      try { 
       decoder_image = BitmapRegionDecoder.newInstance(filePath, 
         false); 


      } catch (IOException e) { 
       e.printStackTrace(); 
      } 
      try { 
       boolean bool_pixel = true; 
       final int width = decoder_image.getWidth(); 
       final int height = decoder_image.getHeight(); 
       // Divide the bitmap into 1100x1100 sized chunks and process it. 
       // This makes sure that the app will not be "overloaded" 
       int wSteps = (int) Math.ceil(width/1100.0); 
       int hSteps = (int) Math.ceil(height/1100.0); 
       Rect rect = new Rect(); 
       for (int h = 0; h < hSteps; h++) { 
        for (int w = 0; w < wSteps; w++) { 
         int w2 = Math.min(width, (w + 1) * 1100); 
         int h2 = Math.min(height, (h + 1) * 1100); 
         rect.set(w * 1100, h * 1100, w2, h2); 
         bitmap_image = decoder_image.decodeRegion(rect, 
           null); 

         try { 
          int bWidth = bitmap_image.getWidth(); 
          int bHeight = bitmap_image.getHeight(); 
          int[] pixels = new int[bWidth * bHeight]; 
          bitmap_image.getPixels(pixels, 0, bWidth, 0, 0, 
            bWidth, bHeight); 
          for (int y = 0; y < bHeight; y++) { 
           for (int x = 0; x < bWidth; x++) { 

            int index = y * bWidth + x; 
            int R = (pixels[index] >> 16) & 0xff; //bitwise shifting 
            int G = (pixels[index] >> 8) & 0xff; 
            int B = pixels[index] & 0xff; 
            total++; 

            if (R == 255){ 
            //Save x,y position          
            } 
            if ((G > (R+2)) && (G > (B+2))) { 
            counter++; 
            } 
           } 
          } 
         } finally { 
          bitmap_image.recycle(); 
         } 
        } 
       } 
      } finally { 
       decoder_image.recycle();      
      } 

這就像一個魅力。有來自互聯網的一些信息,我重拍這個爲我自己的代碼。

但我現在想要什麼,即:當他檢測到一個「全」紅色像素(255)時,他必須顯示該像素的x,y位置。

我認爲這很容易做到,但因爲我把圖像切成卡盤,我得到了很奇怪的x,y位置(我認爲)。

我會快速解釋爲什麼我要這樣做:在圖像中,它們是2個紅色像素,而這2個像素必須轉換成矩形,但我沒有得到好的x,y位置。

因此,要明確問題:如何獲取紅色像素的x,y位置。

也許很容易,但不知何故,我只是沒有得到正確的位置,甚至沒有接近。我在Photoshop中打開圖像來檢查它,看看紅色像素的x,y是什麼,但是我的應用程序給出了非常奇怪的座標。

如果您需要更多信息或其他內容,請發表評論。

感謝不已, Bigflow

+0

因此,您需要創建一個計算像素位置的算法? – Th0rndike 2012-03-29 13:17:52

+0

傑克的回答是正確的 – Bigflow 2012-03-29 14:33:06

回答

1

COORDS 應該被rect.left + x和rect.top + Y。你已經嘗試過了嗎?

+0

工作完美,謝謝。 – Bigflow 2012-03-29 14:32:46

0

x和y是圖像中水平和垂直像素位置的自身全局座標,取決於您尊重的座標系的原點以及圖像如何根據它移動。

相關問題