The Java binding of OpenCV has a number of data classes in the form of MatOfxxx, where xxx is other datatype such as Int, Float, Point. This example will demonstrate the use of MatOfInt class. These classes will usually be used for parameters passing between different OpenCV functions. We can manipulate it similar to array and List.
import org.opencv.core.Core; import org.opencv.core.MatOfInt; import java.util.ArrayList; void setup() { size(640, 480); println(Core.VERSION); System.loadLibrary(Core.NATIVE_LIBRARY_NAME); noLoop(); } void draw() { background(0); MatOfInt mat1 = new MatOfInt(1, 3, 5, 7, 9); MatOfInt mat2 = new MatOfInt(); ArrayList list = new ArrayList(); for (int i : mat1.toArray()) { println("From array " + i); } for (int i=0; i < 10; i++) { list.add(i); } mat2.fromList(list); for (int i : mat2.toList()) { println("From list " + i); } } |
The first matrix mat1 is initialized with a number of integers, 1, 3, 5, 7, and 9. The second matrix mat2 is empty. The variable list is an empty ArrayList of Integer.
The first for loop converts mat1 into a linear array and print all the elements. The second for loop appends 10 elements to the ArrayList, list. The matrix, mat2 then converts the list into itself as an matrix. The last for loop converts back the mat2 to a Java List and print out all the elements.