12/30/2015

How to run *.run file in Linux ? (simple tip)


Just change mode
and run!
'./' means sh command


# chmod a+x install_test.run
# ./install_test.run


Linux is not easy to me.....

12/22/2015

Mac computers that use OpenCL and OpenGL graphics

link to
https://support.apple.com/en-us/HT202823

There is no product use nvidia graphic card now(2015.12).
So apple's computer can not use cuda library.

Many machine learning related library use CUDA. cuDNN, cuBlas..
Now I use MAC book pro 2014 retina 15'' that embedded geforce gt 750m.
But I have to concern whether I buy apple notebook or not for next notebook.

I like apple products. but for my study, I need nvidia graphic card for using cuda.





12/21/2015

Opencv Condensation(particle filter) example source code





Dear Jihad anwar

Here is source code you asking.
I don't remember equation about particle filter well now.
But source code is still run well. ^^
I hope to help your study.

Note. Opencv 2.4.9 and 32 bit.

Thank you.


#include < iostream>
#include < vector>

#include <  stdio.h>      
#include <  opencv2\opencv.hpp>    
#include < opencv2\legacy\legacy.hpp>

#ifdef _DEBUG           
#pragma comment(lib, "opencv_core249d.lib")   
#pragma comment(lib, "opencv_imgproc249d.lib")   //MAT processing   
#pragma comment(lib, "opencv_objdetect249d.lib") //HOGDescriptor   
//#pragma comment(lib, "opencv_gpu249d.lib")   
//#pragma comment(lib, "opencv_features2d249d.lib")   
#pragma comment(lib, "opencv_highgui249d.lib")   
#pragma comment(lib, "opencv_ml249d.lib")   
//#pragma comment(lib, "opencv_stitching249d.lib");   
//#pragma comment(lib, "opencv_nonfree249d.lib");   
#pragma comment(lib, "opencv_video249d.lib")
#pragma comment(lib, "opencv_legacy249d.lib")
#else   
#pragma comment(lib, "opencv_core249.lib")   
#pragma comment(lib, "opencv_imgproc249.lib")   
#pragma comment(lib, "opencv_objdetect249.lib")   
//#pragma comment(lib, "opencv_gpu249.lib")   
//#pragma comment(lib, "opencv_features2d249.lib")   
#pragma comment(lib, "opencv_highgui249.lib")   
#pragma comment(lib, "opencv_ml249.lib")   
//#pragma comment(lib, "opencv_stitching249.lib");   
//#pragma comment(lib, "opencv_nonfree249.lib");   
#pragma comment(lib, "opencv_video249d.lib")   
#endif   



using namespace cv;
using namespace std;

// (1) functions for calculating the likelihood
float calc_likelihood (IplImage * img, int x, int y)
{
 float b, g, r;
 float dist = 0.0, sigma = 50.0;

 b = img->imageData[img->widthStep * y + x * 3];       //B
 g = img->imageData[img->widthStep * y + x * 3 + 1];   //G
 r = img->imageData[img->widthStep * y + x * 3 + 2];   //R
 dist = sqrt (b * b + g * g + (255.0 - r) * (255.0 - r));

 return 1.0 / (sqrt (2.0 * CV_PI) * sigma) * expf (-dist * dist / (2.0 * sigma * sigma));
}

void ProccTimePrint( unsigned long Atime , string msg)   
{   
 unsigned long Btime=0;   
 float sec, fps;   
 Btime = getTickCount();   
 sec = (Btime - Atime)/getTickFrequency();   
 fps = 1/sec;   
 printf("%s %.4lf(sec) / %.4lf(fps) \n", msg.c_str(),  sec, fps );   
}   



int main ()
{

 float TakeTime;   
 unsigned long Atime, Btime;   

 int i, c;
 double w = 0.0, h = 0.0;
 CvCapture *capture = 0;
 IplImage *frame = 0;

 int n_stat = 4;
 int n_particle = 1000;
 vector<  float > vx(n_particle);
 vector<  float > vy(n_particle);

 CvConDensation *cond = 0;
 CvMat *lowerBound = 0;
 CvMat *upperBound = 0;

 int xx, yy;

 // (2)you want to create a capture structure with respect to the camera with the specified number by the command argument
 
 capture = cvCreateCameraCapture(0);

 // (3)The one frame captured, and obtains the capture size.
 frame = cvQueryFrame (capture);
 w = frame->width;
 h = frame->height;

 cvNamedWindow ("Condensation", CV_WINDOW_AUTOSIZE);

 // (4)Condensation create a structure.
 cond = cvCreateConDensation (n_stat, 0, n_particle);

 // (5) it will specify the minimum and maximum values of the state vector can be taken for each dimension.
 lowerBound = cvCreateMat (4, 1, CV_32FC1);
 upperBound = cvCreateMat (4, 1, CV_32FC1);

 cvmSet (lowerBound, 0, 0, 0.0);
 cvmSet (lowerBound, 1, 0, 0.0);
 cvmSet (lowerBound, 2, 0, -10); //-10.0);
 cvmSet (lowerBound, 3, 0, -10); //-10.0);
 cvmSet (upperBound, 0, 0, w);
 cvmSet (upperBound, 1, 0, h);
 cvmSet (upperBound, 2, 0, 10); //10.0);
 cvmSet (upperBound, 3, 0, 10); //10.0);

 // (6)Condensation Initialize the structure
 cvConDensInitSampleSet (cond, lowerBound, upperBound);

 // (7)ConDensation Specify the dynamics of the state vector in the algorithm
 cond->DynamMatr[0] = 1.0;
 cond->DynamMatr[1] = 0.0;
 cond->DynamMatr[2] = 1.0;
 cond->DynamMatr[3] = 0.0;
 cond->DynamMatr[4] = 0.0;
 cond->DynamMatr[5] = 1.0;
 cond->DynamMatr[6] = 0.0;
 cond->DynamMatr[7] = 1.0;
 cond->DynamMatr[8] = 0.0;
 cond->DynamMatr[9] = 0.0;
 cond->DynamMatr[10] = 1.0;
 cond->DynamMatr[11] = 0.0;
 cond->DynamMatr[12] = 0.0;
 cond->DynamMatr[13] = 0.0;
 cond->DynamMatr[14] = 0.0;
 cond->DynamMatr[15] = 1.0;

 // (8)re-set the noise parameters.

 while (1) {
  frame = cvQueryFrame (capture);
  Atime = getTickCount(); //시작 시간 
  float a=0,b=0,c=0,d=0,e=0;

  // (9) It calculates the likelihood for each particle.
  for (i = 0; i <  n_particle; i++) {
   xx = (int) (cond->flSamples[i][0]);
   yy = (int) (cond->flSamples[i][1]);

   vx[i] = cond->flSamples[i][0];
   vy[i] = cond->flSamples[i][1];


   if (xx <  0 || xx >= w || yy <  0 || yy >= h) {
    cond->flConfidence[i] = 0.0;
   }
   else {
    cond->flConfidence[i] = calc_likelihood (frame, xx, yy);
    cvCircle (frame, cvPoint (xx, yy), 1, CV_RGB (0, 0, 255), -1);   
    
   }
  }

  
  // (10)  estimate the state of the next model
  cvConDensUpdateByTime (cond);
  printf("crrection \n");
  
  ProccTimePrint( Atime , "time :"); //처리시간 출력  

  cv::Point statePt(cond->State[0], cond->State[1]);
  cvCircle (frame, statePt, 5, CV_RGB (255, 255, 255), 5);  
  printf("-----------\n");
  
  cvShowImage ("Condensation", frame);
  
  if (cvWaitKey (10) > 10 )
   break;

 }

 cvDestroyWindow ("Condensation");

 cvReleaseCapture (&capture);
 cvReleaseConDensation (&cond);
 cvReleaseMat (&lowerBound);
 cvReleaseMat (&upperBound);

 return 0;
}




12/16/2015

Opencv Hog+SVM example of pedestrian detection


source code


#include < iostream>    
#include "opencv2\objdetect\objdetect.hpp"
#include "opencv2\highgui\highgui.hpp"
#include "opencv2\imgproc\imgproc.hpp"
#include "opencv2\cudaobjdetect.hpp"
#include "opencv2\cudaimgproc.hpp"
#include "opencv2\cudawarping.hpp"


#ifdef _DEBUG               
#pragma comment(lib, "opencv_core300d.lib")       
#pragma comment(lib, "opencv_highgui300d.lib")    
#pragma comment(lib, "opencv_imgcodecs300d.lib")  
#pragma comment(lib, "opencv_objdetect300d.lib")  
#pragma comment(lib, "opencv_imgproc300d.lib")  
#pragma comment(lib, "opencv_videoio300d.lib")    
#pragma comment(lib, "opencv_cudaobjdetect300d.lib")  
#pragma comment(lib, "opencv_cudawarping300d.lib")
#pragma comment(lib, "opencv_cudaimgproc300d.lib")

