12/29/2016

opencv mouse event example code


...
void CallBackFunc(int event, int x, int y, int flags, void* userdata)
{

    if (event == EVENT_LBUTTONDOWN)
    {
        printf("lLBUTTONDOWN down %d, %d \n", x, y);

        circle((*(Mat*)userdata), Point(x, y), 2, CV_RGB(255, 0, 0), 3);
    }
    else if (event == EVENT_RBUTTONDOWN)
    {
        printf("RBUTTONDOWN down %d, %d \n", x, y);
    }
    else if (event == EVENT_MBUTTONDOWN)
    {
        printf("MBUTTONDOWN down %d, %d \n", x, y);
    }
    else if (event == EVENT_MOUSEMOVE)
    {
        printf("move %d, %d \n", x, y);
    }

    //imshow("img", (*(Mat*)userdata)); //show
}


int main(int, char)
{
    namedWindow("img", 0);
    Mat img = imread("gh.jpg");
    setMouseCallback("img", CallBackFunc, &img);

    while (1)
    {
        imshow("img", img); //show
        if (waitKey(10) > 0)
            break;
    }

    destroyAllWindows();

    return 0;
}
...



findContours, drawContours example code

The code is various example code for drawing contours.


...
namedWindow("show1", 0);
 namedWindow("threshold", 0);
 namedWindow("contours", 0);

 Mat img;
 img = imread("pattern.jpg");

 cvtColor(img, img, CV_RGB2GRAY);
 
 imshow("show1", img);


 threshold(img, img, 128, 255, CV_THRESH_BINARY);
 imshow("threshold", img);


 Mat dst = Mat::zeros(img.rows, img.cols, CV_8UC3);
 
 vector< vector< Point> > contours;
 vector< Vec4i> hierarchy;

 findContours(img.clone(), contours, hierarchy,
  RETR_CCOMP, CHAIN_APPROX_SIMPLE);

 
 //ex 1)
 drawContours(dst, contours, -1, CV_RGB(255,0,0), 1, 8, hierarchy);

 
 // iterate through all the top-level contours,
 // draw each connected component with its own random color
 //ex 2)
 int idx = 0;
 for (; idx >= 0; idx = hierarchy[idx][0])
 {
  Scalar color(rand() & 255, rand() & 255, rand() & 255);
  //drawContours(dst, contours, idx, color, FILLED, 8, hierarchy);
  drawContours(dst, contours, idx, color, 1, 8, hierarchy);
 }
 


 //ex3
 for (int i = 0; i < contours.size(); ++i)
 {
  
  for (int j = 0; j < contours[i].size() - 1; ++j)
  {
   line(dst, contours[i][j], contours[i][j + 1], CV_RGB(255, 0, 0), 1);
  }
  line(dst, contours[i][0], contours[i][contours[i].size()-1], CV_RGB(255, 0, 0), 1);
  
 }

 //ex4
 for (int i = 0; i < contours.size(); ++i)
 {

  Scalar color(rand() & 255, rand() & 255, rand() & 255);
  //drawContours(dst, contours, idx, color, FILLED, 8, hierarchy);
  drawContours(dst, contours, i, color, 1, 8, hierarchy);
 }
 

 //imshow("show3", img);
 imshow("contours", dst);


...



12/28/2016

File exist check code



bool fileExists(const char* path)
{
  FILE* file;
  bool exists;

  file = fileOpen(path, "r");
  exists = file != 0;

  if (file)
    fclose(file);

  return exists;
}

FILE* fileOpen(const char* path, const char* mode)
{
  char path_[PATH_MAX];

  assert(path);

  resolvePath(path_, sizeof(path_), path);

  FILE* fp;
  fopen_s(&fp, path_, mode);
  return fp;
}

explanation of _CRT_SECURE_NO_WARNINGS, VO_MSE_FUNC_THROUGH_POINTERS option in Visual Studio

I was then able to build your project proceeding as follows:
-in the Preprocessor Definitions, you did not add both compiler options _CRT_SECURE_NO_WARNINGS and VO_MSE_FUNC_THROUGH_POINTERS
_CRT_SECURE_NO_WARNINGS is necessary to use the strcpy function
VO_MSE_FUNC_THROUGH_POINTERS is needed to get the pointers to the exported functions of the libraries
-Also, as your code is c++, you need to add extern "C" to "voEngine engine = NULL;" and "#include "
// global engine (for the sake of readability)
extern "C" voEngine engine = NULL;

// common utility functions
extern "C" {
#include
}

12/19/2016

Rotation Matrix Convert : Rotation(3x1) -> Matrix(3x3) -> Quntenion(4x1) -> Matrix(3x3) -> Rotation(3x1)

Rotation convert Test

Rotation(3x1) -> Matrix(3x3) -> Quntenion(4x1) -> Matrix(3x3) ->Rotation(3x1)

///
clc;
clear all;
%Rotation(3x1) -> Matrix(3x3) -> Quntenion(4x1) -> Matrix(3x3) ->
%Rotation(3x1)

% Rotation vector of x,y,z axis.
Rv = [13 20 50];

% 3x3 matrix of R vector (Results of the Rm1 and Rm2 is similar.)
Rm1 = rodrigues(Rv*pi/180)
Rm2 = mRotMat(Rv)

% Quntenion vector of R matrix
Rq1 = matrix2quaternion(Rm1)
Rq2 = matrix2quaternion(Rm2)

% R matrix of Q vector
Rm1_1 = quaternion2matrix(Rq1)
Rm2_2 = quaternion2matrix(Rq1)

% R vector of R matrix
Rv_1 = rodrigues(Rm1_1(1:3,1:3)) * 180/pi
Rv_2 = rodrigues(Rm2_2(1:3,1:3)) * 180/pi


