0% found this document useful (0 votes)
14 views

Code MTT

Uploaded by

aadnanzia.2019
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views

Code MTT

Uploaded by

aadnanzia.2019
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 18

Image Processing

/*
* im_processing.cpp
*
* Created on: Sep 3, 2018
* Author: bilal
*/
#include <stdio.h>
#include <math.h>
#include <memory.h>
#include <vector>
#include "opencv2/opencv.hpp"
#include "opencv2/highgui.hpp"
#include "ImProcessing.hpp"
#include "Tracker.hpp"

using namespace std;


using namespace cv;

static double CalcError(obj_struct& tgt, obj_struct& cand);

static double w_pos = 0.4;


static double w_image = 0.6;
static double w_area = 0.5;
static double w_aspect = 0.5;
static double min_error;
static double min_image_error, image_error;

Mat frame_in, frame_gray, frame_bin;

bool verbose;
int min_size = 80;
int max_size = 12000;
int min_thresh = 120;
int max_thresh = 245;
int base_thresh = 150;

obj_struct ExtractBestObject( Mat& frame, Mat& frame2, vector<obj_struct>&


obj_array,
obj_struct tgt, vector<int> thresh_dist)
{
obj_struct tgt_meas; // Target extracted by image processing
// Image
processing is our
// instrument of
measurement

int j;
double min_error; // Max possible min_error
double error;

//************************************************
// Image Processing on base threshold
//************************************************
Preprocess(frame, frame2, base_thresh);
ExtractObjects(frame2, obj_array);

//************************
// Object Matching
//************************
j = BestMatch(tgt, obj_array);

return tgt_meas;

}
obj_struct ExtractBestObject(Mat& frame, Mat& frame2, vector<obj_struct>&
obj_array,
obj_struct tgt_pred)
{
vector<int> thresh_dist;
thresh_dist.clear();
return ExtractBestObject(frame, frame2, obj_array, tgt_pred,
thresh_dist);
}

// Preprocess the frame


// 1. Convert frame to GRAY scale
// 2. Convert to binary image
Mat Preprocess(Mat& src, Mat& dest, int thresh)
{
Mat frame;
cvtColor(src, frame, COLOR_BGR2GRAY);
threshold(frame, dest, thresh, 255, THRESH_BINARY);
return dest;
}

// Extract Objects from a frame


// by performing following steps
// 1. Call connectedComponentsWithStats()
// 2. Extract only objects of interest
// - within a threshold range
// - within a ROI
// 3. Convert the info to an array of objects
//
int ExtractObjects(Mat& frame, vector<obj_struct>& obj_array)
{
int m, n;
Mat labels, stats, centroids;
Mat stats2, centroids2;

m = connectedComponentsWithStats(frame, labels,
stats, centroids);
n = ExtractUsefulLabels(stats, stats2, centroids,
centroids2, min_size, max_size);

if(verbose)
cout << "Total labels: " << m << " "
<< "Useful labels: " << n << endl;
ToObjects(stats2, centroids2, obj_array);
return n;
}
int ExtractObjects(Mat& frame, vector<obj_struct>& obj_array, int thresh)
{
int m, n;
Mat labels, stats, centroids;
Mat stats2, centroids2;

m = connectedComponentsWithStats(frame, labels,
stats, centroids);
n = ExtractUsefulLabels(stats, stats2, centroids,
centroids2, min_size, max_size);

if(verbose)
cout << "Total labels: " << m << " "
<< "Useful labels: " << n << endl;
ToObjects(stats2, centroids2, obj_array, thresh);
return n;
}

obj_struct ObjMove(obj_struct& obj, int x, int y)


{
obj.x += x;
obj.y += y;
obj.cx += x;
obj.cy += y;
obj.pred_cx += x;
obj.pred_cy += y;

return obj;
}

int SearchObject(Mat& frame, obj_struct& obj, vector<int> thresh_dist)


