Java上でOpenCVを使用して、画像の読み込みおよび書き出しを行うテンプレートです。
UnsatisfiedErrorを防ぐために、静的フィールドでライブラリを読み込んでいます。
import org.opencv.core.Mat;
import org.opencv.imgcodecs.Imgcodecs;
import org.opencv.core.Core;
public class imgTest {
public static Mat loadImage(String imagePath) {
Imgcodecs imageCodecs = new Imgcodecs();
return imageCodecs.imread(imagePath);
}
public static void saveImage(Mat imageMatrix, String targetPath) {
Imgcodecs imgcodecs = new Imgcodecs();
imgcodecs.imwrite(targetPath, imageMatrix);
}
public static void main(String[] args) {
System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
String path = "img1.tif";
String fout = "img_out.png";
Mat imMat = loadImage(path);
saveImage(imMat, fout);
}
}
