java - 使用openCV模板匹配器,在没有所需对象的情况下,无论如何都能找到匹配项。如何获得异常(exception)?

标签 java opencv automated-tests

我需要检查银行卡上的 Logo 。是否可以以这样的方式修复我的代码:当 Logo 不匹配时,系统不会搜索相似的元素,而是抛出异常?

我尝试更改阈值方法中的变量,但没有结果。

  private static void isTemplateImageExist(String sourceScreenshotPath,
                                                 String sourceScreenshot,
                                                 String templateScreenshotPath,
                                                 String templateImage,
                                                 String resultImage){
            CustomLogger.info("Getting images for comparing");
            System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
            Mat source = Highgui.imread(sourceScreenshotPath + sourceScreenshot, Highgui.CV_LOAD_IMAGE_COLOR);
            Mat template = Highgui.imread(templateScreenshotPath + templateImage);
            Mat outputImage = new Mat();
            int machMethod = Imgproc.TM_CCOEFF;
            CustomLogger.info("Comparing images");
            //Template matching method
            Imgproc.threshold(source, outputImage, 1, 250, Imgproc.THRESH_TOZERO);
            Imgproc.matchTemplate(source, template, outputImage, machMethod);
            drawRectangleOnResultImage(outputImage,source,template,sourceScreenshotPath,resultImage);
        }

如果 Logo 不匹配,我希望看到异常。

最佳答案

使用outputImage获取最小值和最大值。检查最大值是否大于某个所需的阈值。如果最大值不大于该阈值,则抛出异常

关于java - 使用openCV模板匹配器,在没有所需对象的情况下,无论如何都能找到匹配项。如何获得异常(exception)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56393694/

相关文章:

java - vText 语音密码不会在 Android 上加载

java - Kotlin 无法获取正确的枚举实例

testing - 如何对 native 应用程序进行自动设备测试?

用于 LBP 训练的 OpenCV traincascade

testing - 可移植、独立于工具的测试自动化

java - 我将如何在 scala 程序中编译和执行 java 程序?

java - 使用对象和接口(interface)从 Map 对象获取 key

java - 如何将一个类的方法用于另一个类的私有(private)成员对象

linux - 使用静态 OpenCV 库交叉编译 ARM 时出现 undefined reference

opencv - 相机标定opencv