{
int m, n;
Mat labels, stats, centroids;
Mat stats2, centroids2;
vector<obj_struct> obj_array;

Rect r;
r.x = obj.x - SWELL;
r.y = obj.y - SWELL;
r.width = obj.width + 2*SWELL;
r.height = obj.height + 2*SWELL;
int thresh = obj.thresh;

Rect boundary;
boundary.x = 0;
boundary.y = 0;
boundary.width = frame.cols;
boundary.height = frame.rows;

if(!Contains(boundary, r)){ // Object outside available region


return POOR_MATCH;
}

Mat roi = frame(r);


Mat frame_bin;
threshold(roi, frame_bin, thresh, 255, THRESH_BINARY);

m = connectedComponentsWithStats(frame_bin, labels,
stats, centroids);
n = ExtractUsefulLabels(stats, stats2, centroids,
centroids2, min_size, max_size);

obj_array.clear();
ToObjects(stats2, centroids2, obj_array, thresh);

for(auto th_dist:thresh_dist){
int th = thresh+th_dist;
if(th > max_thresh || th < min_thresh)
continue;
threshold(roi, frame_bin, th, 255, THRESH_BINARY);

m = connectedComponentsWithStats(frame_bin, labels,
stats, centroids);
n = ExtractUsefulLabels(stats, stats2, centroids,
centroids2, min_size, max_size);

ToObjects(stats2, centroids2, obj_array, th);


}

// Move to local coordinates


ObjMove(obj, -r.x, -r.y);
int j = BestMatch(obj, obj_array, thresh);
if(j == POOR_MATCH || j == OBJ_NO_FREE){// Object not found
}
else{
obj = obj_array[j];
}
// Move back to global coordinates
ObjMove(obj, r.x, r.y);
return j;
}
int ToObjects(Mat& stats, Mat& centroids, vector<obj_struct>& obj_array)
{
int i;
for(i=0;i<stats.rows;i++){
obj_struct obj;
memset(&obj, 0, sizeof(obj));
obj.x = stats.row(i).at<int>(CC_STAT_LEFT);
obj.y = stats.row(i).at<int>(CC_STAT_TOP);
obj.width = stats.row(i).at<int>(CC_STAT_WIDTH);
obj.height = stats.row(i).at<int>(CC_STAT_HEIGHT);
obj.area = stats.row(i).at<int>(CC_STAT_AREA);

obj.aspect = (double)obj.width/obj.height;
obj.cx = centroids.row(i).at<double>(0);
obj.cy = centroids.row(i).at<double>(1);

obj_array.push_back(obj);

}
return i;
}
int ToObjects(Mat& stats, Mat& centroids, vector<obj_struct>& obj_array, int
thresh)
{
int i;
for(i=0;i<stats.rows;i++){
obj_struct obj;
memset(&obj, 0, sizeof(obj));
obj.x = stats.row(i).at<int>(CC_STAT_LEFT);
obj.y = stats.row(i).at<int>(CC_STAT_TOP);
obj.width = stats.row(i).at<int>(CC_STAT_WIDTH);
obj.height = stats.row(i).at<int>(CC_STAT_HEIGHT);
obj.area = stats.row(i).at<int>(CC_STAT_AREA);

obj.aspect = (double)obj.width/obj.height;
obj.cx = centroids.row(i).at<double>(0);
obj.cy = centroids.row(i).at<double>(1);
obj.thresh = thresh;

obj_array.push_back(obj);
}
return i;
}

int ExtractUsefulLabels(Mat& in_stats, Mat& out_stats,


Mat& in_centroids, Mat& out_centroids, int min_size, int
max_size)
{
int i, j;
int area;

out_stats = in_stats.clone();
out_centroids = in_centroids.clone();
for(j=0,i=1;i<in_stats.rows;i++){
area = in_stats.at<int>(i,CC_STAT_AREA);
if(area > min_size && area < max_size){
in_stats.row(i).copyTo(out_stats.row(j));
in_centroids.row(i).copyTo(out_centroids.row(j));
j++;
}
}
out_stats.resize(j);
out_centroids.resize(j);
return j;
}

