Time detemination

I want to use the system clock (or time) but in milliseconds.
I tried different functions such as clock(), time_t and suseconds_t.
clock() gives nonsense digits and suseconds_t does not change. Since I am newbie in programming and I need to calculate the time for each loop in milliseconds; Can you check what is wrong with my headings or what to use for such a purpose?
Thanks alot!

#include "opencv2/objdetect/objdetect.hpp"
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/core/core.hpp"

#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <time.h>
#include <ctime>
#include <fstream>

using namespace std;
using namespace cv;



/** Function Headers */
int detectAndDisplay( Mat frame, int start );







/** @function main */
int main( int argc, const char** argv )
{

clock_t start;
start = clock();
int nn;



while( true )
{
{ cout << t<<endl;
nn = detectAndDisplay( frame, start );
start = nn;
//cout <<"nn= "<<nn<<endl;
//cout <<"mt ="<< nn<<endl;
}

else
{ printf(" --(!) No captured frame -- Break! \n\n"); break; }

int c = waitKey(10);
if( (char)c == 'c' ) { break; }
}
}
return 0;
}

/** @function detectAndDisplay */
int detectAndDisplay( Mat frame , int start)
{
std::vector<Rect> maizes;
Mat frame_gray, imgHSV, ehsan;
ehsan = frame;
int mt = start;

//Convert the captured frame from BGR to HSV
cvtColor(frame, imgHSV, COLOR_BGR2HSV);

//Threshold the image --> RGB to binary
Mat imgThresholded;
inRange(imgHSV, Scalar(iLowH, iLowS, iLowV), Scalar(iHighH, iHighS, iHighV), imgThresholded);

//morphological opening (remove small objects from the foreground)
erode(imgThresholded, imgThresholded, getStructuringElement(MORPH_ELLIPSE, Size(rl, rl)) );
dilate( imgThresholded, imgThresholded, getStructuringElement(MORPH_ELLIPSE, Size(rl, rl)) );

//morphological closing (fill small holes in the foreground)
dilate( imgThresholded, imgThresholded, getStructuringElement(MORPH_ELLIPSE, Size(fl, fl)) );
erode(imgThresholded, imgThresholded, getStructuringElement(MORPH_ELLIPSE, Size(fl, fl)) );
imwrite("check.jpg", imgThresholded);
// Change back the binary image to RGB
Mat dest;
cvtColor(imgThresholded, imgThresholded, CV_GRAY2BGR);
bitwise_and(frame, imgThresholded, dest); //imshow("Filtered RGB", dest);*/

// Change back the RGB to hist.gray for better detection
cvtColor( dest, frame_gray, CV_BGR2GRAY );
equalizeHist( frame_gray, frame_gray );

//-- Detect maizes
maize_cascade.detectMultiScale( frame_gray, maizes, 1.1, 2, 0|CV_HAAR_SCALE_IMAGE, Size(30, 30) );

// Distance of two detected maize plants
//int dist_m;
if (maizes.size()==0)
{/*char blue[] = { 0x1b, '[', '1', ';', '3', '4', 'm', 0 };
char normal[] = { 0x1b, '[', '0', ';', '3', '9', 'm', 0 };
cout << blue << "************************No Maize****************" << normal << endl;
cout << blue << "No Maize detection" << normal << endl;
cout<<"clock"<<clock()<<endl;
cout << blue << "******************************************" << normal << endl;*/ }

else
{
size_t i = 0; Point center( maizes[i].x + maizes[i].width*0.5, maizes[i].y + maizes[i].height*0.5 );
int xm = maizes[i].x;
int ym = maizes[i].y;
int dist_m = sqrt(xm*xm + ym*ym);
// ellapsed time between two frames
clock_t el;
el = clock() - start;
//cout <<"check point for detection"<<endl;
int g=dist_m/el;
//cout <<"adad = "<<el<<endl;
if (dist_m/el>=0)
{/*cout <<"-----------------"<<endl;
cout <<"A new Maize is detected"<<endl;
cout <<"at Time ="<< clock()<<endl;
cout <<"-----------------"<<endl;*/

ellipse( frame, center, Size( maizes[i].width*0.5, maizes[i].height*0.5), 0, 0, 360, Scalar( 0, 0, 255 ), 4, 8, 0 );
mt = clock();
}
}

//imshow( window_name, frame );
//imshow("Gray Segmentation", frame_gray);
return mt;
}

Last edited on
Hi,

Does this help? It is written by one of our most respected and knowledgeable person on this forum :+) http://www.cplusplus.com/forum/general/193311/#msg930382

Btw, could you please always use code tags in the future? http://www.cplusplus.com/articles/z13hAqkS/

Good Luck !!
Thank you very much for your reply.
Actually I could not use the code you shared. I believe that timer is an important part of all computerized systems. Therefore, I wonder if to obtain the current time in milliseconds we need to use such a long code buddy.
I am using raspberry pi 2 and chrono heading is not defined on it.
I found the following code:

int main(int argc, char* argv[])
{
struct timeval tp;
gettimeofday(&tp, NULL);
long long mslong = (long long) tp.tv_sec * 1000L + tp.tv_usec / 1000; //get current timestamp in milliseconds
std::cout << mslong << std::endl;
}

Can you help me use it as a timer rather showing the entire seconds since many days ago?
Can you help me use it as a timer rather showing the entire seconds since many days ago?


Well if you have a timestamp in milliseconds, then all you need to do is subtract the end time from the beginning.

Please always use code tags :+)

I am using raspberry pi 2 and chrono heading is not defined on it.


Is it that the header file doesn't exist, or you don't have C++11 or C++14 enabled?

... we need to use such a long code buddy.


It was only long because it timed several different things, the heart of it is a simple subtraction.
Topic archived. No new replies allowed.