Pixel manipulation
Pixel manipulation is often required for one to access pixels in an image. There are several ways to do this and each one has its advantages and disadvantages. A straightforward method to do this is the put(row, col, value)
method. For instance, in order to fill our preceding matrix with values {1, 2, 3}
, we will use the following code:
for(int i=0;i<image.rows();i++){ for(int j=0;j<image.cols();j++){ image.put(i, j, new byte[]{1,2,3}); } }
Tip
Note that in the array of bytes {1, 2, 3}
, for our matrix, 1
stands for the blue channel, 2
for the green, and 3
for the red channel, as OpenCV stores its matrix internally in the
BGR (blue, green, and red) format.
It is okay to access pixels this way for small matrices. The only problem is the overhead of JNI calls for big images. Remember that even a small 640 x 480 pixel image has 307,200 pixels and if we think about a colored image, it has 921,600 values in a matrix. Imagine that it might take around 50ms to make...