gpt4 book ai didi

Opencv(3.0.0)模板匹配的Java实现

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:42:34 27 4
gpt4 key购买 nike

我想使用 Opencv 匹配给定图片中的模板(小图像)。

我从这个端口找到了以下代码:OpenCV Template Matching example in Android

问题是启动 openvc 3.0.0,highgui 被分解为新的 videoio 和 imgcodecs,下面的代码正在使用 highgui。

package opencv;

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.core.Scalar;
import org.opencv.highgui.Highgui;
import org.opencv.imgproc.Imgproc;

class MatchingDemo {
public void run(String inFile, String templateFile, String outFile, int match_method) {
System.out.println("\nRunning Template Matching");

Mat img = Highgui.imread(inFile);
Mat templ = Highgui.imread(templateFile);

// / Create the result matrix
int result_cols = img.cols() - templ.cols() + 1;
int result_rows = img.rows() - templ.rows() + 1;
Mat result = new Mat(result_rows, result_cols, CvType.CV_32FC1);

// / Do the Matching and Normalize
Imgproc.matchTemplate(img, templ, result, match_method);
Core.normalize(result, result, 0, 1, Core.NORM_MINMAX, -1, new Mat());

// / Localizing the best match with minMaxLoc
MinMaxLocResult mmr = Core.minMaxLoc(result);

Point matchLoc;
if (match_method == Imgproc.TM_SQDIFF || match_method == Imgproc.TM_SQDIFF_NORMED) {
matchLoc = mmr.minLoc;
} else {
matchLoc = mmr.maxLoc;
}

// / Show me what you got
Core.rectangle(img, matchLoc, new Point(matchLoc.x + templ.cols(),
matchLoc.y + templ.rows()), new Scalar(0, 255, 0));

// Save the visualized detection.
System.out.println("Writing "+ outFile);
Highgui.imwrite(outFile, img);

}
}

public class TemplateMatching {
public static void main(String[] args) {
System.loadLibrary("opencv_java246");
new MatchingDemo().run(args[0], args[1], args[2], Imgproc.TM_CCOEFF);
}
}

最佳答案

读写函数已从 org.opencv.highgui.Highgui 移至 org.opencv.imgcodecs.Imgcodecs。绘图功能已从 org.opencv.core.Core 移至 org.opencv.imgproc.Imgproc。所以来源变成:

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.core.Scalar;
import org.opencv.imgcodecs.Imgcodecs;
import org.opencv.imgproc.Imgproc;

class MatchingDemo {
public void run(String inFile, String templateFile, String outFile,
int match_method) {
System.out.println("\nRunning Template Matching");

Mat img = Imgcodecs.imread(inFile);
Mat templ = Imgcodecs.imread(templateFile);

// / Create the result matrix
int result_cols = img.cols() - templ.cols() + 1;
int result_rows = img.rows() - templ.rows() + 1;
Mat result = new Mat(result_rows, result_cols, CvType.CV_32FC1);

// / Do the Matching and Normalize
Imgproc.matchTemplate(img, templ, result, match_method);
Core.normalize(result, result, 0, 1, Core.NORM_MINMAX, -1, new Mat());

// / Localizing the best match with minMaxLoc
MinMaxLocResult mmr = Core.minMaxLoc(result);

Point matchLoc;
if (match_method == Imgproc.TM_SQDIFF
|| match_method == Imgproc.TM_SQDIFF_NORMED) {
matchLoc = mmr.minLoc;
} else {
matchLoc = mmr.maxLoc;
}

// / Show me what you got
Imgproc.rectangle(img, matchLoc, new Point(matchLoc.x + templ.cols(),
matchLoc.y + templ.rows()), new Scalar(0, 255, 0));

// Save the visualized detection.
System.out.println("Writing " + outFile);
Imgcodecs.imwrite(outFile, img);

}
}

public class TemplateMatching {

public static void main(String[] args) {
System.loadLibrary("opencv_java300");
new MatchingDemo().run(args[0], args[1], args[2], Imgproc.TM_CCOEFF);
}

}

关于Opencv(3.0.0)模板匹配的Java实现,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31462915/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com