11/16/2011

Thread Test in the C++ console mode.

The goal of this source code is how to send large data to the thread and how to wait until all thread finish their job.


I made 3 threads for testing.
I use struct to send the large data.
and I use the flags for waiting the time of the all thread finish.


<source code>


//////////////////////////////////////////////////////////////////////////
// Made by J.H.KIM, 2011 / feelmare@daum.net, feelmare@gmail.com        //
// blog : http://feelmare.blogspot.com                                  //
// My Lab : VISLAB(http://me.pusan.ac.kr)                               //
//////////////////////////////////////////////////////////////////////////

#include <stdio.h>
#include <process.h>
#include <windows.h>

struct InputData
{
    int * data;
    int width;
    int height;
    int OriginW;
    int reserved;
    int flag;
};

void ThreadProcessing(void* pParam);


void main()
{

    //Data Allocation and Setting
    int W = 100;
    int H = 100;
    int * buffer;
    buffer =new int[W*H];
   
    for(int i=0; i<W*H; ++i)
    {
        buffer[i] = i;           
    }
   
    int sx,sy,w,h;

    //thread1, preparing Transite values
    InputData th1_input;   
    sx = 0; sy = 0; w = 80; h = 90; //(80x90) image processing
    th1_input.width = w;
    th1_input.height = h;
    th1_input.OriginW = W;
    th1_input.data = &buffer[sy*W+sx];
    th1_input.reserved = 1;
    th1_input.flag = TRUE;
    _beginthread(ThreadProcessing,0,&th1_input);

    //thread2, preparing Transite values
    InputData th2_input;   
    sx = 10; sy = 10; w = 50; h = 50; //(50x50) image processing
    th2_input.width = w;
    th2_input.height = h;
    th2_input.OriginW = W;
    th2_input.data = &buffer[sy*W+sx];
    th2_input.reserved = 2;
    th2_input.flag = TRUE;
    _beginthread(ThreadProcessing,0,&th2_input);

    //thread3, preparing Transite values
    InputData th3_input;   
    sx = 10; sy = 10; w = 20; h = 20; //(10x10) image processing
    th3_input.width = w;
    th3_input.height = h;
    th3_input.OriginW = W;
    th3_input.data = &buffer[sy*W+sx];
    th3_input.reserved = 3;
    th3_input.flag = TRUE;
    _beginthread(ThreadProcessing,0,&th3_input);


    //wait until all thread is finished.
    while( th1_input.flag || th2_input.flag || th3_input.flag)
    {
        Sleep(100);
    }

    //Data buffer release   
    delete[] buffer;


}


void ThreadProcessing(void* pParam){

    //copy struct data to variable
    int w = ((InputData*)pParam)->width;
    int h = ((InputData*)pParam)->height;
    int ow = ((InputData*)pParam)->OriginW;

    //Processing Sum(v^2.)
    int v;
    double sum=0;
    for(int j=0; j<h; ++j)
    {
        for(int i=0; i<w; ++i)
        {
            v = ((InputData*)pParam)->data[j*ow+i];
            sum += (v*v);
        }       
    }

    printf("%d thread value = %lf\n", ((InputData*)pParam)->reserved , sum);

    ((InputData*)pParam)->flag = FALSE;
}




Please leave comment if the source code has problem.
Thank you~

11/15/2011

Web Cam Capture source code using OpenCV

This source code is very simple. But the code will be help to somebody who start to study the opencv.
Thank you~.


    //image class   
    IplImage* image = 0;
    //camera point
    CvCapture* capture = cvCaptureFromCAM(0);
    //make window
    cvNamedWindow( "camera");

    //image class for processing
    IplImage* image2=0;
    //make window
    cvNamedWindow( "camera2");

    unsigned char R,G,B;
    while(1) {
        //capture a frame form cam
        cvGrabFrame( capture );
        //copy to image class
        image = cvRetrieveFrame( capture );

        //create image at one time in the roof
        if(image2 == 0)
        {
            image2  = cvCreateImage(cvSize(image->width, image->height), image->depth, image->nChannels);
        }

        //simple image processing
        for(int i=0; i<image->height; ++i)
        {
            for(int j=0; j<image->width; ++j)
            {
                //to get R,G,B value
                B = (unsigned char)image->imageData[i*image->widthStep+j*3+0];
                G = (unsigned char)image->imageData[i*image->widthStep+j*3+1];
                R = (unsigned char)image->imageData[i*image->widthStep+j*3+2];

                //image color converting
                B = 255-B;
                G = 255-G;
                R = 255-R;

                //copy R,G,B value to image2 
                image2->imageData[i*image2->widthStep+j*3+0] = B;
                image2->imageData[i*image2->widthStep+j*3+1] = G;
                image2->imageData[i*image2->widthStep+j*3+2] = R;
            }
        }

        //show window
        cvShowImage( "camera", image );
        cvShowImage( "camera2", image2 );

        //break
        if( cvWaitKey(10) >= 0 )
            break;
    }

    //release imge
    cvReleaseImage(&image2);
    //release capture point
    cvReleaseCapture( &capture );
    //close the window
    cvDestroyWindow( "camera" );
    cvDestroyWindow( "camera2" );