目录
-
Mat 类快速矩阵写法
Mat m = (Mat_<int>(2,2)<<1,0,0,-1)
-
num2Str
//应该不用前两个,我没试
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
using namespace std;
string convertToString(double d);
int main(int argc, char** argv)
{
Mat img1 = imread(file, -1);
pyrDown(img1, img1, Size(img1.cols/2, img1.rows/2));
imshow("1",img1);
putText(img1, convertToString(basetest1), Point(50, 90), CV_FONT_HERSHEY_COMPLEX, 1, Scalar(0,0,0), 2 );
putText(img1, "Origional CORREL", Point(50, 40), CV_FONT_HERSHEY_COMPLEX, 1, Scalar(0,0,0), 2 );
imshow("img", img1);
imwrite("13.jpg", img1);
waitKey();
return 1;
}
string convertToString(double d)
{
ostringstream os;
if (os << d)
{
return os.str();
}
return "invalid conversion";
}
-
Mat矩阵先声明,后初始化的写法
//就是拆开来了,自己没想到
Mat img;
img = Mat(200, 200, CV_8UC1, Scalar::all(0));
-
C++的文件读写
#include <iostream>
#include <fstream>
using namespace std;
void main()
{
ifstream file_in ("read.txt"); /* 读取的文件 */
ofstream file_out("write.txt"); /* 写入的文件 */
string filename;
while (getline(file_in, filename ))
{
cout << filename << endl;
}
file_out << "测试内容" << "\t" << endl;
system("pause");
return ;
}
-
数字转字符串或者拼接字符串
#include <iostream>
#include <fstream>
int main(int argc, char**argv){
ostringstream os;
os << "这里写字符串 " << i ;
imshow(os.str(), colorImg);
return 0;
}
-
OpenCV 二值化快速写法
#include <opencv2/opencv.hpp>
using namespace cv;
Mat gray;
int edgeThresh = 100;
int main(int argc, char ** argv){
gray = imread("stuff.jpg", 0);
Mat edge = gray >= edgeThresh;
imshow("edge", edge);
threshold(gray, gray, edgeThresh, 255, THRESH_BINARY);
imshow("gray", gray);
waitKey();
return 1;
}
-
导入dll文件
#include "Test.h"
#pragma comment(lib, "Test.lib")
-
Opencv Mat与数组互转
Array to Mat
___________________________________________________
double arrayA[2] = {1,2};
Mat A = Mat(1, 2, CV_64FC1, arrayA);
Mat to Array
____________________________________________
Mat a = Mat(3, 3, CV_64FC1, Scalar::all(0));
double* A = reinterpret_cast<double*>(a.data);
// 其实很简单,只是注意这里的指针类型强转就好
-
清空输入缓存区
scanf("%*[^\n]");
scanf("%*c");
-
结构体初始化
#include <iostream>
using namespace std;
struct XYZ1{
int x;
int y;
int z;
};
typedef struct XYZ2{
int x;
int y;
int z;
};
int main(){
XYZ1 a = {1,2,3};
XYZ2 b = {1,2,3};
getchar();
return 0;
}
-
cv::String 与 CString 互转
CString -> cv::String
USES_CONVERSION;
CString str = "abcdefg";
cv::String cvStr = W2A(str);
cv::String -> CString
USES_CONVERSION;
cv::String b = "abcdefg;
std::string c = b;
CString a = A2W(c.c_str());