7/29/2014

no duplication random, (stl random_shuffle example source code)

sometimes, we need no duplication random sequence data.
simple method is using shuffle random in stl.

refer to this source code.

#include < stdio.h>
#include < vector>
#include < algorithm>
#include < cstdlib>
#include < ctime>
#include < iostream>
using namespace std;


void main()
{
 //set size and initialize
 vector< int > A(10);
 for(int i=0; i< A.size(); ++i)
  A[i] = i;

 //confirm
 printf("----origin data \n");
 for(int i=0; i< A.size(); ++i)
  printf("[%d] - %d \n", i, A[i] );
 printf("----\n");

 //random 
 srand( unsigned (time(0) ) );
 random_shuffle( A.begin(), A.end() );

 //confirm
 printf("---- After shuffle \n");
 for(int i=0; i< A.size(); ++i)
  printf("[%d] - %d \n", i, A[i] );
 printf("----\n");
}


..

7/28/2014

(OpenCV Study) OpticalFlow Gpu feature extraction and matching (GoodFeaturesToTrackDetector_GPU, gpu:: PyrLKOpticalFlow example source code)

This is example source code of Gpu mode optical flow and matching.
Source code is little bit complex in matching refine part.


function of BruteForceMatcher_GPU is added for more accurate matching.
The result is like that.


The source code is here..

...
Mat Ma = Mat::eye(3, 3, CV_64FC1);
 cout << Ma << endl;
 double dm[3][3] = { { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9 } };
 Mat Mb = Mat(3, 3, CV_64F, dm);
 cout << Mb << endl;
 
 //Matrix - matrix operations :
 Mat Mc;
 cv::add(Ma, Mb, Mc); // Ma+Mb   -> Mc
 cout << Ma+Mb << endl;
 cout << Mc << endl;
 cv::subtract(Ma, Mb, Mc);      // Ma-Mb   -> Mc
 cout << Ma - Mb << endl;
 cout << Mc << endl;
 Mc = Ma*Mb; //Ma*Mb;
 cout << Mc << endl;
 
 //Elementwise matrix operations :
 cv::multiply(Ma, Mb, Mc);   // Ma.*Mb   -> Mc
 cout << Mc << endl;
 Mc = Ma.mul(Mb);
 cout << Mc << endl;
 cv::divide(Ma, Mb, Mc);      // Ma./Mb  -> Mc
 cout << Mc << endl;
 Mc = Ma + 10; //Ma + 10 = Mc
 cout << Mc << endl;

 //Vector products :
 double va[] = { 1, 2, 3 };
 double vb[] = { 0, 0, 1 };
 double vc[3];

 Mat Va(3, 1, CV_64FC1, va);
 Mat Vb(3, 1, CV_64FC1, vb);
 Mat Vc(3, 1, CV_64FC1, vc);

 double res = Va.dot(Vb); // dot product:   Va . Vb -> res
 Vc = Va.cross(Vb);    // cross product: Va x Vb -> Vc
 cout << res << " " << Vc << endl;


 //Single matrix operations :
 Mc = Mb.t();      // transpose(Ma) -> Mb (cannot transpose onto self)
 cout << Mc << endl;
 cv::Scalar t = trace(Ma); // trace(Ma) -> t.val[0] 
 cout << t.val[0] << endl; 
 double d = determinant(Ma); // det(Ma) -> d
 cout << d << endl;
 Mc = Ma.inv();         // inv(Mb) -> Mc
 invert(Ma, Mc);
 cout << Mc << endl;


 //Inhomogeneous linear system solver :
 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;


 //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);
 Mat value, vector;
 eigen(data, value, vector);
 cout << "Eigenvalues" << value << endl;
 cout << "Eigenvectors" << endl;
 cout << vector << endl;


 //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 << w << endl;
 cout << u << endl;
 cout << v << endl;
...


refer to this posting
optical flow and matching using gpu
http://feelmare.blogspot.kr/2014/07/opencv-study-opticalflow-gpu-feature.html
orb feature and matching using gpu
http://feelmare.blogspot.kr/2014/07/opencv-study-orb-gpu-feature-extraction.html
surf feature and matching using gpu
http://feelmare.blogspot.kr/2014/07/opencv-study-surf-gpu-and-matching.html

(Opencv Study) Orb gpu feature extraction and Matching (ORB_GPU, BruteForceMatcher_GPU example source code)

This is example source cod of ORB_GPU feature detection and matching.
ORB feature is known extraction speed is faster than surf and sift.
By the way, in my test case, speed time is not so fast.
But surf and sift is nofree algorithm. orb is free to use in commercial project.

This figure is matching result of orb example.
Note, this is gpu version result.


 

The example source code is here.
Especially, ORB is using Hamming matching method. L1 and L2 cann't use.

#include < stdio.h>
#include < iostream>

