【问题标题】:Java implementation of Opencv (3.0.0) template matchingOpencv(3.0.0)模板匹配的Java实现
【发布时间】:2015-10-06 09:54:34
【问题描述】:

我想使用 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);
    }
}

【问题讨论】:

  • 啊,这很简单;) Imgcodecs.imwrite(...), Imgcodecs.imread(...), Imgproc.rectangle(...) (据我所知)现在)

标签: java opencv opencv3.0 highgui matchtemplate


【解决方案1】:

OpenCV 3.0.0 不再有 Highui。请改用 Imgcodecs.imread。

点击此链接。

The import org.opencv.highgui cannot be resolved

【讨论】:

    【解决方案2】:

    读写功能已从 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);
        }
    
    }
    

    【讨论】:

      猜你喜欢
      • 2014-01-29
      • 1970-01-01
      • 1970-01-01
      • 2013-02-28
      • 1970-01-01
      • 2017-06-17
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多