【问题标题】:how to find xy coordinate of pixel in pixel array from android bitmap如何从android位图中找到像素数组中像素的xy坐标
【发布时间】:2011-05-07 16:39:44
【问题描述】:

我有一个使用 Bitmap.getPixels() 方法从位图派生的 int 数组。此方法使用位图中的像素填充数组。当我遍历该数组时,如何获得每个像素的 xy 坐标?在此先感谢 Mat。

[更新] 谢谢你的数学。我试过下面的代码。我有一个位图,其中我将前 50000 像素更改为白色。我现在想遍历位图并将所有白色像素更改为红色。 atm 只有一条红线穿过位图顶部的白色像素块。你有什么想法吗?非常感谢。

int length = bgr.getWidth()*bgr.getHeight();
                    int[] pixels = new int[length];
                    bgr.getPixels(pixels,0,bgr.getWidth(),0,0,bgr.getWidth(),bgr.getHeight());
                    for (int i=0;i<50000;i++){
                    // If the bitmap is in ARGB_8888 format

                        pixels[i] = Color.WHITE;//0xffffffff;

                      }

                    bgr.setPixels(pixels,0,bgr.getWidth(),0,0,bgr.getWidth(),bgr.getHeight());




                        int t = 0;
                    int y  = t / bgr.getWidth();
                    int x = t - (y * bgr.getWidth());

                  for( t = 0; t < length; t++){

                      int pixel = bgr.getPixel(x,y);

                      if(pixel == Color.WHITE){

                          bgr.setPixel(x,y,Color.RED);
                          x++;y++;
                      }
                  }

【问题讨论】:

  • 通常与indexXY = (y * bgr.getWidth()) + x)一起给出,其中0 &lt;= y &lt; bgr.getHeight()0 &lt;= x &lt; bgr.getWith()。您可能希望分别使用 x 和 y 进行迭代以使其更快一些。
  • 另外,如果您使用 i、y = i / bgr.getWidth()x = i - (y * bgr.getWidth()) 进行迭代。不过,您可能想在将它们发送到生产环境之前自己检查这些数学。
  • @harism 嗨,谢谢你,如果你不介意看一下,我已经更新了这个问题。

标签: android bitmap pixel-manipulation


【解决方案1】:

这是一个代码示例,它可能符合您的描述。至少我理解你的目标;

int length = bgr.getWidth()*bgr.getHeight();
int[] pixels = new int[length];

bgr.getPixels(pixels,0,bgr.getWidth(),0,0,bgr.getWidth(),bgr.getHeight());

// Change first 50000 pixels to white. You most definitely wanted
// to check i < length too, but leaving it as-is.
for (int i=0;i<50000;i++){
    pixels[i] = Color.WHITE;
}

// I'm a bit confused why this is here as you have pixels[] to do
// modification on. And it would be a bit more efficient way to do all of
// these changes on pixels array before setting them back to bgr.
// But taken this is an experiment with Bitmaps (or homework, hopefully not ;)
// rather good idea actually.
bgr.setPixels(pixels, 0, bgr.getWidth(), 0, 0, bgr.getWidth(), bgr.getHeight());

for (int i=0; i < length; ++i) {
    int y = i / bgr.getWidth();
    int x = i - (y * bgr.getWidth());
    int pixel = bgr.getPixel(x, y);
    if(pixel == Color.WHITE){
        bgr.setPixel(x ,y , Color.RED);
    }
}

【讨论】:

  • 嘿,非常感谢解决了:) 这不是家庭作业,我正在做志愿工作岗位面试。我已经签了合同,所以如果我能做到,我可能会有一份工作。我只是在学习 android 和图像处理,所以一切都是新的。最终,该应用程序将定位一个圆圈内的像素,然后使用球化算法处理这些像素......希望如此;)再次感谢!
  • @turtleboy,不管是什么原因,我都修改了我的答案。希望更好地对应您问题的标题。这次使用 i 进行迭代。
  • 我可以将它用于我所要求的东西吗:stackoverflow.com/questions/21692813/…
猜你喜欢
  • 2017-11-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-07-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多