#include < opencv2\opencv.hpp>
#include < opencv2/core/core.hpp>
#include < opencv2/highgui/highgui.hpp>
//#include < opencv2/video/background_segm.hpp>
#include < opencv2\gpu\gpu.hpp>
#include < opencv2\stitching\detail\matchers.hpp >  
//#include < opencv2\nonfree\features2d.hpp >    


#ifdef _DEBUG        
#pragma comment(lib, "opencv_core247d.lib")
#pragma comment(lib, "opencv_gpu247d.lib")
#pragma comment(lib, "opencv_features2d247d.lib")
#pragma comment(lib, "opencv_highgui247d.lib")
#pragma comment(lib, "opencv_nonfree247d.lib")
#else
#pragma comment(lib, "opencv_core247.lib")
#pragma comment(lib, "opencv_gpu247.lib")
#pragma comment(lib, "opencv_features2d247.lib")
#pragma comment(lib, "opencv_highgui247.lib")
#pragma comment(lib, "opencv_nonfree247.lib");
#endif 



using namespace cv;
using namespace std;



void main()
{

 

 gpu::GpuMat img1(imread("C:\\videoSample\\Image\\Picture6.jpg", CV_LOAD_IMAGE_GRAYSCALE)); 
    gpu::GpuMat img2(imread("C:\\videoSample\\Image\\Picture7.jpg", CV_LOAD_IMAGE_GRAYSCALE)); 


 unsigned long t_AAtime=0, t_BBtime=0;
 float t_pt;
 float t_fpt;
 t_AAtime = getTickCount(); 


 
 //extractFeatures
 gpu::ORB_GPU orb(2000);
 gpu::GpuMat keypoints1GPU, keypoints2GPU; 
    gpu::GpuMat descriptors1GPU, descriptors2GPU; 

 
 orb(img1, gpu::GpuMat(), keypoints1GPU, descriptors1GPU);
 orb(img2, gpu::GpuMat(), keypoints2GPU, descriptors2GPU);

 cout << "FOUND " << keypoints1GPU.cols << " keypoints on first image" << endl; 
    cout << "FOUND " << keypoints2GPU.cols << " keypoints on second image" << endl; 

 gpu::BruteForceMatcher_GPU< Hamming > matcher;    
 vector< vector< DMatch> > matches; 
 matcher.knnMatch(descriptors1GPU, descriptors2GPU, matches, 2); 
 
 //matching
 std::vector< DMatch > good_matches;
 for(int k = 0; k < min(descriptors1GPU.rows-1,(int) matches.size()); k++) 
    {
        if((matches[k][0].distance < 0.6*(matches[k][1].distance)) && ((int) matches[k].size()<=2 && (int) matches[k].size()>0))
        {
            good_matches.push_back(matches[k][0]);
        }
    }    


 t_BBtime = getTickCount();
 t_pt = (t_BBtime - t_AAtime)/getTickFrequency();
 t_fpt = 1/t_pt;
 printf("%.4lf sec/ %.4lf fps\n",  t_pt, t_fpt );


 vector< KeyPoint> keypoints1, keypoints2;
    vector< float> descriptors1, descriptors2;
    orb.downloadKeyPoints(keypoints1GPU, keypoints1);
    orb.downloadKeyPoints(keypoints2GPU, keypoints2);
 printf("%d %d\n", keypoints1.size(), keypoints2.size() );

 
 Mat img_matches; 
 Mat img11, img22;
 img1.download(img11);
 img2.download(img22);
 Mat outImg;

    drawMatches(img11, keypoints1, img22, keypoints2, good_matches, img_matches);
 //drawKeypoints(img11, kp1, outImg);

 namedWindow("matches", 0);
    imshow("matches", img_matches);

    waitKey(0);
}


//
refer to this posting
optical flow and matching using gpu
http://feelmare.blogspot.kr/2014/07/opencv-study-opticalflow-gpu-feature.html
orb feature and matching using gpu
http://feelmare.blogspot.kr/2014/07/opencv-study-orb-gpu-feature-extraction.html
surf feature and matching using gpu
http://feelmare.blogspot.kr/2014/07/opencv-study-surf-gpu-and-matching.html

7/27/2014

(OpenCV Study) Surf GPU and Matching (SURF_GPU, BruteForceMatcher_GPU example source code)

This is example source code of Matching using surf and bruteForceMathing of gpu version.
I think this simple example source code is useful to your gpu mode feature matching project.

This is source image.
The RC car is my favorite machine.


 
 
The figure is result of surf matching.



Processing time is 0.4028second and 2.4828 fps.
Image size is 1920x1080.


#include < stdio.h>
#include < iostream>

#include < opencv2\opencv.hpp>
#include < opencv2/core/core.hpp>
#include < opencv2/highgui/highgui.hpp>
#include < opencv2\gpu\gpu.hpp>
#include < opencv2\stitching\detail\matchers.hpp >  