int DrawBBoxes(Mat& frame, const Scalar& color, vector<obj_struct>& obj_array)


{
int n = obj_array.size();
for(obj_struct obj: obj_array){
if(!obj.miss_num)
DrawBBox(frame, color, obj);
else
DrawBBox(frame, color/2, obj);

}
return n;
}
int DrawBBox(Mat& frame, const Scalar& color, obj_struct box)
{
Rect r;
r.x = box.x;
r.y = box.y;
r.width = box.width;
r.height = box.height;
rectangle(frame, r, color);
return 1;
}

int DrawCentroids(Mat& frame, const Scalar& color, vector<obj_struct>&


obj_array)
{
int n = obj_array.size();
for(obj_struct tgt: obj_array){
Rect r;
r.x = tgt.cx-2;
r.y = tgt.cy-2;
r.width = 5;
r.height = 5;
rectangle(frame, r, color);

}
return n;
}
int PrintObjID(Mat& frame, const Scalar& color, obj_struct box)
{
char s[100];
if(box.miss_num)
sprintf(s, "%03d %02d", box.obj_id, box.miss_num);
else
sprintf(s, "%03d", box.obj_id);

putText(frame, s, Point(box.x, box.y-8), FONT_HERSHEY_SIMPLEX, 0.3,


Scalar(255, 255, 255));

sprintf(s, "%03.0f %02.0f", box.thresh, 100*box.Pd_image);


putText(frame, s, Point(box.x, box.y+box.height+8),
FONT_HERSHEY_SIMPLEX, 0.3,
Scalar(255, 255, 255));

return 1;
}
int PrintObjIDs(Mat& frame, const Scalar& color, vector<obj_struct>& obj_array)
{
int n = obj_array.size();
for(obj_struct tgt: obj_array){
PrintObjID(frame, color, tgt);
}
return n;
}
int VacateFrame(Mat& frame, vector<obj_struct>& obj_array)
{
for(obj_struct obj: obj_array){
Rect r;
r.x = obj.x;
r.y = obj.y;
r.width = obj.width;
r.height = obj.height;
rectangle(frame, r, 0, FILLED);
}
return obj_array.size();
}
// return true if r1 contains r2
bool Contains(Rect r1, Rect r2)
{
int ex2, ey2;
ex2 = r2.x + r2.width - 1;
ey2 = r2.y + r2.height - 1;

if(!r1.contains(Point(r2.x, r2.y)))
return 0;
else if(!r1.contains(Point(r2.x, ey2)))
return 0;
else if(!r1.contains(Point(ex2, r2.y)))
return 0;
else if(!r1.contains(Point(ex2, ey2)))
return 0;
else
return 1;
}
bool Overlap(Rect r1, Rect r2)
{
int ex1, ey1;
int ex2, ey2;
ex1 = r1.x + r1.width - 1;
ey1 = r1.y + r1.height - 1;
ex2 = r2.x + r2.width - 1;
ey2 = r2.y + r2.height - 1;
// If one rectangle is to the left
if(ex1 < r2.x || ex2 < r1.x)
return 0;
// If one rectangle is above
else if(ey1 < r2.y || ey2 < r1.y)
return 0;
else
return 1;
}
bool Overlap(obj_struct obj1, obj_struct obj2)
{
Rect r1, r2;

r1.x = obj1.x;
r1.y = obj1.y;
r1.width = obj1.width;
r1.height = obj1.height;
r2.x = obj2.x;
r2.y = obj2.y;
r2.width = obj2.width;
r2.height = obj2.height;

return Overlap(r1, r2);


}
void RemoveOverlappingObjs(vector<obj_struct>& obj_array, vector<obj_struct>&
obj_array_local)
{
for(auto iter = obj_array.begin(); iter < obj_array.end(); iter++){
for(auto obj2:obj_array_local){
if(Overlap(*iter, obj2)){
obj_array.erase(iter);
iter--; // To compensate for erasing
break;
}
}
}
}

int BestMatch(obj_struct& tgt, vector<obj_struct>& obj_array)


