parent
ac9a62c796
commit
7557e80fa0
After Width: | Height: | Size: 502 KiB |
After Width: | Height: | Size: 64 KiB |
After Width: | Height: | Size: 121 KiB |
After Width: | Height: | Size: 130 KiB |
@ -0,0 +1,21 @@
|
||||
package base;
|
||||
|
||||
import org.opencv.core.Mat;
|
||||
import org.opencv.highgui.HighGui;
|
||||
|
||||
import java.net.URL;
|
||||
|
||||
public class DllLoad {
|
||||
static {
|
||||
URL systemResource = ClassLoader.getSystemResource("lib/x64/opencv_java460.dll");
|
||||
System.load(systemResource.getPath());
|
||||
//System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
|
||||
//注意程序运行的时候需要在VM option添加该行 指明opencv的dll文件所在路径
|
||||
//-Djava.library.path=$PROJECT_DIR$\opencv\x64
|
||||
}
|
||||
|
||||
public static void show(Mat mat) {
|
||||
HighGui.imshow("展示", mat);
|
||||
HighGui.waitKey();
|
||||
}
|
||||
}
|
@ -0,0 +1,26 @@
|
||||
package base;
|
||||
|
||||
import org.opencv.core.Mat;
|
||||
import org.opencv.core.Rect;
|
||||
import org.opencv.highgui.HighGui;
|
||||
import org.opencv.imgcodecs.Imgcodecs;
|
||||
|
||||
import java.io.File;
|
||||
|
||||
/**
|
||||
* 图片剪切
|
||||
*/
|
||||
public class ImageCut extends DllLoad {
|
||||
|
||||
public static void main(String[] args) {
|
||||
File file = new File("imgs/3.png");
|
||||
Mat src = Imgcodecs.imread(file.getAbsolutePath());
|
||||
|
||||
// 剪切为原图像的1/4
|
||||
Rect rect = new Rect(0, 0, src.width() / 2, src.height() / 2);
|
||||
Mat dst = new Mat(src, rect);
|
||||
|
||||
show(dst);
|
||||
System.exit(0);
|
||||
}
|
||||
}
|
@ -0,0 +1,26 @@
|
||||
package base;
|
||||
|
||||
import org.opencv.core.Mat;
|
||||
import org.opencv.core.Size;
|
||||
import org.opencv.imgcodecs.Imgcodecs;
|
||||
import org.opencv.imgproc.Imgproc;
|
||||
|
||||
import java.io.File;
|
||||
|
||||
/**
|
||||
* 图片缩放操作
|
||||
*/
|
||||
public class ImageResize extends DllLoad {
|
||||
|
||||
public static void main(String[] args) {
|
||||
File file = new File("imgs/3.png");
|
||||
Mat src = Imgcodecs.imread(file.getAbsolutePath());
|
||||
|
||||
// 放大两倍
|
||||
Mat dst = new Mat();
|
||||
Imgproc.resize(src, dst, new Size(src.width() * 2, src.height() * 2));
|
||||
|
||||
show(dst);
|
||||
System.exit(0);
|
||||
}
|
||||
}
|
@ -0,0 +1,33 @@
|
||||
package base;
|
||||
|
||||
import org.opencv.core.Mat;
|
||||
import org.opencv.core.Point;
|
||||
import org.opencv.imgcodecs.Imgcodecs;
|
||||
import org.opencv.imgproc.Imgproc;
|
||||
|
||||
import java.io.File;
|
||||
|
||||
/**
|
||||
* 图片旋转
|
||||
*/
|
||||
public class ImageRotate extends DllLoad {
|
||||
|
||||
public static void main(String[] args) {
|
||||
File file = new File("imgs/3.png");
|
||||
Mat src = Imgcodecs.imread(file.getAbsolutePath());
|
||||
|
||||
Mat dst = new Mat(src.size(), src.type());
|
||||
//旋转中心点
|
||||
Point c = new Point(src.width() / 2, src.height() / 2);
|
||||
//旋转角度
|
||||
int angle = 45;
|
||||
//缩放系数
|
||||
int scale = 1;
|
||||
//获取变换矩阵
|
||||
Mat matrix2D = Imgproc.getRotationMatrix2D(c, angle, scale);
|
||||
Imgproc.warpAffine(src,dst,matrix2D,dst.size(),Imgproc.INTER_NEAREST);
|
||||
|
||||
show(dst);
|
||||
System.exit(0);
|
||||
}
|
||||
}
|
@ -0,0 +1,21 @@
|
||||
package base;
|
||||
|
||||
import org.opencv.core.Mat;
|
||||
import org.opencv.imgcodecs.Imgcodecs;
|
||||
|
||||
import java.io.File;
|
||||
|
||||
/**
|
||||
* 图片保存
|
||||
*/
|
||||
public class ImageSave extends DllLoad {
|
||||
|
||||
public static void main(String[] args) {
|
||||
File file = new File("imgs/3.png");
|
||||
Mat src = Imgcodecs.imread(file.getAbsolutePath());
|
||||
|
||||
// 保存为其他文件
|
||||
Imgcodecs.imwrite("imgs/4.png", src);
|
||||
System.exit(0);
|
||||
}
|
||||
}
|
@ -0,0 +1 @@
|
||||
Java OpenCV 基本操作学习
|
Loading…
Reference in new issue