#ifdef _DEBUG        
#pragma comment(lib, "opencv_core247d.lib")
#pragma comment(lib, "opencv_gpu247d.lib")
#pragma comment(lib, "opencv_features2d247d.lib")
#pragma comment(lib, "opencv_highgui247d.lib")
#pragma comment(lib, "opencv_nonfree247d.lib")
#else
#pragma comment(lib, "opencv_core247.lib")
#pragma comment(lib, "opencv_gpu247.lib")
#pragma comment(lib, "opencv_features2d247.lib")
#pragma comment(lib, "opencv_highgui247.lib")
#pragma comment(lib, "opencv_nonfree247.lib")
#endif 


using namespace cv;
using namespace std;

void main()
{
gpu::GpuMat img1(imread("C:\\videoSample\\Image\\Picture6.jpg", CV_LOAD_IMAGE_GRAYSCALE)); 
    gpu::GpuMat img2(imread("C:\\videoSample\\Image\\Picture7.jpg", CV_LOAD_IMAGE_GRAYSCALE)); 

 
 /////////////////////////////////////////////////////////////////////////////////////////
 unsigned long t_AAtime=0, t_BBtime=0;
 float t_pt;
 float t_fpt;
 t_AAtime = getTickCount(); 
 /////////////////////////////////////////////////////////////////////////////////////////

    gpu::SURF_GPU surf(400);
    // detecting keypoints & computing descriptors 
    gpu::GpuMat keypoints1GPU, keypoints2GPU; 
    gpu::GpuMat descriptors1GPU, descriptors2GPU; 
    surf(img1, gpu::GpuMat(), keypoints1GPU, descriptors1GPU); 
    surf(img2, gpu::GpuMat(), keypoints2GPU, descriptors2GPU); 
    
    cout << "FOUND " << keypoints1GPU.cols << " keypoints on first image" << endl; 
    cout << "FOUND " << keypoints2GPU.cols << " keypoints on second image" << endl; 

    // matching descriptors 
    gpu::BruteForceMatcher_GPU< L2< float> > matcher;    
 vector< vector< DMatch> > matches; 
 matcher.knnMatch(descriptors1GPU, descriptors2GPU, matches, 2); 
    
    // downloading results  Gpu -> Cpu
    vector< KeyPoint> keypoints1, keypoints2; 
    vector< float> descriptors1, descriptors2; 
    surf.downloadKeypoints(keypoints1GPU, keypoints1);
    surf.downloadKeypoints(keypoints2GPU, keypoints2);
    //surf.downloadDescriptors(descriptors1GPU, descriptors1); 
    //surf.downloadDescriptors(descriptors2GPU, descriptors2);

 vector< KeyPoint> matchingKey1, matchingKey2;
 std::vector< DMatch > good_matches;
 for(int k = 0; k < min(descriptors1GPU.rows-1,(int) matches.size()); k++) 
    {
        if((matches[k][0].distance < 0.6*(matches[k][1].distance)) && ((int) matches[k].size()<=2 && (int) matches[k].size()>0))
        {
            good_matches.push_back(matches[k][0]);

        }
    }
 


 
 t_BBtime = getTickCount();
 t_pt = (t_BBtime - t_AAtime)/getTickFrequency();
 t_fpt = 1/t_pt;
 printf("feature extraction = %.4lf / %.4lf \n",  t_pt, t_fpt );



 
 
 
    // drawing the results 
    Mat img_matches; 
 Mat img11, img22;
 img1.download(img11);
 img2.download(img22);
 
 //drawMatches(img11, matchingKey1, img22, matchingKey2, good_matches, img_matches); 
    drawMatches(img11, keypoints1, img22, keypoints2, good_matches, img_matches); 

 //drawKeypoints(img11, keypoints1, img_matches);
 

    namedWindow("matches", 0); 
    imshow("matches", img_matches); 
    waitKey(0); 
}




refer to this posting
optical flow and matching using gpu
http://feelmare.blogspot.kr/2014/07/opencv-study-opticalflow-gpu-feature.html
orb feature and matching using gpu
http://feelmare.blogspot.kr/2014/07/opencv-study-orb-gpu-feature-extraction.html
surf feature and matching using gpu
http://feelmare.blogspot.kr/2014/07/opencv-study-surf-gpu-and-matching.html

7/08/2014

STL vector sort, lambda version


//vector
vector< pair > vote;

//data input
~~
~~

//sort
 sort(vote.begin(), vote.end(), [](const pair& A, const pair& B){ return A.second > B.second; } );

7/03/2014

(Python, openCV study), k-means example source code of python and C++, and processing time comparing

Example source code of K-means algorithm in OpenCV,
The source code are two version, one is python and other is C++.
And I compare processing time, I do same condition such as same image, same parameter, and I checked same result.

