1/31/2017

Including VTK when building opencv

First, we download VTK and build it.
http://www.vtk.org/download/

and
We set the path where VTK is built, when we build opencv.


Note that, we need to include the build path, not the installed path.

http://cvlecture.marearts.com/2016/12/opencv-build-shared-opencv.html
If you go to the above address, I have built VTK 7.1.
And new content will continue to be added.

Good luck!

1/19/2017

OpenCV add, subtract, multiply, divide operation image and scalar, example code

opencv lecture 4-1
example code

< gist start >
http://study.marearts.com/2017/01/opencv-add-subtract-multiply-divide.html
http://cvlecture.marearts.com/2017/01/opencv-4-3.html
#include "opencv2/opencv.hpp"
#include "opencv2/cuda.hpp"
#include "opencv2\cudaarithm.hpp"
#include <iostream>
using namespace std;
using namespace cv;
//TBB need for using this
class Parallel_process : public cv::ParallelLoopBody
{
private:
cv::Mat& inImg;
cv::Mat& outImg;
int widhStep;
int m_stride;
cv::Scalar scalar; //B,G,R
public:
Parallel_process(cv::Mat& inputImgage, Mat& outImage, Scalar& inScalar)
: inImg(inputImgage), outImg(outImage), scalar(inScalar){
widhStep = inputImgage.size().width * 3;
m_stride = inputImgage.size().width * 2;
}
virtual void operator()(const cv::Range& range) const
{
//thread
for (int i = range.start; i < range.end; i++)
{
int s1 = i*widhStep;
for (int iw = 0; iw< inImg.size().width; iw++)
{
int s2 = iw * 3;
int mc = s1 + s2;
unsigned char B1 = (unsigned char)(inImg.data[mc + 0]);
unsigned char G1 = (unsigned char)(inImg.data[mc + 1]);
unsigned char R1 = (unsigned char)(inImg.data[mc + 2]);
int B2 = B1 + scalar[0];
int G2 = G1 + scalar[1];
int R2 = R1 + scalar[2];
if (B2 > 255)
B2 = 255;
if (G2 > 255)
G2 = 255;
if (R2 > 255)
R2 = 255;
if (B2 < 0)
B2 = 0;
if (G2 < 0)
G2 = 0;
if (R2 < 0)
R2 = 0;
outImg.data[mc + 0] = B2;
outImg.data[mc + 1] = G2;
outImg.data[mc + 2] = R2;
}
}
}
};
int main(int, char)
{
Mat img = imread("scret.jpg");
Mat img_add;
Mat img_absdiff;
Mat img_subtract;
Mat img_mul;
Mat img_div;
Mat img_parallel;
unsigned long AAtime = 0, BBtime = 0; //check processing time
//cpu add
AAtime = getTickCount();
add(img, Scalar(200, 200, 200), img_add); //Value is between 0 and 255
//img_add = img + Scalar(200, 200, 200);
BBtime = getTickCount();
printf("add cpu %.2lf sec \n", (BBtime - AAtime) / getTickFrequency()); //check processing time
//cpu subtraction
AAtime = getTickCount();
subtract(img, Scalar(200, 200, 200), img_subtract); //Value is between 0 and 255
//img_subtract = img - Scalar(200, 200, 200)
BBtime = getTickCount();
printf("subtract %.2lf sec \n", (BBtime - AAtime) / getTickFrequency()); //check processing time
//cpu absdiff
AAtime = getTickCount();
absdiff(img, Scalar(200, 200, 200), img_absdiff); //Value is between 0 and 255
BBtime = getTickCount();
printf("absdiff %.2lf sec \n", (BBtime - AAtime) / getTickFrequency()); //check processing time
//cpu *, /
multiply(img, 20, img_mul);
divide(img, 20, img_div);
//img_mul = img * 20;
//img_div = img / 20;
cuda::GpuMat gimg;
cuda::GpuMat gout1, gout2;
//gpu add
gimg.upload(img);
cuda::absdiff(gimg, Scalar(10, 2, 100), gout1);
cuda::add(gimg, Scalar(-200, -200, -200), gout2);
//tbb
AAtime = getTickCount();
img_parallel = Mat(img.size(), img.type());
cv::parallel_for_(cv::Range(0, img.rows), Parallel_process(img, img_parallel, Scalar(-200, -200, -200)));
BBtime = getTickCount();
printf("parallel %.2lf sec \n", (BBtime - AAtime) / getTickFrequency()); //check processing time
imshow("img", img);
imshow("img_add", img_add);
imshow("img_absdiff", img_absdiff);
imshow("img_subtract", img_subtract);
imshow("img_mul", img_mul);
imshow("img_div", img_div);
imshow("img_parallel", img_parallel);
waitKey(0);
return 0;
}

