IplImage Pixel Access JavaCV

后端 未结 2 1156
庸人自扰
庸人自扰 2021-02-09 15:54

I\'m trying to access Pixel by Pixel of an IplImage. Im using Java and Processing, and sometimes I need to access pixel by pixel. I\'ve done this so far, but I don\'t know what\

2条回答
  •  情话喂你
    2021-02-09 16:23

    The fastest way to iterate over each pixel in JavaCV is:

    ByteBuffer buffer = image.getByteBuffer();
    
    for(int y = 0; y < image.height(); y++) {
        for(int x = 0; x < image.width(); x++) {
            int index = y * image.widthStep() + x * image.nChannels();
    
            // Used to read the pixel value - the 0xFF is needed to cast from
            // an unsigned byte to an int.
            int value = buffer.get(index) & 0xFF;
    
            // Sets the pixel to a value (greyscale).
            buffer.put(index, value);
    
            // Sets the pixel to a value (RGB, stored in BGR order).
            buffer.put(index, blue);
            buffer.put(index + 1, green);
            buffer.put(index + 2, red);
        }
    }
    

提交回复
热议问题