Opencv学习(二)
1. 函数命名
cvAction(表示核心函数Set,Create)Target(图像区域)Mod(变量类型)
矩阵函数类型
通用CV_
S:符号整数
U:无符号整数
F:浮点数
CV_8UC1 8位无符号整数单通道矩阵
图像数据类型
IPL_DEPTH_32F 表示32位浮点数图像
CVPoint p=cvPoint(int x,int y);
CVPoint2D32f p=cvPoint2D32f(float x,float y);
CVSize r=cvSize(int width,int height);
CVRect r=cvRect(intx,int y,int width,int height);
分配矩阵
CvMat*cvCreateMat(int rows,int cols,int type);
CvMat*M=cvCreateMat(4,4,CV_32FC1);
cvReleaseMat(&M);
CvMat*cvCloneMat(CvMat*);复制矩阵。
CvMat*M1=cvCreateMat(4,4CV_32FC1);
CvMat*M2;
M2=cvCloneMat(M1);
初始化矩阵
Double a[ ]={1,2,3,4,
5,6,7,8,
9,10,11,12};
CvMatMa=cvMat(3,4,CV_64FC1,a);或;
CvMat Ma;
cvintMatHeader(&Ma,3,4,CV_64FC1,a);
访问矩阵
1. 直接访问
cvmSet(M,i,j,2,0);M(I,j)赋值
t=cvmGet(M,I,j);
已知对齐方式的直接访问:
CvMat*M =cvCreateMat (4,4,CV_32FC1);
Int n =M->cols;
Float *data =M->data.fl;
Data[ i*n+j]=3.0;
未知对齐方式的直接访问
CvMat*M =cvCreateMat (4,4,CV_32FC1);
Int step =M->step/sizeof/(float);
Float *data =M->data.fl;
(data+i*step)[j]=3.0;
直接访问一个已知初始化的矩阵
Double a[16];
CvMat Ma=cvMat(3,4,CV_64FCI,a);
A[i*4+j]=2.0
GUI命令
创建定位一个窗口:
cvNameWindow(“win1”,CV_WINDOW_AUTOSIZE);
cvMoveWindow(“win1”,100,100);
加载一幅图片
IplImage*img=0;
Img=cvLoadImage(fileName);
If(!img)printf(“Could not load image file:%s\n”,fileName);
显示一幅图像
cvShowImage(“win1”,img);
关闭窗口
cvDestroyWindow(“win1”);
缩放窗口
cvResizeWindow(“win1”,100,100);
分配一幅图像
IpIImage*img1=cvCreatImage(cvSize(640,480),IPL_DEPTH_8U,1);
cvReleseImage(&img);//释放一幅图像
IpIImage*img2;
Img2=cvCloneImage(img 1);
读入图像文件,先进行图像反转,然后将图像显示在屏幕上。
#include
#include
#include
#include
#include
int main(int argc, char *argv[])
{
IplImage*img = 0;
int height, width,step, channels;
uchar *data;
int i, j, k;
if (argc < 2)
{
printf("Usage:InvImage
exit(0);
}
img= cvLoadImage(argv[1]);
if (!img)
{
printf("Could not load image file:%s\n", argv[1]);
exit(0);
}
height= img->height;
width= img->width;
step= img->widthStep;
channels= img->nChannels;
data= (uchar*)img->imageData;
printf("Processing a%dx%d image with %d channels\n", height, width, channels);
cvNamedWindow("mainWin", CV_WINDOW_AUTOSIZE);
cvMoveWindow("mainWin", 100,100);
for (i = 0; i
data[i*step+ j*channels + k] = 255 - data[i*step + channels + k];
cvShowImage("mainWin", img);
cvWaitKey(30);
cvReleaseImage(&img);
return 0;
}
从文件中读取图像
IplImage*img=0;
Img=cvLoadImage(filename);
If(!img)printf(“could not load image file:%s\n”,filename);
调整默认设置(3-通道)
Img=cvLoadImage,(filename,flag);>0(3通道)=0(单通道灰度图像)<0(图像的通道数决定);
向文件写图像:
If(!cvSeveImage(outFileName,img))
Printf(“Couldnot save:%s\n”,outFilename);
图片格式转换:
#include
#include
#include
int main(int argc, char**argv)
{
IplImage *src;
if (argc != 3)
{
printf("CONV:Image format convertion,supportJPG,BMP,TIE,PNG,PPM\n");
printf("Usage:conv srcImage dstImage\n");
return 0;
}
if (strstr(argv[1],".jpg")==NULL
&&strstr(argv[1], ".bmp") == NULL
&&strstr(argv[1], ".tif") == NULL
&&strstr(argv[1], ".png") == NULL
&&strstr(argv[1], ".ppm") == NULL)
||(strstr(argv[2], ".jpg") == NULL
&&strstr(argv[2],".bmp") == NULL
&&strstr(argv[2],".tif") == NULL
&&strstr(argv[2],".png") == NULL
&&strstr(argv[2],".ppm") == NULL
))
{
printf("WARNING:CONV only suportJPG,BMP,TIF.PPM.TAG,andPPM\n");
}
else
{
if ((src =cvLoadImage(argv[1],-1)) != 0){
cvSaveImage(argv[2], src);
cvReleaseImage(&src);
printf("\n Convert succeesfully.\n");
}
else {
printf("\n*** Read or write image Fils***\n");
}
}
return 0;
}
访问图片元素
间接访问:
IplImage*img=cvCreateImage(cvSize(640,480),IPL_DEPTH_8U,1);
CVScalar s;
S=cvGet2D(img,ij);
Printf(“intensity=%f\n”,s.val[0]);
s.val{0}=111;
cvSet2D(img,ij,s);
间接访问:
((uchar*)(img->imageData+i*img->widthStep))[j]=111;
多通道
((uchar*)(img->imageData+i*img->widthstep))[j*img->nChannels+0]=111;BGR
((uchar*)(img->imageData+i*img->widthstep))[j*img->nChannels+1]=112;
((uchar*)(img->imageData+i*img->widthstep))[j*img->nChannels+2]=113;
多通道浮点数用float*
指针直接访问;
Int height =img->height;
Int width=->img->width;
Int step=->img->widthStep/sizeof(unchar);
Uchar*data (uchar*)img->imageData;
Data[i*step+j]=111;
多通道 int channels= img ->nChannels;
Data[i*step+j*channels+k]=111;
将灰度图像转换成彩色图像
CvConvertImage(src,dst,flags=0);
浮点(单字节灰度(彩色)图像);
Flags CV_CVTIMG_FLIP.垂直翻转 CV_CVTIMG_SWAP_RB 交换R和B通道。。
彩色转换为灰色
CvCvtColor(cimg,gimg.CV_BGR2GRAY);
画矩形
cvRectangle(img,cvPoint(100,100),cvPoint(200,200),CVScalar(255,0,0),1);
画圆形
CvCircle(img,cvPoint(100,100),20,cvScalar(0,255,0),1);
画线段
cvLine(img,cvPoint(100,100) ,cvPoint(200,200),CVScalar(255,0,0),1);
画多边形
视屏的使用和操作
一.捕获某一帧
IpImage*img =0;
If(!cvGrabFrame(capture)){
Printf(“Could notgrab a frame\n\7”);
Exit(0);
}
保存视屏文件
初始化视频写入
对视屏写入并初始化
CvvideoWriter*writer=0;
Int isColor=1;
Int fps=25;
Int frameW=640;
Int frameH=480;
Writer=cvCreateVideoWriter(“out”)
CvtSeqToArray
CvMemStorage *stor;
CvSeq*cont;
CvPoint *PointArray;
CvPoint2D32f*PointArray2D32f;
Float temp;
Stor=cvCreateMemStorage(0);
Cont=cvCreateSeq(CV_SEQ_ELTYPE_POINT,sizeof(CvSeq),sizeof(CvPoint),stor);
CvFlushSeq Writer