Ask Your Question

Locosuelto's profile - activity

2016-03-31 14:38:28 -0600 asked a question How can I add more selections to track at the same time?

I was playing around with opencv projects and just found and modified this code which uses a any camera to get the image and make one selection with the mouse to track items in real time, but now I really want it to track 3 items at the same time. I dont understand the code at all so i dont know how to make more selections to track in the same window.

Here's the code, I use visual studio, sorry if its a mess:

#include "stdafx.h"
#include <iostream>
#include "opencv2/opencv.hpp"
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/objdetect/objdetect.hpp>


#include <sstream>
#include <string>
#include <iostream>
#include <opencv/highgui.h>
#include <opencv/cv.h>

#include <stdlib.h>
#include <stdio.h>

#include <vector>

using namespace cv; 
using namespace std;

Point point1, point2; /* vertical points of the bounding box */
int drag = 0;
Rect rect; /* bounding box */
Mat img, roiImg; /* roiImg - the part of the image in the bounding box */
Mat img1;
int select_flag = 0;
bool go_fast = true;
    Point matchLoc;
            static Mat lol;

Mat mytemplate;

bool key = false;

void track(cv::Mat &img, const cv::Mat &templ, const cv::Rect &r )
{
    static int n = 0;
    if (select_flag)
    {
        templ.copyTo(mytemplate);
        select_flag = false;
        go_fast = true;
    }


    cv::Mat result;
    /// Do the Matching and Normalize
    matchTemplate( img, mytemplate, result, CV_TM_SQDIFF_NORMED );
    normalize( result, result, 0, 1, NORM_MINMAX, -1, Mat() );

    /// Localizing the best match with minMaxLoc
    double minVal; double maxVal; Point minLoc; Point maxLoc;

    minMaxLoc( result, &minVal, &maxVal, &minLoc, &maxLoc, Mat() );
    matchLoc = minLoc;

    rectangle( img, matchLoc, Point( matchLoc.x + mytemplate.cols , matchLoc.y + mytemplate.rows ), CV_RGB(0, 255, 0), 3, 8, 0);

    std::cout << matchLoc << "\n";
    stringstream ss;
ss << matchLoc;
string str = ss.str();

stringstream ss2;
ss2 << point1.x;
string str2 = ss2.str();

stringstream ss3;
ss3 <<  point1.y;
string str3 = ss3.str();

                 putText(img, str, Point(matchLoc.x, matchLoc.y - 10), 1, 1, Scalar(0, 255, 0), 0.5);
                 putText(img, "1", Point((matchLoc.x + mytemplate.cols)-10 , (matchLoc.y + mytemplate.rows )-10 ), 1, 1, Scalar(0, 255, 0), 0.5);

                  if(true){ 
                    putText(img, "1{", Point(0, 50), 1, 1, Scalar(0, 255, 255), 1);
                     putText(img, str2, Point(15, 50), 1, 1, Scalar(0, 255, 255), 1);
                     putText(img, ",", Point(45, 50), 1, 1, Scalar(0, 255, 255), 1);
                    putText(img, str3, Point(50, 50), 1, 1, Scalar(0, 255, 255), 1);
                 }
                  if (point1.x == matchLoc.x | point1.x > matchLoc.x -39 && point1.x < matchLoc.x +39 | point1.y > matchLoc.y -39 && point1.y < matchLoc.y +39| point1.y == matchLoc.y)
                  {
                      key = true;

                  }else
                  {
                      key = false;

                  }

}

///MouseCallback function

void mouseHandler(int event, int x, int y, int flags, void *param)
{
    if (event == CV_EVENT_LBUTTONDOWN && !drag)
    {
        /* left button clicked. ROI selection begins */
        point1 = Point(x, y);
        drag = 1;
    }

    if (event == CV_EVENT_MOUSEMOVE && drag)
    {
        /* mouse dragged. ROI being selected */
        Mat img1 = img.clone();
        point2 = Point(x, y);
        rectangle(img1, point1, point2, CV_RGB(255, 255, 255), 0.5);
        imshow("image", img1);
    }

    if (event == CV_EVENT_LBUTTONUP && drag)
    {


        point2 = Point(x, y);
        rect = Rect(point1 ...
(more)