2021年7月

“欲速则不达,见小利则大事不成。”

代码:

#include <iostream>
#include <opencv2/core.hpp>
#include <opencv2/videoio.hpp>
#include <opencv2/highgui.hpp>

using namespace std;
using namespace cv;
using namespace std;


int main(int argc, char* argv[])
{
    cout<<"OpenCV4 Example."<<endl;
    cout<<"OpenCV Version = "<<getVersionString()<<endl;
    webcam();
    
    return 0;
}

void webcam()
{
    Mat frame;
    VideoCapture cap;
    int deviceID = 0;             // 0 = open default camera
    int apiID = cv::CAP_ANY;      // 0 = autodetect default API
    cap.open(deviceID, apiID);
    if (!cap.isOpened()) {
        cerr << "ERROR! Unable to open camera\n";
        return;
    }
    cout << "Start grabbing" << endl << "Press any key to terminate" << endl;
    for (;;)
    {
        cap.read(frame);
        if (frame.empty()) {
            cerr << "ERROR! blank frame grabbed\n";
            break;
        }
        imshow("Live", frame);
        if (waitKey(40) >= 0)
            break;
    }
}

运行结果:

./opencv-demo 
OpenCV4 Example.
OpenCV Version = 4.5.2
Start grabbing
Press any key to terminate

运行效果:
opencv-webcam-capture.png

代码:

#include <iostream>
#include <opencv2/core.hpp>
#include <opencv2/videoio.hpp>
#include <opencv2/highgui.hpp>
#include "opencv2/objdetect.hpp"
#include <opencv2/imgproc.hpp>

using namespace std;
using namespace cv;

void face_detect()
{
    Mat frame;
    VideoCapture cap;
    cap.open("http://ivi.bupt.edu.cn/hls/cctv2.m3u8");
    if (!cap.isOpened()) {
        cerr << "ERROR! Unable to open video\n";
        return;
    }
    double fps = cap.get(cv::CAP_PROP_FPS);
    cout << "fps = "<< fps <<endl;
    //获取cap视频流的每帧大小
    int width = cap.get(cv::CAP_PROP_FRAME_WIDTH);
    int height = cap.get(cv::CAP_PROP_FRAME_HEIGHT);
    cout<<"width = "<<width <<", height = "<<height<<endl;
    Size S(width, height);

    Scalar blue(255, 0, 0);
    CascadeClassifier classifier;
    classifier.load("./xml/haarcascade_frontalface_alt.xml");
    std::vector<Rect> detected_objects;
    int cnt = 0;
    for (;;)
    {
        cap.read(frame);
        if (frame.empty()) {
            cerr << "ERROR! blank frame grabbed\n";
            break;
        }
        if(cnt++ % 5 == 0)
        {           
            classifier.detectMultiScale( frame, detected_objects);
            for ( size_t i = 0; i < detected_objects.size(); i++ )
            {
                rectangle(frame, detected_objects[i], blue, 3);
            } 
        }

        imshow("Live", frame);
        if (waitKey(38) >= 0)
            break;
    }
}

运行结果:

./opencv-demo OpenCV4 Example.
OpenCV Version = 4.5.2
[NULL @ 0x556838ec4680] non-existing SPS 0 referenced in buffering period
[NULL @ 0x556838ec4680] SPS unavailable in decode_picture_timing
[h264 @ 0x556838eed000] non-existing SPS 0 referenced in buffering period
[h264 @ 0x556838eed000] SPS unavailable in decode_picture_timing
fps = 25
width = 720, height = 576
[h264 @ 0x55683916bfc0] error while decoding MB 30 33, bytestream -5
[h264 @ 0x556838f67040] error while decoding MB 23 1, bytestream -15
[h264 @ 0x556838fcca00] error while decoding MB 30 14, bytestream -11
[h264 @ 0x55683916bfc0] error while decoding MB 9 27, bytestream -9

运行效果:
opencv4-face-detect.png

<bits/stdc++.h>
万能的头文件bits/stdc++.h,几乎包含所有的可用到的C++库函数。以后写代码就可以只引用这一个头文件。

