运行效果
代码
#include <iostream>
#include "opencv2/opencv.hpp"
#include <time.h>
#include <string.h>
int main()
{
cv::namedWindow("camera", cv::WINDOW_FREERATIO);
cv::VideoCapture camera;
camera.open(0);
camera.set(cv::CAP_PROP_FRAME_WIDTH, 9999);
camera.set(cv::CAP_PROP_FRAME_HEIGHT, 9999);
cv::Mat frame;
camera.read(frame);
std::cout << "frame info->" << "width:" << frame.cols << ",height:" << frame.rows << std::endl;
clock_t begin, end;
int counter = 0;
double timeUse = 0;
double FPS = 0;
cv::Mat m0;
while (true)
{
if (counter == 0)
begin = clock();
camera.read(frame);
cv::flip(frame, frame, 1);
m0 = frame.clone();
counter++;
static char tempBuf[30];
static cv::Size textSize;
static int val;
if (counter == 10)
{
end = clock();
timeUse = (end - begin) / (double)CLOCKS_PER_SEC;
FPS = 1 / timeUse * 10;
sprintf(tempBuf, "%04.1f", FPS);
textSize = cv::getTextSize(tempBuf, cv::FONT_HERSHEY_PLAIN, 3.0, 2, &val);
std::cout << "val:" << val << std::endl;
counter = 0;
}
cv::putText(m0, tempBuf, cv::Point(frame.cols - textSize.width, textSize.height + 10), cv::FONT_HERSHEY_PLAIN,
3.0, cv::Scalar(0, 0, 255), 2);
cv::imshow("camera", m0);
if (cv::waitKey(1) == '1')
{
break;
}
}
cv::destroyAllWindows();
camera.release();
return 0;
}