#else       
#pragma comment(lib, "opencv_core300.lib")       
#pragma comment(lib, "opencv_highgui300.lib")    
#pragma comment(lib, "opencv_imgcodecs300.lib")    
#pragma comment(lib, "opencv_objdetect300.lib")  
#pragma comment(lib, "opencv_imgproc300.lib")  
#pragma comment(lib, "opencv_videoio300.lib")    
#pragma comment(lib, "opencv_cudaobjdetect300.lib")
#pragma comment(lib, "opencv_cudawarping300.lib")
#pragma comment(lib, "opencv_cudaimgproc300.lib")
#endif        

using namespace std;
using namespace cv;


void main()
{


 cv::Ptr< cv::cuda::HOG> d_hog = cv::cuda::HOG::create(Size(48, 96)); //Size(64, 128));// 
 d_hog->setSVMDetector(d_hog->getDefaultPeopleDetector());
 

 //video loading
 Mat img; 
 VideoCapture cap("M:\\____videoSample____\\tracking\\TownCentreXVID.avi");

 //loading test
 cap >> img;
 if (img.empty())
  return;

 //window
 namedWindow("pedestrian", 0);

 //processing time check
 unsigned long AAtime = 0, BBtime = 0;
 
 //resize
 double scale = float(800) / img.cols;
 cuda::GpuMat GpuImg, rGpuImg;
 GpuImg.upload(img);
 cuda::resize(GpuImg, rGpuImg, Size(GpuImg.cols * scale, GpuImg.rows * scale));
 Mat rInimg;
 rGpuImg.download(rInimg);

 while (1)
 {
  //time check
  AAtime = getTickCount();

  //loading
  cap >> img;
  if (img.empty())
   break;

  //resize
  GpuImg.upload(img);
  cuda::resize(GpuImg, rGpuImg, Size(GpuImg.cols * scale, GpuImg.rows * scale));
  rGpuImg.download(rInimg);
  cuda::cvtColor(rGpuImg, rGpuImg, CV_BGR2GRAY);
  
  
  vector< Point> found_locations;
  d_hog->detect(rGpuImg, found_locations);

  std::vector< cv::Rect> found_locations_rect;
  d_hog->detectMultiScale(rGpuImg, found_locations_rect);


  for (int i = 0; i < found_locations_rect.size(); ++i)
  {

   cv::rectangle(rInimg, found_locations_rect[i], CvScalar(0, 0, 255), 1);   
  }


  imshow("pedestrian", rInimg);
  waitKey(10);

  //time check
  BBtime = getTickCount();
  double s_time = (BBtime - AAtime) / getTickFrequency();
  double fps_time = 1/s_time;
  printf("%.2lf sec / %.2lf fps \n", s_time, fps_time); 

 }


}

11/28/2015

(example code) print string to debug output window in visual studio C++

#ifdef DEBUG
   wchar_t str[100];
   swprintf(str,L"if %d %lf\n", valueInt, valueDouble);
   OutputDebugString( str );
#endif

11/11/2015

Get Rotation and Translation from 2 groups of 3d points (calculate R, T between 2 points.)

I made this example source code referencing from this site.
http://nghiaho.com/?page_id=671


Key idea(main processing) is using SVD(singular Value Decomposition)

I  made first group consist of 3d points by random selection.



Second groups of 3d points is made by random rotation and translation.
Blue color is points of second group.


How to find R,T between 2 groups of 3d points ?
for detail, see below matlab source code.
After get R,T, second group can transform to original position.



matlab source code.

printf

%Get R,T from 2 groups of 3d points

%The first group is created by random selection
A3pt = rand(3, 10);
figure(10);
plot3(A3pt(1,:),A3pt(2,:),A3pt(3,:),'r.');%, axis equal

%The second group is made by random R,T from first group
v1=0.6*(2*rand-1); 
v2=0.6*(2*rand-1); 
v3=0.6*(2*rand-1);
R1=[1 0 0;0 cos(v1) -sin(v1);0 sin(v1) cos(v1)];
R2=[cos(v2) 0 sin(v2);0 1 0;-sin(v2) 0 cos(v2)];
R3=[cos(v3) -sin(v3) 0;sin(v3) cos(v3) 0;0 0 1];
R=R3*R2*R1;
T = rand(3,1);

B3pt = R*A3pt; %Rotation
for i=1:3 %dimension
        B3pt(i,:)=B3pt(i,:)+T(i);      % translation
end

%show 2 group
figure(1);
plot3(A3pt(1,:),A3pt(2,:),A3pt(3,:),'r.',B3pt(1,:),B3pt(2,:),B3pt(3,:),'bo');%, axis equal

%% get R,T
MeanA = mean(A3pt, 2);
MeanB = mean(B3pt, 2);

HH=zeros(3,3);
n = length(A3pt);
for i=1:n
    tA = A3pt(:,i) - MeanA;
    tB = B3pt(:,i) - MeanB;
    hh = tB * tA';
    HH = HH + hh;
end

[U,~,V]=svd(HH); 
Ri=V*U'; %get R
Ti=MeanA-Ri*MeanB; %Get T


%% confirm

B3pt_=Ri*B3pt;                       % Rotation 시키기 Apply transformation
for i=1:3 %dimension
    B3pt_(i,:)=B3pt_(i,:)+Ti(i);      % translation 시키기
end

%show 2 group
figure(2);
plot3(A3pt(1,:),A3pt(2,:),A3pt(3,:),'r.',B3pt_(1,:),B3pt_(2,:),B3pt_(3,:),'bo');%, axis equal


    
    
    



...

11/09/2015

integer to string ( _itoa_s )

see code


...
int increase = 0;
char str[100];
_itoa_s(time(0),str,10); //10 means decimal so 8:octal, 16Lhex, 2:binary
sprintf_s(str, "%s_%d", str, increase);
printf("%s \n", str );


...

2 rect intersection, union and overlap ratio in opencv Rect

using & and | operator and area() method.

see code


..
Rect A(100, 100, 100, 100); //x, y, width, hegiht
Rect B(80, 80, 100, 100); //x, y, width, hegiht

printf("intersection area= %d\n", (A&B).area());
printf("union area = %d\n", (A|B).area());
printf("instersection ratio %lf \n", (A&B).area() / float( (A | B).area() ));


..



11/08/2015

long delay opencv gpu/cuda/GpuMat

When using opencv GpuMat, there is a long delay in a use of the first line code.


In this page,
http://answers.opencv.org/question/52304/long-delay-on-cvgpugpumatupload-after-upgrade-to-gtx970/
There is solution in article.

In one word.



Add 5.2 option in CUDA_ARCH_BIN table when cmake setting.
In my case, this delay problem cleared, after build by set this option.

Thank you.



11/05/2015

