Java (JDK 21) 调用 OpenCV (4.8.0)
2023-12-14 16:44:17
Java 调用 OpenCV
一.OpenCV 下载和安装
可以下载编译好的包,也可以下载源码自行编译
双击安装 opencv-4.8.0-windows.exe 默认为当前目录
安装即解压缩
根据系统位数选择
将 x64 目录下 opencv_java480.dll 放到 JDK 目录
将 opencv-480.jar 引入项目依赖
二.创建 Java Maven 项目
工程截图
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.example</groupId>
<artifactId>opencv</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<maven.compiler.source>20</maven.compiler.source>
<maven.compiler.target>20</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>org.opencv</groupId>
<artifactId>opencv</artifactId>
<version>4.8.0</version>
<scope>system</scope>
<systemPath>${project.basedir}/src/main/resources/lib/opencv-480.jar</systemPath>
</dependency>
</dependencies>
</project>
测试代码
package org.example;
import org.opencv.core.Core;
import org.opencv.core.Mat;
import org.opencv.core.Size;
import org.opencv.highgui.HighGui;
import org.opencv.imgcodecs.Imgcodecs;
import org.opencv.imgproc.Imgproc;
/**
* @author Administrator
*/
public class Main {
/**
* load opencv_java480
*/
static {
System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
}
public static void main(String[] args) {
String img = "C:\\Users\\Administrator\\Desktop\\IMG_20140207_165557.jpg";
//read image
Mat src = Imgcodecs.imread(img);
//define a dst mat
Mat dst = new Mat();
// resize
Imgproc.resize(src,dst,new Size(1600,900));
//show
HighGui.imshow("dst",dst);
HighGui.resizeWindow("dst",1600,900);
// show delay some ms auto close
HighGui.waitKey(1000);
// exit
System.exit(0);
}
}
显示效果
三.其他测试
package org.example;
import org.opencv.core.Core;
import org.opencv.core.CvType;
import org.opencv.core.Mat;
import org.opencv.core.Size;
import org.opencv.highgui.HighGui;
import org.opencv.imgcodecs.Imgcodecs;
import org.opencv.imgproc.Imgproc;
/**
* @author Administrator
*/
public class Main {
/**
* load opencv_java480
*/
static {
System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
}
/**
* 低通滤波器 均值滤波 使图像模糊、消除噪点
*/
static float[] vague_shaper = new float[]{
1/9,1/9,1/9,
1/9,1/9,1/9,
1/9,1/9,1/9
};
/**
* 低通滤波器 高斯滤波 使图像模糊
*/
static float[] gauss_shaper = new float[]{
1/16,2/16,1/16,
2/16,4/16,2/16,
1/16,2/16,1/16
};
/**
* 高通滤波器 锐化卷积核
*/
static float[] sharpening = new float[]{
-1,-1,-1,
-1,8,-1,
-1,-1,-1
};
/**
* 一阶微算子 pre_witt 处理噪声多、灰度突变的图像
*/
static float[] pre_witt = new float[]{
-1,-1,-1,
0,0,0,
1,1,1
};
/**
* 一阶微算子 sobel pre_witt 的改进版
*/
static float[] sobel_vertical = new float[]{
-1,0,1,
-2,0,2,
-1,0,1
};
/**
* 一阶微算子 sobel pre_witt 的改进版
*/
static float[] sobel_horizontal = new float[]{
1,2,1,
0,0,0,
-1,-2,-1
};
public static void main(String[] args) {
String imgF = "C:\\Users\\Administrator\\Desktop\\IMG_20220204_132906.jpg";
Mat img = Imgcodecs.imread(imgF);
Mat src = new Mat();
Mat dst = new Mat();
//重置大小
Imgproc.resize(img,src,new Size(1600,900));
int rows = src.rows();
int cols = src.cols();
int channels = src.channels();
//展示
HighGui.resizeWindow("dst",1600,900);
HighGui.imshow("dst",src);
HighGui.waitKey(0);
//灰度化
Imgproc.cvtColor(src,dst,Imgproc.COLOR_RGB2GRAY);
HighGui.imshow("dst",dst);
HighGui.waitKey(0);
//定义卷积核
Mat operator = new Mat(3,3, CvType.CV_32FC1);
//模糊
operator.put(0,0,vague_shaper);
Imgproc.filter2D(src,dst,-1,operator);
HighGui.imshow("vague_shaper",dst);
HighGui.waitKey(0);
//模糊
operator.put(0,0,gauss_shaper);
Imgproc.filter2D(src,dst,-1,operator);
HighGui.imshow("gauss_shaper",dst);
HighGui.waitKey(0);
//高通
operator.put(0,0,sharpening);
Imgproc.filter2D(src,dst,-1,operator);
HighGui.imshow("sharpening",dst);
HighGui.waitKey(0);
//pre_witt
operator.put(0,0,pre_witt);
Imgproc.filter2D(src,dst,-1,operator);
HighGui.imshow("pre_witt",dst);
HighGui.waitKey(0);
//sobel_vertical
operator.put(0,0,sobel_vertical);
Imgproc.filter2D(src,dst,-1,operator);
HighGui.imshow("sobel_vertical",dst);
HighGui.waitKey(0);
//sobel_horizontal
operator.put(0,0,sobel_horizontal);
Imgproc.filter2D(src,dst,-1,operator);
HighGui.imshow("sobel_horizontal",dst);
HighGui.waitKey(0);
//二值化
Mat gray = new Mat();
Imgproc.cvtColor(src,gray,Imgproc.COLOR_RGB2GRAY);
Imgproc.threshold(gray,dst,100,255,Imgproc.THRESH_BINARY);
HighGui.imshow("binary",dst);
HighGui.waitKey(0);
//边缘检测
Imgproc.Canny(src,dst,128,255);
HighGui.imshow("edge",dst);
HighGui.waitKey(0);
//反色
dst = src.clone();
byte[] data = new byte[rows * cols * channels];
dst.get(0,0,data);
int index,r,g,b;
for (int rs = 0 ; rs < rows ; rs++){
//通道值横向排列
for (int c = 0 ; c < cols * channels ; c = c + channels){
//取位置
index = rs * cols * channels + c;
//取RGB并翻转
b = 255 - data[index] & 0xff;
g = 255 - data[index + 1] & 0xff;
r = 255 - data[index + 2] & 0xff;
//回写
data[index] = (byte) b;
data[index + 1] = (byte) g;
data[index + 2] = (byte) r;
}
}
dst.put(0,0,data);
HighGui.imshow("overturn",dst);
HighGui.waitKey(0);
System.exit(0);
}
}
原图
灰度图
锐化图
Prewitt 算子
sobel_vertical
sobel_horizontal
binary
边缘检测
反色
文章来源:https://blog.csdn.net/weixin_42176639/article/details/134843740
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。 如若内容造成侵权/违法违规/事实不符,请联系我的编程经验分享网邮箱:veading@qq.com进行投诉反馈,一经查实,立即删除!
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。 如若内容造成侵权/违法违规/事实不符,请联系我的编程经验分享网邮箱:veading@qq.com进行投诉反馈,一经查实,立即删除!