简体   繁体   中英

OpenCv matching template in Video [Java]

please i try to make a new application in java for matching picture and video, the matching of template in picture working fine , but when i try to do it for video i always have this error message :

OpenCV Error: Assertion failed ((depth == CV_8U || depth == CV_32F) && type == _templ.type() && _img.dims() <= 2) in cv::matchTemplate, file C:\\builds\\master_PackSlaveAddon-win64-vc12-static\\opencv\\modules\\imgproc\\src\\templmatch.cpp, line 1062 Exception in thread "main" CvException [org.opencv.core.CvException: cv::Exception: C:\\builds\\master_PackSlaveAddon-win64-vc12-static\\opencv\\modules\\imgproc\\src\\templmatch.cpp:1062: error: (-215) (depth == CV_8U || depth == CV_32F) && type == _templ.type() && _img.dims() <= 2 in function cv::matchTemplate ]

This is my function for matching video with picture , someone can help please .

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;
         }

        }

    }

make sure you are accessing the video correctly for that you can use :

while(camera.read(frame)) 

Since, it's a video you need to access all the frames in it so use while.

And also your result image ie

Mat result = new Mat();

must go like below, so that both the images are of same size and are of same color code.

So change it to this ,

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

Run the code and tell me weather it works..

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM