Opencv 小知识

mat结构有图像头和数据区两部分组成,下面的ABC虽然有各自不同的图像头,但数据区一样,操作修改其中一个其他的数据都变化
Mat A, C; // creates just the header parts
A = imread(argv[1], CV_LOAD_IMAGE_COLOR); // here we’ll know the method used (allocate matrix)
Mat B(A); // Use the copy constructor
C = A; // Assignment
所以常定义感兴趣区域,只不过图像头不一样,只操作原数据区的一部分
Mat D (A, Rect(10, 10, 100, 100) ); // using a rectangle
2 Mat E = A(Range:all(), Range(1,3)); // using row and column boundaries
1.输出图像的内存分配是自动完成的
2.不需要考虑内存的释放
3.赋值运算和构造只拷贝图像头
4.可通过clone()或者copyTo()来复制图像
1 Mat F = A.clone();
2 Mat G;
3 A.copyTo(G);


CV_[每个值的字节数][有符号或无符号][数据类型]C[The channel number]
Mat E = Mat::eye(4, 4, CV_64F);
cout << "E = " << endl << " " << E << endl << endl;
Mat O = Mat::ones(2, 2, CV_32F);
cout << "O = " << endl << " " << O << endl << endl;
Mat Z = Mat::zeros(3,3, CV_8UC1);
cout << "Z = " << endl << " " << Z << endl << endl;


Mat M;
M.create(4,4, CV_8UC(2));
自定义
Mat C = (Mat_<double>(3,3) << 0, -1, 0, -1, 5, -1, 0, -1, 0);
cout << "C = " << endl << " " << C << endl << endl;
随机矩阵
Mat R = Mat(3, 2, CV_8UC3);
randu(R, Scalar::all(0), Scalar::all(255));
//融合图像
Mat roi(m_currentimg, Rect(10,10,100,100));
Mat otherroi(m_otherimg, Rect(10,10,100,100));
addWeighted(roi,0.5,otherroi,0.5,0,roi);


//画线,矩形,圆,椭圆
>>画圆和矩形时,线的后杜设为-1是,填充矩形和圆
/*line(m_currentimg,Point(10,10),Point(100,100),RGB(255,0,0),3,8);
rectangle(m_currentimg,Point(10,10),Point(100,100),RGB(255,0,0),3,8);
circle(m_currentimg,Point(200,200),100,RGB(0,255,0),3,8);
ellipse(m_currentimg,Point(200,200),Size(50,30),0,0,180,RGB(0,255,0),3,8);*/


//绘制文字
double fontScale = 1;//字体大小缩放
int thickness = 3;//粗细
Mat img(m_currentimg);
int baseline=0;
Size textSize = getTextSize(text, fontFace,
fontScale, thickness, &baseline);
baseline += thickness;
// center the text,文字左下角位置
Point textOrg((img.cols - textSize.width)/2,
(img.rows + textSize.height)/2);
// draw the box
rectangle(img, textOrg + Point(0, baseline),
textOrg + Point(textSize.width, -textSize.height),
Scalar(0,0,255));
// ... and the baseline first
line(img, textOrg + Point(0, thickness),
textOrg + Point(textSize.width, thickness),
Scalar(0, 255, 0));
// then put the text itself
putText(img, text, textOrg, fontFace, fontScale,
Scalar::all(255), thickness, 8);


//访问矩阵数据的4种方法
/*int channels = I.channels();//127
int nRows = I.rows * channels;
int nCols = I.cols;
if (I.isContinuous())
{
nCols *= nRows;
nRows = 1;
}
int i,j;
uchar* p;
for( i = 0; i < nRows; ++i)
{
p = I.ptr<uchar>(i);
for ( j = 0; j < nCols; ++j)
{
p[j] = table[p[j]];
}

}*/


/*MatIterator_<uchar> it, end;//127
for( it = I.begin<uchar>(), end = I.end<uchar>(); it != end; ++it)

*it = table[*it];*/


/*for( int i = 0; i < I.rows; ++i)//122
for( int j = 0; j < I.cols; ++j )

I.at<uchar>(i,j) = table[I.at<uchar>(i,j)];*/


/*Mat lookUpTable(1, 256, CV_8U);
uchar* p = lookUpTable.data;
for( int i = 0; i < 256; ++i)
p[i] = table[i];
LUT(I, lookUpTable,I);*/


//算法运行时间统计
double t = (double)getTickCount();
//.....处理的算法
t = ((double)getTickCount() - t)*1000/getTickFrequency();
cout << "Times passed in seconds: " << t <<"ms"<< endl;




new_image.at<Vec3b>(y,x)[c] =saturate_cast<uchar>( alpha*( image.at<Vec3b>(y,x)[c] ) + beta );
saturate_cast<uchar>();该函数是传入参数为有效的uchar型


膨胀:将内核覆盖区域的最大相素值提取,并代替锚点位置的相素。图像中的亮区开始”扩展”;
腐蚀:它提取的是内核覆盖下的相素最小值,亮区变细
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值