Tuesday 25 February 2014

Accessing the elements of IplImage

The following code demonstrates the way of accessing the elements of an IplImage. It accesses one by one the elements of the IplImage, increases them by 1 and prints them to screen.

NOTE:  Every time you call put or get, that's a java member function call, which in turn calls a C wrapper function, which again calls a C opencv library function. Therefore, in the general case you should avoid accessing elements one by one unless necessary, as it can make the program considerably slower! 

    // Load IplImage (ImgName is a variable of type String)
    IplImage image = cvLoadImage(ImgName);

    // Define which of the 3 channels of a color image 
    // you want to access (for grayscale images, the 
    // channel is always 0). RGB images are stored as 
    // BGR in JavaCV so: 0-> Blue, 1-> Green, 2-> Red
    int channel = 0; // Blue channel  

    ByteBuffer ImageBuffer = 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() + channel;
            
            // Read the pixel value - the 0xFF is needed to cast 
            // from an unsigned byte to an int.
            int value = ImageBuffer.get(Index) & 0xFF;

            // Put the increased by 1 value in IplImage
            ImageBuffer.put(Index, (byte) (value+1));

            System.out.print( " " + (ImageBuffer.get(Index) & 0xFF));
        }
        System.out.println("\n");
    }

1 comment:

  1. Sands Casino | Vegas Slots & Casino Games | Official Site
    Come experience the glamour of Vegas with Sands Casino. Play your favorite Vegas slots, table games, and live entertainment in our luxurious hotel 샌즈 카지노 주소 & casino.

    ReplyDelete