第一次接触OpenCV,学习的几个实例

VS2015编译opencv3.1.0 xfeatures2d

https://blog.csdn.net/a876657621/article/details/78232451

vs2015 opencv配置参见:https://blog.csdn.net/hxiaohai/article/details/50600521
如下几个参考实例:
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
using namespace cv;
using namespace std;
int main()
{
#if 0 //拼接
 vector< Mat > vImg;
 Mat rImg, Img;
 Img = imread("1.jpg");
 if (!Img.data)
 {
  cout << "the image is not exist!" << endl;
  return -1;
 }
 vImg.push_back(imread("1.jpg"));
 vImg.push_back(imread("2.jpg"));
 vImg.push_back(imread("3.jpg"));
 Stitcher stitcher = Stitcher::createDefault(false);
 unsigned long AAtime = 0, BBtime = 0;  //check processing time  
 AAtime = getTickCount();           //check processing time  
 stitcher.stitch(vImg, rImg);
 BBtime = getTickCount();           //check processing time   
 printf("%.2lf sec \n", (BBtime - AAtime) / getTickFrequency()); //check processing time  
 resize(rImg, rImg, Size(800, 600), 0, 0, CV_INTER_LINEAR);
 namedWindow("result");
 imshow("result", rImg);
 waitKey(0);
#endif
#if 1 //camera 
 cv::VideoCapture videoCapture = cv::VideoCapture(0);
 cv::namedWindow("Video");
 if (videoCapture.isOpened())
 {
  cout << "camera is opened" << endl;
 }
 else
 {
  cout << "camera is not opened" << endl;
  return -1;
 }
 Mat frame;
 string strCurDate;
 int counts = 1;

 while (counts)
 {
  videoCapture.read(frame);//从摄像头中读取当前这一帧
  std::time_t now = std::time(NULL);
  // 定义两个变量,存储转换结果
  struct tm tmTmp;
  char stTmp[128];
  // 转换为字符串并输出
  memset(stTmp, 0, sizeof(stTmp));
  //asctime_s(stTmp, &tmTmp);
  // 转换为tm结构
  localtime_s(&tmTmp, &now);
  strftime(stTmp, sizeof(stTmp), "%Y/%m/%d %H:%M:%S", &tmTmp);
  char *p = strstr(stTmp, "\n");
  if (p != NULL) *p = 0;
  strCurDate = stTmp;
  
  cv::putText(frame, strCurDate, cv::Point(2, videoCapture.get(CV_CAP_PROP_FRAME_HEIGHT)-20), cv::FONT_ITALIC, 0.5, cv::Scalar(255, 0, 0), 1, cv::LINE_AA);
  imshow("Video", frame);
  waitKey(30);
  counts++;
 }
 videoCapture.release();
 cv::destroyWindow("Video"); /*显示窗口销毁*/
 waitKey(0);
#endif

#if 0 //
 string text = "Funny text inside the box";
 int fontFace = FONT_HERSHEY_SCRIPT_SIMPLEX;
 double fontScale = 2;
 int thickness = 3;
 Mat img(600, 800, CV_8UC3, Scalar::all(0));
 int baseline = 0;
 Size textSize = getTextSize(text, fontFace,
  fontScale, thickness, &baseline);
 baseline += thickness;
 // center the text
 Point textOrg((img.cols - textSize.width) / 2,
  (img.rows + textSize.height) / 2);
 // draw the box
 rectangle(img, textOrg + Point(0, baseline),
  textOrg + Point(textSize.width, -textSize.height),
  Scalar(0, 0, 255));
 // ... and the baseline first
 line(img, textOrg + Point(0, thickness),
  textOrg + Point(textSize.width, thickness),
  Scalar(0, 0, 255));
 // then put the text itself
 putText(img, text, textOrg, fontFace, fontScale,
  Scalar::all(255), thickness, 8);
 namedWindow("Video");
 imshow("Video", img);
 waitKey(0);
 cv::destroyWindow("Video"); /*显示窗口销毁*/
#endif


}


你可能感兴趣的:(OpenCV)