### Question:
OpenCVUtils { public static Mat bufferedImageToMat(BufferedImage image) { System.loadLibrary(Core.NATIVE_LIBRARY_NAME); if (image != null) { Mat mat; int numComponents = image.getColorModel().getNumComponents(); switch (numComponents) { case 1: mat = new Mat(image.getHeight(), image.getWidth(), CV_8UC1); break; case 2: mat = new Mat(image.getHeight(), image.getWidth(), CV_8UC2); break; case 3: mat = new Mat(image.getHeight(), image.getWidth(), CV_8UC3); break; case 4: mat = new Mat(image.getHeight(), image.getWidth(), CV_8UC4); break; default: throw new RuntimeException("Image has an unsupportable number of channels: " + numComponents); } if (image.getType() == BufferedImage.TYPE_BYTE_INDEXED) { image = convertByteIndexedImage(image); } byte[] pixelData = ((DataBufferByte) image.getRaster().getDataBuffer()).getData(); mat.put(0, 0, pixelData); Mat mat3; if (numComponents == 3) { mat3 = mat; } else { mat3 = new Mat(image.getHeight(), image.getWidth(), CV_8UC3); mat.convertTo(mat3, CV_8UC3); } return mat3; } return null; }  static Mat bufferedImageToMat(BufferedImage image); static BufferedImage matToBufferedImage(Mat mat);  }### Answer:
@Test public void testBufferedImageToMat() throws IOException { Mat mat = matForImage(TEST_4_CHANNEL_IMAGE, 4); assertEquals(4, mat.channels()); mat = matForImage(TEST_3_CHANNEL_IMAGE, 3); assertEquals(3, mat.channels()); mat = matForImage(TEST_GRAYSCALE_IMAGE, 1); assertEquals(1, mat.channels()); }