//region 图像匹配 3.0
private Mat imageMatch(Image ori, Image tbi) {
System.setProperty("java.awt.headless", "false");
System.out.println(System.getProperty("java.library.path"));
URL url = ClassLoader.getSystemResource("lib/opencv/opencv_java4100.dll");
System.load(url.getPath());
Mat img = bToM_Rotate(ori, 0);
Mat result = new Mat();
int angle = 0;
Mat template = bToM_Rotate(tbi, angle);
Mat dst = img.clone();
Imgproc.cvtColor(template, template, Imgproc.COLOR_BGR2GRAY);
Imgproc.GaussianBlur(template, template, new Size(3, 3), 0);
Imgproc.Canny(template, template, 50, 80);
Imgproc.cvtColor(dst, dst, Imgproc.COLOR_BGR2GRAY);
Imgproc.GaussianBlur(dst, dst, new Size(3, 3), 0);
Imgproc.Canny(dst, dst, 50, 80);
Imgproc.matchTemplate(dst, template, result, Imgproc.TM_CCOEFF_NORMED);
Point matchLoc = new Point();
try {
while (true) {
// 找到匹配度最高的位置
Core.MinMaxLocResult mmr = Core.minMaxLoc(result);
double v = mmr.maxVal * 1000;
System.out.println(angle + " : " + v); // 旋转角度
angle += 10;
if (v >= 99) { // 模糊匹配相似度阈值
matchLoc = mmr.maxLoc;
// HighGui.imshow("dst", dst);
// HighGui.imshow("template", template);
break;
}else if (angle >= 360){
System.out.println(angle + " : " + v); // 旋转角度
break;
} else {
template = imageUtils.bToM_Rotate(tbi, angle);
// HighGui.imshow("template" + angle, template);
Imgproc.resize(template, template, new Size(dst.cols() * 0.5, dst.rows() * 0.5));
Imgproc.cvtColor(template, template, Imgproc.COLOR_BGR2GRAY);
Imgproc.GaussianBlur(template, template, new Size(3, 3), 0);
Imgproc.Canny(template, template, 30, 50);
}
Imgproc.matchTemplate(dst, template, result, Imgproc.TM_CCOEFF_NORMED);
}
} catch (Exception e) {
System.out.println(e.getMessage());
}
// 绘制矩形标出匹配区域
Rect rect = new Rect((int) matchLoc.x, (int) matchLoc.y, template.cols(), template.rows());
Imgproc.rectangle(img, rect.tl(), rect.br(), new Scalar(0, 255, 0), Imgproc.LINE_4);
return img;
}
//endregion
// 根据角度旋转图像并转为 opencv用的 Mat类型
public Mat bToM_Rotate(Image ori, int angel) {
BufferedImage rotate = Rotate(ori, angel);
BufferedImage grayImage = new BufferedImage(rotate.getWidth(),rotate.getHeight(), BufferedImage.TYPE_3BYTE_BGR);
ColorConvertOp op = new ColorConvertOp(null);
grayImage = op.filter(rotate, grayImage);
// System.setProperty("java.awt.headless", "false");
// System.out.println(System.getProperty("java.library.path"));
URL url = ClassLoader.getSystemResource("lib/opencv/opencv_java4100.dll");
System.load(url.getPath());
Mat src = Mat.zeros(new Size(rotate.getWidth(), rotate.getHeight()), CvType.CV_8UC3);
byte[] imageData = ((DataBufferByte) grayImage.getRaster().getDataBuffer()).getData();
src.put(0,0,imageData);
// saveImage("byteData", "jpg", src);
return src;
}
//region 图片旋转
/**
* 对图片进行旋转
*
* @param src 被旋转图片
* @param angel 旋转角度
* @return 旋转后的图片
*/
public static BufferedImage Rotate(Image src, int angel) {
int src_width = src.getWidth(null);
int src_height = src.getHeight(null);
// 计算旋转后图片的尺寸
Rectangle rect_des = CalcRotatedSize(new Rectangle(new Dimension(
src_width, src_height)), angel);
BufferedImage res = null;
res = new BufferedImage(rect_des.width, rect_des.height,
BufferedImage.TYPE_INT_RGB);
Graphics2D g2 = res.createGraphics();
// 进行转换
g2.translate((rect_des.width - src_width) / 2,
(rect_des.height - src_height) / 2);
g2.rotate(Math.toRadians(angel), src_width / 2, src_height / 2);
g2.drawImage(src, null, null);
return res;
}
/**
* 计算旋转后的图片
*
* @param src 被旋转的图片
* @param angel 旋转角度
* @return 旋转后的图片
*/
public static Rectangle CalcRotatedSize(Rectangle src, int angel) {
// 如果旋转的角度大于90度做相应的转换
if (angel >= 90) {
if (angel / 90 % 2 == 1) {
int temp = src.height;
src.height = src.width;
src.width = temp;
}
angel = angel % 90;
}
double r = Math.sqrt(src.height * src.height + src.width * src.width) / 2;
double len = 2 * Math.sin(Math.toRadians(angel) / 2) * r;
double angel_alpha = (Math.PI - Math.toRadians(angel)) / 2;
double angel_dalta_width = Math.atan((double) src.height / src.width);
double angel_dalta_height = Math.atan((double) src.width / src.height);
int len_dalta_width = (int) (len * Math.cos(Math.PI - angel_alpha
- angel_dalta_width));
int len_dalta_height = (int) (len * Math.cos(Math.PI - angel_alpha
- angel_dalta_height));
int des_width = src.width + len_dalta_width * 2;
int des_height = src.height + len_dalta_height * 2;
return new Rectangle(new Dimension(des_width, des_height));
}
//endregion
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· AI 智能体引爆开源社区「GitHub 热点速览」
· C#/.NET/.NET Core技术前沿周刊 | 第 29 期(2025年3.1-3.9)
· 从HTTP原因短语缺失研究HTTP/2和HTTP/3的设计差异