OpenCV
参考:https://blog.csdn.net/yhmabcdef/article/details/75214020
复制:https://blog.csdn.net/shuzhe66/article/details/40817389
OpenCV下载地址 https://opencv.org/releases.html 下载win的,我下的是2.4.9,之后安装;
eclipse 搭建环境 具体参考 https://blog.csdn.net/guanjungao/article/details/30313593
加入opencv-249.jar(安装目录有),之后配置opencv\opencv-249\opencv\build\java\x64\opencv_java249.dll()
模板匹配
将原图和要匹配原图的小图;
- 代码TemplateMachingUtil 匹配模板工具类
import org.junit.Test;
import org.opencv.core.Core;
import org.opencv.core.Core.MinMaxLocResult;
import org.opencv.core.CvType;
import org.opencv.core.Mat; import org.opencv.core.Point; import org.opencv.highgui.Highgui; import org.opencv.imgproc.Imgproc; /** * * @版权: Copyright (c) 2016-2017 *********公司技术开发部 * @author : huangjiapeng * @E-mail:[email protected] * @版本: 1.0 * @创建日期: 2018年7月21日 下午1:54:17 * @ClassName TemplateMachingUtil * @类描述-Description: TODO * @修改记录: * @版本: 1.0 */ /** * @author Administrator * */ public class TemplateMachingUtil { /** * * @param * @Description: TODO 测试 * @return void */ @Test public void TestFixedPosition() { MyPoint p = TemplateMachingUtil.fixedPosition("G:\\a\\r22.png", "G:\\a\\r2.png"); // p点 也是一个表示图片位置的,至于如何转你需要的位置形式,你自己去百度 } /** * * @param sourceImage * 原图 * @param dstImage * 从原图中要找的图 * @Description: TODO * @return MyPoint matchLoc.x, matchLoc.y也是一个表示位置的,至于如何转你需要的,你自己去百度 */ public static MyPoint fixedPosition(String sourceImage, String dstImage) { System.loadLibrary("opencv_java249"); TemplateMaching macher = new TemplateMaching(); macher.setSource(sourceImage); macher.setDst(dstImage); /**************************************/ String sourcePath = sourceImage, dstPath = dstImage; Mat source, dst; // 将文件读入为OpenCV的Mat格式 source = Highgui.imread(sourcePath); dst = Highgui.imread(dstPath); // 创建于原图相同的大小,储存匹配度 System.out.println("行" + source.rows() + " " + "列" + source.cols()); Mat result = Mat.zeros(source.rows(), source.cols(), CvType.CV_32FC1); // 调用模板匹配方法 Imgproc.matchTemplate(source, dst, result, Imgproc.TM_SQDIFF); // 规格化 Core.normalize(result, result, 0, 1, Core.NORM_MINMAX, -1); // 获得最可能点,MinMaxLocResult是其数据格式,包括了最大、最小点的位置x、y MinMaxLocResult mlr = Core.minMaxLoc(result); Point matchLoc = mlr.minLoc; /*****************************************/ // 下面注释代码为 :在原图上的对应模板可能位置画一个绿色矩形 // Core.rectangle(source, matchLoc, new Point(matchLoc.x + dst.width(), // matchLoc.y + dst.height()), // new Scalar(0, 255, 0)); // 将结果输出到对应位置 路径 G:\\a\\TMOutPut2.png // Highgui.imwrite("G:\\a\\TMOutPut2.png", source); /*****************************************/ MyPoint p = new MyPoint(matchLoc.x, matchLoc.y); return p; } }
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- MyPoint类
/**
*
* @版权: Copyright (c) 2016-2017 *********公司技术开发部
* @author : huangjiapeng
* @E-mail:[email protected]
* @版本: 1.0
* @创建日期: 2018年7月21日 下午1:47:31
* @ClassName MyPoint
* @类描述-Description: TODO
* @修改记录:
* @版本: 1.0
*/
public class MyPoint { private double x; private double y; public double getX() { return x; } public void setX(double x) { this.x = x; } public double getY() { return y; } public void setY(double y) { this.y = y; } /** * @param x * @param y */ public MyPoint(double x, double y) { super(); this.x = x; this.y = y; } @Override public String toString() { return "MyPoint [x=" + x + ", y=" + y + "]"; } }