< gist end >




1/11/2017

MFC, CIPAddressCtrl ctrol, ip address set, get, using BYTE and CString


usage for CIPAddressCtrl  in mfc

< gist code >
http://study.marearts.com/2017/01/mfc-cipaddressctrl-ctrol-ip-address-set.html
.....................................................................
//set, get code
//get address to Cstring
CString ipaddress;
BYTE ip[4];
m_ServeripAddress.GetAddress(ip[0], ip[1], ip[2], ip[3]);
ipaddress.Format(_T("%d.%d.%d.%d"), ip[0], ip[1], ip[2], ip[3]);
//set and get address to byte
BYTE gip[4];
getipABCD(ipaddress, gip[0], gip[1], gip[2], gip[3]);
m_ServeripAddress.SetAddress(gip[0], gip[1], gip[2], gip[3]);
.....................................................................
void getipABCD(CString in, BYTE &a, BYTE &b, BYTE &c, BYTE &d)
{
char* szIpAddress = WtoC(in);
BYTE m_IpAddr[4] = { 0, };
char *p;
int i = 0;
p = strtok(szIpAddress, ".");
m_IpAddr[i] = atoi(p);
while (p != NULL)
{
i++;
p = strtok(NULL, ".");
if (p)
m_IpAddr[i] = atoi(p);
}
a = m_IpAddr[0];
b = m_IpAddr[1];
c = m_IpAddr[2];
d = m_IpAddr[3];
}

< end >

1/05/2017

OpenCV Trackbar Exmaple source code

< git hub - gits >
http://study.marearts.com/2017/01/opencv-trackbar-exmaple-source-code.html
#include "opencv2/opencv.hpp"
#include < iostream>
using namespace cv;
using namespace std;
int g_slider; //slider pos value
int g_slider_max; //slider max value
void on_trackbar(int, void*)
{
if (g_slider % 2 == 0)
g_slider = g_slider + 1;
printf("%d\n", g_slider);
}
int main()
{
// Read image from file
Mat img = imread("anapji.jpg");
Mat img2;
//set
g_slider = 0;
g_slider_max = 21;
//window name
namedWindow("My Window", 1);
//make trackbar call back
createTrackbar("TrackbarName", "My Window", &g_slider, g_slider_max, on_trackbar);
//show the image
while (1)
{
Sobel(img, img2, CV_8U, 1, 0, g_slider);
imshow("My Window", img2);
// Wait until user press some key
int r = waitKey(10);
if (r > 0)
break;
}
return 0;
}

///


another examples..

simple trackbar ex) in image
http://study.marearts.com/2016/07/opencv-30-trackbar-usage-simple-example.html

QString to string, string to QString

simple example code
< github >
http://study.marearts.com/2017/01/qstring-to-string-string-to-qstring.html
//string to QString
QString str = QString::fromUtf8(content.c_str());
//Qstring to string
string str = QString.toUtf8().constData();

///


1/03/2017

In MFC, File exist check and delete the file, example source code.



If file exist then delete the file, example source code in MFC

< github code >
http://study.marearts.com/2017/01/in-mfc-file-exist-check-and-delete-file.html
#include "afx.h"
CFileStatus fs;
if (CFile::GetStatus(_T("AutoLogIn.CPR"), fs))
{
::DeleteFile(_T("AutoLogIn.CPR") );
}
___

MFC Encode / Decode example souce code



WtoC function code is here
http://study.marearts.com/2017/01/unicode-cstring-convert-to-char.html

And refer to GetMacAddress code
http://study.marearts.com/2017/01/get-mac-address-in-mfc.html

