当前位置:   article > 正文

Java整合OpenCV实现全景图切割合并

java整合opencv

一共分为三步实现:

1,windows安装OpenCV
2,springboot整合OpenCV
3,实现全景图切割合并

windows安装OpenCV

下载OpenCV
官网下载地址:https://opencv.org/releases/
这里下载的是windows版本的,如需下载安装lunix版本,请移步:
Lunix安装/编译/部署OpenCV4.5.2生成so文件
在这里插入图片描述
下载完成后得到exe程序安装包
在这里插入图片描述
运行exe安装包后得到
在这里插入图片描述
到这一步其实就已经完成了,特别简单!!!
但是我当时在网上看的时候好多都是配置环境变量,但我并没有配,照样可以用,不知道是因为什么

springboot整合OpenCV

首先需要初始化一个springboot项目
在这里插入图片描述
然后找到我们刚才安装的OpenCV目录
opencv\build\java\x64\opencv_java452.dll
opencv\build\java\opencv-452.jar
在这里插入图片描述
把jar包和dll放入lib文件夹下
在这里插入图片描述
在项目中添加OpenCV依赖
在这里插入图片描述
在这里插入图片描述
配置pom.xml依赖(不配置这一步正常启动可以,但是打jar就不可以了)
在这里插入图片描述

 <!-- OpenCV -->
        <dependency>
            <groupId>org</groupId>
            <artifactId>opencv</artifactId>
            <version>4.5.2</version>
            <scope>system</scope>
            <systemPath>${pom.basedir}\src\main\resources\lib\opencv-452.jar</systemPath>
        </dependency>


		<configuration>
            <includeSystemScope>true</includeSystemScope>
        </configuration>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13

到这一步算是已经整合完毕,也是挺简单的,下面就是具体全景图切割合并的代码

实现全景图切割合并

ConverUtil.java

import org.opencv.core.*;
import org.opencv.imgcodecs.Imgcodecs;
import org.opencv.imgproc.Imgproc;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.awt.image.DataBufferByte;
import java.io.*;
import java.net.URL;

public class ConverUtil {

    /**
     * 读取本地图片文件
     *
     * @param filePath
     * @return Mat
     */
    public static final Mat matRead(String filePath) {
        //return Highgui.imread("/home/night/webvr/vr.jpg");
//      return Imgcodecs.imread("/home/night/webvr/vr.jpg");
        return Imgcodecs.imread(filePath);
    }

