获取特定像素的x,y位置 [英] Get x,y position of a specific pixel

查看:151
本文介绍了获取特定像素的x,y位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I got a little problem, I got a code, that reads all pixels and takes the RGB color of the pixel. But before that, I cut the picture into chunks, so I can also calculate larger images, without exceeding memory. Here is the code:

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();                    
            }

这就像一个护身符.从互联网上获得了一些信息,然后我用自己的代码对其进行了重新制作.

This works like a charm. Had some info from the internet, and I remade this for my own code.

但是我现在想要的就是:当他检测到一个全"红色像素(255)时,他必须显示该像素的x,y位置.

But what do I want now, that is: when he detects a "full" red pixel (255), he must show the x,y position of that pixel.

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

I thought this was very easy to do, but because I cut the image into chucks I get very weird x,y positions (what I think).

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

I will explain fast why I want this: In the image, their are 2 red pixels, and those 2 must be transformed into a rectangle, but I don't get the good x,y positions.

因此,为了使问题更明确:如何获得红色像素的x,y位置.

So to get the question clear: How do I get the x,y location of the red pixel.

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

Maybe very easy, but somehow I just don't get the right location, not even close. I check this with opening the image in Photoshop, and look what the x,y of the red pixel is, but my app gives very weird coordinates.

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

If you need more info or something else, please comment.

已经谢谢你了, 大流量

Thanks already, Bigflow

推荐答案

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

Coords should be rect.left + x and rect.top + y. Have you tried this already?

这篇关于获取特定像素的x,y位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