图像的阈值图像生成Threshold Imgproc.threshold
package opencv_java_demo; import org.opencv.core.*; import org.opencv.imgcodecs.*; import org.opencv.imgproc.Imgproc; public class Threshold { public static void main(String[] args) { try{ System.loadLibrary(Core.NATIVE_LIBRARY_NAME); Mat src=Imgcodecs.imread("./images/lenna.jpg",Imgcodecs.CV_LOAD_IMAGE_GRAYSCALE); //读取图像到矩阵中,取灰度图像 if(src.empty()){ throw new Exception("no file"); } Imgcodecs.imwrite("./images/in.jpg", src); //输出灰度图像值 Mat dst=new Mat(); //定义新矩阵 Imgproc.threshold(src, dst, 100.0, 200.0, Imgproc.THRESH_BINARY_INV); Imgcodecs.imwrite("./images/threshold_THRESH_BINARY_INV.jpg", src); Imgproc.threshold(src, dst, 100.0, 200.0, Imgproc.THRESH_TRUNC); Imgcodecs.imwrite("./images/threshold_THRESH_TRUNC.jpg", src); Imgproc.threshold(src, dst, 100.0, 200.0, Imgproc.THRESH_BINARY); Imgcodecs.imwrite("./images/threshold_THRESH_BINARY.jpg", src); Imgproc.threshold(src, dst, 100.0, 200.0, Imgproc.THRESH_TOZERO); Imgcodecs.imwrite("./images/threshold_THRESH_TOZERO.jpg", src); Imgproc.threshold(src, dst, 100.0, 200.0, Imgproc.THRESH_TOZERO_INV); Imgcodecs.imwrite("./images/threshold_THRESH_TOZERO_INV.jpg", src); }catch(Exception e){ System.out.println("例外:" + e); } } }