Panoramic overview
Image projection occurs whenever a plane image is mapped to a curved surface, and vice versa, which is particularly common in panoramic photography. For example, the sphere of the earth can be mapped to a flat piece of paper. Since the entire field of view around us can be considered as the surface of the sphere (for all observation angles), we need a method that can project the sphere into the 2-D plane for photo printing.
Small perspectives are relatively easy to deform and project onto flat paper. However, some deformation is inevitable when trying to map a spherical image to a plane. Therefore, each type of projection attempts to avoid one type of distortion at the expense of other types of distortions. As the field of view increases, the viewing arc becomes more curved, so that the differences between the types of panoramic projections become more significant. When to use which projection depends largely on each projection application. Here, we focus on several of the most commonly used ones.
Preface
Due to the project needs, I made my own demo and learned a lot from it. So I shared it. I hope that those who have this need will avoid detours. There are many online tutorials on how to install opencv. I will not explain in detail here. I installed opencv-3.3.0
As shown in the above figure, find the corresponding jar package. Here we will talk about how to import this jar into the Maven repository
mvn install:install-file -Dfile=D:/opencv-3.0.0/opencv/build/java/opencv-300.jar -DgroupId=com.suibian -DartifactId=opencv-300 -Dversion=3.3.0 -Dpackaging=jar -DgeneratePom=true -DcreateChecksum=true
<groupId>com.suibian</groupId> <artifactId>opencv-300</artifactId> <version>3.3.0</version>
OK, so Java can operate opencv through this jar. Let's take a look. Of course, you can think like this, which means you are very simple. Let's take a look at the comparison between the path in include in opencv and the package in jar
Haha, there is no key stitching for panoramic maps, and Java-oriented 2015 has not been updated since. There is no key class for synthesizing panoramic maps (fools) as shown in the following figure, but C++ provides me with
So what to do? How to use java to call C++ code. We know that when dealing with the operating system, C/C++ is undoubtedly more suitable than Java. Most of the applications of opencv graphics processing are developed in C++. Everyone is familiar with native keyword, but we want to click in and see how it is implemented, but we can't click in because it is not written in java. It may be C/C++. Native corresponds to XXX.dll files in the bin directory under jre in java. So we can package the C++ code we want to use into a dll format file and put it in the bin directory. Of course, this involves the parameters and return values of the method. Without saying much, install visual studio 2017, install tutorials and many online tutorials
Click File --> New --> Project ---> Windows Desktop --> Dynamic Link Library (Dll), so that the project will be successfully created. Next, add the code
This is the C++ code for panoramic image synthesis
#include "stdafx.h"#include <iostream>#include <opencv2/core/core.hpp>#include <opencv2/highgui/highgui.hpp>#include <opencv2/imgproc/imgproc.hpp>#include <opencv2/opencv.hpp>#include <opencv2/opencv.hpp>using namespace std;using namespace cv;bool try_use_gpu = false;vector<Mat> imgs;string result_name = "D:/result1.jpg";int _tmain(int argc, char * argv[]){ Mat img1 = imread("D:/quanjingtu/hh/1.jpg"); Mat img2 = imread("D:/quanjingtu/hh/2.jpg"); Mat img3 = imread("D:/quanjingtu/hh/3.jpg"); Mat img4 = imread("D:/quanjingtu/hh/4.jpg"); Mat img5 = imread("D:/quanjingtu/hh/5.jpg"); Mat img6 = imread("D:/quanjingtu/hh/6.jpg"); Mat img7 = imread("D:/quanjingtu/hh/7.jpg"); Mat img8 = imread("D:/quanjingtu/hh/8.jpg"); Mat img9 = imread("D:/quanjingtu/hh/9.jpg"); Mat img10 = imread("D:/quanjingtu/hh/10.jpg"); //Mat img6 = imread("6.jpg"); if (img1.empty() || img2.empty()) { cout << "Can't read image" << endl; return -1; } imgs.push_back(img1); imgs.push_back(img2); imgs.push_back(img3); imgs.push_back(img4); imgs.push_back(img5); imgs.push_back(img6); imgs.push_back(img7); imgs.push_back(img8); imgs.push_back(img9); imgs.push_back(img10); //imgs.push_back(img6); Stitcher stitcher = Stitcher::createDefault(try_use_gpu); //Use the stitch function for splicing Mat pano; Stitcher::Status status = stitcher.stitch(imgs, pano); imwrite(result_name, pano); Mat pano2 = pano.clone(); // Display the source image and the result image//imshow("panomic image", pano); if (waitKey() == 27) return 0; //imwrite(result_name, pano);}So how does java interact with C++? Through jni technology in java
Let's understand JNI in java
Java Native Interface (JNI for short), Java is a cross-platform language, and sometimes local code needs to be called. Sun provides a JNI interface, which calls each other with the operating system local code. The following figure shows the calling principle of native in Java
The meridians are all clear, so let's start
public class OpenCVUtil { static { //The system.load here is used to load the dynamic link library generated by C++. In fact, you can decide that it is not necessarily static. System.loadLibrary("OpenCVUtil"); } public static native String changeArrValue(String str); public static void main(String[] args) throws UnsupportedEncodingException { String base="D:/quanjingtu/gg"+"/"; int length=5; String url=""; for (int i=1;i<=length;i++){ if (i==1){ url=url+base+i+".jpg"; }else { url=url+","+base+i+".jpg"; } } //System.out.println(url); String temp =new String(changeArrValue(url).getBytes(),"GBK"); System.out.println(temp); ; }}Define native method and compile the java file into .class file
Enter the cmd command window, open the corresponding compiled class file directory and execute the javah command
Generate the corresponding com_lianxi_securitytest_opencv_OpenCVUtil.h file
#include <jni.h>/* Header for class com_lianxi_securitytest_opencv_OpenCVUtil */#ifndef _Included_com_lianxi_securitytest_opencv_OpenCVUtil#define _Included_com_lianxi_securitytest_opencv_OpenCVUtil#ifdef __cplusplusextern "C" {#endif/* * Class: com_lianxi_securitytest_opencv_OpenCVUtil * Method: changeArrValue * Signature: (Ljava/lang/String;)Ljava/lang/String; */JNIEXPORT jstring JNICALL Java_com_lianxi_securitytest_opencv_OpenCVUtil_changeArrValue (JNIEnv *, jclass, jstring);#ifdef __cplusplus}#endif#endifOnly the native method is processed, copy the file to the location of the vs 2017 project
#include "stdafx.h"#include "com_lianxi_securitytest_opencv_OpenCVUtil.h"#include<iostream>#include <opencv2/core/core.hpp>#include <opencv2/highgui/highgui.hpp>#include <opencv2/imgproc/imgproc.hpp>#include <opencv2/opencv.hpp>#include <vector>#include <string>using namespace std;using namespace cv;bool try_use_gpu = false;vector<Mat> imgs;string result_name = "D:/result.jpg";JNIEXPORT jstring JNICALL Java_com_lianxi_securitytest_opencv_OpenCVUtil_changeArrValue(JNIEnv *env, jclass obj, jstring prompt) { //This is the received string, multiple paths const char* str;//.......................................................//imgs.push_back(img6); Stitcher stitcher = Stitcher::createDefault(try_use_gpu); // Use the stitch function to splice Mat pano; Stitcher::Status status = stitcher.stitch(imgs, pano); imwrite(result_name, pano); Mat pano2 = pano.clone(); // Display the source image and the result image//imshow("panogue image", pano); if (waitKey() == 27) if (status != Stitcher::OK) { return env->NewStringUTF("picture failure!!!"); } //return env->NewStringUTF(result_name.c_str); string newsstr = "picture success!!!URL=" + result_name; return env->NewStringUTF(const_cast<char*>(newstr.c_str()));}The above is the corresponding C++ code
Click Generate --->Regenerate the solution to generate the corresponding dll file, and then put it in the JDK jre/bin directory
Run the java program, the result is as follows
Take a look at the synthetic panoramic view
Summarize
The above is the entire content of this article. I hope that the content of this article has certain reference value for everyone's study or work. If you have any questions, you can leave a message to communicate. Thank you for your support to Wulin.com.