6/21/2018

String split by delimiter (c++/c)

refer to souce code

...
void CForeAndBack::getStrings(string csvFname, vector<string> eachStr, string delimiter)
{
    //parsing
    size_t pos = 0;
    while ((pos = csvFname.find(delimiter)) != std::string::npos) {
        string token = csvFname.substr(0, pos);
        std::cout << token << std::endl;
        eachStr.push_back(token);
        csvFname.erase(0, pos + delimiter.length());
    }
    std::cout << csvFname << std::endl;
    eachStr.push_back(csvFname);
}
...


6/20/2018

make folder in the c code

simple example code

..
#include <direct.h>

..
string outputFolder = "output";
_mkdir(outputFolder.c_str());
..
..

6/18/2018

TCHAR to string

simple sample code

...
TCHAR buff[1024];
///
///..something to do
///
wstring test(&buff[0]); //convert to wstring
string strFolder(test.begin(), test.end()); //and convert to string.
...

5/30/2018

How to get current directory in window application.

Example source code to get current directory in window application


..
CString CurrentDirectoryToCString()
{
    TCHAR buff[MAX_PATH];
    memset(buff, 0, MAX_PATH);
    ::GetCurrentDirectory(MAX_PATH, buff);
    CString strFolder = buff;

    return strFolder;
}
..

OpenCV, Dash line drawing example source code.

I can know all coordinate from A point to B point using "LineIterator" function in OpenCV.
So this source code is applied by this "LineIterator".


..
Mat DrawDashLine(Mat inMat, Point start, Point end, int gap, Scalar color)
{
    Mat rMat;
    rMat = inMat.clone();
    
    cv::LineIterator it(rMat, start, end, 8);
    vector< pair<cv::Point, cv::Point> > vecPt_pair;
    vector< cv::Point > vecPt;

    Point A, B;
    A = start;
    for (int i = 0, j = 0; i < it.count; i++, it++)
    {
        
        if (i % gap == 0)
        {
            //update end point
            B = it.pos();

            if(j%2)
                line(rMat, A, B, color, 2);

            //update start point
            A = B;
            j++;
        }
    }

    return rMat;
}

int main()
{
    Mat img(500, 500, CV_8UC3);
    img.setTo(0);

    Mat rImg = DrawDashLine(img, Point(20, 20), Point(300, 300), 10, CV_RGB(255, 0, 0));

    namedWindow("test", 0);
    cv::imshow("test", rImg);
    waitKey(0);
    
    return 0;
}

..







5/27/2018

OpenCV Integral Test source code


...
int main(int, char)
{
    
    Mat img = Mat(10, 10, CV_8UC1);
    randu(img, 0, 10);

    cout << "origin" << endl << img << endl;

    //int x=3, y=4, w=4, h=4;
    int x = 1, y = 1, w = 2, h = 2;

    int sum = 0;
    for (int i = 0; i < w; ++i)
    {
        for (int j = 0; j < h; ++j)
        {
            sum = sum + img.at<unsigned char>((y + j), (x + i));
        }
    }

    printf("for loop : sum = %d \n", sum);


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

    Mat integralImg;
    integral(img, integralImg, CV_64F);

    cout << endl << "integral" << endl << integralImg << endl;

    double p1 = integralImg.at<double>((y), (x));
    double p2 = integralImg.at<double>((y), (x + w));
    double p3 = integralImg.at<double>((y + h), (x));
    double p4 = integralImg.at<double>((y + h), (x + w));

    printf("\n p1:%lf, p2:%lf, p3:%lf, p4:%lf\n", p1, p2, p3, p4);
    printf("integral : sum = %lf \n", (p1 + p4) - (p2 + p3));

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

}



...


result

AdaptiveThreshold test code in opencv


...
#include "opencv2/opencv.hpp"
#include "opencv2/highgui.hpp"
#include "opencv2/cudaarithm.hpp"
#include <iostream>
#include <vector>

#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_cudaarithm331.lib")
#endif        


using namespace std;
using namespace cv;



int main(int, char)
{

    namedWindow("img", 0);
    namedWindow("threshold", 0);
    namedWindow("mean_thres", 0);
    namedWindow("gauss_thres", 0);


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

    Mat gray, binary, binary1, binary2;
    cvtColor(img, gray, CV_RGB2GRAY);

    threshold(gray, binary, 128, 255, CV_THRESH_BINARY);
    adaptiveThreshold(gray, binary1, 255, ADAPTIVE_THRESH_MEAN_C, THRESH_BINARY, 5, 5);
    adaptiveThreshold(gray, binary2, 255, ADAPTIVE_THRESH_GAUSSIAN_C, THRESH_BINARY, 5, 5);


    imshow("img", img);

    imshow("threshold", binary);
    imshow("mean_thres", binary1);
    imshow("gauss_thres", binary2);


    waitKey(0);

    return 0;
}


...

result