Encode code
//
http://study.marearts.com/2017/01/mfc-encode-decode-example-souce-code.html
bool CEPenService_PenCommunicatorDlg::Encode(CString id, CString pass, CString mac)
{
//char *bBuff;
CString csFile;
FILE *pFile;
char bBuff[100];
char * cid = WtoC(id);
char * cpass = WtoC(pass);
char * cmac = WtoC(mac);
sprintf_s(bBuff, "%s+%s+%s+", cid, cpass, cmac);
DWORD dwFileLen = sizeof(bBuff);
delete cid;
delete cpass;
delete cmac;
HCRYPTPROV hProv;
HCRYPTHASH hHash;
HCRYPTKEY hKey;
char csPass[100] = "rlawjdgus";
// CSP(Crystographic Service Provider) 핸들 얻기
if (!CryptAcquireContext(&hProv, NULL, MS_ENHANCED_PROV, PROV_RSA_FULL, 0))
{
if (!CryptAcquireContext(&hProv, NULL, MS_ENHANCED_PROV, PROV_RSA_FULL, CRYPT_NEWKEYSET))
{
//printf("encode fail");
return false;
}
}
// 해쉬 만들기
CryptCreateHash(hProv, CALG_SHA, 0, 0, &hHash);
// 해쉬 값 계산
CryptHashData(hHash, (BYTE*)csPass, sizeof(csPass), 0);
// 키 만들기\tab
CryptDeriveKey(hProv, CALG_RC4, hHash, 0x0080 * 0x10000, &hKey);
// 암호화\tab
CryptEncrypt(hKey, 0, TRUE, 0, (BYTE*)bBuff, &dwFileLen, dwFileLen);
// 해쉬 없애기
CryptDestroyHash(hHash);
// CSP 핸들 풀어주기
CryptReleaseContext(hProv, 0);
FILE* fp;
// 암호화된 파일 저장하기
fopen_s(&fp, "AutoLogIn.CPR", "wb");
fwrite(bBuff, 1, dwFileLen, fp);
fclose(fp);
return true;
}
view raw MFC_Encode.cpp hosted with ❤ by GitHub

..

Decode code
//
http://study.marearts.com/2017/01/mfc-encode-decode-example-souce-code.html
bool Decode()
{
BYTE *bBuff;
DWORD dwFileLen;
CString csFile;
FILE *pFile;
// 암호화된 파일 저장하기
fopen_s(&pFile, "AutoLogIn.CPR", "rb");
//pFile = fopen(csFile, "rb");
if (!pFile)
{
::AfxMessageBox(_T("error"));
return false;
}
dwFileLen = _filelength(_fileno(pFile));
bBuff = new BYTE[dwFileLen];
// 파일 읽어서 버퍼에 저장
fread(bBuff, 1, dwFileLen, pFile);
fclose(pFile);
HCRYPTPROV hProv;
HCRYPTHASH hHash;
HCRYPTKEY hKey;
//CString csPass=_T("rlawjdgus");
char csPass[100] = "rlawjdgus"; //encription key
//m_Pass.GetWindowText(csPass);
// CSP(Crystographic Service Provider) 핸들 얻기
if (!CryptAcquireContext(&hProv, NULL, MS_ENHANCED_PROV, PROV_RSA_FULL, 0))
{
if (!CryptAcquireContext(&hProv, NULL, MS_ENHANCED_PROV, PROV_RSA_FULL, CRYPT_NEWKEYSET))
{
::AfxMessageBox(_T("ecode fail"));
return false;
}
}
// 해쉬 만들기
CryptCreateHash(hProv, CALG_SHA, 0, 0, &hHash);
// 해쉬 값 계산
CryptHashData(hHash, (BYTE*)csPass, sizeof(csPass), 0);
// 키 만들기\tab
CryptDeriveKey(hProv, CALG_RC4, hHash, 0x0080 * 0x10000, &hKey);
// 복호화
CryptDecrypt(hKey, 0, TRUE, 0, bBuff, &dwFileLen);
// 해쉬 없애기
CryptDestroyHash(hHash);
// CSP 핸들 풀어주기
CryptReleaseContext(hProv, 0);
CString str;
str.Format(_T("%s"), CString(bBuff));
::AfxMessageBox(str);
return true;
}
view raw MFC_Decode.cpp hosted with ❤ by GitHub

..

Unicode CString convert to char *

The returned char * will need to be freed after use.