Motion flow and direction (gpu, cuda version/ example source code / opti...




opencv 3.0 cuda version optical flow

More detail refer to example source code.


..
#include <  iostream>    
#include "opencv2\objdetect\objdetect.hpp"
#include "opencv2\highgui\highgui.hpp"
#include "opencv2\imgproc\imgproc.hpp"
#include "opencv2\cudaobjdetect.hpp"
#include "opencv2\cudaimgproc.hpp"
#include "opencv2\cudawarping.hpp"
#include <  opencv2\bgsegm.hpp>  
#include <  opencv2\cudabgsegm.hpp>  
#include <  opencv2\cudaoptflow.hpp>  



#ifdef _DEBUG               
#pragma comment(lib, "opencv_core300d.lib")       
#pragma comment(lib, "opencv_highgui300d.lib")    
#pragma comment(lib, "opencv_imgcodecs300d.lib")  
#pragma comment(lib, "opencv_objdetect300d.lib")  
#pragma comment(lib, "opencv_imgproc300d.lib")  
#pragma comment(lib, "opencv_videoio300d.lib")    
#pragma comment(lib, "opencv_cudaobjdetect300d.lib")  
#pragma comment(lib, "opencv_cudawarping300d.lib")
#pragma comment(lib, "opencv_cudaimgproc300d.lib")
#pragma comment(lib, "opencv_cudabgsegm300d.lib")  
#pragma comment(lib, "opencv_cudaoptflow300d.lib") 
#else       
#pragma comment(lib, "opencv_core300.lib")       
#pragma comment(lib, "opencv_highgui300.lib")    
#pragma comment(lib, "opencv_imgcodecs300.lib")    
#pragma comment(lib, "opencv_objdetect300.lib")  
#pragma comment(lib, "opencv_imgproc300.lib")  
#pragma comment(lib, "opencv_videoio300.lib")    
#pragma comment(lib, "opencv_cudaobjdetect300.lib")
#pragma comment(lib, "opencv_cudawarping300.lib")
#pragma comment(lib, "opencv_cudaimgproc300.lib")
#pragma comment(lib, "opencv_cudabgsegm300.lib")  
#pragma comment(lib, "opencv_cudaoptflow300.lib") 
#endif        

using namespace cv;
using namespace std;

static void download(const cuda::GpuMat& d_mat, vector< Point2f>& vec);
static void download(const cuda::GpuMat& d_mat, vector< uchar>& vec);
static void drawArrows(Mat& frame, const vector< Point2f>& prevPts, const vector< Point2f>& nextPts, const vector< uchar>& status, Scalar line_color = Scalar(0, 0, 255));


void main()
{
 //variable
 cuda::GpuMat GpuImg, rGpuImg_Bgray;
 cuda::GpuMat oldGpuImg_Agray;

 //video
 Mat img, dImg_rg, dimg;
 VideoCapture cap("M:\\____videoSample____\\tracking\\TownCentreXVID.avi");
 cap >> img;
 if (img.empty())
  return;

 //scale
 double scale = 800. / img.cols;

 //first gpumat
 GpuImg.upload(img);
 cuda::resize(GpuImg, oldGpuImg_Agray, Size(GpuImg.cols * scale, GpuImg.rows * scale));
 cuda::cvtColor(oldGpuImg_Agray, oldGpuImg_Agray, CV_BGR2GRAY);
 
 cuda::GpuMat d_prevPts;
 cuda::GpuMat d_nextPts;
 cuda::GpuMat d_status;
 Ptr< cuda::CornersDetector> detector = cuda::createGoodFeaturesToTrackDetector(oldGpuImg_Agray.type(), 4000, 0.01, 0);
 //opticla flow
 Ptr< cuda::SparsePyrLKOpticalFlow> d_pyrLK = cuda::SparsePyrLKOpticalFlow::create(Size(21, 21), 3, 30);

 unsigned long Atime, Btime;
 float TakeTime;

 while (1)
 {
  Atime = getTickCount();

  cap >> img;
  if (img.empty())
   break;

  //get image
  GpuImg.upload(img);
  cuda::resize(GpuImg, rGpuImg_Bgray, Size(GpuImg.cols * scale, GpuImg.rows * scale));
  rGpuImg_Bgray.download(dimg);
  cuda::cvtColor(rGpuImg_Bgray, rGpuImg_Bgray, CV_BGR2GRAY);
  rGpuImg_Bgray.download(dImg_rg);


  //A,B image  
  //oldGpuImg_Agray;
  //rGpuImg_Bgray;

  //feature
  detector->detect(oldGpuImg_Agray, d_prevPts);
  d_pyrLK->calc(oldGpuImg_Agray, rGpuImg_Bgray, d_prevPts, d_nextPts, d_status);


  //old
  oldGpuImg_Agray = rGpuImg_Bgray;
    

  // Draw arrows
  vector< Point2f> prevPts(d_prevPts.cols);
  download(d_prevPts, prevPts);

  vector< Point2f> nextPts(d_nextPts.cols);
  download(d_nextPts, nextPts);

  vector< uchar> status(d_status.cols);
  download(d_status, status);

  drawArrows(dimg, prevPts, nextPts, status, Scalar(255, 0, 0));

  //show
  imshow("PyrLK [Sparse]", dimg);  
  imshow("origin", dImg_rg);
  if (waitKey(10)>0)
   break;


  Btime = getTickCount();
  TakeTime = (Btime - Atime) / getTickFrequency();  
  printf("%lf sec / %lf fps \n", TakeTime, 1 / TakeTime);

 }

}



static void download(const cuda::GpuMat& d_mat, vector< uchar>& vec)
{
 vec.resize(d_mat.cols);
 Mat mat(1, d_mat.cols, CV_8UC1, (void*)&vec[0]);
 d_mat.download(mat);
}

static void download(const cuda::GpuMat& d_mat, vector< Point2f>& vec)
{
 vec.resize(d_mat.cols);
 Mat mat(1, d_mat.cols, CV_32FC2, (void*)&vec[0]);
 d_mat.download(mat);
}

static void drawArrows(Mat& frame, const vector< Point2f>& prevPts, const vector< Point2f>& nextPts, const vector< uchar>& status, Scalar line_color)
{
 for (size_t i = 0; i <  prevPts.size(); ++i)
 {
  if (status[i])
  {
   int line_thickness = 1;

   Point p = prevPts[i];
   Point q = nextPts[i];

   double angle = atan2((double)p.y - q.y, (double)p.x - q.x);

   double hypotenuse = sqrt((double)(p.y - q.y)*(p.y - q.y) + (double)(p.x - q.x)*(p.x - q.x));

   if (hypotenuse <  1.0)
    continue;

   // Here we lengthen the arrow by a factor of three.
   q.x = (int)(p.x - 3 * hypotenuse * cos(angle));
   q.y = (int)(p.y - 3 * hypotenuse * sin(angle));

   // Now we draw the main line of the arrow.
   line(frame, p, q, line_color, line_thickness);

   // Now draw the tips of the arrow. I do some scaling so that the
   // tips look proportional to the main line of the arrow.

   p.x = (int)(q.x + 9 * cos(angle + CV_PI / 4));
   p.y = (int)(q.y + 9 * sin(angle + CV_PI / 4));
   line(frame, p, q, line_color, line_thickness);

   p.x = (int)(q.x + 9 * cos(angle - CV_PI / 4));
   p.y = (int)(q.y + 9 * sin(angle - CV_PI / 4));
   line(frame, p, q, line_color, line_thickness);
  }
 }
}



...

10/01/2015

Deep learning Library to work easily with opencv on window environment.

I was wrapping caffe library to use easy with opencv and window os environment(named MareDeepDLL).

MareDeepDll is referenced on
http://caffe.berkeleyvision.org/installation.html
https://initialneil.wordpress.com/


The dll is made on this environment
Window 10 64bit,
VS 2013 64bit
OpenCV 3.0 64bit
cuda 6.5 64bit
tbb 64bit
..
All dependency is as follows:


This code is example to use the dll
...
#include < iostream>  
#include < stdio.h>
#include < vector>
#include < time.h>

#include <  opencv2\opencv.hpp>    
#include <  opencv2\core.hpp>  
#include <  opencv2\highgui.hpp>   
#include <  opencv2\videoio.hpp>    
#include <  opencv2\imgproc.hpp>  



#include "DeepDll_B.h"

#ifdef _DEBUG            
#pragma comment(lib, "opencv_core300d.lib")  
#pragma comment(lib, "opencv_highgui300d.lib")    
#pragma comment(lib, "opencv_imgcodecs300d.lib")  
#pragma comment(lib, "opencv_imgproc300d.lib") //line, circle  
#else    
#pragma comment(lib, "opencv_core300.lib")  
#pragma comment(lib, "opencv_highgui300.lib")  
#pragma comment(lib, "opencv_imgcodecs300.lib")  
#pragma comment(lib, "opencv_imgproc300.lib") //line, circle  

//DEEP lib
#pragma comment(lib, "MareDeepDLL.lib")  
#endif   

using namespace cv;
using namespace std;

void main()
{

 //DEEP Class
 MareDeepDll_B cDeep;

 //load model and structure
 cDeep.SetNet("lenet_test-memory-1.prototxt", "lenet_iter_10000.caffemodel");
 //gpu using on
 cDeep.GPU_using();
 

 for (int i = 1; i <  14; ++i)
 {
  // time check..
  unsigned long AAtime = 0, BBtime = 0;
  AAtime = getTickCount();


  //make file name
  char str[256];  
  sprintf_s(str, "%d.jpg", i);
  printf("%s\n", str);
  

  //img load and preprocessing
  Mat img = imread(str);
  resize(img, img, Size(28, 28));
  cvtColor(img, img, CV_BGR2GRAY);  


  ////////////
  //classify
  vector< double> rV;
  //image and class num (caution!! class num is dependented by learning condition.) lenet is classify one number in 10 digits.
  rV = cDeep.eval(img, 10);
  /////////////

  //result out
  for (int i = 0; i <  rV.size(); i++) {
   printf("Probability to be Number %d is %.3f\n", i, rV[i]);   
  }

  // processing time check.
  BBtime = getTickCount();
  printf("%.2lf sec / %.2lf fps\n", (BBtime - AAtime) / getTickFrequency(), 1 / ((BBtime - AAtime) / getTickFrequency()));

  //draw
  namedWindow("test", 0);
  imshow("test", img);
  waitKey(0);
 }

}


...
Lenet model was used to test the deep learning classification.
Many other models are introduced on github model zoo.
https://github.com/BVLC/caffe/wiki/Model-Zoo
You can apply other case, on code cDeep.SetNet("lenet_test-memory-1.prototxt", "lenet_iter_10000.caffemodel"); , first param means model structure and second param means the result of deep learning.




If you request to Google Plus to me, I will send the dll with the application code(project).




9/22/2015

opencv rtsp connection using vlc library

RTSP(Real Time Streaming Protocol) is video streaming, it usually sent from network camera.

VideoCapture function in opencv also can get rtsp video, but difficult to receive a stable image.
This is simple example using videocapture.
...
int main()
{

 VideoCapture cap("rtsp://192.168.0.50");

 namedWindow("fish", 0);
 Mat fishEye;
 while (1)
 {
  cap >> fishEye;
  if (fishEye.empty())
   return 0;

  imshow("fish", fishEye);
  if (waitKey(10) > 0)
   break;
 }

 return 0;

}
...

We can receive more reliable RTSP using VLC library.
Of course, there are many different ways(LIVE555, FFmpeg, Qt api...).

#include < windows.h>
#include < vlc/vlc.h>

#include < opencv2\opencv.hpp>    
#include < opencv2\core.hpp>  
#include < opencv2\highgui.hpp>   

#pragma comment(lib, "libvlc.lib")

#ifdef _DEBUG
#pragma comment(lib, "opencv_core300d.lib")  
#pragma comment(lib, "opencv_highgui300d.lib")
#pragma comment(lib, "opencv_imgproc300d.lib")
#pragma comment(lib, "opencv_imgcodecs300d.lib")
#else
#pragma comment(lib, "opencv_core300.lib")  
#pragma comment(lib, "opencv_highgui300.lib")
#pragma comment(lib, "opencv_imgproc300.lib")
#pragma comment(lib, "opencv_imgcodecs300.lib")
#endif

using namespace cv;
using namespace std;

struct ctx
{
 Mat* image;
 HANDLE mutex;
 uchar* pixels;
};
bool isRunning = true;

Size getsize(const char* path)
{
 libvlc_instance_t *vlcInstance;
 libvlc_media_player_t *mp;
 libvlc_media_t *media;

 char *vlc_argv[6];
 vlc_argv[0] = new char[3];
 strcpy_s(vlc_argv[0], 3, "-I");
 vlc_argv[1] = new char[6];
 strcpy_s(vlc_argv[1], 6, "dummy"); // Don't use any interface
 vlc_argv[2] = new char[16];
 strcpy_s(vlc_argv[2], 16, "--ignore-config"); // Don't use VLC's config
 vlc_argv[3] = new char[128];
 strcpy_s(vlc_argv[3], 128, "--plugin-path=/plugins");
 int vlc_argc = 4;

 vlcInstance = libvlc_new(vlc_argc, vlc_argv);

 for (int i = 0; i < vlc_argc; i++)
  delete[] vlc_argv[i];

 media = libvlc_media_new_location(vlcInstance, path);
 mp = libvlc_media_player_new_from_media(media);

 libvlc_media_release(media);
 libvlc_video_set_callbacks(mp, NULL, NULL, NULL, NULL);
 libvlc_video_set_format(mp, "RV24", 100, 100, 100 * 24 / 8); // pitch = width * BitsPerPixel / 8
 //libvlc_video_set_format(mp, "RV32", 100, 100, 100 * 4);
 libvlc_media_player_play(mp);

 Sleep(2000);//wait a while so that something get rendered so that size info is available
 unsigned int width = 640, height = 480;

 for (int i = 0; i < 30 && height == 0; i++)
 {
  Sleep(50);
  libvlc_video_get_size(mp, 0, &width, &height);

  if (width != 0 && height != 0)
   break;
 }


 if (width == 0 || height == 0)
 {
  width = 640;
  height = 480;
 }

 libvlc_media_player_stop(mp);
 libvlc_release(vlcInstance);
 libvlc_media_player_release(mp);
 return Size(width, height);
}


void *lock(void *data, void**p_pixels)
{
 struct ctx *ctx = (struct ctx*)data;
 WaitForSingleObject(ctx->mutex, INFINITE);
 *p_pixels = ctx->pixels;
 return NULL;

}

void display(void *data, void *id){
 (void)data;
 assert(id == NULL);
}

void unlock(void *data, void *id, void *const *p_pixels)
{

 struct ctx *ctx = (struct ctx*)data;
 Mat frame = *ctx->image;
 if (frame.data)
 {
  imshow("frame", frame);
  if (waitKey(1) == 27)
  {
   isRunning = false;
   //exit(0);
  }
 }
 ReleaseMutex(ctx->mutex);
}


int main()
{
 string url = "rtsp://...";
 //vlc sdk does not know the video size until it is rendered, so need to play it a bit so that size is     known
 Size sz = getsize(url.c_str());

 // VLC pointers
 libvlc_instance_t *vlcInstance;
 libvlc_media_player_t *mp;
 libvlc_media_t *media;

 char *vlc_argv[6];
 vlc_argv[0] = new char[3];
 strcpy_s(vlc_argv[0], 3, "-I");
 vlc_argv[1] = new char[6];
 strcpy_s(vlc_argv[1], 6, "dummy"); // Don't use any interface
 vlc_argv[2] = new char[16];
 strcpy_s(vlc_argv[2], 16, "--ignore-config"); // Don't use VLC's config
 vlc_argv[3] = new char[128];
 strcpy_s(vlc_argv[3], 128, "--plugin-path=/plugins");
 int vlc_argc = 4;

 vlcInstance = libvlc_new(vlc_argc, vlc_argv);

 for (int i = 0; i < vlc_argc; i++)
  delete[] vlc_argv[i];

 media = libvlc_media_new_location(vlcInstance, url.c_str());
 mp = libvlc_media_player_new_from_media(media);
 libvlc_media_release(media);


 struct ctx* context = (struct ctx*)malloc(sizeof(*context));
 context->mutex = CreateMutex(NULL, FALSE, NULL);
 context->image = new Mat(sz.height, sz.width, CV_8UC3);
 context->pixels = (unsigned char *)context->image->data;

 libvlc_video_set_callbacks(mp, lock, unlock, display, context);
 libvlc_video_set_format(mp, "RV24", sz.width, sz.height, sz.width * 24 / 8); // pitch = width *     BitsPerPixel / 8
 //libvlc_video_set_format(mp, "RV32", sz.width, sz.height, sz.width * 4);

 libvlc_media_player_play(mp);
 while (isRunning)
 {
  //imshow("rtsp", *(context->image));
  Sleep(1);  
 }

 libvlc_media_player_stop(mp);
 libvlc_release(vlcInstance);
 libvlc_media_player_release(mp);
 free(context);

 return 0;
}


This example code is referenced in here
http://stackoverflow.com/questions/23529620/opencv-and-network-cameras-or-how-to-spy-on-the-neighbors

To use this example code, you should install VLC player, and set dependency referencing include, lib, dll and plugins.
And If you are using opencv 64 bits then you also install 64-bit vlc.

Test video


9/09/2015

opencv 3.0 cascade_gpu example source code

opencv 2.49 example is here
http://study.marearts.com/2014/09/opencv-face-detection-using-adaboost.html



#include < iostream>    
#include "opencv2\objdetect\objdetect.hpp"
#include "opencv2\highgui\highgui.hpp"
#include "opencv2\imgproc\imgproc.hpp"
#include "opencv2\cudaobjdetect.hpp"
#include "opencv2\cudaimgproc.hpp"

#ifdef _DEBUG               
#pragma comment(lib, "opencv_core300d.lib")       
#pragma comment(lib, "opencv_highgui300d.lib")    
#pragma comment(lib, "opencv_imgcodecs300d.lib")  
#pragma comment(lib, "opencv_objdetect300d.lib")  
#pragma comment(lib, "opencv_imgproc300d.lib")  
#pragma comment(lib, "opencv_cudaobjdetect300d.lib")  
#else       
#pragma comment(lib, "opencv_core300.lib")       
#pragma comment(lib, "opencv_highgui300.lib")    
#pragma comment(lib, "opencv_imgcodecs300.lib")    
#pragma comment(lib, "opencv_objdetect300.lib")  
#pragma comment(lib, "opencv_imgproc300.lib")  
#pragma comment(lib, "opencv_cudaobjdetect300.lib")  
#endif        

using namespace std;
using namespace cv;


void main()
{
 //for time measure  
 float TakeTime;
 unsigned long Atime, Btime;

 //window  
 namedWindow("origin");

 //load image  
 Mat img = imread("sh.jpg");
 Mat grayImg; //adaboost detection is gray input only.  
 cvtColor(img, grayImg, CV_BGR2GRAY);

 //load xml file  
 string trainface = ".\\haarcascade_frontalface_alt.xml";

 //declaration  
 Ptr< cuda::CascadeClassifier> cascade_gpu = cuda::CascadeClassifier::create(trainface);

 
 /////////////////////////////////////////////  
 
 //gpu case face detection code  
 cuda::GpuMat faceBuf_gpu;
 cuda::GpuMat GpuImg;
 vector< Rect> faces;

 GpuImg.upload(grayImg);
 Atime = getTickCount();

 cascade_gpu->detectMultiScale(GpuImg, faceBuf_gpu);
 cascade_gpu->convert(faceBuf_gpu, faces);
 Btime = getTickCount();
 TakeTime = (Btime - Atime) / getTickFrequency();
 printf("detected face(gpu version) =%d / %lf sec take.\n", faces.size(), TakeTime);
 Mat faces_downloaded;
 if (faces.size() >= 1)
 {
  for (size_t i = 0; i < faces.size(); ++i)
   rectangle(img, faces[i], Scalar(255));
 } 

 /////////////////////////////////////////////////  
 //result display  
 imshow("origin", img);
 waitKey(0);
}


mnist image dataset (jpg files)

The MNIST dataset is a dataset of handwritten digits, comprising 60 000 training examples and 10 000 test examples. The dataset can be downloaded from http://yann.lecun.com/exdb/mnist/.
MNIST data is provided in binary file for user convenience.

But, in my case, this binary type is more difficult.
I am needed image files.

I want to start whole process of deep learning(make leveldb file, learning, classify) using MNIST data and compare to example result in caffe model.


This site tells me a hint how to make image datas.

This is my matlab code for making jpg file.

%%
clc;
clear all;

% Change the filenames if you've saved the files under different names
% On some platforms, the files might be saved as 
% train-images.idx3-ubyte / train-labels.idx1-ubyte


images = loadMNISTImages('t10k-images.idx3-ubyte');
labels = loadMNISTLabels('t10k-labels.idx1-ubyte');

%images = loadMNISTImages('train-images.idx3-ubyte');
%labels = loadMNISTLabels('train-labels.idx1-ubyte');

 
% We are using display_network from the autoencoder code
display_network(images(:,1:60000)); % Show the first 100 images
disp(labels(1:1));

% save one digit number to jpg file
%{
[w h]=size(images)
endIndex = h;

dindex=zeros(10,1);
for j=1:endIndex
    aImg = reshape( images(:,j), 28, 28);
    imshow(aImg);
    
    label = labels(j);
    dindex( label+1 ) = dindex( label+1 ) +1;
    dataD = strcat('./data2/mnist_', num2str( label ), '_', num2str( dindex(label+1) ), '.jpg' );
    
    %dindex
    imwrite(aImg, dataD);
    dataD
end




fileID = fopen('test.txt','w');
%make path and label
dindex=zeros(10,1);
for j=1:endIndex
    
    label = labels(j);
    dindex( label+1 ) = dindex( label+1 ) +1;
    dataD = strcat('/data2/mnist_', num2str( label ), '_', num2str( dindex(label+1) ), '.jpg' );
    fprintf(fileID,'%s %d\n',dataD, label);    
end
fclose(fileID);
%}
---

And images of mnist files


If you need images of mnist, request to me, I will send by email.
The file size is approximately 37 MB.

Thank you.

full data is here.
https://www.amazon.com/clouddrive/share/S4KGsmQIRtfINLbZ9O3nF5QNh5Xk4ZG702sWKKhybDf?ref_=cd_ph_share_link_copy

6/12/2015

MSER text detection example (opencv 300)

MSER text detection example



...
#include < iostream>  
#include < opencv2\opencv.hpp>  
#include < opencv2\highgui.hpp>  
#include < opencv2\imgcodecs.hpp>  
#include < opencv2\core.hpp>
#include < opencv2\imgproc.hpp>
#include < opencv2\text.hpp>
#include < opencv2/features2d.hpp>


#ifdef _DEBUG             
#pragma comment(lib, "opencv_core300d.lib")     
#pragma comment(lib, "opencv_highgui300d.lib")  
#pragma comment(lib, "opencv_imgcodecs300d.lib")
#pragma comment(lib, "opencv_text300d.lib")
#pragma comment(lib, "opencv_features2d300d.lib")
#pragma comment(lib, "opencv_imgproc300d.lib")

#else     
#pragma comment(lib, "opencv_core300.lib")     
#pragma comment(lib, "opencv_highgui300.lib")  
#pragma comment(lib, "opencv_imgcodecs300.lib")  
#pragma comment(lib, "opencv_text300.lib")
#pragma comment(lib, "opencv_features2d300.lib")
#pragma comment(lib, "opencv_imgproc300d.lib")
#endif      

using namespace std;
using namespace cv;

void groups_draw(Mat &src, vector< Rect> &groups);

void main()
{

 Mat inImg = imread(".\\scenetext01.jpg");
 
 vector< Mat> channels;
 text::computeNMChannels(inImg, channels);

 int cn = (int)channels.size();
 // Append negative channels to detect ER- (bright regions over dark background)
 for (int c = 0; c < cn - 1; c++)
  channels.push_back(255 - channels[c]);

 


 Ptr< text::ERFilter> er_filter1 = text::createERFilterNM1(text::loadClassifierNM1(".\\trained_classifierNM1.xml"), 
  16, 0.00015f, 0.13f, 0.2f, true, 0.1f);
 Ptr< text::ERFilter> er_filter2 = text::createERFilterNM2(text::loadClassifierNM2(".\\trained_classifierNM2.xml"), 0.5);

 vector< vector< text::ERStat> > regions(channels.size());

 for (int c = 0; c< (int)channels.size(); c++)
 {
  er_filter1->run(channels[c], regions[c]);
  er_filter2->run(channels[c], regions[c]);
 }


 // Detect character groups
 cout << "Grouping extracted ERs ... ";
 vector< vector< Vec2i> > region_groups;
 vector< Rect> groups_boxes;
 text::erGrouping(inImg, channels, regions, region_groups, groups_boxes, text::ERGROUPING_ORIENTATION_HORIZ);

 groups_draw(inImg, groups_boxes);
 imshow("grouping", inImg);

 waitKey(0);

}


void groups_draw(Mat &src, vector< Rect> &groups)
{
 for (int i = (int)groups.size() - 1; i >= 0; i--)
 {
  if (src.type() == CV_8UC3)
   rectangle(src, groups.at(i).tl(), groups.at(i).br(), Scalar(0, 255, 255), 3, 8);
  else
   rectangle(src, groups.at(i).tl(), groups.at(i).br(), Scalar(255), 3, 8);
 }
}

///

OpenCV MSER example (opencv 300 )

MSER example




...
#include < iostream>  
#include < opencv2\opencv.hpp>  
#include < opencv2\highgui.hpp>  
#include < opencv2\imgcodecs.hpp>  
#include < opencv2\core.hpp>
#include < opencv2\imgproc.hpp>
#include < opencv2\text.hpp>
#include < opencv2/features2d.hpp>


#ifdef _DEBUG             
#pragma comment(lib, "opencv_core300d.lib")     
#pragma comment(lib, "opencv_highgui300d.lib")  
#pragma comment(lib, "opencv_imgcodecs300d.lib")
#pragma comment(lib, "opencv_text300d.lib")
#pragma comment(lib, "opencv_features2d300d.lib")
#pragma comment(lib, "opencv_imgproc300d.lib")

#else     
#pragma comment(lib, "opencv_core300.lib")     
#pragma comment(lib, "opencv_highgui300.lib")  
#pragma comment(lib, "opencv_imgcodecs300.lib")  
#pragma comment(lib, "opencv_text300.lib")
#pragma comment(lib, "opencv_features2d300.lib")
#pragma comment(lib, "opencv_imgproc300d.lib")
#endif      

using namespace std;
using namespace cv;
void main()
{

 Mat inImg = imread("M:\\____videoSample____\\SceneText\\SceneText01.jpg");

 Mat textImg;
 cvtColor(inImg, textImg, CV_BGR2GRAY);
 //Extract MSER
 
 vector< vector< Point> > contours;
 vector< Rect> bboxes;
 Ptr< MSER> mser = MSER::create(21, (int)(0.00002*textImg.cols*textImg.rows), (int)(0.05*textImg.cols*textImg.rows), 1, 0.7); 
 mser->detectRegions(textImg, contours, bboxes); 

 for (int i = 0; i < bboxes.size(); i++)
 {
  rectangle(inImg, bboxes[i], CV_RGB(0, 255, 0));
 }


 namedWindow("t");
 imshow("t", inImg);
 waitKey(0);

}


6/10/2015

Line equation study in 3D


Equations of a straight line in 3D space.

Above that of any points straight line in three-dimensional space, it can be represented as a vector r.


It represents a 3D point coordinates as follows:




Let's further understanding through exercises


ex1) Find the vector equation of a line through (-16, 4, 11) and (8, 0, -5)


