diff --git a/imgs/1.png b/imgs/1.png new file mode 100644 index 0000000..43b94cb Binary files /dev/null and b/imgs/1.png differ diff --git a/imgs/2.png b/imgs/2.png new file mode 100644 index 0000000..93b0ffe Binary files /dev/null and b/imgs/2.png differ diff --git a/imgs/3.png b/imgs/3.png new file mode 100644 index 0000000..cb3ae25 Binary files /dev/null and b/imgs/3.png differ diff --git a/imgs/4.png b/imgs/4.png new file mode 100644 index 0000000..03678c0 Binary files /dev/null and b/imgs/4.png differ diff --git a/src/main/java/Test.java b/src/main/java/Test.java index 634a285..3cc4b51 100644 --- a/src/main/java/Test.java +++ b/src/main/java/Test.java @@ -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.imgcodecs.Imgcodecs; import org.opencv.imgproc.Imgproc; import java.io.File; -import java.net.URL; import java.util.ArrayList; import java.util.List; -public class Test { - 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 class Test extends DllLoad { 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()); - HighGui.imshow("源图片", src); - HighGui.waitKey(); +// HighGui.imshow("源图片", src); +// HighGui.waitKey(); // 放大图像 // Mat resize = new Mat(); // 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 Mat gary = new Mat(); Imgproc.cvtColor(src, gary, Imgproc.COLOR_BGR2GRAY); - HighGui.imshow("灰度化图片", gary); - HighGui.waitKey(); +// HighGui.imshow("灰度化图片", gary); +// HighGui.waitKey(); //图像边缘处理 https://blog.csdn.net/ren365880/article/details/103938232 Mat edges = new Mat(); Imgproc.Canny(gary, edges, 200, 200, 3, false); - HighGui.imshow("边缘处理", edges); - HighGui.waitKey(); +// HighGui.imshow("边缘处理", edges); +// HighGui.waitKey(); //发现轮廓 List list = new ArrayList(); @@ -83,8 +84,7 @@ public class Test { * 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 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 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.waitKey(); diff --git a/src/main/java/base/DllLoad.java b/src/main/java/base/DllLoad.java new file mode 100644 index 0000000..754994a --- /dev/null +++ b/src/main/java/base/DllLoad.java @@ -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(); + } +} diff --git a/src/main/java/base/ImageCut.java b/src/main/java/base/ImageCut.java new file mode 100644 index 0000000..58a0e52 --- /dev/null +++ b/src/main/java/base/ImageCut.java @@ -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); + } +} diff --git a/src/main/java/base/ImageResize.java b/src/main/java/base/ImageResize.java new file mode 100644 index 0000000..fd75f21 --- /dev/null +++ b/src/main/java/base/ImageResize.java @@ -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); + } +} diff --git a/src/main/java/base/ImageRotate.java b/src/main/java/base/ImageRotate.java new file mode 100644 index 0000000..46a0cde --- /dev/null +++ b/src/main/java/base/ImageRotate.java @@ -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); + } +} diff --git a/src/main/java/base/ImageSave.java b/src/main/java/base/ImageSave.java new file mode 100644 index 0000000..c2d6c5b --- /dev/null +++ b/src/main/java/base/ImageSave.java @@ -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); + } +} diff --git a/src/main/java/base/README.md b/src/main/java/base/README.md new file mode 100644 index 0000000..42fef5b --- /dev/null +++ b/src/main/java/base/README.md @@ -0,0 +1 @@ +Java OpenCV 基本操作学习 \ No newline at end of file