< gist code start >
http://study.marearts.com/2017/01/unicode-cstring-convert-to-char.html
char * WtoC(CString str)
{
//CString str; //형변환할 문자열이 저장된 CString 변수
wchar_t* wchar_str; //첫번째 단계(CString to wchar_t*)를 위한 변수
char* char_str; //char* 형의 변수
int char_str_len; //char* 형 변수의 길이를 위한 변수
//1. CString to wchar_t* conversion
wchar_str = str.GetBuffer(str.GetLength());
//2. wchar_t* to char* conversion
//char* 형에 대한길이를 구함
char_str_len = WideCharToMultiByte(CP_ACP, 0, wchar_str, -1, NULL, 0, NULL, NULL);
char_str = new char[char_str_len]; //메모리 할당
//wchar_t* to char* conversion
WideCharToMultiByte(CP_ACP, 0, wchar_str, -1, char_str, char_str_len, 0, 0);
//Done.
return char_str;
}

< gist code end >


#tags
wchar_t, WtoC, WideCharToMultiByte, WideCharToMultiByte

useful site for CString converting


http://www.flounder.com/cstring.htm#Converting%20a%20CString%20to%20an%20integer


Get Mac Address in MFC


GetMacAddress in MFC

Get by using GetAdaptersInfo function
//
http://study.marearts.com/2017/01/get-mac-address-in-mfc.html
CString GetMACAddress()
{
CString strGateWay = _T("");
CString strMACAddress = _T("");
IP_ADAPTER_INFO ipAdapterInfo[5];
DWORD dwBuflen = sizeof(ipAdapterInfo);
DWORD dwStatus = GetAdaptersInfo(ipAdapterInfo, &dwBuflen);
if (dwStatus != ERROR_SUCCESS)
{
strMACAddress.Format(_T("Error for GetAdaptersInfo : %d"), dwStatus);
AfxMessageBox(strMACAddress);
return _T("");
}
PIP_ADAPTER_INFO pIpAdapterInfo = ipAdapterInfo;
do{
strGateWay = (CString)pIpAdapterInfo->GatewayList.IpAddress.String;
if (strGateWay[0] == '0')
{
pIpAdapterInfo = pIpAdapterInfo->Next;
}
else
{
strMACAddress.Format(_T("%02X-%02X-%02X-%02X-%02X-%02X"),
pIpAdapterInfo->Address[0],
pIpAdapterInfo->Address[1],
pIpAdapterInfo->Address[2],
pIpAdapterInfo->Address[3],
pIpAdapterInfo->Address[4],
pIpAdapterInfo->Address[5]
);
break;
}
} while (pIpAdapterInfo);
return strMACAddress;
}

..
Get by using ip address
//If localhost -> GetMacAddress(_T("*"));
//if have ip -> GetMacAddress(_T("192.168.1.1"));//
http://study.marearts.com/2017/01/get-mac-address-in-mfc.html
CString GetMacAddressbyIP(CString strIP)
{
NCB Ncb;
UCHAR uRetCode;
LANA_ENUM lenum;
int i;
CString strOutput = _T("");
CString string;
ADAPTER_STATUS Adapter;
memset(&Ncb, 0, sizeof(Ncb));
Ncb.ncb_command = NCBENUM;
Ncb.ncb_buffer = (UCHAR *)&lenum;
Ncb.ncb_length = sizeof(lenum);
uRetCode = Netbios(&Ncb);
for (i = 0; i < lenum.length; i++)
{
memset(&Ncb, 0, sizeof(Ncb));
Ncb.ncb_command = NCBRESET;
Ncb.ncb_lana_num = lenum.lana[i];
uRetCode = Netbios(&Ncb);
memset(&Ncb, 0, sizeof(Ncb));
Ncb.ncb_command = NCBASTAT;
Ncb.ncb_lana_num = lenum.lana[i];
strcpy_s((char*)Ncb.ncb_callname, sizeof(Ncb.ncb_callname), (LPSTR)(LPCTSTR)strIP.GetBuffer(0));
Ncb.ncb_buffer = (unsigned char *)&Adapter;
Ncb.ncb_length = sizeof(Adapter);
uRetCode = Netbios(&Ncb);
if (uRetCode == 0)
{
string.Format(_T("%02X:%02X:%02X:%02X:%02X:%02X"),
Adapter.adapter_address[0],
Adapter.adapter_address[1],
Adapter.adapter_address[2],
Adapter.adapter_address[3],
Adapter.adapter_address[4],
Adapter.adapter_address[5]);
strOutput += string;
}
}
return strOutput;
}

..


1/02/2017

CString to string


..
string CstringToString(CString str)
{
    CT2CA pszConvertedAnsiString(str);
    std::string s(pszConvertedAnsiString);

    return s;
}
..