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);
  }
 }
}



...