简体   繁体   中英

Get x,y position of a specific pixel

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

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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