1.Mat 的几种初始化方法:
通过外部指针赋值:
Mat m(height, width,CV_8UC(3),rgb) ;
初始化为任意像素:
初始化为0:Mat mask = Mat::zeros( pic.size(),CV_8UC1 );
imread 的第二个参数
>0 读取三通道
=0 读取灰度图
<0 按照其本来的通道读取,可以有alpha通道
2. Mat 读像素值,和写像素值
//read
Vec3b p= m[0].at<Vec3b>(0,0);
//write
_m[i].at<Vec3b>(k,j) = p[3];
unsigned char* p = &(dst.data[(j*width + i)*3 ]) ;
p[0],p[1], p[2] 分别代表BGR
Float 类型
Mat img1(ROWS , COLS , CV_32FC1);
for (int i=0; i<ROWS ; i++)
{
float* pData1=img5.ptr<float>(i);
for (int j=0; j<COLS ; j++)
{
pData1[j] = 3.2f;
}
}
3. 画矩形
rectangle( m, out, Scalar(0,0,255), 1, 8, 0 );
画圆: circle( img, center, WINDOW_WIDTH/32 , Scalar(0,0,255) , thickness, linType );
4. 存储图片
char filename[100];
sprintf( filename,"template_%d.png",i );
vector<int> params;
params.push_back(CV_IMWRITE_PNG_COMPRESSION);
params.push_back(9);
imwrite(filename, _m, params );
5. 将logo加到原图上
addWeighted(roi, 0.5, logo, 0.3, 0., roi);
1、 第1个参数,输入图片1,
2、第2个参数,图片1的融合比例
3、第3个参数,输入图片2
4、第4个参数,图片2的融合比例
5、第5个参数,偏差
6、第6个参数,输出图片
6. 颜色空间转换
cvtColor( src,dst, COLOR_YUV420sp2BGR)
7. 通道分离
split( src, channels );
img0 = channels.at(0);
8. 通道合并
9 直方图均衡化
equalizeHist( src, dst );
10 二值图像中寻找轮廓
findContours()
11 绘制轮廓
drawContours()
12 图片修复
inpaintMask = Mat::zeros(srcImage1.size(), CV_8U);
inpaint(srcImage1, inpaintMask, inpaintedImage, 3, CV_INPAINT_TELEA);
13 获取运算时间
unsigned long begin = getTickCount();
... ...
unsigned long interv = getTickCount()-begin;
int val = 1000*(interv)/getTickFrequency() ;
14 腐蚀和膨胀
//Point(-1,-1),内核中心点。省略时为默认值。
//iterations:膨胀次数。省略时为默认值1。
dilate(binary,bg,cv::Mat(),cv::Point(-1,-1),6)
腐蚀函数为erode, 参数和上述一样
15:waitKey()
waitKey()函数的功能是不断刷新图像,频率时间为delay,单位为ms。
返回值为当前键盘按键值。
所以显示图像时,如果需要在imshow(“xxxx”,image)后吐舌头加上waitKey()为大于等于0的数即可,那么程序将会停在显示函数处,不运行其他代码;直到键盘值为key的响应之后。
16 BGR转灰度图
17 bgr转换为lab空间为何转换后是0-255?
-
L = L * 255 \ 100, a = a + 128, b = b + 128
const std::string videoStreamAddress = "rtsp://192.168.1.156:554/ch1/1";
if(!vcap.open(videoStreamAddress)) {}
打开视频
VideoCapture inputVideo("test.mp4");
打开摄像头
VideoCapture cap(0);
cap.set( CV_CAP_PROP_FRAME_WIDTH,640);
cap.set( CV_CAP_PROP_FRAME_HEIGHT,480 );
if(!cap.isOpened()){
}
20 查找面积最大的轮廓,绘制轮廓, 匹配轮廓矩形
//【4】定义轮廓和层次结构
Mat dstImage( src.size(),CV_8U );
vector<vector<Point> > contours;
vector<Vec4i> hierarchy;
findContours( color_mask, contours, hierarchy,
CV_RETR_TREE, CV_CHAIN_APPROX_SIMPLE );
//找出面积最大的轮廓,并绘制。
for(int index = 0; index >= 0; index = hierarchy[index][0] )
{
Scalar color( rand()&255, rand()&255, rand()&255 );
//Scalar color( rand()&255, rand()&255, rand()&255 );
double g_dConArea = abs(contourArea(contours[index], true));
cout<<g_dConArea<<endl;
if( g_dConArea > 10000 )
drawContours( dstImage, contours, index, color, CV_FILLED, 8, hierarchy );
}
//查找矩形
RotatedRect box = minAreaRect( dstImage );
21 putText 写文字 函数的参数解释
void putText(Mat&img, const string& text, Point org, intfontFace, double fontScale, Scalar color, intthickness=1, int lineType=8, bool bottomLeftOrigin=false)
参数为
- img – 图像矩阵
- text – string型 文字内容
- org – 文字坐标,以左下角为原点
- fontFace – 字体类型 (包括 FONT_HERSHEY_SIMPLEX,FONT_HERSHEY_PLAIN, FONT_HERSHEY_DUPLEX, FONT_HERSHEY_COMPLEX,FONT_HERSHEY_TRIPLEX, FONT_HERSHEY_COMPLEX_SMALL, FONT_HERSHEY_SCRIPT_SIMPLEX, orFONT_HERSHEY_SCRIPT_COMPLEX,)
- fontScale –字体大小
- color – 字体颜色
- thickness – 字体粗细
- lineType – Line type. See the line for details.
- bottomLeftOrigin – When true, the image data origin is at the bottom-left corner. Otherwise, it is at the top-left corner.
如:putText(img, "blog.csdn.net/boksic" , Point(0, int(img.rows*0.9)), CV_FONT_HERSHEY_COMPLEX,img.cols/400, cvScalar(200, 200, 200, 0));