The winner of processing speed is C++.
Despite C++ contain many "for" syntax but faster than python.

Check example source code.

This is input image


C++ version.
...
#include <  stdio.h>   
#include <  iostream>   
#include <  opencv2\opencv.hpp>   


#ifdef _DEBUG           
#pragma comment(lib, "opencv_core247d.lib")   
#pragma comment(lib, "opencv_imgproc247d.lib")   //MAT processing   
#pragma comment(lib, "opencv_highgui247d.lib")   
#else   
#pragma comment(lib, "opencv_core247.lib")   
#pragma comment(lib, "opencv_imgproc247.lib")   
#pragma comment(lib, "opencv_highgui247.lib")   
#endif  

using namespace cv;
using namespace std;

void main()
{

 unsigned long AAtime=0, BBtime=0; //check processing time   
 unsigned long inAtime=0, inBtime=0;
 AAtime = getTickCount(); //check processing time   

 inAtime = getTickCount(); //check processing time  
 Mat src = imread( "mare-08.jpg", 1 );
 Mat samples(src.rows * src.cols, 3, CV_32F);
 for( int y = 0; y < src.rows; y++ )
  for( int x = 0; x < src.cols; x++ )
   for( int z = 0; z < 3; z++)
    samples.at< float>(y + x*src.rows, z) = src.at< Vec3b>(y,x)[z];
 inBtime = getTickCount(); //check processing time    
 printf("in Data preparing %.2lf sec \n",  (inBtime - inAtime)/getTickFrequency() ); //check processing time   

 inAtime = getTickCount(); //check processing time  
 int clusterCount = 5;
 Mat labels;
 int attempts = 10;
 Mat centers;
 kmeans(samples, clusterCount, labels, TermCriteria(CV_TERMCRIT_ITER|CV_TERMCRIT_EPS, 10, 1.0), attempts, KMEANS_RANDOM_CENTERS, centers );
 inBtime = getTickCount(); //check processing time    
 printf("K mean processing %.2lf sec \n",  (inBtime - inAtime)/getTickFrequency() ); //check processing time   


 inAtime = getTickCount(); //check processing time  
 Mat new_image( src.size(), src.type() );
 for( int y = 0; y < src.rows; y++ )
 {
  for( int x = 0; x < src.cols; x++ )
  { 
   int cluster_idx = labels.at< int>(y + x*src.rows,0);
   new_image.at< Vec3b>(y,x)[0] = centers.at< float>(cluster_idx, 0);
   new_image.at< Vec3b>(y,x)[1] = centers.at< float>(cluster_idx, 1);
   new_image.at< Vec3b>(y,x)[2] = centers.at< float>(cluster_idx, 2);
  }
 }
 inBtime = getTickCount(); //check processing time    
 printf("out Data Preparing processing %.2lf sec \n",  (inBtime - inAtime)/getTickFrequency() ); //check processing time   

 BBtime = getTickCount(); //check processing time    
 printf("Total processing %.2lf sec \n",  (BBtime - AAtime)/getTickFrequency() ); //check processing time   
  
 //imshow( "clustered image", new_image );
 imwrite("clustered_image.jpg", new_image);
 //waitKey( 0 );
 
}
...
result image and processing time of C++ version




Python Version
...
import numpy as np
import cv2
from matplotlib import pyplot as plt




e1 = cv2.getTickCount()

inA = cv2.getTickCount()
img = cv2.imread('mare-08.jpg')
Z = img.reshape((-1,3))
# convert to np.float32
Z = np.float32(Z)
inB = cv2.getTickCount()
print("in data preparing", (inB-inA)/cv2.getTickFrequency())

# define criteria, number of clusters(K) and apply kmeans()
inA = cv2.getTickCount()
criteria = (cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER, 10, 1.0)
K = 5
ret,label,center = cv2.kmeans(Z,K,criteria,10,cv2.KMEANS_RANDOM_CENTERS)
inB = cv2.getTickCount()
print("K-means ", (inB-inA)/cv2.getTickFrequency())


# Now convert back into uint8, and make original image
inA = cv2.getTickCount()
center = np.uint8(center)
res = center[label.flatten()]
res2 = res.reshape((img.shape))
inB = cv2.getTickCount()
print("out data preparing", (inB-inA)/cv2.getTickFrequency())

#print(center)
#print(label)

e2 = cv2.getTickCount()
time = (e2 - e1)/cv2.getTickFrequency()
print("total time", time, 1/time)

cv2.imshow('res2',res2)
cv2.waitKey(0)
cv2.destroyAllWindows()
...

The result of python

7/02/2014

python study, dateutil install method

simple way is
1. download dateutil from https://pypi.python.org/pypi/python-dateutil and unzip
2. copy dateutil foler to the python\Lib\site-packages
3. restart IDE
4. test import dateutil