{

unsigned int i, j;
double error, Pd, Pd_image;

if(obj_array.empty()){
min_error = 1;
return POOR_MATCH; // Future - some other value
}

j = 0;
min_error = CalcError(tgt, obj_array[0]);
min_image_error = image_error;

for(i=1;i<obj_array.size();i++){
error = CalcError(tgt, obj_array[i]);
if(error < min_error){
min_error = error;
min_image_error = image_error;
j = i;
}
}
Pd = 1-min_error;
Pd_image = 1-min_image_error;

if(Pd < MinPd){


return POOR_MATCH;
}
else if(obj_array[j].obj_id){ // Object already
matched
return OBJ_NO_FREE;
}
else{
obj_struct& obj = obj_array[j];

obj.Pd = Pd;
obj.Pd_image = Pd_image;

// Inherited Parameters
obj.obj_id = tgt.obj_id;
obj.state = tgt.state;
obj.pred_cx = tgt.pred_cx;
obj.pred_cy = tgt.pred_cy;
obj.vx = tgt.vx;
obj.vy = tgt.vy;
obj.pred_aspect = tgt.aspect;
obj.pred_area = tgt.area;
obj.pred_thresh = tgt.thresh;

// In case object re-detected


obj.miss_num = 0;

}
return j;
}
int BestMatch(obj_struct& tgt, vector<obj_struct>& obj_array, int th)
{

unsigned int i, j;
double error, Pd, Pd_image;

if(obj_array.empty()){
min_error = 1;
return POOR_MATCH; // Future - some other value
}

j = 0;
min_error = CalcError(tgt, obj_array[0]);
if(tgt.thresh > th)
min_error *= 0.99; // Bias towards higher threshold
min_image_error = image_error;

for(i=1;i<obj_array.size();i++){
error = CalcError(tgt, obj_array[i]);
if(obj_array[i].thresh > th)
error *= 0.99; // Bias towards higher threshold
if(error < min_error){
min_error = error;
min_image_error = image_error;
j = i;
}
}
Pd = 1-min_error;
Pd_image = 1-min_image_error;

if(Pd < MinPd){


return POOR_MATCH;
}
else if(obj_array[j].obj_id){ // Object already
matched
return OBJ_NO_FREE;
}
else{
obj_struct& obj = obj_array[j];

obj.Pd = Pd;
obj.Pd_image = Pd_image;

// Inherited Parameters
obj.obj_id = tgt.obj_id;
obj.state = tgt.state;
obj.pred_cx = tgt.pred_cx;
obj.pred_cy = tgt.pred_cy;
obj.vx = tgt.vx;
obj.vy = tgt.vy;
obj.pred_aspect = tgt.aspect;
obj.pred_area = tgt.area;
obj.pred_thresh = tgt.thresh;

// In case object re-detected


obj.miss_num = 0;

}
return j;
}

double MinErrorBestMatch(void)
{
return min_error;
}

static double CalcError(obj_struct& tgt, obj_struct& cand)


{

const double max_area_ratio = 1.5;


const double max_pos_error = 32;
const double max_aspect_ratio = 1.5;
double area_ratio;
double area_error;
double aspect_ratio;
double aspect_error;
double pos_error, pos_error_x, pos_error_y;
double e_net;

// ***********************
// Area error calculation
// ***********************

// Ratio of areas >= 1


if(tgt.area >= cand.area)
area_ratio = (double)tgt.area/cand.area;
else
area_ratio = (double)cand.area/tgt.area;
// Normalized area error
area_error = (area_ratio - 1)/(max_area_ratio - 1);

// Area error saturation


if(area_error > 1.0)
area_error = 1.0;

// ***************************
// Position error calculation
// ***************************

pos_error_x = fabs((tgt.cx - cand.cx)/max_pos_error);


pos_error_y = fabs((tgt.cy - cand.cy)/max_pos_error);

// Position error saturation


if(pos_error_x > 1.0)
pos_error_x = 1.0;
if(pos_error_y > 1.0)
pos_error_y = 1.0;

pos_error = (pos_error_x + pos_error_y)/2;

// *************************
// Aspect error calculation
// *************************

// Ratio of aspects >= 1


if(tgt.aspect >= cand.aspect)
aspect_ratio = (double)tgt.aspect/cand.aspect;
else
aspect_ratio = (double)cand.aspect/tgt.aspect;

// Normalized area error


aspect_error = (aspect_ratio - 1)/(max_aspect_ratio - 1);

//aspect_error = fabs(tgt.aspect - cand.aspect)/max_aspect_error;

//Aspect error saturation


if(aspect_error > 1)
aspect_error = 1;

image_error = w_area*area_error + w_aspect*aspect_error;


if(image_error > 1)
image_error = 1;

// Net Error
e_net = w_pos*pos_error + w_image*image_error;

if(e_net > 1.0) // Only for finite precision effects


e_net = 1.0;

return e_net;
}