How to get vector b ?






Let see one more example.

ex2) Find the vector equation of a line through (-7, -7, 2) and parallel to the line in ex1)

vector b is same with ex1), because parallel.

And this line through on (-7, -7, 2), so equation is like that





And let's see one more example.
Don't you interesting?

ex3) Find line equation of vector b.





ex4)
Firstly, let find AX vector.


And we should get t value.
How? the principles of inner product.
It is a vector perpendicular to the inner product is '0'.
So.



input t value into AX vector. then..





ex5)

Find the acute angle between line 1 and line 2

where 


->
The directions are and   in L1 and L2.

In inner product rule, 
 

So, we will follow below formula..


->










6/08/2015

Dense optical flow test in 2 continuous images(opencv 2.4.9)

Dear Rahma
I hope to help this code to you.
Thank you.




...
#include < stdio.h>    
  
#include < opencv2\opencv.hpp>    
#include < opencv2/core/core.hpp>    
#include < opencv2/highgui/highgui.hpp>    
#include < opencv2\nonfree\features2d.hpp >        
  
  
  
#ifdef _DEBUG            
#pragma comment(lib, "opencv_core249d.lib")    
#pragma comment(lib, "opencv_imgproc249d.lib")   //MAT processing    
#pragma comment(lib, "opencv_objdetect249d.lib") //HOGDescriptor    
#pragma comment(lib, "opencv_features2d249d.lib")    
#pragma comment(lib, "opencv_highgui249d.lib")    
#pragma comment(lib, "opencv_video249d.lib")    
#else    
#pragma comment(lib, "opencv_core249.lib")    
#pragma comment(lib, "opencv_imgproc249.lib")    
#pragma comment(lib, "opencv_objdetect249.lib")    
#pragma comment(lib, "opencv_features2d249.lib")    
#pragma comment(lib, "opencv_highgui249.lib")   
#pragma comment(lib, "opencv_video249.lib")    
#endif     
  
