gpt4 book ai didi

java - 如何匹配多个对象

转载 作者:行者123 更新时间:2023-12-02 06:29:37 25 4
gpt4 key购买 nike

如何使用单个模板匹配多个对象?
我想通过阈值匹配多个对象。

当我匹配单个对象时,我使用了这段代码。

System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
Mat img = Highgui.imread("/test/test_img.jpg");//input image
if(img.empty())
throw new Exception("no image");
Mat tpl = Highgui.imread("/test/test_tpl.jpg");//template image
if(tpl.empty())
throw new Exception("no template");
Mat result = new Mat();
Imgproc.matchTemplate(img, tpl,result,Imgproc.TM_CCOEFF_NORMED);//Template Matching
Core.MinMaxLocResult maxr = Core.minMaxLoc(result);
Point maxp = maxr.maxLoc;
Point maxop = new Point(maxp.x + tpl.width(), maxp.y + tpl.height());
Mat dst = img.clone();
Core.rectangle(dst, maxp, maxop, new Scalar(255,0,0), 2);//draw a rectangle
Highgui.imwrite("/test/test.jpg", dst);//save image

最佳答案

这个对我有用:

    Mat img = Highgui.imread("/test/test_img.jpg");//input image
if(img.empty())
throw new Exception("no image");
Mat tpl = Highgui.imread("/test/test_tpl.jpg");//template image
if(tpl.empty())
throw new Exception("no template");
Mat result = new Mat();
Imgproc.matchTemplate(img, tpl,result,Imgproc.TM_CCOEFF_NORMED);//Template Matching
Imgproc.threshold(result, result, 0.1, 1, Imgproc.THRESH_TOZERO);
double threshold = 0.95;
double maxval;
Mat dst;
while(true)
{
Core.MinMaxLocResult maxr = Core.minMaxLoc(result);
Point maxp = maxr.maxLoc;
maxval = maxr.maxVal;
Point maxop = new Point(maxp.x + tpl.width(), maxp.y + tpl.height());
dst = img.clone();
if(maxval >= threshold)
{
System.out.println("Template Matches with input image");

Core.rectangle(img, maxp, new Point(maxp.x + tpl.cols(),
maxp.y + tpl.rows()), new Scalar(0, 255, 0),5);
Core.rectangle(result, maxp, new Point(maxp.x + tpl.cols(),
maxp.y + tpl.rows()), new Scalar(0, 255, 0),-1);
}else{
break;
}
}
Highgui.imwrite("test.jpg", dst);//save image

例如

模板: coin

和结果: marioWorld

关于java - 如何匹配多个对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20185837/

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