/** @file stdc++.h
 *  This is an implementation file for a precompiled header.
 */

文件内容:

// 17.4.1.2 Headers

// C
#ifndef _GLIBCXX_NO_ASSERT
#include <cassert>
#endif
#include <cctype>
#include <cerrno>
#include <cfloat>
#include <ciso646>
#include <climits>
#include <clocale>
#include <cmath>
#include <csetjmp>
#include <csignal>
#include <cstdarg>
#include <cstddef>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>

#if __cplusplus >= 201103L
#include <ccomplex>
#include <cfenv>
#include <cinttypes>
#include <cstdalign>
#include <cstdbool>
#include <cstdint>
#include <ctgmath>
#include <cuchar>
#include <cwchar>
#include <cwctype>
#endif

// C++
#include <algorithm>
#include <bitset>
#include <complex>
#include <deque>
#include <exception>
#include <fstream>
#include <functional>
#include <iomanip>
#include <ios>
#include <iosfwd>
#include <iostream>
#include <istream>
#include <iterator>
#include <limits>
#include <list>
#include <locale>
#include <map>
#include <memory>
#include <new>
#include <numeric>
#include <ostream>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdexcept>
#include <streambuf>
#include <string>
#include <typeinfo>
#include <utility>
#include <valarray>
#include <vector>

#if __cplusplus >= 201103L
#include <array>
#include <atomic>
#include <chrono>
#include <codecvt>
#include <condition_variable>
#include <forward_list>
#include <future>
#include <initializer_list>
#include <mutex>
#include <random>
#include <ratio>
#include <regex>
#include <scoped_allocator>
#include <system_error>
#include <thread>
#include <tuple>
#include <typeindex>
#include <type_traits>
#include <unordered_map>
#include <unordered_set>
#endif

#if __cplusplus >= 201402L
#include <shared_mutex>
#endif

使用万能头文件有利有不利,但优点肯定大于缺点。
优点

  • 在竞赛中节约时间;
  • 减少了编写所有必要头文件的工作量;
  • 对于使用的每个函数,不用记住GNU C++的所有STL;

缺点

  • 不属于GNU C++库的标准头文件,在部分情况下可能会失败;
  • 使用它将包含许多不必要的东西,并增加编译时间;
  • 这个头文件不是C++标准的一部分,因此是不可移植的,应该避免;
  • 编译器每次编译翻译单元时都必须实际读取和分析每个包含的头文件;

TLE是(time limit exceeded)缩写,超出时间限制。

关于输入输出优化有一条让不要用cin/cout,而是用scanf和printf.
Change methods of Input-Output: You must choose proper input-output functions and data structure that would help you in optimization.
In C++, do not use cin/cout – use scanf and printf instead.

其实在看了分析文章《C++的輸出入cin/cout和scanf/printf誰比較快?》

只需要注意到以下三个地方,就可以愉快的使用cin/cout了。
1:sync_with_stdio 函數:和stdio同步

std::ios_base::sync_with_stdio(false);

2:endl 和 flush 物件:cout的缓冲区优化

简单点就是使用cout<'\n'替换cout<<endl;

3:cin.tie(0):cin和cout綁定

cin.tie(0);

最后是测试代码

ios_base::sync_with_stdio(false);
cin.tie(0);
for(int i = 0; i < (int)1e7; i++){
    cin>>a;
    cout<<a+1<<'\n';
}
for(int i = 0; i < (int)1e7; i++){
    scanf("%d\n",&a);
    printf("%d\n",a+1); 
}

直接上代码,asn文件如下:
ASN Example

DEFINITIONS AUTOMATIC TAGS ::= 
BEGIN 

StructWList ::= SEQUENCE OF ListItem 
ListItem ::= INTEGER 
END

c/c++ Example

int main()
{

ListItem_t *li = nullptr;
StructWList_t swl = {0};

swl.list.count = 0;
swl.list.size = 0;
li = reinterpret_cast<ListItem_t*>(calloc(1, sizeof *li));
ASN_SEQUENCE_ADD(&swl, li);
ASN_STRUCT_FREE_CONTENTS_ONLY(asn_DEF_StructWList, &swl);

}