繁体   English   中英

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

[英]Get x,y position of a specific pixel

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

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

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

x和y分别是图像在图像中的水平和垂直像素位置,全局坐标取决于您尊重的坐标系的原点以及图像如何根据其移动。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM