显示图片的类在 这个博客。
看了一眼 opencv 官网,已经更新到 4.5 预览版了,官方的 Java api 文档好像也把方法说明给加上了,比我当初看的好了不知道多少 T^T
如果 Java 的文档没有写详细,那就把方法名复制一下,到同版本的 c++ 的文档找说明,c++ 文档是最全的。
Java 文档:https://docs.opencv.org/master/javadoc
C++ 文档:https://docs.opencv.org/master/index.html
更新于 2020年9月9日21点。谢谢各位的阅读。


橘子派

OpenCV With Java 例程

图像直接阀值画操作

/**
 * 图像直接阈值化操作
 */
public class AdaptiveThreshold {

    public static void main(String[] args) {
        try {
            System.loadLibrary(Core.NATIVE_LIBRARY_NAME);

            //以灰度图像的方式读取图像.
            Mat src = Imgcodecs.imread("./images/Lena.jpg", Imgcodecs.CV_LOAD_IMAGE_GRAYSCALE);
            new ShowImage(src);
            Mat dst = new Mat();

            //对一个数组应用一个自适应阈值。
//            该函数根据公式将灰度图像转换为二进制图像:
            Imgproc.adaptiveThreshold(src, dst, 200,
                    Imgproc.ADAPTIVE_THRESH_GAUSSIAN_C, Imgproc.THRESH_BINARY, 7, 8);

            new ShowImage(dst);

        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

矩阵和图像的函数之加和阿尔法融合

/**
 * 矩阵和图像的函数之加和阿尔法融合
 */
public class Add {
    public static void main(String[] args) {
        try {
            System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
			
            //两张图片大小要一致,不然会报错【Sizes of input arguments do not match】。
            Mat src1 = Imgcodecs.imread("./images/3.jpg");
            Mat src2 = Imgcodecs.imread("./images/2.jpg");

            //读取图像到矩阵中
            if (src1.empty() || src2.empty()) {
                throw new Exception("no file");
            }

            Mat dst = new Mat();

            Core.add(src1, src2, dst);//src1 和 src2 相加, 赋值给 dst
            new ShowImage(dst);

        } catch (Exception e) {
            System.out.println("例外:" + e);
        }
    }
}

add 方法 添加 mask 掩码

/**
 * add 方法 添加 mask 掩码
 */
public class AddMask {

    private static Mat makeMask(Mat src) {
        Size sz = new Size(src.width(), src.height());
        Mat mask = Mat.zeros(sz, CvType.CV_8U);
        for (int y = src.height() / 4; y < src.height() * 3 / 4; y++) {
            for (int x = src.height() / 4; x < src.width(); x++) {
                mask.put(y, x, 255);
            }
        }
        return mask;
    }

    public static void main(String[] args) {
        try {
            System.loadLibrary(Core.NATIVE_LIBRARY_NAME);

            //读取图像到矩阵中
            Mat src1 = Imgcodecs.imread("./images/2.jpg");
            Mat src2 = Imgcodecs.imread("./images/3.jpg");

            if (src1.empty() || src2.empty()) {
                throw new Exception("no file");
            }

            Mat dst = new Mat();
            Mat mask = makeMask(src1);
            Core.add(src1, src2, dst, mask);

            dst = src1.clone();
            new ShowImage(dst);

            Core.add(src1, src2, dst, mask);
            new ShowImage(dst);
        } catch (Exception e) {
            System.out.println("例外:" + e);
        }
    }
}

图片的ROI的领域合成 AddRoi

/**
 * 图片的ROI的领域合成 AddRoi
 */
public class AddRoi {

    public static void main(String[] args) {
        try {
            System.loadLibrary(Core.NATIVE_LIBRARY_NAME);

            Mat src1 = Imgcodecs.imread("./images/2.jpg");
            Mat src2 = Imgcodecs.imread("./images/3.jpg");

            if (src1.empty() || src2.empty()) {
                throw new Exception("no file");
            }

            Mat dst = src1.clone();

            Rect roiArea = new Rect(src1.width() / 4, src1.height() / 4, src1.width() / 2, src1.height() / 2);

            Mat src1Roi = new Mat(src1, roiArea);
            Mat src2Roi = new Mat(src2, roiArea);
            Mat dstRoi = new Mat(dst, roiArea);

//            new ShowImage(dstRoi);
            Core.add(src1Roi, src2Roi, dstRoi);
//            new ShowImage(dstRoi); // 颜色更亮了,我觉得应该是颜色值相加之后导致的。
        } catch (Exception e) {
            System.out.println("例外:" + e);
        }
    }
}

图像的Scalar 标量合成

/**
 * 图像的Scalar 标量合成 
 */
public class AddScalar {

    public static void main(String[] args) {
        try {
            System.loadLibrary(Core.NATIVE_LIBRARY_NAME);

            Mat src1 = Imgcodecs.imread("./images/2.jpg");

            //读取图像到矩阵中
            if (src1.empty()) {
                throw new Exception("no file");
            }

            Mat dst = new Mat();

            new ShowImage(src1);

            Core.add(src1, new Scalar(0, 0, 255), dst);//相当于给图片加上一层色彩蒙图Scalar(255,0,0)是蓝色,类似,还有绿色和红色。

            new ShowImage(dst);

        } catch (Exception e) {
            System.out.println("例外:" + e);
        }
    }
}

akaze的特征提取与图像匹配

/**
 * akaze的特征提取与图像匹配 akaze
 */
public class akaze {

    public static void main(String[] args) {
        try {
            System.loadLibrary(Core.NATIVE_LIBRARY_NAME);

            Mat src1 = Imgcodecs.imread("./images/2.jpg");
            Mat src2 = Imgcodecs.imread("./images/Lena.jpg");
            if (src1.empty() || src2.empty()) {
                throw new Exception("no file");
            }

            MatOfKeyPoint keyPoint1 = new MatOfKeyPoint();
            MatOfKeyPoint keyPoint2 = new MatOfKeyPoint();

            FeatureDetector sifDetecotr = FeatureDetector.create(FeatureDetector.AKAZE);//用它的名字创建一个功能检测器。

            sifDetecotr.detect(src1,keyPoint1);//在图像(第一个变体)或图像集(第二种变体)中检测关键点。
            sifDetecotr.detect(src2,keyPoint2);

            DescriptorExtractor extractor = DescriptorExtractor.create(DescriptorExtractor.AKAZE);//根据名称创建一个描述符提取器。

            Mat descriptor1 = new Mat(src1.rows(), src1.cols(), src1.type());
            extractor.compute(src1,keyPoint1,descriptor1);//计算在一个图像(第一个变体)或图像集(第二个变种)中检测到的一组关键点的描述符。
            Mat descriptor2 = new Mat(src2.rows(), src2.cols(), src2.type());
            extractor.compute(src2,keyPoint2,descriptor2);

            MatOfDMatch matches = new MatOfDMatch();
            DescriptorMatcher matcher = DescriptorMatcher.create(DescriptorMatcher.BRUTEFORCE);//用默认参数(使用默认的构造函数)创建给定类型的描述符matcher。

            matcher.match(descriptor1,descriptor2,matches);//从查询集中找到每个描述符的最佳匹配。

            Mat dst = new Mat();
            Features2d.drawMatches(src1,keyPoint1,src2,keyPoint2,matches,dst);//从两个图像中提取出的关键点匹配。

            new ShowImage(dst);

        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

图像的二值图反色处理

/**
 * 图像的二值图反色处理
 */
public class Bitwise_not {
    public static void main(String[] args) {
        System.loadLibrary(Core.NATIVE_LIBRARY_NAME);

        Mat src = Imgcodecs.imread("./images/2.jpg");

        if (src.empty()) {
            System.out.println("图片路径为空");
            return;
        }

        Mat dst = src.clone();

        Core.bitwise_not(src,dst);//对二进制数据进行“非”操作,即对图像(灰度图像或彩色图像均可)每个像素值进行二进制“非”操作
        new ShowImage(dst);
    }
}

图像的模糊化Blur处理 Blur

/**
 * 图像的模糊化Blur处理 Blur
 */
public class Blur {

    public static void main(String[] args) {
        System.loadLibrary(Core.NATIVE_LIBRARY_NAME);

        Mat src = Imgcodecs.imread("./images/Lena.jpg");

        if (src.empty()) {
            System.out.println("图像路径为空");
        }

        Mat dst = src.clone();

        //图像模糊化处理
        Imgproc.blur(src,dst,new Size(15,15));

        new ShowImage(dst);
    }
}

图像的方框滤波BoxFilter

/**
 * 图像的方框滤波BoxFilter 
 */
public class BoxFilter {

    public static void main(String[] args) {
        System.loadLibrary(Core.NATIVE_LIBRARY_NAME);

        Mat src = Imgcodecs.imread("./images/Lena.jpg");

        if (src.empty()) {
            System.out.println("图像路径为空");
            return;
        }

        Mat dst = src.clone();

        Imgproc.boxFilter(src,dst,src.depth(),new Size(10,10));//用方框过滤器模糊一个图像
        new ShowImage(dst);
    }
}

图像的边缘检测Canny

/**
 * 图像的边缘检测Canny
 */
public class Canny {

    public static void main(String[] args) {
        System.loadLibrary(Core.NATIVE_LIBRARY_NAME);

        Mat src = Imgcodecs.imread("./images/Lena.jpg");

        if (src.empty()) {
            System.out.println("图像路径为空");
            return;
        }

        Mat dst = src.clone();

        Imgproc.Canny(src,dst,40.0,200.0);
        new ShowImage(dst);
    }
}

在图像上画圆 circle

/**
 * 在图像上画圆 circle
 */
public class Circle {

    public static void main(String[] args) {
        System.loadLibrary(Core.NATIVE_LIBRARY_NAME);

        //生成 400 * 400 的灰色图像矩阵
        Mat img = new Mat(400, 400, CvType.CV_8UC3, new Scalar(150, 150, 150));
        new ShowImage(img);

        //圆心为(200,200),半径为50,绿色
        Imgproc.circle(img, new Point(200, 200), 50, new Scalar(255, 0, 0));
        new ShowImage(img);

        //重置 img
        img = new Mat(400, 400, CvType.CV_8UC3, new Scalar(150, 150, 150));

        //圆心为 (200,300),半径为 100 的圆
        Imgproc.circle(img, new Point(200, 300), 100, new Scalar(0, 255, 0), 3);
        new ShowImage(img);

        img = new Mat(400, 400, CvType.CV_8UC3, new Scalar(150, 150, 150));
        Imgproc.circle(img, new Point(200, 200), 150, new Scalar(0, 0, 255));
    }
}

在已有的图像上画圆圈

/**
 * 在已有的图像上画圆圈
 */
public class CircleOnImg {

    public static void main(String[] args) {
        System.loadLibrary(Core.NATIVE_LIBRARY_NAME);

        Mat img = Imgcodecs.imread("./images/Lena.jpg");

        if (img.empty()) {
            System.out.println("图像路径为空");
            return;
        }

        Imgproc.circle(img,new Point(50,50),40,new Scalar(255,0,0),2);

        Imgproc.circle(img,new Point(50,100),80,new Scalar(0,255,0),5);

        new ShowImage(img);
    }
}

Mat 创建图像

/**
 * Mat 创建图像
 */
public class CreateImage {
    public static void main(String[] args) {
        System.loadLibrary(Core.NATIVE_LIBRARY_NAME);

        //创建一张红色的图片,矩阵类型为 CvType.CV_8UC3 ,Scalar(0, 0, 255) 设置颜色,这里是红色。
        Mat image = new Mat(100, 200, CvType.CV_8UC3, new Scalar(0, 0, 255));
        new ShowImage(image);
    }
}

生成对角矩阵

/**
 * 生成对角矩阵 Mat.eye
 */
public class CreateEyeMat {

    public static void main(String[] args) {
        System.loadLibrary(Core.NATIVE_LIBRARY_NAME);

        //生成 3*3的矩阵,类型为CvType.CV_8UC1
        Mat eyeMat = Mat.eye(3, 3, CvType.CV_8UC1);
        System.out.println("mat="+ eyeMat.dump());
    }
}

图像的角点检测

/**
 * 图像的角点检测
 */
public class DetectConers {

    public static void main(String[] args) {
        System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
        final int maxCorners = 50,blockSize =3;
        final double qualityLevel = 0.01,minDistance = 20.0,k=0.04;
        final boolean userHarrisDetector = false;
        MatOfPoint corners = new MatOfPoint();

        Mat src = Imgcodecs.imread("./images/2.jpg");
        if (src.empty()) {
            System.out.println("图片路径不正确");
            return ;
        }
        Mat dst = src.clone();
        Mat gray = new Mat();

        Imgproc.cvtColor(src,gray,Imgproc.COLOR_BGR2GRAY);//转成灰度图像

        //计算角点
//        image:8位或32位浮点型输入图像,单通道
//        corners:保存检测出的角点
//        maxCorners:角点数目最大值,如果实际检测的角点超过此值,则只返回前maxCorners个强角点
//        qualityLevel:角点的品质因子
//        minDistance:对于初选出的角点而言,如果在其周围minDistance范围内存在其他更强角点,则将此角点删除
//        mask:指定感兴趣区,如不需在整幅图上寻找角点,则用此参数指定ROI。也可以new Mat()来代替,这样就是没有mask.
//        blockSize:计算协方差矩阵时的窗口大小
//        useHarrisDetector:指示是否使用Harris角点检测,如不指定,则计算shi-tomasi角点
//        harrisK:Harris角点检测需要的k值
        Imgproc.goodFeaturesToTrack(gray,corners,maxCorners,qualityLevel,minDistance,new Mat(),blockSize,userHarrisDetector,k);
        Point[] pCorners = corners.toArray();
        for (int i = 0; i < pCorners.length; i++) {
            Imgproc.circle(dst,pCorners[i],2,new Scalar(255,255,0),1);
        }
        new ShowImage(dst);
    }
}

检测图像人脸眼睛

/**
 * 检测图像人脸眼睛
 */
public class DetectEye {

    private static Mat dobj(Mat src) {
        Mat dst = src.clone();

        //级联分类器类用于对象检测。
        CascadeClassifier objDetector = new CascadeClassifier("D://Program Files/opencv/sources/data/haarcascades/haarcascade_eye.xml");
        MatOfRect objDetections = new MatOfRect();
        objDetector.detectMultiScale(dst, objDetections);
        if (objDetections.toArray().length <= 0) {
            return src;
        }
        for (Rect rect : objDetections.toArray()) {
            Imgproc.rectangle(dst, new Point(rect.x, rect.y), new Point(rect.x + rect.width, rect.y + rect.height), new Scalar(0, 0, 255), 2);
        }
        return dst;
    }

    public static void main(String[] args) {

        System.loadLibrary(Core.NATIVE_LIBRARY_NAME);

        Mat src = Imgcodecs.imread("./images/2.jpg");
        if (src.empty()) {
            System.out.println("图片路径不正确");
            return;
        }
        Mat dst = dobj(src);
        new ShowImage(dst);
    }
}

图片的修减

/**
 * 图片的修减
 */
public class Trimming {
    public static void main(String[] args) {
        System.loadLibrary(Core.NATIVE_LIBRARY_NAME);

        Mat src = Imgcodecs.imread("./images/2.jpg");
        if (src.empty()) {
            System.out.println("图片路径不正确");
            return;
        }

        new ShowImage(src);
        
        Rect roi = new Rect(40, 40, src.width() - 40, src.height() - 40);

        Mat dst = new Mat(src, roi);

        new ShowImage(dst);
    }
}

图像的阈值图像生成

/**
 * 图像的阈值图像生成
 */
public class Threshold {

    public static void main(String[] args) {
        System.loadLibrary(Core.NATIVE_LIBRARY_NAME);

        //输入图像时将其转为灰度图
        Mat src = Imgcodecs.imread("./images/2.jpg", Imgcodecs.CV_LOAD_IMAGE_GRAYSCALE);

        if (src.empty()) {
            System.out.println("图片路径不正确");
            return;
        }

        Mat dst = new Mat();

        Imgproc.threshold(src, dst, 100, 200, Imgproc.THRESH_BINARY_INV);
        new ShowImage(dst);

        Imgproc.threshold(src, dst, 100, 200, Imgproc.THRESH_TRUNC);
        new ShowImage(dst);

        Imgproc.threshold(src, dst, 100, 200, Imgproc.THRESH_BINARY);
        new ShowImage(dst);

        Imgproc.threshold(src, dst, 100, 200, Imgproc.THRESH_TOZERO);
        new ShowImage(dst);

        Imgproc.threshold(src, dst, 100, 200, Imgproc.THRESH_TOZERO_INV);
        new ShowImage(dst);
    }
}

图片加上水印

/**
 * 图片加上水印
 */
public class TextOnImg {

    public static void main(String[] args) {
        System.loadLibrary(Core.NATIVE_LIBRARY_NAME);

        Mat src = Imgcodecs.imread("./images/2.jpg");

        if (src.empty()) {
            System.out.println("图片路径不正确");
            return;
        }

        Mat dst = src.clone();

        Point p = new Point(50, src.rows() / 2 - 50);

        Imgproc.putText(dst, "Hello World!", p, Core.FONT_HERSHEY_SIMPLEX, 0.8, new Scalar(50, 60, 80), 2);

        new ShowImage(dst);
    }
}

提取图像的RGB三原色分割图像

/**
 * 提取图像的RGB三原色分割图像
 */
public class Split {
    public static void main(String[] args) {
        System.loadLibrary(Core.NATIVE_LIBRARY_NAME);

        Mat src = Imgcodecs.imread("./images/2.jpg");

        if (src.empty()) {
            System.out.println("图片路径不正确");
            return;
        }

        List<Mat> dst = new ArrayList<>();
        Core.split(src,dst);

        new ShowImage(dst.get(0));
        new ShowImage(dst.get(1));
        new ShowImage(dst.get(2));
    }
}

图像索贝尔Sobel处理,边缘检测

/**
 * 图像索贝尔Sobel处理,边缘检测
 */
public class Sobel {

    public static void main(String[] args) {
        System.loadLibrary(Core.NATIVE_LIBRARY_NAME);

        Mat src = Imgcodecs.imread("./images/2.jpg");

        if (src.empty()) {
            System.out.println("图片路径不正确");
            return;
        }
        Mat dst = src.clone();

        Imgproc.Sobel(src,dst,-1, 0,1);
        new ShowImage(dst);
    }
}

图像的旋转

/**
 * 图像的旋转
 */
public class Rotate {

    public static void main(String[] args) {
        System.loadLibrary(Core.NATIVE_LIBRARY_NAME);

        Mat src = Imgcodecs.imread("./images/2.jpg");

        if (src.empty()) {
            System.out.println("图片路径不正确");
            return;
        }
        Mat dst = src.clone();

        Point center = new Point(src.width() / 2.0, src.height());
        //center : 原图像的旋转中心
        //angle : 旋转角的度。正值意味着逆时针旋转(坐标原点被假定为左上角)
        //scale : 各向同性的比例因子
        Mat affineTrans = Imgproc.getRotationMatrix2D(center, 33.0, 1.0);//计算一个二维旋转的仿射矩阵


        //src : 输入的图像
        //dst :  输出的图像
        //M : 变换矩阵。
        //dsize : 输出图象的大小
        Imgproc.warpAffine(src, dst, affineTrans, dst.size(), Imgproc.INTER_NEAREST);
        new ShowImage(dst);

        affineTrans = Imgproc.getRotationMatrix2D(center, 110, 1.1);
        Imgproc.warpAffine(src,dst,affineTrans,dst.size(),Imgproc.INTER_NEAREST);
        new ShowImage(dst);
    }
}

修改图像大小

/**
 * 修改图像大小
 */
public class Resize {

    public static void main(String[] args) {
        System.loadLibrary(Core.NATIVE_LIBRARY_NAME);

        Mat src = Imgcodecs.imread("./images/1.jpg");

        if (src.empty()) {
            System.out.println("图片路径不正确");
            return;
        }

        Mat dst = src.clone();

        float scale = 0.5f;
        float width = src.width();
        int height = src.height();

        Imgproc.resize(src,dst,new Size(width*scale,height*scale));
        new ShowImage(dst);

        scale = 1.5f;
        Imgproc.resize(src,dst,new Size(width*scale,height*scale));
        new ShowImage(dst);

        Imgproc.resize(src,dst,new Size(400,400));
        new ShowImage(dst);
    }
}

图像透视视角转换

/**
 * 图像透视视角转换
 */
public class Perspective {

    public static void main(String[] args) {
        System.loadLibrary(Core.NATIVE_LIBRARY_NAME);

        Mat src = Imgcodecs.imread("./images/2.jpg");

        if (src.empty()) {
            System.out.println("图片路径不正确");
            return;
        }

        int xMargin,yMargin;
        int x0 = src.cols() / 4;
        int x1 = (src.cols()/4)*3;
        int y0 = src.cols();
        int y1 = (src.cols()/4)*3;
        Mat dst = new Mat();

        List<Point> listSrcs = Arrays.asList(new Point(x0, y0), new Point(x0, y1),new Point(x1,y1), new Point(x1, y0));
        Mat srcPoints = Converters.vector_Point_to_Mat(listSrcs, CvType.CV_32F);

        xMargin = src.cols()/10;
        yMargin = src.rows()/10;
        List<Point> listDsts = Arrays.asList(new Point(x0 + xMargin, y0 + yMargin), listSrcs.get(1), listSrcs.get(2), new Point(x1, y0));
        Mat dstPoints = Converters.vector_Point_to_Mat(listDsts, CvType.CV_32F);

        Mat perspectiveMmat = Imgproc.getPerspectiveTransform(srcPoints, dstPoints);
        Imgproc.warpPerspective(src,dst,perspectiveMmat,src.size(),Imgproc.INTER_LINEAR);
        new ShowImage(dst);

        xMargin=src.cols()/8;
        yMargin=src.cols()/8;
        listDsts.set(0, listSrcs.get(0));
        listDsts.set(1, listSrcs.get(1));
        listDsts.set(2, new Point(x1-xMargin,y1-yMargin));
        listDsts.set(3, new Point(x1-xMargin,y0-yMargin));
        dstPoints=Converters.vector_Point_to_Mat(listDsts,CvType.CV_32F);

        perspectiveMmat=Imgproc.getPerspectiveTransform(srcPoints, dstPoints);
        Imgproc.warpPerspective(src, dst, perspectiveMmat, src.size(),Imgproc.INTER_LINEAR);
        new ShowImage(dst);

        xMargin=src.cols()/6;
        yMargin=src.cols()/6;
        listDsts.set(0, new Point(x0+xMargin,y0+yMargin));
        listDsts.set(1, listSrcs.get(1));
        listDsts.set(2, new Point(x1-xMargin,y1-yMargin));
        listDsts.set(3, listSrcs.get(3));
        dstPoints=Converters.vector_Point_to_Mat(listDsts,CvType.CV_32F);

        perspectiveMmat=Imgproc.getPerspectiveTransform(srcPoints, dstPoints);
        Imgproc.warpPerspective(src, dst, perspectiveMmat, src.size(),Imgproc.INTER_LINEAR);

        new ShowImage(dst);
    }
}

模块化

/**
 * 模块化
 */
public class Mozaic {

    public static void main(String[] args) {
        System.loadLibrary(Core.NATIVE_LIBRARY_NAME);

        Mat src = Imgcodecs.imread("./images/2.jpg");

        if (src.empty()) {
            System.out.println("图片路径不正确");
            return;
        }

        Mat dst = new Mat();

        Imgproc.resize(src,dst,new Size(),0.2,0.2,Imgproc.INTER_NEAREST);
        Imgproc.resize(src,dst,new Size(),1/0.2,1/0.2,Imgproc.INTER_NEAREST);

        new ShowImage(dst);
    }
}

图像的平均模糊

/**
 * 图像的平均模糊
 */
public class MediaBlur {
    public static void main(String[] args) {
        System.loadLibrary(Core.NATIVE_LIBRARY_NAME);

        Mat src = Imgcodecs.imread("./images/2.jpg");

        if (src.empty()) {
            System.out.println("图片路径不正确");
            return;
        }

        Mat dst = src.clone();

        //图像模糊化处理
        Imgproc.medianBlur(src,dst,11);
        new ShowImage(dst);

        Imgproc.medianBlur(src,dst,33);
        new ShowImage(dst);
    }
}

在图形图像上画线

/**
 * 在图形图像上画线
 */
public class LinesOnImage {

    public static void main(String[] args) {
        System.loadLibrary(Core.NATIVE_LIBRARY_NAME);

        Mat img = Imgcodecs.imread("./images/2.jpg");

        if (img.empty()) {
            System.out.println("图片路径不正确");
            return;
        }

        //画线,在img 图像中,从(50,50)坐标到(200,50),颜色为红色
        Imgproc.line(img, new Point(50, 50), new Point(200, 50), new Scalar(255, 0, 0));
        Imgproc.line(img, new Point(50, 50), new Point(50, 200), new Scalar(0, 255, 0));
        Imgproc.line(img, new Point(50, 200), new Point(200, 200), new Scalar(180, 180, 180));
        Imgproc.line(img, new Point(200, 200), new Point(200, 50), new Scalar(0, 0, 255));
        new ShowImage(img);
    }
}

图形图像的拉普拉斯平滑

/**
 * 图形图像的拉普拉斯平滑
 */
public class Laplacian {

    public static void main(String[] args) {
        System.loadLibrary(Core.NATIVE_LIBRARY_NAME);

        Mat src = Imgcodecs.imread("./images/psb.jpg");

        if (src.empty()) {
            System.out.println("图片路径不正确");
            return;
        }

        Mat dst = src.clone();

        Imgproc.Laplacian(src,dst,0);
        Imgcodecs.imwrite("./result/psb.jpg",dst);
    }
}

图形图像的高斯模糊

/**
 * 图形图像的高斯模糊
 */
public class GaussianBlur {

    public static void main(String[] args) {
        System.loadLibrary(Core.NATIVE_LIBRARY_NAME);

        Mat src = Imgcodecs.imread("./images/2.jpg");

        if (src.empty()) {
            System.out.println("图片路径不正确");
            return;
        }
        Mat dst = src.clone();

        //图像模糊化处理
        Imgproc.GaussianBlur(src,dst,new Size(13,13),10,10);
        new ShowImage(dst);

        Imgproc.GaussianBlur(src,dst,new Size(31,5),80,3);
        new ShowImage(dst);
    }
}

图形图像的翻转

/**
 * 图形图像的翻转
 */
public class Flip {
    public static void main(String[] args) {
        System.loadLibrary(Core.NATIVE_LIBRARY_NAME);

        Mat src = Imgcodecs.imread("./images/2.jpg");

        if (src.empty()) {
            System.out.println("图片路径不正确");
            return;
        }
        Mat dst = src.clone();

        //倒过来
        Core.flip(src,dst,0);

        //翻转
        Core.flip(src,dst,1);

        //倒过来加翻转
        Core.flip(src,dst,-1);
        new ShowImage(dst);
    }
}

图像的侵蚀

/**
 * 图像的侵蚀
 */
public class Erode {

    public static void main(String[] args) {
        System.loadLibrary(Core.NATIVE_LIBRARY_NAME);

        Mat src = Imgcodecs.imread("./images/2.jpg");

        if (src.empty()) {
            System.out.println("图片路径不正确");
            return;
        }

        Mat dst = new Mat();

        Imgproc.erode(src,dst,new Mat());
        new ShowImage(dst);
    }
}

图形图像的均衡

/**
 * 图形图像的均衡
 */
public class Equalize {

    public static void main(String[] args) {
        System.loadLibrary(Core.NATIVE_LIBRARY_NAME);

        Mat src = Imgcodecs.imread("./images/2.jpg",Imgcodecs.CV_LOAD_IMAGE_GRAYSCALE);

        if (src.empty()) {
            System.out.println("图片路径不正确");
            return;
        }
        Mat dst = new Mat();
        //辉度平滑
        Imgproc.equalizeHist(src,dst);
        new ShowImage(src);
        new ShowImage(dst);
    }
}

消除图像上的目标

/**
 * 消除图像上的目标,(有问题,但是大概就这样)
 */
public class EliminateObject {

    public static void main(String[] args) {
        System.loadLibrary(Core.NATIVE_LIBRARY_NAME);

        Mat src = Imgcodecs.imread("./images/2.jpg");

        Mat mask = makeMask(src);

        if (src.empty()) {
            System.out.println("图片路径不正确");
            return;
        }

        Mat dst = src.clone();
        Photo.inpaint(src, mask, dst, 1, Photo.INPAINT_TELEA);

        new ShowImage(dst);
    }

    private static Mat makeMask(Mat src) {
        Size sz = new Size(src.width(), src.height());
        Mat mask = Mat.zeros(sz, CvType.CV_8U);
        for (int y = src.height() / 4; y < src.height() * 3 / 4; y++) {
            for (int x = src.height() / 4; x < src.width(); x++) {
                mask.put(y, x, 255);
            }
        }
        return mask;
    }
}

图像的膨胀

/**
 * 图像的膨胀
 */
public class Dilate {

    public static void main(String[] args) {
        System.loadLibrary(Core.NATIVE_LIBRARY_NAME);

        Mat src = Imgcodecs.imread("./images/2.jpg");

        if (src.empty()) {
            System.out.println("图片路径不正确");
            return;
        }

        Mat dst = new Mat();

        Imgproc.dilate(src,dst,new Mat());
        new ShowImage(dst);
    }
}

图像检测人脸图像

/**
 * 图像检测人脸图像
 */
public class DetectFace {

    public static void main(String[] args) {
        System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
        Mat src = Imgcodecs.imread("./images/psb4.jpg");
        if (src.empty()) {
            System.out.println("图片路径不正确");
            return ;
        }
        Mat dst = dobj(src);
        new ShowImage(dst);
    }

    private static Mat dobj(Mat src){
        Mat dst = src.clone();

        CascadeClassifier objDetector = new CascadeClassifier("D://Program Files/opencv/sources/data/lbpcascades/lbpcascade_frontalface.xml");

        MatOfRect objDetections = new MatOfRect();

        objDetector.detectMultiScale(dst,objDetections);

        if (objDetections.toArray().length <= 0) {
            return src;
        }

        for (Rect rect : objDetections.toArray()) {
            Imgproc.rectangle(dst,new Point(rect.x,rect.y),new Point(rect.x+rect.width,rect.y+rect.width),new Scalar(0,0,255),2);
        }
        return dst;
    }
}