using namespace std;    
using namespace cv; 

void drawOptFlowMap (const Mat& flow, Mat& cflowmap, int step, const Scalar& color);

void main()
{
 Mat prev_im,nex_im,flow;

 prev_im=imread("1.jpg",0);
 imshow("previm",prev_im);


 nex_im=imread("2.jpg",0);
 imshow("nextim",nex_im);


 calcOpticalFlowFarneback(prev_im,nex_im,flow,0.5, 1, 12, 2, 7, 1.5, 0); 

 Mat cflow;
 cvtColor(prev_im, cflow, CV_GRAY2BGR);
 drawOptFlowMap(flow, cflow, 10, CV_RGB(0, 255, 0));  


 imshow("OpticalFlowFarneback", cflow);
 waitKey(0);

}


void drawOptFlowMap (const Mat& flow, Mat& cflowmap, int step, const Scalar& color) {  
 for(int y = 0; y < cflowmap.rows; y += step)  
  for(int x = 0; x < cflowmap.cols; x += step)  
  {  
   const Point2f& fxy = flow.at< Point2f>(y, x);  
   line(cflowmap, Point(x,y), Point(cvRound(x+fxy.x), cvRound(y+fxy.y)),  
    color);  
   circle(cflowmap, Point(cvRound(x+fxy.x), cvRound(y+fxy.y)), 1, color, -1);  
  }  
}  

