An OpenCV example

A snippet in OpenCV for some useful mathematical operations. It mostly focuses on the old opencv style matrix to new opencv style matrix conversion (and vice-versa) and some elementary mathematical operations. It also includes a trivial matrix display function. I’ll give a more robust matrix display function in future.

[code lang=”cpp”]
// OpenCV_Test.cpp : Defines the entry point for the console application.
//

#include <stdio.h>
#include <opencv2opencv.hpp>
#include <string>
#include <fstream>

using namespace std;

// I wrote this method to display matrix contents. It is only for
// double (64 bit) data type. I shall write a more general version
// in near future
void dispMat(CvMat *N, string varName){
double *data = N->data.db;
int rowNum = N->rows;
int colNum = N->cols;

printf("n========= Matrix Display System ==============n");
printf("(%d,%d) %s n",rowNum,colNum,varName.c_str());
for(int i=0;i<rowNum;i++){
if(colNum>10)
printf("Row No=%dn",i+1);
for(int j=0;j<colNum;j++)
printf("%0.2ft",data[i*colNum+j]);
printf("n");
}
printf ("=================================================");
printf("n");
}

int main(int argc, char* argv[])
{
int a=0;

double c[] = {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16};
double d[] = {2,1,6,-4,0.5,4,9,1,9,-1,-0.3,0.7,3,2,1,1.5};

// Declaring Matrix as a Pointer and initialization
// This is the old style of OpenCV Matrices
CvMat* M = cvCreateMat(4,4,CV_64FC1);
cvInitMatHeader(M,4,4,CV_64FC1,d);
dispMat(M,"M = ");

// Declaring Matrix as variable and initialization
CvMat N = cvMat(4,4,CV_64FC1,c);
dispMat(&N,"N = ");

// Declaring Matrix as pointer without initialization
CvMat* M2;

//Cloning a Matrix
M2 = cvCloneMat(M);
dispMat(M2,"M2 = ");

//indirect access to matrix element
//get
printf("n(2,3)th element in M2 = %0.2fn",cvmGet(M2,2,3));
//set
cvmSet(M2,2,3,90);
printf("(2,3)th element is now set to = %0.2fn",
cvmGet(M2,2,3));

cout<<"nPress Enter to continuen";cin.get();
// ================================================

//Set zero
cvSetZero(M2);
dispMat(M2,"nM2 (cvSetZero)n");

//Set Identity
cvSetIdentity(M2);
dispMat(M2,"nM2 (cvSetIdentity)n");

cout<<"nPress Enter to continuen";cin.get();

// ============ OpenCV 2 Test =====================
cv::Mat A = cv::Mat(M,true);
cv::Mat J = cv::Mat(4,1,CV_64FC1,cv::Scalar(1));

cv::Mat B = (J.t())*(A);

CvMat AA = A;
dispMat(&AA,"OpenCV 2 Variable: A");

AA = J;
dispMat(&AA,"OpenCV 2 Variable: J");

// Try this: B = (J.t()*A).t();
B = (J.t()*A);
B = B.t();
AA = B; // Also try this: AA = B.t();
dispMat(&AA,"OpenCV 2 Variable:(J’*A)’");

cout<<"nPress Enter to continuen";cin.get();
// =========== Predefined openCV 2 Matrices ========
A = cv::Mat::zeros(5,2,CV_64FC1);
AA = A;
dispMat(&AA,"Predefined matrices: zeros(5,2,cv_64fc1)");

A = cv::Mat::ones(5,2,CV_64FC1);
AA = A;
dispMat(&AA,"Predefined matrices: ones(5,2,cv_64fc1)");

A = cv::Mat::eye(5,5,CV_64FC1);
AA = A;
dispMat(&AA,"Predefined matrices: eye(5,5,cv_64fc1)");

cout<<"nPress Enter to continuen";cin.get();
// =========== Predefined openCV 2 Matrices ========
printf("nn Element (1,2) = %f",A.at<double>(2,1));

// === Writing opencv Matrix (M2) to and from Filestream =====

ofstream ofs("d:\test.dat",ios::out|ios::binary);

dispMat(M2,"Matrix before writing to file");

double p =5;int s=3;
double* m2Dat = M2->data.db;

ofs<<p<<endl<<s;
ofs.write((char *) m2Dat,M2->cols*M2->rows*sizeof(double));

ofs.close();

p=0;s=0;
m2Dat[0]=-1;
m2Dat[1]=-1;
m2Dat[2]=-1;
m2Dat[3]=-1;
m2Dat[4]=-1;
m2Dat[5]=-1;
m2Dat[6]=-1;
m2Dat[7]=-1;
m2Dat[8]=-1;
m2Dat[9]=-1;
m2Dat[10]=-1;
m2Dat[11]=-1;
m2Dat[12]=-1;
m2Dat[13]=-1;
m2Dat[14]=-1;
m2Dat[15]=-1;

dispMat(M2,"Matrix after changing data");

ifstream ifs("d:\test.dat",ios::in|ios::binary);

ifs>>p>>s;
printf("%0.3fn",p);
printf("%dn",s);
ifs.read((char *)m2Dat,M2->cols*M2->rows*sizeof(double));

dispMat(M2,"Matrix after Loading again");

CvMat* M3 = cvCreateMat(4,4,CV_64FC1);
cvInitMatHeader(M3,4,4,CV_64FC1,m2Dat);
dispMat(M3,"Matrix reading from file");

ifs.close();

// Releasing Matrix Handle
cvReleaseData(M);
cvReleaseData(M2);
cvReleaseData(M3);

return 0;
}
[/code]

Note: This post assumes you have OpenCV 2.3 installed. If not, please check the following post
http://www.itanveer.com/2011/installing-opencv-231/