    /**
     * 读取网络图片
     *
     * @param urlStr
     * @return
     */
    public static final BufferedImage bufferReadUrl(String urlStr) {
        BufferedImage image = null;
        try {
            URL url = new URL(urlStr);
            image = ImageIO.read(url);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return image;
    }


    /**
     * 图片文件保存
     *
     * @param fileName
     * @param mat
     */
    public static final void matSave(String fileName, Mat mat) {
        //Highgui.imwrite(fileName, mat);
        //Imgcodecs.imwrite(fileName, mat);
        BufferedImage buff = matToBuffer(".jpg", mat);
        bufferSave(fileName, buff);
    }

    /**
     * 读取图片文件
     *
     * @param filePath
     * @return
     */
    public static final BufferedImage bufferRead(String filePath) {
        try {
            return ImageIO.read(new File(filePath));
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 图片文件保存
     *
     * @param fileName
     * @param buff
     */
    public static final void bufferSave(String fileName, BufferedImage buff) {
        try {
            ImageIO.write(buff, "JPEG", new File(fileName));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static final Mat matResize(Mat mat, int width, int height) {
        Mat target = new Mat(width, height, mat.type());
        //Imgproc.resize(src, dst, dsize);
        Imgproc.resize(mat, target, new Size(width, height), 0, 0, Imgproc.INTER_LINEAR);
        return target;
    }

    /**
     * Mat转换为BufferedImage,已测,可用
     *
     * @param fileExt
     * @param mat
     * @return
     */
    public static final BufferedImage matToBuffer(String fileExt, Mat mat) {
        MatOfByte mob = new MatOfByte();
        Imgcodecs.imencode(fileExt, mat, mob);
        // convert the "matrix of bytes" into a byte array
        byte[] byteArray = mob.toArray();
        BufferedImage bufImage = null;
        try {
            InputStream in = new ByteArrayInputStream(byteArray);
            bufImage = ImageIO.read(in);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return bufImage;
    }


    /**
     * BufferedImage转换为Mat,已测,可用
     *
     * @param bufferedImage
     * @param imgType
     * @return
     */
    public static Mat bufferToMat(BufferedImage bufferedImage, int imgType) {
        final int matType = CvType.CV_8UC3;

        if (bufferedImage == null) {
            throw new IllegalArgumentException("bufferToMat-> BufferedImage == null");
        }

        if (bufferedImage.getType() != imgType) {
            BufferedImage image = new BufferedImage(bufferedImage.getWidth(), bufferedImage.getHeight(), imgType);
            Graphics2D g = image.createGraphics();
            try {
                g.setComposite(AlphaComposite.Src);
                g.drawImage(bufferedImage, 0, 0, null);
            } finally {
                g.dispose();
            }
        }

        byte[] pixels = ((DataBufferByte) bufferedImage.getRaster().getDataBuffer()).getData();
        Mat mat = Mat.eye(bufferedImage.getHeight(), bufferedImage.getWidth(), matType);
        mat.put(0, 0, pixels);
        return mat;
    }

    /**
     * 将Mat图片进行Base64编码
     * base64编码后的字符串中经常包含“+”号,在C#环境中发送给服务器后,服务器把“+”存成了“ ”空格,而在Java环境下,“+”号依然是加号
     * 所以在java环境中,解码之前,需要先把编码后的字符串中的“ ”替换成“+”号
     * <code>String des = des.replaceAll("\\+", "%2B");</code>
     *
     * @param mat
     * @return
     */
/*
    public static final String matEncoder(Mat mat) {
        BufferedImage buff = matToBuffer(".jpg", mat);
        ByteArrayOutputStream byteout = new ByteArrayOutputStream();
        try {
            ImageIO.write(buff, "JPEG", byteout);
        } catch (IOException e) {
            e.printStackTrace();
        }
        BASE64Encoder encoder = new BASE64Encoder();
        return encoder.encode(byteout.toByteArray());
    }
*/

    /**
     * 图像整体向左旋转90度
     *
     * @param src Mat
     * @return 旋转后的Mat
     */
    public static Mat rotateRight(Mat src, int flipCode) {
        Mat tmp = new Mat();
        // 此函数是转置、(即将图像逆时针旋转90度,然后再关于x轴对称)
        Core.transpose(src, tmp);
        Mat result = new Mat();
        // flipCode = 0 绕x轴旋转180, 也就是关于x轴对称
        // flipCode = 1 绕y轴旋转180, 也就是关于y轴对称
        // flipCode = -1 此函数关于原点对称
        Core.flip(tmp, result, flipCode);
        return result;
    }

}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141
  • 142
  • 143
  • 144
  • 145
  • 146
  • 147
  • 148
  • 149
  • 150
  • 151
  • 152
  • 153
  • 154
  • 155
  • 156
  • 157
  • 158
  • 159
  • 160
  • 161
  • 162
  • 163
  • 164
  • 165
  • 166
  • 167
  • 168
  • 169
  • 170
  • 171
  • 172
  • 173
  • 174
  • 175
  • 176
  • 177
  • 178
  • 179
  • 180
  • 181
  • 182
  • 183
  • 184
  • 185
  • 186
  • 187
  • 188
  • 189

OpenCVUtil.java

import org.opencv.core.Core;
import org.opencv.core.CvType;
import org.opencv.core.Mat;
import org.opencv.core.Scalar;
import org.opencv.imgproc.Imgproc;

import java.awt.image.BufferedImage;
import java.io.File;

public class OpenCVUtil {


    static {
        String opencvDllName = "E:/Dowin_workspace/tjcloud-designer/tjcloud-designer-web/src/main/resources/lib/opencv_java452.dll";               //Linux 环境
        //String opencvSoName = "/usr/local/share/java/opencv4/libopencv_java452.so";               //Linux中so文件地址
        System.load(opencvDllName);

      
    }

    double[][] imageTransform =
            {
                    {0, 0},
                    {Math.PI / 2, 0},
                    {Math.PI, 0},
                    {-Math.PI / 2, 0},
                    {0, -Math.PI / 2},
                    {0, Math.PI / 2}
            };


    public static void main(String[] args) {
        OpenCVUtil util = new OpenCVUtil();
        BufferedImage buff = ConverUtil.bufferRead("C:\\Users\\XQD\\Desktop\\kt.jpg");
        //全景图切割
        Mat[] cube = util.shear(buff, 1024, 1024);
        //预览图合成
        Mat preview = util.mergeImage(cube, 512);
    }

    /**
     * 全景图切割,返回6张图
     * ossPanoPath  oss存储地址
     * panoImgPath  本地存储地址
     */
    public Mat[] shear(BufferedImage buff, int targetWidth, int targetHeight) {
        Mat mat = ConverUtil.bufferToMat(buff, buff.getType());
        Mat[] cube = new Mat[6];
        for (int i = 0; i < 6; i++) {
            cube[i] = sideCubeMapImage(mat, i, targetWidth, targetHeight);
        }
        return cube;
    }

    /**
     * @Description: 全景图切割,单面处理
     * @Param: [source, sideId, sideWidth, sideHeight]
     * @return: org.opencv.core.Mat
     * @Author: XQD
     * @Date:2021/10/22 13:51
     */
    private Mat sideCubeMapImage(Mat source, final int sideId, final int sideWidth, final int sideHeight) {
        Mat result = new Mat(sideWidth, sideHeight, source.type());
        System.out.println("==========handle " + sideId + " start ===========");
        // 获取图片的行列数量
        float sourceWidth = source.cols();
        float sourceHeight = source.rows();
        //分配图的x,y轴
        Mat mapx = new Mat(sideHeight, sideWidth, CvType.CV_32F);
        Mat mapy = new Mat(sideHeight, sideWidth, CvType.CV_32F);

        //计算相邻ak和相反an的三角形张成球体中心
        final double an = Math.sin(Math.PI / 4);
        final double ak = Math.cos(Math.PI / 4);

        double ftu = imageTransform[sideId][0];
        double ftv = imageTransform[sideId][1];

        //对于每个图像计算相应的源坐标
        for (int y = 0; y < sideHeight; y++) {
            for (int x = 0; x < sideWidth; x++) {

                //将坐标映射在平面上
                float nx = (float) y / (float) sideHeight - 0.5f;
                float ny = (float) x / (float) sideWidth - 0.5f;

                nx *= 2;
                ny *= 2;

                // Map [-1, 1] plane coord to [-an, an]
                // thats the coordinates in respect to a unit sphere
                // that contains our box.
                nx *= an;
                ny *= an;

                double u, v;

                // Project from plane to sphere surface.
                if (ftv == 0) {
                    // Center faces 中心面
                    u = Math.atan2(nx, ak);
                    v = Math.atan2(ny * Math.cos(u), ak);
                    u += ftu;
                } else if (ftv > 0) {
                    // Bottom face 低面
                    double d = Math.sqrt(nx * nx + ny * ny);
                    v = Math.PI / 2 - Math.atan2(d, ak);
                    u = Math.atan2(ny, nx);
                } else {
                    // Top face 顶面
                    //cout << "aaa";
                    double d = Math.sqrt(nx * nx + ny * ny);
                    v = -Math.PI / 2 + Math.atan2(d, ak);
                    u = Math.atan2(-ny, nx);
                }

                // Map from angular coordinates to [-1, 1], respectively.
                u = u / (Math.PI);
                v = v / (Math.PI / 2);

                // Warp around, if our coordinates are out of bounds.
                while (v < -1) {
                    v += 2;
                    u += 1;
                }
                while (v > 1) {
                    v -= 2;
                    u += 1;
                }

                while (u < -1) {
                    u += 2;
                }
                while (u > 1) {
                    u -= 2;
                }

                // Map from [-1, 1] to in texture space
                u = u / 2.0f + 0.5f;
                v = v / 2.0f + 0.5f;

                u = u * (sourceWidth - 1);
                v = v * (sourceHeight - 1);

                mapx.put(x, y, u);
                mapy.put(x, y, v);
            }
        }

        // Do actual  using OpenCV's remap
        Imgproc.remap(source, result, mapx, mapy, Imgproc.INTER_LINEAR, Core.BORDER_CONSTANT, new Scalar(0, 0, 0));
		// 均值模糊(降噪)
        Mat image = source.clone();
        Imgproc.blur(source, image, new Size(3,3),new Point(-1,-1));
        if (sideId == 0) {
            ConverUtil.matSave("E:\\data\\kct.tiles\\a.jpg", result);
        } else if (sideId == 1) {
            ConverUtil.matSave("E:\\data\\kct.tiles\\b.jpg", result);
        } else if (sideId == 2) {
            ConverUtil.matSave("E:\\data\\kct.tiles\\c.jpg", result);
        } else if (sideId == 3) {
            ConverUtil.matSave("E:\\data\\kct.tiles\\d.jpg", result);
        } else if (sideId == 4) {
        	//旋转角度
            result = ConverUtil.rotateRight(result, 1);
            ConverUtil.matSave("E:\\data\\kct.tiles\\e.jpg", result);
        } else if (sideId == 5) {
        	//旋转角度
            result = ConverUtil.rotateRight(result, 0);
            ConverUtil.matSave("E:\\data\\kct.tiles\\f.jpg", result);
        }
        System.out.println("==========handle " + sideId + " over ===========");
        return result;
    }

    /**
     * 全景预览图合成
     */
    public Mat mergeImage(Mat[] cube, int width) {
        Mat mat = new Mat(width * cube.length, width, cube[0].type());
        for (int i = 0; i < cube.length; i++) {
            Mat side = ConverUtil.matResize(cube[i], 512, 512);
            mat.put(i * 512, 0, getByte(side));
        }
        ConverUtil.matSave("E:\\data\\kct.tiles\\preview.jpg", mat);
        return mat;
    }

    public byte[] getByte(Mat mat) {
        int width = mat.cols();
        int height = mat.rows();
        int dims = mat.channels();
        byte[] rgbdata = new byte[width * height * dims];
        mat.get(0, 0, rgbdata);
        return rgbdata;
    }
}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141
  • 142
  • 143
  • 144
  • 145
  • 146
  • 147
  • 148
  • 149
  • 150
  • 151
  • 152
  • 153
  • 154
  • 155
  • 156
  • 157
  • 158
  • 159
  • 160
  • 161
  • 162
  • 163
  • 164
  • 165
  • 166
  • 167
  • 168
  • 169
  • 170
  • 171
  • 172
  • 173
  • 174
  • 175
  • 176
  • 177
  • 178
  • 179
  • 180
  • 181
  • 182
  • 183
  • 184
  • 185
  • 186
  • 187
  • 188
  • 189
  • 190
  • 191
  • 192
  • 193
  • 194
  • 195
  • 196
  • 197
  • 198

效果图:
在这里插入图片描述
参考:https://blog.csdn.net/licheng989/article/details/79614833

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/很楠不爱3/article/detail/623273
推荐阅读
相关标签
  

闽ICP备14008679号