...

6/02/2015

opencv 3.0 rc1, example source code for surf and matching (gpu version)

This code is SURF and Matching test in opencv 3.0 rc1.

Especially, for using surf class, we have to add extra library when build opencv 3.0 rc1.
The extra lib is opened in github named opencv_contrib.

refer to this page.
http://study.marearts.com/2015/01/mil-boosting-tracker-test-in-opencv-30.html

surf(gpu version) is included in xfeatures2d/cuda.hpp
and cpu version is xfeatures2d/nonfree.hpp

As we know, surf, sift are still nonfree. very not good!..-.-

necessary dlls

result


Now, refer this example code.
This cod is converted from http://study.marearts.com/2014/07/opencv-study-surf-gpu-and-matching.html.


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


#include < opencv2\opencv.hpp>  
#include < opencv2\core.hpp>
#include < opencv2\highgui.hpp> 
#include < opencv2\cudaarithm.hpp>
#include < opencv2\xfeatures2d\cuda.hpp>
#include < opencv2\cudafeatures2d.hpp>
//#include < opencv2\xfeatures2d\nonfree.hpp>



#ifdef _DEBUG          
#pragma comment(lib, "opencv_core300d.lib")
#pragma comment(lib, "opencv_highgui300d.lib")  
#pragma comment(lib, "opencv_imgcodecs300d.lib")
#pragma comment(lib, "opencv_cudafeatures2d300d.lib")
#pragma comment(lib, "opencv_xfeatures2d300d.lib")
#pragma comment(lib, "opencv_features2d300d.lib")
#else  
#pragma comment(lib, "opencv_core300.lib")
#pragma comment(lib, "opencv_highgui300.lib")
#pragma comment(lib, "opencv_imgcodecs300.lib")
#pragma comment(lib, "opencv_cudafeatures2d300.lib")
#pragma comment(lib, "opencv_xfeatures2d300.lib")
#pragma comment(lib, "opencv_features2d300.lib")
#endif   


using namespace cv;
using namespace std;

void main()
{


 
 cuda::GpuMat img1(imread("M:\\____videoSample____\\Image\\A2.jpg", CV_LOAD_IMAGE_GRAYSCALE));
 cuda::GpuMat img2(imread("M:\\____videoSample____\\Image\\A3.jpg", CV_LOAD_IMAGE_GRAYSCALE));

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

 
 cuda::SURF_CUDA surf(400); 
 // detecting keypoints & computing descriptors   
 cuda::GpuMat keypoints1GPU, keypoints2GPU;
 cuda::GpuMat descriptors1GPU, descriptors2GPU;
 surf(img1, cuda::GpuMat(), keypoints1GPU, descriptors1GPU);
 surf(img2, cuda::GpuMat(), keypoints2GPU, descriptors2GPU);

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

 // matching descriptors   
 vector< vector< DMatch> > matches;
 Ptr< cuda::DescriptorMatcher > matcher = cuda::DescriptorMatcher::createBFMatcher();
 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);

 namedWindow("matches", 0);
 imshow("matches", img_matches);
 waitKey(0);
 
 matcher.release();

}


6/01/2015

opencv 3.0 rc1, background subtraction mog2 example code

This is mog2 example code in opencv 3.0 rc1 version.

It will be help when you convert opencv 2.x code to 3.x opencv version.
Thank you.

This is mog2 example of 2.4.9 version.
http://study.marearts.com/2015/05/basic-mog2-example-souce-using-opencv.html

...
#include < iostream>  
#include < opencv2\opencv.hpp>  
#include < opencv2\videoio.hpp>  
#include < opencv2\highgui.hpp>  
//#include < opencv2\core\cuda.hpp>
#include < opencv2\core.hpp>
//#include < opencv2\bgsegm.hpp>
#include < opencv2\cudabgsegm.hpp>

#ifdef _DEBUG             
#pragma comment(lib, "opencv_core300d.lib")     
#pragma comment(lib, "opencv_highgui300d.lib")  
#pragma comment(lib, "opencv_videoio300d.lib")  
#pragma comment(lib, "opencv_cudabgsegm300d.lib")  

#else     
#pragma comment(lib, "opencv_core300.lib")     
#pragma comment(lib, "opencv_highgui300.lib")  
#pragma comment(lib, "opencv_videoio300.lib")  
#pragma comment(lib, "opencv_cudabgsegm300.lib")  
#endif      

