|
@@ -1,7 +1,6 @@
|
|
package com.management.platform.util;
|
|
package com.management.platform.util;
|
|
|
|
|
|
import org.opencv.core.*;
|
|
import org.opencv.core.*;
|
|
-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;
|
|
|
|
|
|
@@ -10,17 +9,24 @@ import java.io.File;
|
|
import static java.lang.Math.E;
|
|
import static java.lang.Math.E;
|
|
|
|
|
|
public class ImageReconizeUtil {
|
|
public class ImageReconizeUtil {
|
|
- public static final double YUZHI = Math.pow(0.1, 10);
|
|
|
|
|
|
+ public static final double YUZHI = 5*Math.pow(0.1, 11);
|
|
static {
|
|
static {
|
|
System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
|
|
System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
|
|
//注意程序运行的时候需要在VM option添加该行 指明opencv的dll文件所在路径
|
|
//注意程序运行的时候需要在VM option添加该行 指明opencv的dll文件所在路径
|
|
//-Djava.library.path=$PROJECT_DIR$\opencv\x64
|
|
//-Djava.library.path=$PROJECT_DIR$\opencv\x64
|
|
}
|
|
}
|
|
public static void main(String[] args) {
|
|
public static void main(String[] args) {
|
|
-// boolean match = isTemplateMatch("C:\\Users\\seya\\Desktop\\360_2.jpg",
|
|
|
|
-// "C:\\picrecongnize\\360Safe\\2.jpg");
|
|
|
|
- boolean match = isTemplateMatch("C:\\Users\\seya\\Desktop\\qyt_2.jpg",
|
|
|
|
- "C:\\\\picrecongnize\\\\360Safe\\\\2.jpg");
|
|
|
|
|
|
+ boolean match = isTemplateMatch("C:\\Users\\seya\\Desktop\\loading.jpg",
|
|
|
|
+ "C:\\\\picrecongnize\\\\Chrome\\\\4-125.jpg");
|
|
|
|
+// match = isTemplateMatch("C:\\Users\\seya\\Desktop\\chrome_gray.jpg",
|
|
|
|
+// "C:\\\\picrecongnize\\\\Chrome\\\\4-125.jpg");
|
|
|
|
+// match = isTemplateMatch("C:\\Users\\seya\\Desktop\\chrome_gray.jpg",
|
|
|
|
+// "C:\\\\picrecongnize\\\\Chrome\\\\2-125.jpg");
|
|
|
|
+// match = isTemplateMatch("C:\\Users\\seya\\Desktop\\chrome_gray.jpg",
|
|
|
|
+// "C:\\\\picrecongnize\\\\Chrome\\\\3-125.jpg");
|
|
|
|
+
|
|
|
|
+// boolean match = isTemplateMatch("C:\\Users\\seya\\Desktop\\chrome2.jpg",
|
|
|
|
+// "C:\\picrecongnize\\Chrome\\bar125.jpg");
|
|
}
|
|
}
|
|
|
|
|
|
public static boolean isTemplateMatch(String sourcePic, String targetPic) {
|
|
public static boolean isTemplateMatch(String sourcePic, String targetPic) {
|
|
@@ -52,11 +58,11 @@ public class ImageReconizeUtil {
|
|
// 1 获取待匹配图片
|
|
// 1 获取待匹配图片
|
|
Mat templete=Imgcodecs.imread(sourcePic);
|
|
Mat templete=Imgcodecs.imread(sourcePic);
|
|
//将rgb灰化处理
|
|
//将rgb灰化处理
|
|
- Imgproc.cvtColor(templete, templete,Imgproc.COLOR_BGR2GRAY);
|
|
|
|
-
|
|
|
|
|
|
+// Imgproc.cvtColor(templete, templete,Imgproc.COLOR_BGR2GRAY);
|
|
|
|
+//
|
|
// 2 获取匹配模板
|
|
// 2 获取匹配模板
|
|
Mat demo=Imgcodecs.imread(targetPic);
|
|
Mat demo=Imgcodecs.imread(targetPic);
|
|
- Imgproc.cvtColor(demo, demo,Imgproc.COLOR_BGR2GRAY);
|
|
|
|
|
|
+// Imgproc.cvtColor(demo, demo,Imgproc.COLOR_BGR2GRAY);
|
|
|
|
|
|
Core.MinMaxLocResult mmr = getLocResult(method, templete, demo);
|
|
Core.MinMaxLocResult mmr = getLocResult(method, templete, demo);
|
|
// 7 绘制匹配到的结果
|
|
// 7 绘制匹配到的结果
|
|
@@ -72,34 +78,34 @@ public class ImageReconizeUtil {
|
|
matchVal = mmr.maxVal;
|
|
matchVal = mmr.maxVal;
|
|
}
|
|
}
|
|
//if not match , resize the targetPic and try to match again!
|
|
//if not match , resize the targetPic and try to match again!
|
|
- if (Math.abs(matchVal) > YUZHI) {
|
|
|
|
- System.out.println("第一次没匹配上, 尝试125%尺寸再次匹配~~");
|
|
|
|
- Mat demoResize = demo.clone();
|
|
|
|
- float scale=1.25f;
|
|
|
|
- float widthT=demo.width();
|
|
|
|
- float heightT=demo.height();
|
|
|
|
- Imgproc.resize(demo, demoResize, new Size(widthT*scale,heightT*scale));
|
|
|
|
- demo = demoResize;
|
|
|
|
- mmr = getLocResult(method, templete, demo);
|
|
|
|
- // 7 绘制匹配到的结果
|
|
|
|
- matchVal = 0;
|
|
|
|
- if (method==Imgproc.TM_SQDIFF_NORMED || method==Imgproc.TM_SQDIFF) {
|
|
|
|
- x = mmr.minLoc.x;
|
|
|
|
- y = mmr.minLoc.y;
|
|
|
|
- matchVal = mmr.minVal;
|
|
|
|
- } else {
|
|
|
|
- x = mmr.maxLoc.x;
|
|
|
|
- y = mmr.maxLoc.y;
|
|
|
|
- matchVal = mmr.maxVal;
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
|
|
+// if (Math.abs(matchVal) > YUZHI) {
|
|
|
|
+// System.out.println("第一次没匹配上, 尝试125%尺寸再次匹配~~");
|
|
|
|
+// Mat demoResize = demo.clone();
|
|
|
|
+// float scale=1.25f;
|
|
|
|
+// float widthT=demo.width();
|
|
|
|
+// float heightT=demo.height();
|
|
|
|
+// Imgproc.resize(demo, demoResize, new Size(widthT*scale,heightT*scale));
|
|
|
|
+// demo = demoResize;
|
|
|
|
+// mmr = getLocResult(method, templete, demo);
|
|
|
|
+// // 7 绘制匹配到的结果
|
|
|
|
+// matchVal = 0;
|
|
|
|
+// if (method==Imgproc.TM_SQDIFF_NORMED || method==Imgproc.TM_SQDIFF) {
|
|
|
|
+// x = mmr.minLoc.x;
|
|
|
|
+// y = mmr.minLoc.y;
|
|
|
|
+// matchVal = mmr.minVal;
|
|
|
|
+// } else {
|
|
|
|
+// x = mmr.maxLoc.x;
|
|
|
|
+// y = mmr.maxLoc.y;
|
|
|
|
+// matchVal = mmr.maxVal;
|
|
|
|
+// }
|
|
|
|
+// }
|
|
System.out.println("匹配度=="+matchVal);
|
|
System.out.println("匹配度=="+matchVal);
|
|
System.out.println("x="+x+", y=" + y);
|
|
System.out.println("x="+x+", y=" + y);
|
|
Imgproc.rectangle(templete,new Point(x,y),new Point(x+demo.cols(),y+demo.rows()),new Scalar( 0, 0, 255),2,Imgproc.LINE_AA);
|
|
Imgproc.rectangle(templete,new Point(x,y),new Point(x+demo.cols(),y+demo.rows()),new Scalar( 0, 0, 255),2,Imgproc.LINE_AA);
|
|
Imgproc.putText(templete,"Match Success",new Point(x,y),Imgproc.FONT_HERSHEY_SCRIPT_COMPLEX, 1.0, new Scalar(0, 255, 0),1,Imgproc.LINE_AA);
|
|
Imgproc.putText(templete,"Match Success",new Point(x,y),Imgproc.FONT_HERSHEY_SCRIPT_COMPLEX, 1.0, new Scalar(0, 255, 0),1,Imgproc.LINE_AA);
|
|
// // 8 显示结果
|
|
// // 8 显示结果
|
|
- HighGui.imshow("模板匹配", templete);
|
|
|
|
- HighGui.waitKey(0);
|
|
|
|
|
|
+// HighGui.imshow("模板匹配", templete);
|
|
|
|
+// HighGui.waitKey(0);
|
|
return matchVal;
|
|
return matchVal;
|
|
}
|
|
}
|
|
|
|
|