12/16/2016

Gpu Mat, Cpu Mat example 2

unsigned long AAtime = 0, BBtime = 0;


 cuda::GpuMat gpuImg, gpuImg_out;


 Mat img, img_out, img_out2;
 img = imread("2mb.jpg");


 gpuImg.upload(img);
 AAtime = getTickCount();
 cuda::bitwise_not(gpuImg, gpuImg_out);

 Ptr< cv::cuda::filter > filter = cuda::createSobelFilter(img.type(), img.type(), 1, 0);
 filter->apply(gpuImg_out, gpuImg_out);
 BBtime = getTickCount();
 gpuImg_out.download(img_out);

 printf("gpu : %.2lf second \n", (BBtime - AAtime) / getTickFrequency());


 AAtime = getTickCount();
 bitwise_not(img, img_out2);
 Sobel(img_out2, img_out2, img_out2.depth(), 1, 0);
 BBtime = getTickCount();
 printf("cpu : %.2lf second \n", (BBtime - AAtime) / getTickFrequency());




 imshow("cpu_img", img_out);
 imshow("cpu_img", img_out2);
 waitKey(0);

Gpu Mat, Cpu Mat example code 1

cuda::GpuMat gpuImg;
 Mat img = imread("ss.jpg");

 unsigned long AAtime = 0, BBtime = 0;
 AAtime = getTickCount();
 gpuImg.upload(img); //upload
 vector< cuda::GpuMat > rgbGpuMat(3);
 cuda::split(gpuImg, rgbGpuMat); //cuda processing

 Mat r, g, b;
 rgbGpuMat[0].download(b); //download
 rgbGpuMat[1].download(g);
 rgbGpuMat[2].download(r);
 BBtime = getTickCount();
 printf("gpu : %.2lf second \n", (BBtime - AAtime) / getTickFrequency());


 vector< mat > rgbMat(3);
 split(img, rgbMat); //cuda processing
 //Mat r, g, b;
 b = rgbMat[0]; //download
 g = rgbMat[1];
 r = rgbMat[2];
 BBtime = getTickCount();
 printf("cpu : %.2lf second \n", (BBtime - AAtime) / getTickFrequency());



 namedWindow("r", 0); //make window
 imshow("r", r); //show
 namedWindow("g", 0); //make window
 imshow("g", g); //show
 namedWindow("b", 0); //make window
 imshow("b", b); //show
 waitKey(0);

SVD (singular value decomposition) example in opencv


//Singular value decomposition :
 Mat w, u, v;
 SVDecomp(data, w, u, v); // A = U W V^T
 //The flags cause U and V to be returned transposed(does not work well without the transpose flags).
 cout << u << endl;
 cout << w << endl;
 cout << v << endl;


Eigen analysis(of a symmetric matrix) in opencv exmaple code


//Eigen analysis(of a symmetric matrix) :
 float f11[] = { 1, 0.446, -0.56, 0.446, 1, -0.239, -0.56, 0.239, 1 };
 Mat data(3, 3, CV_32F, f11);
 cout << "input data" << endl;
 cout << data << endl;
 Mat value, vector;
 eigen(data, value, vector);
 cout << "Eigenvalues" << endl << value << endl;
 cout << "Eigenvectors" << endl << vector << endl;

proof
cout << vector << endl;
 cout << endl << "AV" << endl;
 cout << data * vector << endl;

 cout << "vV" << endl;
 Mat ValueEye = Mat::eye(3, 3, CV_32F);
 ValueEye.at< float>(0, 0) = value.at< float>(0, 0);
 ValueEye.at< float>(1, 1) = value.at< float>(1, 0);
 ValueEye.at< float>(2, 2) = value.at< float>(2, 0);
 cout << vector*ValueEye << endl;

Inhomogeneous linear system solver in opencv (Example code)


double dm2[3][3] = { { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9 } };
 Mat A(3, 3, CV_64FC1, dm2);
 Mat x(3, 1, CV_64FC1);
 double vvb[] = { 14, 32, 52 };
 Mat b(3, 1, CV_64FC1, vvb);
 cv::solve(A, b, x, DECOMP_SVD); //// solve (Ax=b) for x
 cout << x << endl;

12/14/2016

ctime_s and ctime example


#include < time.h >
#include < stdio.h >

int main(void)
{
time_t result = time(NULL);
//printf("%ld \n", result);
//printf("%s \n", ctime(&result));

char str[26];
ctime_s(str,sizeof str,&result);
printf("%s \n", str);

}


-> result
Thu Dec 15 02:29:54 2016