opencv基础总结

数据类型(整数)

数据类型 字节数 数据类型 字节数
BOOL 4 sbyte 1
bytet 2 short 2
int 4 ushort 2
uint 4 uchar 1
long 8 ulong 8

宏定义数据类型

typedef  signed char        int8_t;   
typedef  unsigned char      uint8_t;     

typedef  short              int16_t;   
typedef  unsigned short     uint16_t; 

typedef  int                int32_t;  
typedef  unsigned int       uint32_t;
   
typedef  long long          int64_t;      
typedef  unsigned long long uint64_t;

随笔

  • abs、fabs、fabsf三个函数都是用来求一个数的绝对值,区别如下:
    int abs(int a); //处理int类型的数据取绝对值
    double fabs(double a); //处理double类型的数据取绝对值
    float fabsf(float a); //处理float类型的数据取绝对值
  • fmaxf(float x,float y); //返回参数的最大数值
  • std::floor(float x): 对x向下取整
  • std::ceil(float x): 对x向上取整
  • auto 类型:
    一是在变量声明时根据初始化表达式自动推断该变量的类型。适用于类型冗长复杂,模板类型等;使用auto关键字的变量必须有初始值。
    二是在声明函数时作为函数返回值的占位符。
  • 两个向量之间点乘和叉乘:
    在这里插入图片描述
    Opencv中Point点类只有二维点和三维点。
    Point2i:最后一个字母表示点中元素的类型。
    b=unsigned char ,w=unsigned short, s=short, i=int, f=float, d=double
    点乘的结果是一个标量。向量中对应元素乘积之和。
//Point2i类的数据成员x、y
	Point2i a(2,3);
	Point2i b(3,4);
	int c = a.dot(b);
	int f = b.dot(a);
	cout << "a.x=" << a.x << endl;
	cout << "a.y=" << a.y << endl;
	cout << "点乘:" << c << endl;
	cout << "点乘:" << f << endl;

叉乘的结果是一个向量。叉乘是针对三维点类的。
在这里插入图片描述

//注意叉乘的顺序
	Point3i d(2, 3, 8);
	Point3i e(3, 4, 5);
	cout << "d.x=" << d.x << endl;
	cout << "d.y=" << d.y << endl;
	cout << "d.z=" << d.z << endl;
	cout << "叉乘:" << d.cross(e) << endl;
	cout << "叉乘:" << e.cross(d) << endl;

opencv中的四维点类 cv::Scalar 四维双精度向量

	Scalar s(1, 2, 3, 4);
	Scalar s_new(s);
	Scalar s_result;
	s_result = s.mul(s_new);//对应元素相乘
	cout << "元素相乘:" << s_result << endl;

字符串

包含文件:string.h
函数名: strstr
函数原型:extern char *strstr(char *str1, char *str2);
功能:从字符串str1中查找是否有字符串str2, 如果有,从str1中的str2位置起,返回str1的指针,如果没有,返回null。
返回值:返回该位置的指针,如找不到,返回空指针。

//字符串初始化 
#include <iostream>
#include <string>
using namespace std;
//字符串初始化  
void strInit()
{
   
	cout << "字符串初始化:" << endl;

	string s1 = "abcdefg";  //初始化方式1  
	string s2("abcdefg");   //初始化方式2  
	string s3 = s2;         //通过拷贝构造函数 初始化s3  
	string s4(7, 's');       //初始化7个s的字符串  

	cout << "s1 = " << s1 << endl;
	cout << "s2 = " << s2 << endl;
	cout << "s3 = " << s3 << endl;
	cout << "s4 = " << s4 << endl;
}

//字符串遍历  
void strErgo()
{
   
	cout << "字符串遍历:" << endl;

	string s1 = "abcdefg";  //初始化字符串  

	//通过数组方式遍历  
	cout << "1、通过数组方式遍历:" << endl;
	for (int i = 0; i < s1.length(); i++)
	{
   
		cout << s1[i] << " ";
	}
	cout << endl;

	//通过迭代器遍历  
	cout << "2、通过迭代器遍历:" << endl;
	for (string::iterator it = s1.begin(); it != s1.end(); it++)
	{
   
		cout << *it << " ";
	}
	cout << endl;

	//通过at()方式遍历  
	cout << "3、通过at()方式遍历:" << endl;
	for (int i = 0; i < s1.length(); i++)
	{
   
		cout << s1.at(i) << " ";        //此方式可以在越界时抛出异常  
	}
	cout << endl;
}

//字符指针和字符串的转换  
void strConvert()
{
   
	cout << "字符指针和字符串的转换:" << endl;
	string s1 = "abcdefg";  //初始化字符串  

	cout << "string转换为char*:" << endl;
	//string转换为char*  
	cout << s1.c_str() << endl;  //s1.c_str()即为s1的char *形式  
	cout << "char*获取string内容:" << endl;
	//通过string类型给char * 赋值
	char buf[64] = {
    0 };
	s1.copy(buf, 7);//复制7个元素  
	cout << buf << endl;
}

//字符串连接  
void strAdd()
{
   
	cout << "字符串连接:" << endl;

	cout << "方式1:" << endl;
	string s1 = "123";
	string s2 = "456";
	s1 += s2;
	cout << "s1 = " << s1 << endl;

	cout << "方式2:" << endl;
	string s3 = "123";
	string s4 = "456";
	s3.append(s4);
	cout << "s3 = " << s3 << endl;
}
int main()
{
   
	//初始化  
	strInit();
	cout << endl;
	//遍历  
	strErgo();
	cout << endl;
	//字符指针类型和字符串转换  
	strConvert();
	cout << endl;
	//字符串连接  
	strAdd();
	cout << endl;
	system("pause");
	return 0;
}

Mat

  • 数据类型
    • S|U|F S–代表—signed int-----有符号整形
      U–代表–unsigned int—无符号整形
      F–代表–float--------------单精度浮点型
    • –C<number_of_channels>----代表一张图片的通道数
      1–灰度图片----------------是单通道图像
      2–RGB彩色图像---------是3通道图像
      3–带Alph通道的RGB图像–是4通道图像
  • 矩阵创建、初始化
cv::Mat  m(行数,列数,数据类型,赋初值)
cv::Mat  S(m1, n1, CV_32FC1, cv::Scalar::all(0));  
//矩阵:m1*n1  CV_32FC1矩阵中元素的类型 ,cv::Scalar::all(0)矩阵中元素初始化为0
cv::Mat b = cv::Mat::zeros(HEIGHT, WIDTH, CV_32F); 
Mat dest = Mat::zeros(src
  • 1
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值