Get x,y position of a specific pixel

女生的网名这么多〃 提交于 2019-12-08 09:07:33

问题


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.

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.

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).

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.

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

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


回答1:


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




回答2:


x and y are horizontal and vertical pixel position in the image by its own, global coordinates depend on the origin of the coordinate system you respect and how the image is shifted according to it.



来源:https://stackoverflow.com/questions/9926055/get-x-y-position-of-a-specific-pixel

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!