c++ 小技巧 (C++ tips)

 

1. c++ map 类型定义键(key)和空值(value)(c++ map define keys and null values)

从 Python 带来的习惯,在使用 python 的时候,经常定义个字典以及他们的键,但是键的值往往是空列表或者空字典,然后在后续的处理中再慢慢进行添加元素。在 python 中只要使用如下命令即可:

1 >>> d={}
2 >>> d["a"]=[]
3 >>> d
4 {'a': []}
5 >>> d["box"]={}
6 >>> d
7 {'a': [], 'box': {}}

现转到了 C++,但还是习惯类似的用法,所以一直在寻找可以实现定义好键然后让键的值为空的方法,尝试了下,发现可以使用以下方法:

1 map<string, vector<Rect>> Result;
2 Result["text"];
3 Result["image"];
4 Result["table"];

 

2. C++ 计算图像非零像素个数 (c++ calucate the number of non-zero pixels in the image)

https://answers.opencv.org/question/55441/count-pixels-of-a-black-and-white-image-opencv-c/

 1 #include <iostream>
 2 #include <opencv2/opencv.hpp>
 3 
 4 using namespace std;
 5 using namespace cv;
 6 
 7 
 8 int main()
 9 {
10     Mat imageCanva(100, 100, CV_8UC1, Scalar(255, 255, 255));
11     vector<Point> whitePixels;
12     findNonZero(imageCanva, whitePixels);
13     int num = whitePixels.size();
14     cout << "The number of white pixels : " << num << endl;
15 }

 

3. 

posted @ 2020-03-13 15:05  ttweixiao9999  阅读(218)  评论(0编辑  收藏  举报