王兵 2 years ago
parent ac9a62c796
commit 7557e80fa0

Binary file not shown.

After

Width:  |  Height:  |  Size: 502 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 64 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 121 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 130 KiB

@ -1,22 +1,23 @@
import org.opencv.core.*; import base.DllLoad;
import org.opencv.core.CvType;
import org.opencv.core.Mat;
import org.opencv.core.MatOfPoint;
import org.opencv.core.MatOfPoint2f;
import org.opencv.core.Point;
import org.opencv.core.Rect;
import org.opencv.core.RotatedRect;
import org.opencv.core.Scalar;
import org.opencv.core.Size;
import org.opencv.highgui.HighGui; import org.opencv.highgui.HighGui;
import org.opencv.imgcodecs.Imgcodecs; import org.opencv.imgcodecs.Imgcodecs;
import org.opencv.imgproc.Imgproc; import org.opencv.imgproc.Imgproc;
import java.io.File; import java.io.File;
import java.net.URL;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
public class Test { public class Test extends 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 main(String[] args) { public static void main(String[] args) {
@ -32,10 +33,10 @@ public class Test {
*/ */
File file = new File("D://1.png"); File file = new File("imgs/3.png");
Mat src = Imgcodecs.imread(file.getAbsolutePath()); Mat src = Imgcodecs.imread(file.getAbsolutePath());
HighGui.imshow("源图片", src); // HighGui.imshow("源图片", src);
HighGui.waitKey(); // HighGui.waitKey();
// 放大图像 // 放大图像
// Mat resize = new Mat(); // Mat resize = new Mat();
// Imgproc.resize(src, resize, new Size(src.cols()*1.5,src.rows()*1.5)); // Imgproc.resize(src, resize, new Size(src.cols()*1.5,src.rows()*1.5));
@ -45,14 +46,14 @@ public class Test {
//图片灰度化 https://blog.csdn.net/ren365880/article/details/103869207 //图片灰度化 https://blog.csdn.net/ren365880/article/details/103869207
Mat gary = new Mat(); Mat gary = new Mat();
Imgproc.cvtColor(src, gary, Imgproc.COLOR_BGR2GRAY); Imgproc.cvtColor(src, gary, Imgproc.COLOR_BGR2GRAY);
HighGui.imshow("灰度化图片", gary); // HighGui.imshow("灰度化图片", gary);
HighGui.waitKey(); // HighGui.waitKey();
//图像边缘处理 https://blog.csdn.net/ren365880/article/details/103938232 //图像边缘处理 https://blog.csdn.net/ren365880/article/details/103938232
Mat edges = new Mat(); Mat edges = new Mat();
Imgproc.Canny(gary, edges, 200, 200, 3, false); Imgproc.Canny(gary, edges, 200, 200, 3, false);
HighGui.imshow("边缘处理", edges); // HighGui.imshow("边缘处理", edges);
HighGui.waitKey(); // HighGui.waitKey();
//发现轮廓 //发现轮廓
List<MatOfPoint> list = new ArrayList<MatOfPoint>(); List<MatOfPoint> list = new ArrayList<MatOfPoint>();
@ -83,8 +84,7 @@ public class Test {
* CHAIN_APPROX_TC89_KCOS = 4; 使teh-Chinl chain * CHAIN_APPROX_TC89_KCOS = 4; 使teh-Chinl chain
*/ */
Imgproc.findContours(edges, list, hierarchy, Imgproc.RETR_TREE, Imgproc.CHAIN_APPROX_NONE); Imgproc.findContours(edges, list, hierarchy, Imgproc.RETR_TREE, Imgproc.CHAIN_APPROX_SIMPLE);
/* /*
* *
@ -96,7 +96,79 @@ public class Test {
* @param thickness线thickness =FILLED * @param thickness线thickness =FILLED
* @param lineType线https://blog.csdn.net/ren365880/article/details/103952856 * @param lineType线https://blog.csdn.net/ren365880/article/details/103952856
*/ */
Imgproc.drawContours(src, list, -1, new Scalar(0, 255, 0), 1, Imgproc.LINE_AA); // Imgproc.drawContours(src, list, -1, new Scalar(0, 255, 0), 1, Imgproc.LINE_AA);
// Mat mat = new Mat();
// Imgproc.Sobel(gary,mat,1,1,0);
// HighGui.imshow("轮廓", mat);
// HighGui.waitKey();
// Imgproc.Sobel(gary,mat,1,0,1);
// HighGui.imshow("轮廓", mat);
// HighGui.waitKey();
for (MatOfPoint matOfPoint : list) {
MatOfPoint2f c = new MatOfPoint2f();
matOfPoint.convertTo(c, CvType.CV_32F);
// double peri = Imgproc.arcLength(c, false);
// MatOfPoint2f c2 = new MatOfPoint2f();
// Imgproc.approxPolyDP(c,c2,0.015*peri,true);
// 查找矩形
Rect rect = Imgproc.boundingRect(c);
if (rect.height < 20) {
continue;
}
if (rect.width < rect.height) {
continue;
}
if (rect.width / rect.height < 2 || rect.width / rect.height > 3) {
continue;
}
System.out.println(rect.width + "," + rect.height);
Imgproc.rectangle(src, rect, new Scalar(0, 255, 0));
// 获取最小面积矩形,可能为旋转状态
RotatedRect rotatedRect = Imgproc.minAreaRect(c);
// 获取四个点
Point[] point = new Point[4];
rotatedRect.points(point);
// 绘制四条边
Imgproc.line(src, point[0], point[1], new Scalar(0, 0, 255));
Imgproc.line(src, point[1], point[2], new Scalar(0, 0, 255));
Imgproc.line(src, point[2], point[3], new Scalar(0, 0, 255));
Imgproc.line(src, point[3], point[0], new Scalar(0, 0, 255));
Mat dst = new Mat(src,rotatedRect.boundingRect());
Mat dst2 = new Mat(dst.size(), dst.type());
//旋转中心点
Point cp = new Point(src.width() / 2, src.height() / 2);
//旋转角度
int angle = 45;
//缩放系数
int scale = 1;
//获取变换矩阵
Mat matrix2D = Imgproc.getRotationMatrix2D(cp, angle, scale);
Imgproc.warpAffine(dst,dst2,matrix2D,dst2.size(),Imgproc.INTER_NEAREST);
show(dst2);
HighGui.waitKey();
// Mat mat = new Mat();
// Imgproc.Sobel(src, mat, -1, 0, 1);
// System.out.println();
// HighGui.imshow("轮廓", mat);
// HighGui.waitKey();
//
// Imgproc.boundingRect()
//
// ArrayList<MatOfPoint> matOfPoints = new ArrayList<>();
// rotatedRect.boundingRect()
// matOfPoints.add()
// Imgproc.drawContours(src, matOfPoints, -1, new Scalar(0, 255, 0), 1, Imgproc.LINE_AA);
}
HighGui.imshow("轮廓", src); HighGui.imshow("轮廓", src);
HighGui.waitKey(); HighGui.waitKey();

@ -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…
Cancel
Save

Powered by TurnKey Linux.