OpenCV Basics - Miscellaneous

转自OpenCV入门教程

1. <opencv2/opencv.hpp>

该文件已经包含了各模块头文件:

#ifndef __OPENCV_ALL_HPP__
#define __OPENCV_ALL_HPP__

#include "opencv2/core.hpp"
#include "opencv2/imgproc.hpp"
#include "opencv2/photo.hpp"
#include "opencv2/video.hpp"
#include "opencv2/features2d.hpp"
#include "opencv2/objdetect.hpp"
#include "opencv2/calib3d.hpp"
#include "opencv2/imgcodecs.hpp"
#include "opencv2/videoio.hpp"
#include "opencv2/highgui.hpp"
#include "opencv2/ml.hpp"

#endif

原则上,在写opencv程序时,可以只包含头文件opencv.hpp,这样可以精简优化代码

2. CV_VERSION

cout该宏,可以检查程序所用的OpenCV库的版本号

3. imwrite

vector<int>compression_params;
compression_params.push_back(IMWRITE_PNG_COMPRESSION);
compression_params.push_back(9);
imwrite("存图名称.png", mat, compression_params);
  • JPEG:params表示0到100的图片质量(CV_IMWRITE_JPEG_QUALITY),默认值为95
  • PNG:params表示压缩级别(CV_IMWRITE_PNG_COMPRESSION),从0到9,其值越大,压缩尺寸越小,压缩时间越长;
  • PPM / PGM / PBM:params表示二进制格式标志(CV_IMWRITE_PXM_BINARY),取值为0或1,默认是1。

4. imshow - OpenGL show GpuMat

cuda::GpuMat d_image;
d_image.upload(image);
namedWindow("Gpumat", WINDOW_OPENGL);  // 设定支持OpenGL!
cuda::cvtColor(d_image, d_image, cv::COLOR_BGR2GRAY);
imshow("Gpumat", d_image);

moveWindow("Gpumat", 0, 200 );  // window也可以移动!

5. TrackBar Useage

6. Mouse Useage

7. Rect

Rect rect = rect1 | rect2;  // 矩形交集
rect = rect1 & rect2;  // 矩形并集
rect = rect + Point(20, -10);  // 矩形平移
rect = rect + Size(-30, -30);  // 矩形缩放

8. Get Time

double time0 = static_cast<double>(cv::getTickCount());
// do sth. to timing
time0 = ((double)cv::getTickCount() - time0)/cv::getTickFrequency();

9. LUT

批量进行图像元素的查找、扫描与操作

Mat lookupTable(1, 256, CV_8U);
// lookupTable 赋值,映射hash查表
cv::LUT(I, lookupTable, O);

另,cv::HammingLUT,用于计算hamming距离

unsigned char a[3] = {1,2,3}, b[3] = {2,3,4};
cv::HammingLUT lut;
std::cout << lut(a,b,3) << std::endl;  // 6 = 2+1+3

10. cv::saturate_cast

防止越界,越界则取最大值

11. File Output/Input: xml, yaml

write example

read example

Loading Disqus comments...
Table of Contents