static volatile int selected;

static void onMouse( int event, int x, int y, int, void * s)


{
if( event != EVENT_LBUTTONDOWN )
return;
Point p = Point(x,y);

vector<obj_struct> * objs = (vector<obj_struct> *)s;


// obj_struct a;
int i;
i = 0;
for(auto a:*objs){
Rect r;
r.x = a.x;
r.y = a.y;
r.width = a.width;
r.height = a.height;
if(r.contains(p)){
selected = i;
break;
}
i++;
}

obj_struct SelectObject(String window, Mat& frame, int thresh)


{

Mat frame2;
vector<obj_struct> objs;

selected = -1;

Preprocess(frame, frame2, thresh);

ExtractObjects(frame2, objs);

DrawBBoxes(frame, Scalar(0,0,255), objs);

imshow(window, frame);
setMouseCallback(window, onMouse, &objs);
while(selected < 0)
waitKey(100);

setMouseCallback(window, NULL, NULL );

objs[selected].thresh = thresh;

return objs[selected];

Tracker
/*
* Tracker.cpp
*
* Created on: Sep 1, 2018
* Author: bilal
*/

#include <stdlib.h>
#include <math.h>
#include <memory.h>
#include "opencv2/opencv.hpp"
#include "ImProcessing.hpp"
#include "Tracker.hpp"

using namespace std;


using namespace cv;

int master_obj_count;
extern vector<int> thresh_dist;
extern Mat frame_in, frame_bin;
// Following function operates globally as well as locally

int CorrelateObjsComplete(vector<obj_struct>& obj_array_prev,


vector<obj_struct>& obj_array, vector<int>& th_dist)
{
int i=0, j;
vector<obj_struct> obj_array_temp;
vector<obj_struct> obj_array_local;

// Local operation
for(auto obj:obj_array_prev){
if(obj.state <=1){ // Initially only global search
obj_array_temp.push_back(obj);
continue;
}
j = SearchObject(frame_gray, obj, th_dist);
if(j<0){ // Object not found -> try globally
if(obj.miss_num < MAX_MISS_NUM){
UpdateTrajectory(obj);
obj_array_temp.push_back(obj);
}
}
else
obj_array_local.push_back(obj);
}
//VacateFrame(frame_bin, obj_array_local);
//imshow("Processed Video", frame_bin);
//waitKey(1000);
obj_array.clear();
ExtractObjects(frame_bin, obj_array, base_thresh);
//DrawBBoxes(frame_in, Scalar(0, 0, 255), obj_array);
//DrawBBoxes(frame_in, Scalar(255, 255, 0), obj_array_local);
//imshow("Video", frame_in);
//waitKey(100);
RemoveOverlappingObjs(obj_array, obj_array_local);
obj_array_prev = obj_array_temp;
CorrelateObjs(obj_array_prev, obj_array);
obj_array.insert(obj_array.begin(),
obj_array_local.begin(), obj_array_local.end());
return obj_array.size();

// Following function operates only globally


int CorrelateObjs(vector<obj_struct>& obj_array_prev, vector<obj_struct>&
obj_array)
{
int i=0, j;
vector<obj_struct> obj_array_temp;

for(auto obj:obj_array_prev){
j = BestMatch(obj, obj_array);
switch(j){
case OBJ_NO_FREE: // Already matched
// Future implementation// Select better of the two
case POOR_MATCH: // Object not found
if(obj.state <= 1){ // Missed before
initialization
// No action deletes the object
}else if(obj.miss_num < MAX_MISS_NUM){
obj.miss_num++;
UpdateTrajectory(obj);
obj_array_temp.push_back(obj);
//obj_array.insert(obj_array.begin(), obj);
//obj_array.push_back(obj);
//cout << obj.cx << endl;
}
break;
default:
break;
}
i++;
}

obj_array.insert(obj_array.begin(),
obj_array_temp.begin(), obj_array_temp.end());

for(auto& obj:obj_array){
if(!obj.obj_id){ // New Object
obj.obj_id = ++master_obj_count;
obj.state = 0;
}
}

return i;
}

obj_struct TrackObj(obj_struct& tgt_meas, obj_struct& tgt_est)


{
double K, K_thresh;
double K1, K2;
double lambda = 0.7; // Forgetting factor

switch(tgt_meas.state){
case 0: // Initialization only
tgt_meas.pred_cx = tgt_meas.cx;
tgt_meas.pred_cy = tgt_meas.cy;
tgt_meas.pred_aspect= tgt_meas.aspect;
tgt_meas.pred_area = tgt_meas.area;
tgt_meas.pred_thresh= tgt_meas.thresh;
tgt_meas.state++;
tgt_est = tgt_meas;
return tgt_meas;

break;
case 1: // Start tracking
K = 1;
K1 = 1;
K2 = 1;
K_thresh = 0;
tgt_meas.state++;
break;
case 2: // High initial gain
case 3:
case 4:
case 5:
lambda = 0.5;
K = 1-lambda;
K1 = 1-lambda*lambda;
K2 = (1-lambda)*(1-lambda);
K_thresh = 0.5;
tgt_meas.state++;
break;
default: // Continue tracking
lambda = 0.8;
//lambda = 1.5 - tgt_meas.Pd_image;
if(lambda > 0.9)
lambda = 0.9;
K = 1-lambda;
K1 = 1-lambda*lambda;
K2 = (1-lambda)*(1-lambda);
K_thresh = 0.4;
}

// Prediction errors
double pred_cx_err = tgt_meas.cx - tgt_meas.pred_cx;
double pred_cy_err = tgt_meas.cy - tgt_meas.pred_cy;
double pred_area_err = tgt_meas.area - tgt_meas.pred_area;
double pred_aspect_err = tgt_meas.aspect - tgt_meas.pred_aspect;
double pred_thresh_err = tgt_meas.thresh - tgt_meas.pred_thresh;

// Correction
tgt_est = tgt_meas;
tgt_est.cx = tgt_meas.pred_cx + K1 *
pred_cx_err;
tgt_est.vx = tgt_meas.vx + K2 *
pred_cx_err;
tgt_est.cy = tgt_meas.pred_cy + K1 *
pred_cy_err;
tgt_est.vy = tgt_meas.vy + K2 *
pred_cy_err;
tgt_est.area = tgt_meas.pred_area + K * pred_area_err;
tgt_est.aspect = tgt_meas.pred_aspect + K * pred_aspect_err;
tgt_est.thresh = tgt_meas.pred_thresh + K_thresh * pred_thresh_err;
if(tgt_est.thresh > max_thresh) tgt_est.thresh = max_thresh;

// Prediction
tgt_est.x += tgt_est.cx - tgt_meas.cx; // Required for drawing BBox
tgt_est.y += tgt_est.cy - tgt_meas.cy;

tgt_meas.vx = tgt_est.vx;
tgt_meas.vy = tgt_est.vy;
tgt_meas.pred_cx = tgt_est.cx + tgt_est.vx;
tgt_meas.pred_cy = tgt_est.cy + tgt_est.vy;
tgt_meas.pred_aspect= tgt_est.aspect;
tgt_meas.pred_area = tgt_est.area;
tgt_meas.pred_thresh= tgt_est.thresh;

return tgt_meas;
}

obj_struct& UpdateTrajectory(obj_struct& obj)


{
obj.x = obj.x + obj.vx;
obj.y = obj.y + obj.vy;
obj.cx = obj.pred_cx;
obj.cy = obj.pred_cy;
obj.area = obj.pred_area;
obj.aspect = obj.pred_aspect;
obj.thresh = obj.pred_thresh;
return obj;
}

int TrackObjects(vector<obj_struct>& obj_array, vector<obj_struct>&


obj_est_array)
{
int i;
obj_struct obj_pred, obj_est;

obj_est_array.clear();
for(i=0;i<obj_array.size();i++){
obj_pred = TrackObj(obj_array[i], obj_est);
obj_est_array.push_back(obj_est);
}
return i;
}

Multi Target Tracker

//============================================================================
// Name : First.cpp
// Author : Bilal
// Version : 1.0
//============================================================================

#include <iostream>
#include <vector>
#include "opencv2/opencv.hpp"
#include "Tracker.hpp"
#include "ImProcessing.hpp"
#include <opencv2/videoio.hpp>

using namespace std;


using namespace cv;

extern vector<obj_struct> cands;

vector<obj_struct> obj_array_prev, obj_array;


vector<obj_struct> obj_est_array;
vector<int> thresh_dist = { -2, 2 };

int main() {

obj_struct tgt_pred; // Predicted target for next frame


obj_struct tgt_est; // Target estimated at current time
obj_struct tgt_meas; // Target extracted by image processing
// Image
processing is our
// instrument of
measurement
static struct timespec current, prev;
char s[100];
int i;
namedWindow("Processed Video", 1);
namedWindow("Video", 1);

//VideoCapture cap("/home/bilal/Videos/Wildlife.wmv");
VideoCapture cap("/home/bilal/Videos/Birds1.avi");
int crop_top = 50;
int crop_bottom = 300;
int frame_width = cap.get(CAP_PROP_FRAME_WIDTH);
int frame_height = cap.get(CAP_PROP_FRAME_HEIGHT);
VideoWriter
video("Birds1_processed.wmv",VideoWriter::fourcc('W','M','V','1'),30,
//VideoWriter video("Birds1_processed.avi",VideoWriter::fourcc('M', 'J',
'P', 'G'),30,
Size(frame_width,(frame_height - crop_bottom)*2));

//VideoCapture cap(0);
if (!cap.isOpened())
return -1;

double n = cap.get(CAP_PROP_FRAME_COUNT);
double x = cap.get(CAP_PROP_FRAME_WIDTH);
double y = cap.get(CAP_PROP_FRAME_HEIGHT);
cap.set(CAP_PROP_FRAME_WIDTH, x);
cap.set(CAP_PROP_FRAME_HEIGHT, y);

double prev_n = 0;

cout << n << endl << x << "x" << y << endl;

cout << "Number of logical CPUs: " << getNumberOfCPUs() << endl;

int thresh = 100;


Mat frame, frame2;
Mat labels, stats, centroids, stats2, centroids2;

for (i = 0; i < 1; i++)


cap >> frame2; // First frames ignored

obj_array_prev.clear();
for (; i < 350; i++) {
Mat frame_temp;
cap >> frame_temp;
Rect roi = Rect(0,crop_top,frame_width, frame_height-
crop_bottom);
Mat frame_in = frame_temp(roi);
Mat frame_disp;
frame_in.copyTo(frame_disp);
frame_disp.resize(frame_in.rows*2);

clock_gettime( CLOCK_REALTIME, &prev);

cvtColor(frame_in, frame_gray, COLOR_BGR2GRAY);

Preprocess(frame_in, frame_bin, base_thresh);

CorrelateObjsComplete(obj_array_prev, obj_array, thresh_dist);


TrackObjects(obj_array, obj_est_array);
obj_array_prev = obj_array;
DrawBBoxes(frame_in, Scalar(0, 0, 255), obj_array);
DrawBBoxes(frame_in, Scalar(255, 255, 0), obj_est_array);
PrintObjIDs(frame_in, Scalar(255, 0, 0), obj_array);
sprintf(s, "Frame no: %d", i);
putText(frame_in, s, Point(0, 50), FONT_HERSHEY_SIMPLEX, 0.6,
Scalar(255, 255, 255));
clock_gettime( CLOCK_REALTIME, &current);

double difference = (current.tv_sec - prev.tv_sec)


+ (double) (current.tv_nsec - prev.tv_nsec) /
1000000000.0;
sprintf(s, "Timer: %4.3f", difference * 1000);
{
cout << s << " " << endl;
}

imshow("Processed Video", frame_bin);


frame_in.copyTo(frame_disp(Rect(0, frame_in.rows, frame_in.cols,
frame_in.rows)));
imshow("Video", frame_disp);
video.write(frame_disp);
waitKey(30);
}

exit(0);
for (i = 1; i < 1000; i++) {
cap >> frame_in;
Preprocess(frame_in, frame_bin, base_thresh);
ExtractObjects(frame_bin, obj_array);
CorrelateObjs(obj_array_prev, obj_array);
TrackObjects(obj_array, obj_est_array);
obj_array_prev = obj_array;
DrawBBoxes(frame_in, Scalar(0, 0, 255), obj_array);
DrawBBoxes(frame_in, Scalar(255, 255, 0), obj_est_array);
PrintObjIDs(frame_in, Scalar(255, 0, 0), obj_array);
sprintf(s, "Frame no: %d", i);
putText(frame, s, Point(0, 50), FONT_HERSHEY_SIMPLEX, 0.6,
Scalar(255, 255, 255));
imshow("Processed Video", frame_bin);
imshow("Video", frame_in);

(waitKey(30) <= 0);


}

exit(0);

for (i = 1; i < 100; i++) {


cap >> frame_in;

clock_gettime( CLOCK_REALTIME, &prev);

//************************
// Image Processing
//************************
tgt_meas = ExtractBestObject(frame, frame2, cands, tgt_pred,
thresh_dist);

if (tgt_meas.Pd < 0.7) {


tgt_meas = tgt_pred;
tgt_meas.Pd = 0.7;
}
//************************
// Tracker
//************************
// tgt_pred = TrackObj(tgt_meas, tgt_est);
//************************
// Update display
//************************
DrawBBoxes(frame, Scalar(0, 0, 255), cands);
DrawCentroids(frame, Scalar(0, 255, 0), cands);
DrawBBoxes(frame2, 255, cands);
DrawCentroids(frame2, 0, cands);

DrawBBox(frame, Scalar(255, 0, 0), tgt_meas);


DrawBBox(frame, Scalar(255, 255, 0), tgt_est);
sprintf(s, "Probability of Detection: %4.2f", tgt_meas.Pd);
putText(frame, s, Point(0, 50), FONT_HERSHEY_SIMPLEX, 0.6,
Scalar(255, 255, 255));
sprintf(s, "Frame No: %i", i);
putText(frame, s, Point(0, 80), FONT_HERSHEY_SIMPLEX, 0.6,
Scalar(255, 255, 255));

sprintf(s, "Threshold: %3.1f", tgt_est.thresh);


putText(frame, s, Point(0, 110), FONT_HERSHEY_SIMPLEX, 0.6,
Scalar(255, 255, 255));

clock_gettime( CLOCK_REALTIME, &current);

imshow("Processed Video", frame2);


imshow("Video", frame);
double difference = (current.tv_sec - prev.tv_sec)
+ (double) (current.tv_nsec - prev.tv_nsec) /
1000000000.0;
sprintf(s, "Timer: %4.3f", difference * 1000);
{
cout << s << " " << endl;
}

n = cap.get(CAP_PROP_POS_MSEC);
//cout << n - prev_n << endl;
(waitKey(n - prev_n) <= 0);
//(waitKey(1)<=0);
prev_n = n;

return 0;
}

You might also like