随记

目录

Mat 类快速矩阵写法

num2Str 

Mat矩阵先声明,后初始化的写法

C++的文件读写

数字转字符串或者拼接字符串

OpenCV 二值化快速写法

导入dll文件

Opencv Mat与数组互转

清空输入缓存区

结构体初始化

cv::String 与 CString 互转


  • 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());

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值