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.x, point1.y, x - point1.x, y - point1.y);
drag = 0;
roiImg = img(rect);
lol = img(rect);
putText(img, "1", Point(point1.x , point1.y ), 1, 1, Scalar(0, 255, 0), 0.5);
}
if (event == CV_EVENT_LBUTTONUP)
{
/* ROI selected */
select_flag = 1;
drag = 0;
}
}
///Main function
int main()
{
int k;
VideoCapture cap(1);
if (!cap.isOpened())
return 1;
/*
VideoCapture cap;
//cap.open("~/Downloads/opencv-2.4.4/samples/cpp/tutorial_code/HighGUI/video-input-psnr-ssim/video/Megamind.avi");
cap.open("./Megamind.avi");
if (!cap.isOpened())
{
printf("Unable to open video file\n");
return -1;
}*/
// Set video to 320x240
cap.set(CV_CAP_PROP_FRAME_WIDTH, 320);
cap.set(CV_CAP_PROP_FRAME_HEIGHT, 240);
cap >> img;
imshow("image", img);
while (1)
{
cap >> img;
if (img.empty())
break;
cv::flip( img, img, 1 );
putText(img, "[ESC] para detener", Point(5, 15), 1, 1, Scalar(0, 123, 231), 1);
if (key == true)
{
putText(img, "SI", Point(10, 80), 1, 1, Scalar(0, 255, 0), 1);
}else
{
putText(img, "NO", Point(10, 80), 1, 1, Scalar(0, 0, 255), 1);
}
if (rect.width == 0 && rect.height == 0)
cvSetMouseCallback("image", mouseHandler, NULL);
else
track(img, roiImg, rect);
if (select_flag == 1)
imshow("Template", roiImg);
imshow("image", img);
k = waitKey(go_fast ? 30 : 10000);
if (k == 27)
break;
}
return 0;
}