5/26/2018

Find contour example source code


Find Contour example source code


...
#include "opencv2/opencv.hpp"
#include "opencv2/highgui.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")    
#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")    
#endif        


using namespace std;
using namespace cv;

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

    cvtColor(img, img, CV_RGB2GRAY);
    img.setTo(0);
    rectangle(img, Rect(10, 10, 200, 200), CV_RGB(255, 255, 255), CV_FILLED);
    imshow("show1", 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);

    // iterate through all the top-level contours,
    // draw each connected component with its own random color
    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);
    }

    imshow("show2", dst);

    waitKey(0);

    return 0;
}
...

5/25/2018

example for applyColorMap usage

example for applyColorMap usage

...
#include "opencv2/opencv.hpp"
#include "opencv2/cuda.hpp"
#include "opencv2\cudaarithm.hpp"
#include <iostream>
#include <time.h>

using namespace std;
using namespace cv;

int main()
{
    
    //image show.
    Mat img = imread("AbyssCGI.jpg");
    
    namedWindow("img", 0);
    imshow("img", img);

    /////////////////applyColorMap
    //basic usage
    Mat im_color;
    applyColorMap(img, im_color, COLORMAP_COOL);//COLORMAP_AUTUMN);
    namedWindow("im_color", 0);
    imshow("im_color", im_color);

    //what about gray?
    Mat img_gray;
    cvtColor(img, img_gray, CV_RGB2GRAY);
    Mat im_color2;
    applyColorMap(img_gray, im_color2, COLORMAP_AUTUMN);
    namedWindow("im_color2", 0);
    imshow("im_color2", im_color2);
    /////////////////////////////////////////

    //////////////////// gray & color test!!
    Mat img2(100, 100, CV_8UC3);
    img2.setTo(Scalar(0, 128, 255));
    Mat im_color3;
    applyColorMap(img2, im_color3, COLORMAP_JET);

    namedWindow("im_color3", 0);
    imshow("im_color3", im_color3);

    //same with resutl of color
    Mat img2_gray;
    cvtColor(img2, img2_gray, CV_RGB2GRAY);
    Mat im_color4;
    applyColorMap(img2, im_color4, COLORMAP_JET);
    namedWindow("im_color4", 0);
    imshow("im_color4", im_color4);
    //////////////////////

    waitKey(0);

    return 0;
}
...


Loop & LUT processing time compare

 Loop & LUT processing time compare


...
#include "opencv2/opencv.hpp"
#include "opencv2/cuda.hpp"
#include "opencv2\cudaarithm.hpp"
#include <iostream>
#include <time.h>

using namespace std;
using namespace cv;

int main(int, char)
{
    unsigned long AAtime = 0, BBtime = 0;
    unsigned char O[1000][1000] = { 1, 2, };

    //very slow;
    AAtime = getTickCount();
    for (int i = 0; i < 1000; ++i)
    {
        for (int j = 0; j < 1000; ++j)
        {
            int t = O[i][j] * 1.14;
            t = (t > 255) ? 255 : t;
            O[i][j] = t;
        }
    }
    BBtime = getTickCount();
    printf("just loop %.5lf sec \n", (BBtime - AAtime) / getTickFrequency()); //chec
    
    //fast
    unsigned char LUT[256];
    AAtime = getTickCount();
    for (int i = 0; i < 256; ++i)
    {
        int t = i * 1.14;
        t = (t > 255) ? 255 : t;
        LUT[i] = t;
    }

    for (int i = 0; i < 1000; ++i)
        for (int j = 0; j < 1000; ++j)
            O[i][j] = LUT[O[i][j]];
    
    BBtime = getTickCount();
    printf("using lut %.5lf sec \n", (BBtime - AAtime) / getTickFrequency()); //chec

    return 0;
}
...


print each character in string and some useful functions

This is example source code for how to print and access each character in string.
And using this function, I made some useful functions.
Thank you.


...
void printAndCountAllCh(string str)
{
    int count = 0;
    for (std::string::iterator it = str.begin(); it != str.end(); ++it) {
        std::cout << count << ": " << *it << endl;
        count++;
    }
}

string changeAtoB(string str, char A, char B)
{
    int count = 0;
    for (std::string::iterator it = str.begin(); it != str.end(); ++it) {
        if (string(1, *it) == string(1, A))
        {
            *it = B;
        }
    }

    return str;
}

string deleteA(string str, char A)
{
    string rstr;
    int count = 0;
    for (std::string::iterator it = str.begin(); it != str.end(); ++it) {
        if (string(1, *it) != string(1, A))
        {
            rstr.push_back(*it);
        }
    }

    return rstr;
}


int countNotCH(string str, char CH)
{
    int count = 0;
    for (std::string::iterator it = str.begin(); it != str.end(); ++it) {
        if (string(1, *it) != string(1, CH))
            count++;
    }
    return count;
}

int main()
{

    string str = "A_B CD_EF GH_I";
    int count;

    printAndCountAllCh(str); //print each char and count
    std::cout << endl;

    string abc = changeAtoB(str, ' ', '_'); //A_B CD_EF GH_I -> A_B_CD_EF_GH_I
    std::cout << abc << endl << endl;

    string bbb = deleteA(str, ' '); //A_B CD_EF GH_I -> A_BCD_EFGH_I
    std::cout << bbb << endl << endl;

    count = countNotCH(str, '_'); //A_B CD_EF GH_I -> 11
    std::cout << count << endl << endl;

    return 0;
}
...




5/22/2018

single color value convert from RGB to YUV and YUV to RGB

Simple color convert code for a pixel value

example for :
RGB to YUV ,YUV to RGB


void rgbToyuv(int r, int g, int b, int& y, int& u, int& v)
{
    Mat img_in(1, 1, CV_8UC3);
    img_in.at<Vec3b>(0, 0)[0] = uint(b);
    img_in.at<Vec3b>(0, 0)[1] = uint(g);
    img_in.at<Vec3b>(0, 0)[2] = uint(r);


    Mat img_out;
    cvtColor(img_in, img_out, CV_BGR2YUV);

    y = uint(img_out.at< cv::Vec3b>(0, 0)[0]);
    u = uint(img_out.at< cv::Vec3b>(0, 0)[1]);
    v = uint(img_out.at< cv::Vec3b>(0, 0)[2]);
}

void yuvTorgb(int y, int u, int v, int& r, int& g, int& b)
{
    Mat img_in(1, 1, CV_8UC3);
    img_in.at<Vec3b>(0, 0)[0] = uint(y);
    img_in.at<Vec3b>(0, 0)[1] = uint(u);
    img_in.at<Vec3b>(0, 0)[2] = uint(v);


    Mat img_out;
    cvtColor(img_in, img_out, CV_YUV2BGR);

    b = uint(img_out.at< cv::Vec3b>(0, 0)[0]);
    g = uint(img_out.at< cv::Vec3b>(0, 0)[1]);
    r = uint(img_out.at< cv::Vec3b>(0, 0)[2]);
}


int main()
{

    int ored = 255;
    int ogreen = 0;
    int oblue = 0;
    int y2, u2, v2;
    printf("origin %d %d %d \n", oblue, ogreen, ored);

    rgbToyuv(ored, ogreen, oblue, y2, u2, v2);
    printf("origin -> yuv: %d %d %d \n", y2, u2, v2);

    int r3, g3, b3;
    yuvTorgb(y2, u2, v2, r3, g3, b3);
    printf("yuv -> rgb: %d %d %d \n", r3, g3, b3);
}