矩阵和图像的操作
其结构
void cvOr(//两个矩阵对应元素做或运行 const CvArr* src1,//矩阵1 const CvArr* src2,//矩阵2 CvArr* dst,//结果矩阵 const CvArr* mask = NULL//矩阵“开关” );
#include <cv.h> #include <highgui.h> #include <stdio.h> int main(int argc, char** argv) { IplImage *src1, *src2,*src3; src1 = cvLoadImage("1.jpg"); src2 = cvLoadImage("9.jpg"); src3 = cvLoadImage("7.jpg"); cvOr(src1,src2,src3); cvShowImage( "测试1", src1); cvShowImage( "测试2", src2); cvShowImage( "测试3", src3); cvWaitKey(); return 0; }
(2)cvOrS函数
其结构
void cvOr(//矩阵与给定标量做或运算 const CvArr* src1,//矩阵1 CvScalar value,//给定变量 CvArr* dst,//结果矩阵 const CvArr* mask = NULL//矩阵“开关” );
#include <cv.h> #include <highgui.h> #include <stdio.h> int main(int argc, char** argv) { IplImage *src1, *src2; src1 = cvLoadImage("1.jpg"); src2 = cvLoadImage("7.jpg"); CvScalar cs; cs.val[0] = 0; cs.val[1] = 255; cs.val[2] = 0; cs.val[3] = 0; cvOrS(src1,cs,src2); cvShowImage( "测试1", src1); cvShowImage( "测试2", src2); cvWaitKey(); return 0; }
(3)cvReduce函数
其结构
CvSize CvReduce(//完成由op指定的约简 const CvArr* src,//目标矩阵 CvArr* dst,//结果矩阵 int dim = -1,//因子系数,//1合并成行,0合并成列,-1转化成对应的dis int op = CV_REDUCE_SUM//指定约简法则 );
op的值 | 结果 |
CV_REDUCE_SUM | 计算所有向量的总和 |
CV_REDUCE_AVG | 计算所有向量的平均值 |
CV_REDUCE_MAX | 计算所有向量中的最大值 |
CV_REDUCE_MIN | 计算所有向量中的最小值 |
实例代码
#include <cv.h> #include <highgui.h> #include <stdio.h> #include <iostream> using namespace std; int main() { double a[5][5] = { {1,0,0,0,6}, {0,2,0,7,0}, {0,0,3,0,0}, {0,9,0,4,0}, {8,0,0,0,5} }; double b[5] = {0}; CvMat va=cvMat(5,5, CV_64FC1,a); CvMat vb=cvMat(5,1, CV_64FC1,b); cout<<"目标矩阵:"<<endl; for(int i=0;i<5;i++) { for(int j=0;j<5;j++) printf("%f\t",cvmGet(&va,i,j)); cout << endl; } cvReduce(&va,&vb,1,CV_REDUCE_SUM); cout << "结果向量"<<endl; for(int i=0;i<5;i++) { printf("%f\t",cvmGet(&vb,i,0)); cout << endl; } getchar(); return 0; }
(4)cvRepeat函数
其结构
void cvRepeat(//复制图像 const CvArr* src,//目标矩阵 CvArr* dst//结果矩阵 );
#include <cv.h> #include <highgui.h> #include <stdio.h> #include <iostream> using namespace std; int main() { IplImage *src1, *src2,*src3; src1 = cvLoadImage("1.jpg"); src2 = cvLoadImage("7.jpg"); cvRepeat(src1,src2); cvShowImage( "测试1", src1); cvShowImage( "测试2", src2); cvWaitKey(); return 0; }
(5)cvScale函数
其结构
void cvScale(//进行线性变换转换 const CvArr* src,//输入矩阵 CvArr* dst,//输出矩阵 double scale,//比例因子 double shift = 0.0//放缩比例 );
#include <cv.h> #include <highgui.h> #include <stdio.h> #include <iostream> using namespace std; int main() { IplImage* src1 = cvLoadImage("1.jpg",CV_LOAD_IMAGE_GRAYSCALE); IplImage* src2 = cvCloneImage(src1); IplImage* dst = cvCreateImage(cvGetSize(src1),IPL_DEPTH_64F,src1->nChannels); double max_Val,min_Val; cvScale(src1,dst,1.0,0.0); cvAdd(dst,dst,dst); cvMinMaxLoc(dst, &min_Val, &max_Val, NULL, NULL, NULL); cvScale(dst,src2,1.0,0.0); cvScale(dst, dst, 1.0/(max_Val-min_Val), 1.0*(-min_Val)/(max_Val-min_Val)); cvMinMaxLoc(dst, &min_Val, &max_Val, NULL, NULL, NULL); cvShowImage("测试1",src1); cvShowImage("测试2",src2); cvShowImage("结果",dst); cvWaitKey(-1); return 0; }
(6)cvSet函数
其结构
void cvSet(//对图像中的像素设置value CvArr* arr,//目标矩阵 CvScalar value,//设置值 const,CvArr* mask = NULL//图像开关 );
#include <cv.h> #include <highgui.h> #include <stdio.h> int main() { IplImage *src1, *src2; src1 = cvLoadImage("1.jpg"); src2 = cvLoadImage("1.jpg"); CvScalar cs; cs.val[0] = 0; cs.val[1] = 0; cs.val[2] = 255; cs.val[3] = 0; cvSet(src1,cs); cvShowImage( "结果图", src1); cvShowImage( "原图", src2); cvWaitKey(); return 0; }
(7)cvSetZero函数
其结构
void cvSetZero(//对图像自动设置为零,相当于cvSet(0.0) CvArr* arr,//目标矩阵 );
#include <cv.h> #include <highgui.h> #include <stdio.h> int main() { IplImage *src1, *src2; src1 = cvLoadImage("1.jpg"); src2 = cvLoadImage("1.jpg"); CvScalar cs; cs.val[0] = 0; cs.val[1] = 0; cs.val[2] = 255; cs.val[3] = 0; cvSetZero(src1); cvShowImage( "结果图", src1); cvShowImage( "原图", src2); cvWaitKey(); return 0; }
to be continued
《学习opencv》笔记——矩阵和图像操作——cvOr,cvOrS,cvrReduce,cvRepeat,cvScale,cvSet and cvSetZero,布布扣,bubuko.com
《学习opencv》笔记——矩阵和图像操作——cvOr,cvOrS,cvrReduce,cvRepeat,cvScale,cvSet and cvSetZero
原文地址:http://blog.csdn.net/zhurui_idea/article/details/28878011