1.使用opencv扫描图像,查找表及时间测量
1.1 时间测量
double t=(double)getTickCount();
//do sonething
t=((double)getTickCount()-t)/getTickFrequency();
cout<<"times passed in second:"<<t<<endl;
1.2 图像矩阵存储
Mat& ScanImageAndReduce(Mat&I,const uchar*const table)
{
//accept only char type matrices
CV_Assert(I,depth()==CV_8U);
int channels=I.channels();
int nRows=I,rows;
int nCols=I.cols*channels;
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]];
}
}
return I;
}
//其他 形式
uchar* p=I.data;
for(unsigned int i=0;i<ncols*nrows;++i)
*p++=table[p*];
1.3. 迭代器方式
Mat& ScanImageAndReduceRandomAccess(Mat& I, const uchar* const table)
{
// accept only char type matrices
CV_Assert(I.depth() == CV_8U);
const int channels=I.channels();
switch(channels)
{
case 1:
{
for(int i=0;i<I.rows;++i)
for(int j=0;j<I.cols;++j)
I.at<uchar>(i,j)==table[I.at<uchar>(i,j)];
break;
}
case 3:
{
Mat_<Vec3b> _I=I;
for(int i=0;i<I.rows;++i)
for(int j=0;j<I.cols;++j)
{
_I(i,j)[0]=table[_I(i,j)[0]];
_I(i,j)[1]=table[_I(i,j)[1]];
_I(i,j)[2]=table[_I(i,j)[2]];
}
I=_I;
break;
}
}
return I;
}
//kakaka
//cv::LUT()
Mat lookUpTable(1,256,CV_8U);
uchar* p=lookUpTable.ptr();
for(int i=0;i<256;++i)
{
p[i]=table[i];
}
2.掩码操作
锐化
void Sharpen(const Mat&myImage,Mat& result)
{
CV_Assert(myImage.depth()-CV_8U);
const int nChannels=myImage.channels();
Result.create(myImage.size(),myImage.type());
for(int j=1;j<myImage.rows-1;j++){
const uchar* previous=myImage.ptr<uchar>(j-1);
const uchar* current=myImage.ptr<uchar>(j);
const uchar* next =myImage.ptr<uchar>(j+1);
uchar* output=Result.ptr<uchar>(j);
for(int i=nChannels;i<nChannels*(myImage.cols-1);++i)
{
*output++=saturate_cast<uchar>(5*current[i]-current[i-nChannels]-
current[i+nChannels]-previous[i]-next[i]);
}
}
Result.row(0).setTo(Scalar(0));
Result.row(Result.rows-1).setTo(Scalar(0));
Result.col(0).setTo(Scalar(0));
Result.row(Result.cols-1).setTo(Scalar(0));
3.改变图像对比度和亮度
Mat new_image = Mat::zeros( image.size(), image.type() );
double alpha = 1.0; /*< Simple contrast control */
int beta = 0; /*< Simple brightness control */
cout << " Basic Linear Transforms " << endl;
cout << "-------------------------" << endl;
cout << "* Enter the alpha value [1.0-3.0]: "; cin >> alpha;
cout << "* Enter the beta value [0-100]: "; cin >> beta;
for(int y=0;y<image.rows;y++){
for(int x=0;x<image.cols;x++){
for(int c=0;c<image.channels();c++){
new_image.at<Vec3b>(y,x)[c]=
saturate_cast<uchar>(alpha*image.at<Vec3b>(y,x)[c]+beta);
}
}
}
Code for the gamma correction:
Mat lookUpTable(1,256,CV_8U);
uchar* p=lookUpTable.ptr();
for(int i=0;i<256;++i)
p[i]=saturate_cast<uchar>(pow(i/255.0,gamma_)*255.0);
Mat res=img.clone();
LUT(img,lookUpTable,res);
4. Mat 常用操作
4.1 定义矩阵
//cv::Size(w,h) //长、宽(列、行)
cv::Mat M(cv::Size(3, num), CV_32FC1, cv::Scalar::all(0));
cv::Mat B=M.clone();
cv::Mat M=cv::Mat::zeros(cv::Size(3,num),CV_32FC1);//全零矩阵
cv::Mat M=cv::Mat::ones(cv::Size(3,num),CV_32FC1);//全1矩阵
//初始化并赋值
Mat kernel = (Mat_<char>(3, 3) << 0, -1, 0,
-1, -5, -1,
0, -1, 0);
Mat kernel = (Mat_<float>(3, 3) << 2.0, -1, 0,
-1, -5, -1,
0, -1, 0);
4.2 像素读取
Mat M(cv::Size(3,3),CV_32FC1);
Mat M_(cv::Size(3,3),CV_64FC1);
Mat M_p(cv::Size(3,3),CV_8UC1);
Mat M_c(cv::Size(3,3),CV_8UC3);
for(int i=0;i<M.rows;++i)
{
for(int j=0;j<M.cols;++j)
{
cout<<M.at<float>(i,j)<<endl;
cout<<M.at<double>(i,j)<<endl;//CV_64FC1
cout<<M.at<uchar>(i,j)<<endl;//CV_8UC1
cout<<M.at<Vec3b>(i,j)[0]<< " " <<M.at<Vec3b>(i,j)[1] <<
M.at<Vec3b>(i,j)[2]<<endl;//CV_8UC3
}
}
*****指针*****
for (int i = 0; i < num - 1; ++i)
{
float* ptr0 = M.ptr<float>(i);
for (int j = i + 1; j < num; ++j)
{
cout<<ptr0[j]<<endl;
}
}
4.3 矩阵运算
//行列读取
M.col(i)
M.row(j)
//每行作做减法
int k = 0;
for (int i = 0; i < num - 1; ++i)
{
for (int j = i + 1; j < num; ++j)
{
B.row(k) = M.row(j) - M.row(i);
k++;
}
}
矩阵区域赋值
D(cv::Range(0, 3), cv::Range(0, 3)) = B.t() * B;
//D(cv::Range(0, 3), cv::Range(3, 4)) = A;
A.copyTo(D(cv::Range(0, 3), cv::Range(3, 4)));
D(cv::Range(3, 4), cv::Range(0, 3)) = A.t();
L3(cv::Range(0,3),cv::Range(0,1)) = B.t() * L2;
L3.row(3) = 1;
//也可用Rect类设置Mat感兴趣区域,在此不做介绍
roi=M(rect(x,y,w,h))
or
p1=Point(x1,y1);
p2=Point(x2,y2);
roi=M(rect(p1,p2));