【问题标题】:OpenCv matching template in Video [Java]视频中的 OpenCv 匹配模板 [Java]
【发布时间】:2017-06-17 10:40:47
【问题描述】:

请我尝试在java中创建一个新的应用程序来匹配图片和视频,图片中模板的匹配工作正常,但是当我尝试为视频做这个时,我总是有这个错误消息:

OpenCV 错误:断言失败 ((depth == CV_8U || depth == CV_32F) && type == _templ.type() && _img.dims()

这是我的视频和图片匹配功能,请有人帮忙。

public int runVedio(String inFile, String templateFile, int match_method) {
        int nbr = 0;
        Mat templ = Imgcodecs.imread(templateFile);

        VideoCapture capture=new VideoCapture(inFile);
        Mat frame = new Mat();
        Mat result = new Mat();
        capture.read(frame); 

        // / Do the Matching and Normalize
        Imgproc.matchTemplate(frame,templ, result, match_method);
        Imgproc.threshold(result, result,0.9,1,Imgproc.THRESH_TOZERO);  

        //Core.normalize(result, result, 0, 1, Core.NORM_MINMAX, -1, new Mat());
        while(true)
        {
        // / Localizing the best match with minMaxLoc
        Core.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;
        }
        if(mmr.maxVal > 0.98)
         {
            // / Show me what you got
            Imgproc.rectangle(frame, matchLoc, 
                new Point(matchLoc.x + templ.cols(),matchLoc.y + templ.rows()), 
                new    Scalar(0,255,0),2);
            Imgproc.rectangle(result, matchLoc, 
                new Point(matchLoc.x + templ.cols(),matchLoc.y + templ.rows()), 
                new    Scalar(0,255,0),-1);     
            nbr++;
         }
         else
         {
             return nbr;
         }

        }

    }

【问题讨论】:

    标签: java opencv template-matching


    【解决方案1】:

    确保您可以正确访问视频以供使用:

    while(camera.read(frame)) 
    

    因为,这是一个视频,您需要访问其中的所有帧,因此请使用 while。

    还有你的结果图像,即

    Mat result = new Mat();
    

    必须像下面这样,使两个图像大小相同,颜色代码相同。

    所以改成这个,

    new Mat(frame.rows(), frame.cols(), Highgui.CV_LOAD_IMAGE_COLOR);
    

    运行代码并告诉我它工作的天气..

    【讨论】:

      猜你喜欢
      • 2016-03-08
      • 2013-02-28
      • 1970-01-01
      • 2015-10-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-01-03
      • 2018-10-02
      相关资源
      最近更新 更多