c++ opencv 常用指令

1.       指针

1)     定义指针最后要删除

float *p = new float[9];

delete []p;

 

 

2.      图片

1)     显示、销毁窗口和保存

itoa(i+1,mid,10);

namedWindow(mid,0);  //0为适应窗口大小

imshow(mid,views[i]);

waitKey();           // waitKey()等待用户输入,可显示出图片

destroyWindow(mid);  //销毁窗口

road =prefix+mid+end;

imwrite(road,views[i]);

 

 

3.     Mat:

1)       Mat初始化:

Mat cammat =Mat::eye(3, 3, CV_32F);

Mat discoeff =Mat::zeros(8, 1, CV_32F);

 

2)      图像元素的访问


三通道:

Vec3i bgr =mat_CV_8UC3.at<Vec3b>(nrow,ncol);

bgr.val[0];bgr.val[1]; bgr.val[2];    分别代表蓝绿红

 

单通道:

uchar val =mat_CV_8UC1.at<uchar>(nrow,ncol);        

int(val);                                 灰度值

 

4.     Vector

头文件 #include<vector>

 

1)       定义:vector<Mat> views;     矩阵数组

vector< vector< Point2f > > pts2;     2维点的数组    

P1x

P2x

P3x

P4x

P1y

P2y

P3y

P4y

  vector<vector< Point3f > > pts3;     3维点的数组

P1x

P2x

P3x

P4x

P1y

P2y

P3y

P4y

P1z

P2z

P3z

P4z

 

2)      Vector元素的添加  :push_back()

For(i=0;i<N;i++){

Mat tempview=Mat::zeros(views[i].size(),CV_8UC1); //tempview每次需重新定义,因为Mat的push_back函数只能加const量

cvtColor(views[i],tempview,CV_RGB2GRAY);

grayviews.push_back(tempview); 

}

 

3)      Vector 元素访问:

Views.at<CV_8UC3>(i) 或 Views.at<Vec3b>(i)

Views.at<CV_8UC1>(i) 或 Views.at<uchar>(i)

或views[i]

 

 

5.     GUI交互

1)控制台中鼠标响应事件,提取匹配角点

HANDLE ih = GetStdHandle(STD_INPUT_HANDLE);

HANDLE oh = GetStdHandle(STD_OUTPUT_HANDLE);

INPUT_RECORDirc;

      DWORD res;

      COORD cp;

cp.X = 0;

cp.Y = 0;

intret = SetConsoleCursorPosition(oh,cp);

while(1){

ReadConsoleInput(ih,&irc,1,&res);

if(res==1){

if(irc.EventType==MOUSE_EVENT){

if(irc.Event.MouseEvent.dwEventFlags== DOUBLE_CLICK){

        MOUSE_EVENT_RECORD mrc =irc.Event.MouseEvent;

      cp.X = mrc.dwMousePosition.X;

      cp.Y = mrc.dwMousePosition.Y;

}}}}}

 

2)opencv图像中提取角点

#include <highgui.h>

void onMouse(int Event,int x,int y,int flags,void*param)

{

      if(Event==4){

    cout<<x<<" "<<y<<endl;

      }

}

main(){

CvCapture *capture=NULL;

IplImage *frame;

cvNamedWindow("im1",0);

Matim1;

im1=imread("Data/1a.jpg");

imshow("im1",im1);

waitKey(10);

cvSetMouseCallback("im1",onMouse,NULL);

//刷新图像1

while(true){

frame = cvQueryFrame(capture);

cvShowImage("im1",frame);

if(cvWaitKey(10)>=0){

   break;

}}

 

 

6.1  文件夹下的文件全部读取

#include<io.h>

 

//定义

_finddata_t file;             //_finddata_t 结构体里存储的是文件信息

char* szFilePack;

longIF;                       //读取文件的句柄

 

//通配符:   /*

szFilePack ="rawimage//*.jpg";

 

//循环读取

if((IF= _findfirst(szFilePack, &file))== -1){

        cout<<"cannot findpinholerawimage"<<endl;

}

else{

    while(!_findnext(IF, &file) ) {

 

        cout<<file.name;

}

}

 

_findclose(IF);

 

 6.2 文件读取方法2

#include <iostream>
#include <string>
#include <sstream>


using namespace std;


int main()
{
int seq = 1;
string end = ".jpg";
string prefix = "D:\\";


stringstream sstr;


sstr << seq;


string com = prefix + sstr.str() + end;


cout << com << endl;


sstr.str("");


        ...

}

你可能感兴趣的:(c++ opencv 常用指令)