using namespace std;
using namespace cv;
int main()
{

 //
 
 VideoCapture cap("M:\\____videoSample____\\blog\\video20.wmv");
 //VideoCapture cap("M:\\____videoSample____\\posco\\cartype1.avi");
 
 //
 Ptr< cuda::BackgroundSubtractorMOG2 > MOG2_g = cuda::createBackgroundSubtractorMOG2(3000, 64); 
 Mat Mog_Mask;
 cuda::GpuMat Mog_Mask_g;

 //
 Mat o_frame;
 cuda::GpuMat o_frame_gpu;
 

 cap >> o_frame;
 if (o_frame.empty())
  return 0;

 /////////////////////////////////////////////////////////////////////////    


 unsigned long AAtime = 0, BBtime = 0;

 //Mat rFrame;    
 Mat showMat_r_blur;
 Mat showMat_r;

 namedWindow("origin");
 namedWindow("mog_mask");

 while (1)
 {
  /////////////////////////////////////////////////////////////////////////    
  cap >> o_frame;
  if (o_frame.empty())
   return 0;


  o_frame_gpu.upload(o_frame);
  AAtime = getTickCount();

  //
  MOG2_g->apply(o_frame_gpu, Mog_Mask_g, -1);
  //pMOG2_g.operator()(o_frame_gpu, Mog_Mask_g, -1);
  //  
  Mog_Mask_g.download(Mog_Mask);

  BBtime = getTickCount();
  float pt = (BBtime - AAtime) / getTickFrequency();
  float fpt = 1 / pt;
  printf("gpu %.4lf / %.4lf \n", pt, fpt);



  o_frame_gpu.download(showMat_r);
  imshow("origin", showMat_r);
  imshow("mog_mask", Mog_Mask);


  /////////////////////////////////////////////////////////////////////////    

  if (waitKey(10) > 0)
   break;
 }

 MOG2_g.release();

 return 0;
}



...

5/28/2015

OpenCV 3.0 rc1, FarnebackOpticalFlow(dense optical flow) example source code.

This article introduces FarnebackOpticalFlow example in opencv 3.0 rc1.
FarnebackOpticalFlow class also was changed dll name to reference.

