How can I add more selections to track at the same time?

asked 2016-03-31 14:35:41 -0500

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)
edit retag flag offensive close merge delete

Comments

This is a rather more general question than we usually answer. So I'll point you to this tutorial, which explains better what your code is doing. Once you have a better idea, try to get it working yourself. If it doesn't work, come back and say "This is what I'm doing and here's why, but this part isn't doing what I thought it should." Then we can help.

Tetragramm gravatar imageTetragramm ( 2016-03-31 15:32:15 -0500 )edit

Isn't this a comment rather than an answer?

LorenaGdL gravatar imageLorenaGdL ( 2016-03-31 15:48:57 -0500 )edit

In a way. More bluntly, we are here to help not do things for you. As I see it, the problem here is the part where you said "I dont understand the code at all". So I provided you with a link that will help you learn. If I simply gave you the code, you would be no closer to understanding, and I would have spent my time to no effect.

If you truly have no idea where to start, then start from the beginning with OpenCV, and with the state of your program. What variables are holding state between frames? Why are they holding what they do, and what needs to be different if you want to track a different target?

Tetragramm gravatar imageTetragramm ( 2016-03-31 16:21:27 -0500 )edit
1

I'm not the OP. Just saying (and this is my personal opinion) that answers should be given when they do answer the asked question. If the question is unclear, not enough effort has been made or further clarification is needed, a simple comment might be enough to highlight the need for an improvement. Your words are helpful, not denying that at all, but I don't feel they're an answer as they don't provide a "closure" to the issue. When using the search bar, I like finding posts with clear to-the-point answers, preferably with code/pseudocode snippets. I know mods try their best to keep the forum useful, but sometimes boundaries are unclear (maybe some guidelines could be provided?). Again, just my very personal and humble opinion (don't take it personally).

LorenaGdL gravatar imageLorenaGdL ( 2016-03-31 16:48:53 -0500 )edit

I dont understand the code at all so i dont know how to make more selections to track in the same window --> OH MY GOD ... please sir, start by reading about trackers and first figure out what your code is doing...

StevenPuttemans gravatar imageStevenPuttemans ( 2016-04-01 02:43:46 -0500 )edit
1

@LorenaGdL --> will keep the guidelines in mind, they will be added somewhere soon.

@Tetragramm --> as Lorena suggested, let us move your answer to a comment. Done as of now :)

StevenPuttemans gravatar imageStevenPuttemans ( 2016-04-01 02:46:06 -0500 )edit