(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
The study blog from marearts.com
Computer Vision & Machine Learning Research Laboratory
#ifdef DEBUG wchar_t str[100]; swprintf(str,L"if %d %lf\n", valueInt, valueDouble); OutputDebugString( str ); #endif
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
...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 );
...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() ));
..#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);
}
}
}
...