вычитание растрового изображения из другого Java-андроида

Как вычесть одно растровое изображение из другого в Android. Помогите с кодом того же


person user3259851    schedule 05.02.2014    source источник
comment
Обратитесь к этой ссылке stackoverflow.com/questions/15105462/   -  person suresh    schedule 05.02.2014
comment
Используйте PorterDuffModes в Android. Взгляните на ссылку.   -  person BlueSword    schedule 05.02.2014
comment
Можем ли мы сделать что-нибудь кроме OpenCV?   -  person user3259851    schedule 05.02.2014


Ответы (1)


Хотя уже слишком поздно, я публикую ответ для других, которые могут оказаться здесь в поисках способа вычитания изображений, как я оказался здесь.

Вы можете использовать getPixel() класса Bitmps, чтобы получить значение цвета в каждой точке изображения. Как только вы получите эти значения для двух растровых изображений, вы можете вычесть значения ARGB, чтобы получить результирующее изображение.

Ниже приведен фрагмент кода

 Bitmap image1 = BitmapFactory.decodeFile(imgFile1.getAbsolutePath());
 Bitmap image2 = BitmapFactory.decodeFile(imgFile2.getAbsolutePath());
 Bitmap image3 = Bitmap.createBitmap(image1.getWidth(), image1.getHeight(), Config.ARGB_8888);

 for(int x = 0; x < image1.getWidth(); x++)
     for(int y = 0; y < image1.getHeight(); y++) {
         int argb1 = image1.getPixel(x, y);
         int argb2 = image2.getPixel(x, y);

         //int a1 = (argb1 >> 24) & 0xFF;
         int r1 = (argb1 >> 16) & 0xFF;
         int g1 = (argb1 >>  8) & 0xFF;
         int b1 = argb1 & 0xFF;

         //int a2 = (argb2 >> 24) & 0xFF;
         int r2 = (argb2 >> 16) & 0xFF;
         int g2 = (argb2 >>  8) & 0xFF;
         int b2 = argb2 & 0xFF;

         //int aDiff = Math.abs(a2 - a1);
         int rDiff = Math.abs(r2 - r1);
         int gDiff = Math.abs(g2 - g1);
         int bDiff = Math.abs(b2 - b1);

         int diff = 
              (255 << 24) | (rDiff << 16) | (gDiff << 8) | bDiff;

         image3.setPixel(x, y, diff);
     }


 try (FileOutputStream out = new FileOutputStream(filename)) {
     image3.compress(Bitmap.CompressFormat.PNG, 100, out); // bmp is your Bitmap instance
     // PNG is a lossless format, the compression factor (100) is ignored
 } catch (IOException e) {
     e.printStackTrace();
 }

Кредиты

https://stackoverflow.com/a/21806219/9640177

https://stackoverflow.com/a/16804467/9640177

https://stackoverflow.com/a/673014/9640177

https://stackoverflow.com/a/5392792/9640177

person mayank1513    schedule 11.12.2018