This example is similar with here(http://study.marearts.com/2014/04/opencv-study-example-source-code-for.html).


This example will help what dll and header file need to use FarnebackOpticalFlow  class.

Required Dlls are follows :
(tbb.dll is my option)

And refer to this example.

#include < iostream>  
#include < opencv2\opencv.hpp>  
#include < opencv2\highgui.hpp>  
//#include < opencv2\imgcodecs.hpp>  
#include < opencv2\videoio.hpp> 
#include < opencv2\core\cuda.hpp>
#include < opencv2\imgproc.hpp>
#include < opencv2\cudawarping.hpp>
#include < opencv2\cudaimgproc.hpp>
//#include < opencv2\cudaarithm.hpp>
#include < opencv2\cudaoptflow.hpp>


#ifdef _DEBUG             
#pragma comment(lib, "opencv_core300d.lib")     
#pragma comment(lib, "opencv_highgui300d.lib")  
//#pragma comment(lib, "opencv_imgcodecs300d.lib")  //imread
#pragma comment(lib, "opencv_videoio300d.lib") //video capture
#pragma comment(lib, "opencv_imgproc300d.lib") //line, circle
#pragma comment(lib, "opencv_cudawarping300d.lib") //cuda::resize
#pragma comment(lib, "opencv_cudaimgproc300.lib") //cuda::cvtcolor
#pragma comment(lib, "opencv_cudaarithm300d.lib") //cuda::farnebackOpticalFlow
#pragma comment(lib, "opencv_cudaoptflow300d.lib") 
#else     
#pragma comment(lib, "opencv_core300.lib")     
#pragma comment(lib, "opencv_highgui300.lib")  
//#pragma comment(lib, "opencv_imgcodecs300.lib")  //imread
#pragma comment(lib, "opencv_videoio300.lib") //video capture
#pragma comment(lib, "opencv_imgproc300.lib") // //line, circle
#pragma comment(lib, "opencv_cudawarping300.lib") //cuda::resize
#pragma comment(lib, "opencv_cudaimgproc300.lib") //cuda::cvtcolor
#pragma comment(lib, "opencv_cudaarithm300.lib") //cuda::farnebackOpticalFlow
#pragma comment(lib, "opencv_cudaoptflow300.lib") 

#endif      

using namespace std;
using namespace cv;

void drawOptFlowMap_gpu(const Mat& flow_xy, Mat& cflowmap, int step, const Scalar& color);

int main()
{

 int s = 1;

 unsigned long AAtime = 0, BBtime = 0;

 //variables  
 Mat GetImg, flow_x, flow_y, next, prvs, flow_xy;

 //gpu variable  
 cuda::GpuMat prvs_gpu, next_gpu, flow_x_gpu, flow_y_gpu, flow_xy_gpu;
 cuda::GpuMat prvs_gpu_o, next_gpu_o;
 cuda::GpuMat prvs_gpu_c, next_gpu_c;

 //file name  
 char fileName[100] = "M:\\____videoSample____\\Rendering\\Wildlife.avi";
 //video file open  
 VideoCapture stream1(fileName);   //0 is the id of video device.0 if you have only one camera
 if (!(stream1.read(GetImg))) //get one frame form video
  return 0;

 //gpu upload, resize, color convert
 prvs_gpu_o.upload(GetImg);

 
 cuda::resize(prvs_gpu_o, prvs_gpu_c, Size(GetImg.size().width / s, GetImg.size().height / s));
 cuda::cvtColor(prvs_gpu_c, prvs_gpu, CV_BGR2GRAY);

 //dense optical flow
 Ptr< cuda::FarnebackOpticalFlow > fbOF = cuda::FarnebackOpticalFlow::create();

 

 //unconditional loop
 while (true) {

  if (!(stream1.read(GetImg))) //get one frame form video     
   break;

   ///////////////////////////////////////////////////////////////////  
  //gpu upload, resize, color convert  
  next_gpu_o.upload(GetImg);
  cuda::resize(next_gpu_o, next_gpu_c, Size(GetImg.size().width / s, GetImg.size().height / s));
  cuda::cvtColor(next_gpu_c, next_gpu, CV_BGR2GRAY);
  ///////////////////////////////////////////////////////////////////  

  AAtime = getTickCount();
  //dense optical flow  
  fbOF->calc(prvs_gpu, next_gpu, flow_xy_gpu);

  BBtime = getTickCount();
  float pt = (BBtime - AAtime) / getTickFrequency();
  float fpt = 1 / pt;
  printf("%.2lf / %.2lf \n", pt, fpt);

  //copy for vector flow drawing  
  Mat cflow;
  resize(GetImg, cflow, Size(GetImg.size().width / s, GetImg.size().height / s));  
  flow_xy_gpu.download(flow_xy);
  drawOptFlowMap_gpu(flow_xy, cflow, 10, CV_RGB(0, 255, 0));
  imshow("OpticalFlowFarneback", cflow);

  ///////////////////////////////////////////////////////////////////  
  //Display gpumat  
  next_gpu.download(next);
  prvs_gpu.download(prvs);
  imshow("next", next);
  imshow("prvs", prvs);

  //prvs mat update  
  prvs_gpu = next_gpu.clone();

  if (waitKey(5) >= 0)
   break;
 }


 return 0;
}

void drawOptFlowMap_gpu(const Mat& flow_xy, Mat& cflowmap, int step, const Scalar& color)
{

 for (int y = 0; y < cflowmap.rows; y += step)
  for (int x = 0; x < cflowmap.cols; x += step)
  {
   Point2f fxy;
   fxy.x = cvRound(flow_xy.at< Vec2f >(y, x)[0] + x);
   fxy.y = cvRound(flow_xy.at< Vec2f >(y, x)[1] + y);


   cv::line(cflowmap, Point(x, y), Point(fxy.x, fxy.y), color);
   cv::circle(cflowmap, Point(fxy.x, fxy.y), 1, color, -1);

  }

}




5/26/2015

5/06/2015

OpenCV300 rc1 - VideoCapture, imread example

This is Video capture example in ver. opencv 3.0 rc1.

In 3.0, location of functions are little changed.
In case of VideoCapture, we should include opencv_videoio300.lib.

So note to this source code for easy coding access.


#include < iostream>
#include < opencv2\opencv.hpp>
#include < opencv2\videoio.hpp>
#include < opencv2\highgui.hpp>

#ifdef _DEBUG           
#pragma comment(lib, "opencv_core300d.lib")   
#pragma comment(lib, "opencv_highgui300d.lib")
#pragma comment(lib, "opencv_videoio300d.lib")
#else   
#pragma comment(lib, "opencv_core300.lib")   
#pragma comment(lib, "opencv_highgui300.lib")
#pragma comment(lib, "opencv_videoio300.lib")
#endif    

using namespace std;
using namespace cv;
void main()
{
 
 Mat test;
 cv::VideoCapture cap("M:\\____videoSample____\\posco\\cartype1.avi");//0); 
 namedWindow("t");
 while(1)
 {
  cap >> test;
  if( test.empty() )
   break;

  imshow("t", test);

  if( waitKey(10) > 0)
   break;
 }
}



----

This is source code of using "imread" function. (need opencv_imgcodecs300.lib )

#include < iostream>
#include < opencv2\opencv.hpp>
#include < opencv2\highgui.hpp>
#include < opencv2\imgcodecs.hpp>

#ifdef _DEBUG           
#pragma comment(lib, "opencv_core300d.lib")   
#pragma comment(lib, "opencv_highgui300d.lib")
#pragma comment(lib, "opencv_imgcodecs300d.lib")
#else   
#pragma comment(lib, "opencv_core300.lib")   
#pragma comment(lib, "opencv_highgui300.lib")
#pragma comment(lib, "opencv_imgcodecs300.lib")
#endif    

using namespace std;
using namespace cv;
void main()
{
 
 Mat test;
 test = imread("M:\\____videoSample____\\torso\\Positive1.png");

 namedWindow("t");
 imshow("t", test);
 waitKey(0);

}





QT + OpenCV error : C1083 opencv2/opencv.hpp no such file or directory

In Qt, setting of "include and lib path" is configured in the .pro file.

For example :

INCLUDEPATH += C:\opencv300rc1\opencv\build\include
LIBS += C:\opencv300rc1\MyBuild\lib\Debug

But in my case, I can not find the reason of error "C1083 opencv.hpp no such file or directory..."

I tried various attempts, but I don't know why error occur.
But I found the way of to avoid the error.
That is using build setting tab (ctrl + 5).
In build environment, add opencv path INCLUDE and LIB.



I solved the problem in this way.
Please note.











Creating dynamic link library including OpenCV(on MS Visual studio)


The method to make dynamic link library is introduced in here.
https://msdn.microsoft.com/en-us//library/ms235636.aspx

This introduction is similar with this.
But if you refer both, you will be more helpful.

And see the this youtube video.
This video is also very useful.
https://www.youtube.com/watch?v=yEqRyQhhto8


firstly, make dll project in VS tool. like this->



And add new class.

And set opencv include and lib path setting.


Now coding your opencv application class.
see the sample code
In here, please note OPENCVDLL_API keyword.
OPENCVDLL_API is replaced dllexport or dllimport.
and It surely have to declare in front of exporting class name.


OpenCVDLL.h
...

#pragma once
// OpenCVDLL.h

#ifdef OPENCVDLL_EXPORTS
#define OPENCVDLL_API __declspec(dllexport) 
#else
#define OPENCVDLL_API __declspec(dllimport) 
#endif

#include < opencv2\opencv.hpp>

#ifdef _DEBUG            
#pragma comment(lib, "opencv_core249d.lib")    
#pragma comment(lib, "opencv_imgproc249d.lib")   //MAT processing    
#else    
#pragma comment(lib, "opencv_core249.lib")    
#pragma comment(lib, "opencv_imgproc249.lib")   
#endif  

namespace opencvdll_mare
{

 class OPENCVDLL_API OpenCVDLL
 {
 public:
  OpenCVDLL(void);
  ~OpenCVDLL(void);
  cv::Mat convert(cv::Mat& inMat);

 };
}


...

OpenCVDLL.cpp
...
#include "OpenCVDLL.h"

namespace opencvdll_mare
{

 OpenCVDLL::OpenCVDLL(void)
 {
 }


 OpenCVDLL::~OpenCVDLL(void)
 {
 }

 cv::Mat OpenCVDLL::convert(cv::Mat& inMat)
 {
  cv::Mat cMat;
  cv::Sobel(inMat, cMat, inMat.depth(), 1, 0);
  return cMat;
 }


}
...

You will get .dll, .lib and .h file after compile the OpenCVDLL project.

".dll, .lib" is build in Release or Debug folder. ".h" is just OpenCVDLL.h .


Now, let's use this dll in normal project.
Firstly, make console project and copy files of .dll, .lib, .h into new project folder.

We have to do lib setting. OpenCV setting is same with normal method.
Our dll setting is same with OpenCV.
But we don't need to set include folder path, because we copy header file in local project.



And coding your application.
Sample is like this.
loadOpenCVDLL/Source.cpp
...
#include < iostream>
using namespace std;

#include < opencv2\opencv.hpp>
#include "OpenCVDLL.h"

#ifdef _DEBUG            

#pragma comment(lib, "opencv_highgui249d.lib")    
#else
#pragma comment(lib, "opencv_highgui249.lib")    
#endif  


using namespace cv;

void main()
{
 

 Mat test = imread("AA.jpg");
 imshow("test",test);

 opencvdll_mare::OpenCVDLL cOpenCVDLL_test;
 Mat outmat = cOpenCVDLL_test.convert(test);
 imshow("dll test", outmat);

 waitKey(0);
}
...


After all, build and check the result.
My result is here.



5/04/2015

Java.lang.NullPointerException error, matlab install on the MAC

I met the this error message, after installing matlab on MAC.


But don't worry about that, this youtube video will solve.
Watch this.



Thank you.

Basic MOG2 example souce using opencv

Basic MOG2 source code.




...
#include < time.h>  
#include < opencv2\opencv.hpp>  
#include < opencv2\gpu\gpu.hpp>  
#include < string>  
#include < stdio.h>  


#ifdef _DEBUG          
#pragma comment(lib, "opencv_core249d.lib")  
#pragma comment(lib, "opencv_imgproc249d.lib")   //MAT processing  
#pragma comment(lib, "opencv_gpu249d.lib")  
#pragma comment(lib, "opencv_highgui249d.lib")  
#else  
#pragma comment(lib, "opencv_core249.lib")  
#pragma comment(lib, "opencv_imgproc249.lib")  
#pragma comment(lib, "opencv_gpu249.lib")  
#pragma comment(lib, "opencv_highgui249.lib")  
#endif     


#define RWIDTH 800  
#define RHEIGHT 600  

using namespace std;  
using namespace cv;  

int main()  
{  
 /////////////////////////////////////////////////////////////////////////  
 gpu::MOG2_GPU pMOG2_g(30);  
 pMOG2_g.history = 3000; //300;  
 pMOG2_g.varThreshold =64; //128; //64; //32;//;   
 pMOG2_g.bShadowDetection = true;  
 Mat Mog_Mask;  
 gpu::GpuMat Mog_Mask_g;  
 /////////////////////////////////////////////////////////////////////////  


 VideoCapture cap("M:\\____videoSample____\\blog\\video20.wmv");//0);  
 /////////////////////////////////////////////////////////////////////////  
 Mat o_frame;  
 gpu::GpuMat o_frame_gpu;  
 /////////////////////////////////////////////////////////////////////////  

 cap >> o_frame;  
 if( o_frame.empty() )  
  return 0;   
 
 /////////////////////////////////////////////////////////////////////////  


 unsigned long AAtime=0, BBtime=0;  

 //Mat rFrame;  
 Mat showMat_r_blur;  
 Mat showMat_r;  

 namedWindow("origin");
 namedWindow("mog_mask");

 while(1)  
 {  
  /////////////////////////////////////////////////////////////////////////  
  cap >> o_frame;  
  if( o_frame.empty() )  
   return 0;  


  o_frame_gpu.upload(o_frame);
  AAtime = getTickCount();
  
  //  
  pMOG2_g.operator()(o_frame_gpu, Mog_Mask_g,-1);  
  //
  Mog_Mask_g.download(Mog_Mask);

  BBtime = getTickCount();
  float pt = (BBtime - AAtime)/getTickFrequency();
  float fpt = 1/pt;
  printf("gpu %.4lf / %.4lf \n",  pt, fpt );


  
  o_frame_gpu.download(showMat_r);
  imshow("origin", showMat_r); 
  imshow("mog_mask", Mog_Mask);


  /////////////////////////////////////////////////////////////////////////  

  if( waitKey(10) > 0)  
   break;  
 }  

 return 0;  
}  

build opencv 3.0 + cuda 6.5 + QT 5.4 + tbb

These are cmake configuration for OpenCV rc1 + CUDA 6.5 + QT 5.4 + TBB 4.3








I thought it will help opencv building beginner.
I will help how to set path and what I should check.

And configuration result is here.



Now I am building on VS 2012... I hope to build with no error.. ^^

Refer to this page, these are my result of building opencv in waste of time.