gpt4 book ai didi

java - 获取特定像素的 x,y 位置

转载 作者:行者123 更新时间:2023-12-01 15:28:04 26 4
gpt4 key购买 nike

我遇到了一个小问题,我有一个代码,它读取所有像素并获取像素的 RGB 颜色。但在此之前,我将图片切成了 block ,这样我也可以计算更大的图像,而不会超出内存。这是代码:

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 位置。

我认为这很容易做到,但是因为我将图像切成 block ,所以我得到了非常奇怪的 x,y 位置(我的想法)。

我会快速解释为什么我想要这个:在图像中,它们是 2 个红色像素,这 2 个像素必须转换为矩形,但我没有得到好的 x,y 位置。

为了弄清楚问题:如何获取红色像素的 x,y 位置。

也许很容易,但不知怎的,我就是没有找到正确的位置,甚至不是很近。我通过在 Photoshop 中打开图像来检查这一点,并查看红色像素的 x,y 是什么,但我的应用程序给出了非常奇怪的坐标。

如果您需要更多信息或其他内容,请发表评论。

已经谢谢了,大流量

最佳答案

坐标应该为 rect.left + x 和 rect.top + y。您已经尝试过了吗?

关于java - 获取特定像素的 x,y 位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9926055/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com