|
@@ -1,6 +1,7 @@
|
|
|
package com.management.platform.util;
|
|
|
|
|
|
import org.opencv.core.*;
|
|
|
+import org.opencv.features2d.ORB;
|
|
|
import org.opencv.highgui.HighGui;
|
|
|
import org.opencv.imgcodecs.Imgcodecs;
|
|
|
import org.opencv.imgproc.Imgproc;
|
|
@@ -14,30 +15,23 @@ import static java.lang.Math.E;
|
|
|
import static org.opencv.imgproc.Imgproc.*;
|
|
|
|
|
|
public class ImageReconizeUtil {
|
|
|
- public static final double YUZHI_HIGH = 2*Math.pow(0.1, 11);
|
|
|
- public static final double YUZHI_NORMAL = 1*Math.pow(0.1, 10);
|
|
|
+// public static final double YUZHI_HIGH = 2*Math.pow(0.1, 11);
|
|
|
+// public static final double YUZHI_NORMAL = 1*Math.pow(0.1, 10);
|
|
|
+ public static final double YUZHI = 0.89;
|
|
|
static {
|
|
|
System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
|
|
|
//注意程序运行的时候需要在VM option添加该行 指明opencv的dll文件所在路径
|
|
|
//-Djava.library.path=$PROJECT_DIR$\opencv\x64
|
|
|
}
|
|
|
public static void main(String[] args) {
|
|
|
- boolean match = isTemplateMatch("C:\\Users\\seya\\Desktop\\my.jpg",
|
|
|
- "C:\\picrecongnize\\develop\\idea_black.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\\cc.jpg",
|
|
|
+ "C:\\picrecongnize\\browser\\CHrome\\chrome.jpg");
|
|
|
|
|
|
-// boolean match = isTemplateMatch("C:\\Users\\seya\\Desktop\\chrome2.jpg",
|
|
|
-// "C:\\picrecongnize\\Chrome\\bar125.jpg");
|
|
|
}
|
|
|
|
|
|
public static boolean isTemplateMatch(String sourcePic, String targetPic) {
|
|
|
- double matchVal = templete(Imgproc.TM_SQDIFF_NORMED, sourcePic, targetPic,true);
|
|
|
- if (Math.abs(matchVal) > 0 && Math.abs(matchVal) < YUZHI_HIGH) {
|
|
|
+ double matchVal = templete(TM_CCOEFF_NORMED, sourcePic, targetPic,true);
|
|
|
+ if (matchVal >= YUZHI) {
|
|
|
System.out.println("找到啦");
|
|
|
return true;
|
|
|
} else {
|
|
@@ -47,8 +41,8 @@ public class ImageReconizeUtil {
|
|
|
}
|
|
|
|
|
|
public static boolean isWholeTemplateMatch(String sourcePic, String targetPic) {
|
|
|
- double matchVal = templete(Imgproc.TM_SQDIFF_NORMED, sourcePic, targetPic, false);
|
|
|
- if (Math.abs(matchVal) > 0 && Math.abs(matchVal) < YUZHI_HIGH) {
|
|
|
+ double matchVal = templete(Imgproc.TM_CCOEFF_NORMED, sourcePic, targetPic, false);
|
|
|
+ if (matchVal >= YUZHI) {
|
|
|
System.out.println("找到啦"+targetPic);
|
|
|
return true;
|
|
|
} else {
|
|
@@ -71,25 +65,25 @@ public class ImageReconizeUtil {
|
|
|
* @return: void
|
|
|
* @date: 2019年5月7日12:16:55
|
|
|
*/
|
|
|
- public static double templete(int method, String sourcePic, String targetPic, boolean cutMatch) {
|
|
|
+ public static double templete(int method, String inFile, String templateFile, boolean cutMatch) {
|
|
|
// 1 获取待匹配图片
|
|
|
// System.out.println("sourcePic="+sourcePic+", targetPic="+targetPic);
|
|
|
- Mat templete=Imgcodecs.imread(sourcePic);
|
|
|
+ Mat srcMat = Imgcodecs.imread(inFile);
|
|
|
if (cutMatch) {
|
|
|
//先裁减,左上角1/4的截图,提高比对速度。
|
|
|
- Rect rect = new Rect(0,0,templete.width()/4, templete.height()/4);
|
|
|
- Mat subMat = new Mat(templete, rect);
|
|
|
- subMat.copyTo(templete);
|
|
|
+ Rect rect = new Rect(0,0,srcMat.width()/4, srcMat.height()/4);
|
|
|
+ Mat subMat = new Mat(srcMat, rect);
|
|
|
+ subMat.copyTo(srcMat);
|
|
|
}
|
|
|
|
|
|
//将rgb灰化处理
|
|
|
// Imgproc.cvtColor(templete, templete,Imgproc.COLOR_BGR2GRAY);
|
|
|
//
|
|
|
// 2 获取匹配模板
|
|
|
- Mat demo=Imgcodecs.imread(targetPic);
|
|
|
+ Mat demo = Imgcodecs.imread(templateFile);
|
|
|
// Imgproc.cvtColor(demo, demo,Imgproc.COLOR_BGR2GRAY);
|
|
|
|
|
|
- Core.MinMaxLocResult mmr = getLocResult(method, templete, demo);
|
|
|
+ Core.MinMaxLocResult mmr = getLocResult(method, srcMat, demo);
|
|
|
// 7 绘制匹配到的结果
|
|
|
double x,y;
|
|
|
double matchVal = 0;
|
|
@@ -103,15 +97,15 @@ public class ImageReconizeUtil {
|
|
|
matchVal = mmr.maxVal;
|
|
|
}
|
|
|
|
|
|
- System.out.println("匹配度=="+matchVal+", target="+targetPic);
|
|
|
+ System.out.println("匹配度=="+matchVal+", target="+templateFile.substring(templateFile.lastIndexOf(File.separator)-10)+", inFile="+inFile.substring(inFile.lastIndexOf(File.separator)));
|
|
|
System.out.println("x="+x+", y=" + y);
|
|
|
//我们匹配的图像在左上角,考虑到用户可能拖动窗口,但是不应该偏差很大。 这里增加判断标准:坐标处在左上方。
|
|
|
|
|
|
-// if (Math.abs(matchVal) < YUZHI_NORMAL) {
|
|
|
-// 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);
|
|
|
+// if (matchVal >= YUZHI) {
|
|
|
+// Imgproc.rectangle(srcMat,new Point(x,y),new Point(x+demo.cols(),y+demo.rows()),new Scalar( 0, 0, 255),2,Imgproc.LINE_AA);
|
|
|
+// Imgproc.putText(srcMat,"Match Success",new Point(x,y),Imgproc.FONT_HERSHEY_SCRIPT_COMPLEX, 1.0, new Scalar(0, 255, 0),1,Imgproc.LINE_AA);
|
|
|
// // 8 显示结果
|
|
|
-// HighGui.imshow("模板匹配", templete);
|
|
|
+// HighGui.imshow("模板匹配", srcMat);
|
|
|
// HighGui.waitKey(0);
|
|
|
// }
|
|
|
|
|
@@ -126,9 +120,10 @@ public class ImageReconizeUtil {
|
|
|
// 4 调用 模板匹配函数
|
|
|
Imgproc.matchTemplate(templete, demo, result, method);
|
|
|
// 5 归一化
|
|
|
- Core.normalize(result, result,0, 1, Core.NORM_MINMAX, -1, new Mat());
|
|
|
+// Core.normalize(result, result,0, 1, Core.NORM_MINMAX, -1, new Mat());
|
|
|
// 6 获取模板匹配结果
|
|
|
Core.MinMaxLocResult mmr = Core.minMaxLoc(result);
|
|
|
return mmr;
|
|
|
}
|
|
|
+
|
|
|
}
|