///


Full source code is here
https://github.com/MareArts/Roation-Convert-Rotation-3x1---Matrix-3x3---Quntenion-4x1---Matrix-3x3---Rotation-3x1-

12/18/2016

OpenCV 3.1 32bit build

OpenCV 3.1 32bit build (github master downed on 2016-12-19)

- 32bit version
- no cuda
- with tbb
- built by Visual studio 2013


Sharing link
https://www.amazon.com/clouddrive/share/wmZzLHBKQ5GNllALxAOUiw3zesq5Kj6sQ08DnyFUJDA?ref_=cd_ph_share_link_copy

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


...
#include "opencv2/opencv.hpp"
#include "opencv2/highgui.hpp"
#include "opencv2/cudaarithm.hpp"

#include <iostream>


#ifdef _DEBUG               
#pragma comment(lib, "opencv_core331d.lib")       
#pragma comment(lib, "opencv_highgui331d.lib")    
#pragma comment(lib, "opencv_imgcodecs331d.lib")  
#pragma comment(lib, "opencv_objdetect331d.lib")  
#pragma comment(lib, "opencv_imgproc331d.lib")  
#pragma comment(lib, "opencv_videoio331d.lib")    

#pragma comment(lib, "opencv_cudaarithm331d.lib")  
#else       
#pragma comment(lib, "opencv_core331.lib")       
#pragma comment(lib, "opencv_highgui331.lib")    
#pragma comment(lib, "opencv_imgcodecs331.lib")    
#pragma comment(lib, "opencv_objdetect331.lib")  
#pragma comment(lib, "opencv_imgproc331.lib")  
#pragma comment(lib, "opencv_videoio331.lib")    

#pragma comment(lib, "opencv_cudaarithm331d.lib")  
#endif        


using namespace std;
using namespace cv;

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

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


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

    return 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

12/08/2016

convert char* to LPWSTR

refer to below example


char text[] = "something";
wchar_t wtext[20];
mbstowcs(wtext, text, strlen(text)+1);//Plus null
LPWSTR ptr = wtext;

12/07/2016

convert tchar * to char * / char * to tchar*

tchar* -> char*

"Tch" is tchar*
"pStrTch" is char*


TCHAR Tch[MAX_PATH + 1];
int lenOftch = (wcslen(Tch) + 1) * 2;
char * pStrTch = (char*)malloc(sizeof(char)*MAX_PATH);

//tchar* to char*
WideCharToMultiByte(CP_UTF8, 0, Tch, -1, pStrTch, lenOftch, NULL, NULL);
//


char* -> tchar*


char charBuff[]="test";
TCHAR szUniCode[256]={0,};
MultiByteToWideChar(CP_ACP, MB_PRECOMPOSED, charBuff, strlen(charBuff), szUniCode, 256);

12/01/2016

Create a console window in MFC

Insert code in stdafx.h

#pragma comment(linker, "/entry:WinMainCRTStartup /subsystem:console")

The print result is output.



string to LPCTSTR

Very simple way~


string s;
CString cs(s.c_str()); //< - lpctstr


11/30/2016

Set up VisualStudio to run programs as an administrator



Enter the project properties and change the Configuration Properties -> Linker -> Manifest File -> UAC Execution Level item to requireAdministrator.


11/21/2016

Keycode and ascii code

KEY
KeyCode
ASCII
KEY
KeyCode
ASCII
0
48
48
Numpad 0
96
48
1
49
49
Numpad 1
97
49
2
50
50
Numpad 2
98
50
3
51
51
Numpad 3
99
51
4
52
52
Numpad 4
100
52
5
53
53
Numpad 5
101
53
6
54
54
Numpad 6
102
54
7
55
55
Numpad 7
103
55
8
56
56
Numpad 8
104
56
9
57
57
Numpad 9
105
57
A
65
65
Multiply
106
42
B
66
66
Add
107
43
C
67
67
Enter
13
13
D
68
68
Subtract
109
45
E
69
69
Decimal
110
46
F
70
70
Divide
111
47
G
71
71
F1
112
 
H
72
72
F2
113
 
I
73
73
F3
114
 
J
74
74
F4
115
 
K
75
75
F5
116
 
L
76
76
F6
117
 
M
77
77
F7
118
 
N
78
78
F8
119
 
O
79
79
F9
120
 
P
80
80
F10
121
 
Q
81
81
F11
122
 
R
82
82
F12
123
 
S
83
83
Backspace
8
8
T
84
84
Tab
9
9
U
85
85
Enter
13
13
V
86
86
Shift
16
0
W
87
87
Left Control
17
0
X
88
88
Right Control
25
0
Y
89
89
Left Alt
18
0
Z
90
90
Right Alt
21
0
a
65
97
Caps Lock
20
0
b
66
98
Esc
27
27
c
67
99
Spacebar
32
32
d
68
100
Page Up
33
0
e
69
101
Page Down
34
0
f
70
102
End
35
0
g
71
103
Home
36
0
h
72
104
Left Arrow
37
0
i
73
105
Up Arrow
38
0
j
74
106
Right Arrow
39
0
k
75
107
Down Arrow
40
0
l
76
108
Insert
45
0
m
77
109
Delete
46
127
n
78
110
Num Lock
144
0
o
79
111
ScrLk
145
0
p
80
112
Pause/Break
19
0
q
81
113
; :
186
59
r
82
114
= +
187
61
s
83
115
– _
189
45
t
84
116
/?
191
47
u
85
117
‘~
192
96
v
86
118
[
219
91
w
87
119
|
220
92
x
88
120
]
221
93
y
89
121
” ‘
222
39
z
90
122
< ,
188
44
 
 
 
> .
190
46