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!
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 );
// 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;*/
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?