opencv4 java投影

          垂直投影

  private org.opencv.core.Mat projectionVerticality(org.opencv.core.Mat mat) {
        org.opencv.core.Mat projectionMat = mat.clone();//曲线救国,获取同样一个mat       
        projectionMat.setTo(new org.opencv.core.Scalar(255));//然后再把颜色换成白色
        Double[] dotList = new Double[mat.cols()];//创建一个list用于存储每一列的黑点数量      
        System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
        System.out.println(mat.dump());
        int col = mat.cols();
        int row = mat.rows();
        for (int x = 0; x < col; x++) {
            dotList[x] = 0.0;
            for (int y = 0; y < row; y++) {
                double binData = mat.get(y, x)[0];
                if (binData == 0) {//黑色

                    dotList[x]++;
                }
            }
        }
        //然后生成投影图
        for (int x = 0; x < mat.cols(); x++) {
            for (int y = 0; y < mat.rows(); y++) {
                if (x == 147) {
                    System.out.println("下一行将会出现错误");
                }
                if (y < dotList[x]) {
                    projectionMat.put(y, x, 0);
                    System.out.println(x + "列" + y + "行");
                }

            }

        }

        return projectionMat;
    }

      水平投影

 private org.opencv.core.Mat projectionHorizontal(org.opencv.core.Mat mat) {
        org.opencv.core.Mat projectionMat = mat.clone();//曲线救国,获取同样一个mat       
        projectionMat.setTo(new org.opencv.core.Scalar(255));//然后再把颜色换成白色
        Double[] dotList = new Double[mat.rows()];//创建一个list用于存储每一列的黑点数量      
        System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
        System.out.println(mat.dump());
        int col = mat.cols();
        int row = mat.rows();
        for (int y = 0; y < row; y++) {
            dotList[y] = 0.0;
            for (int x = 0; x < col; x++) {
                double binData = mat.get(y, x)[0];
                if (binData == 0) {//黑色                   
                    dotList[y]++;
                }
            }
        }
        //然后生成投影图
        for (int y = 0; y < mat.rows(); y++) {
            for (int x = 0; x < mat.cols(); x++) {
                try {
                    if (x < dotList[y]) {
                        projectionMat.put(y, x, 0);
                    }
                } catch (Exception e) {
                    System.out.println("正在处理第:" + y + "行,第" + x + "列");
                }
            }
        }
        return projectionMat;
    }

猜你喜欢

转载自www.cnblogs.com/jnhs/p/11329414.html
今日推荐