การทำ edge detection

Posted by Undermine on 05:26 comments (1)

ตัวอย่างนี้แสดงถึงวิธีการใช้คำสั่งของ OpenCV เพื่อทำ edge detection โดยใช้วิธีของ Canny

#include 
using namespace std;

#include 
#include 
#include 

#define CAMERA_ID 0
#define ORG_IMAGE_DISPLAY  "original image"
#define PROCESSED_IMAGE_DISPLAY "processed image"

#define STRONG_EDGE_TH 100
#define WEAK_EDGE_TH 10

int main( int argc, char **argv )
{
    // create camera capture object.
    CvCapture *pCapture = NULL;
    pCapture = cvCreateCameraCapture( CAMERA_ID );
    if ( pCapture == NULL )
        {
            cout << "ERROR: Failed to open camera" << endl;
            return EXIT_FAILURE;
        }

    // create windows for displaying the captured image
    // and the captured image.
    cvNamedWindow( ORG_IMAGE_DISPLAY );
    cvNamedWindow( PROCESSED_IMAGE_DISPLAY );

    // capture, process, and display
    // until user hit any key of the keyboard
    IplImage *pOrgImage = NULL;
    IplImage *pProcessedImage = NULL;
    do
    {
        // release old image
        // before creating a new one
        if ( pProcessedImage != NULL )
            {
             cvReleaseImage( &pProcessedImage );
             pProcessedImage = NULL;
            }

        // capture new image
        pOrgImage = cvQueryFrame( pCapture );

        // create image which has the same width, height, depth, and number of channels
        // as the original image for put the result of processing.
        pProcessedImage = cvCreateImage( cvSize( pOrgImage->width, pOrgImage->height ),
                    pOrgImage->depth, 1 );

        // do canny edge dectection
        cvCvtColor( pOrgImage, pProcessedImage, CV_BGR2GRAY );
        cvCanny( pProcessedImage, pProcessedImage, STRONG_EDGE_TH, WEAK_EDGE_TH );

        // display images
        cvShowImage( ORG_IMAGE_DISPLAY, pOrgImage );
        cvShowImage( PROCESSED_IMAGE_DISPLAY, pProcessedImage );
    } while ( cvWaitKey( 1 ) == -1 );

    // release old image
    if ( pProcessedImage != NULL )
        {
            cvReleaseImage( &pProcessedImage );
            pProcessedImage = NULL;
        }

    // before exiting the program,
    // destroy the display windows and
    // release the cameara capture object.
    cvDestroyWindow( ORG_IMAGE_DISPLAY );
    cvDestroyWindow( PROCESSED_IMAGE_DISPLAY );
    cvReleaseCapture( &pCapture );
    return 0;
}

การตัดสี

Posted by Undermine on 05:24 comments (0)

ตัวอย่างนี้แสดงวิธีการเบื้องต้น เพื่อแสดงวิธีการเข้าถึงข้อมูลแต่ละ pixel ของภาพ เพื่อทำการตัดสีตามค่าช่วงสีที่กำหนดไว้

#include
using namespace std;
#include
#include
#include
#define CAMERA_ID 0
#define ORG_IMAGE_DISPLAY "original image"
#define PROCESSED_IMAGE_DISPLAY "processed image"
// interested color value
#define RED_MIN 0
#define RED_MAX 255
#define GREEN_MIN 0
#define GREEN_MAX 100
#define BLUE_MIN 0
#define BLUE_MAX 100
// channel indices
#define BLUE_CH 0
#define GREEN_CH 1
#define RED_CH 2
int main( int argc, char **argv )
{
// create camera capture object.
CvCapture *pCapture = NULL;
pCapture = cvCreateCameraCapture( CAMERA_ID );
if ( pCapture == NULL )
{
cout << "ERROR: Failed to open camera" << endl;
return EXIT_FAILURE;
}
// create windows for displaying the captured image
// and the captured image.
cvNamedWindow( ORG_IMAGE_DISPLAY );
cvNamedWindow( PROCESSED_IMAGE_DISPLAY );
// capture, process, and display
// until user hit any key of the keyboard
IplImage *pOrgImage = NULL;
IplImage *pProcessedImage = NULL;
do
{
// release old image
// before creating a new one
if ( pProcessedImage != NULL )
{
cvReleaseImage( &pProcessedImage );
pProcessedImage = NULL;
}
// capture new image
pOrgImage = cvQueryFrame( pCapture );
// create image which has the same width, height, depth, and number of channels
// as the original image for put the result of processing.
// Then fill the created image with 0 value ( black image ).
pProcessedImage = cvCreateImage( cvSize( pOrgImage->width, pOrgImage->height ),
pOrgImage->depth, pOrgImage->nChannels );
cvSetZero( pProcessedImage );
// threshold the image by accessing all pixels of the image
for ( int y = 0; y < pOrgImage->height; y++ )
{
for ( int x = 0; x < pOrgImage->width; x++ )
{
int offset_x = x*pOrgImage->nChannels;
int offset_y = y*pOrgImage->widthStep;
unsigned char *pPixel = (unsigned char*)pOrgImage->imageData + (offset_x + offset_y);
unsigned char *pDstPixel = (unsigned char*)pProcessedImage->imageData + (offset_x + offset_y);
if ( ( pPixel[BLUE_CH] >= BLUE_MIN && pPixel[BLUE_CH] <= BLUE_MAX )
&& ( pPixel[GREEN_CH] >= GREEN_MIN && pPixel[GREEN_CH] <= GREEN_MAX )
&& ( pPixel[RED_CH] >= RED_MIN && pPixel[RED_CH] <= RED_MAX ) )
{
pDstPixel[RED_CH] = pDstPixel[GREEN_CH] = pDstPixel[BLUE_CH] = 255;
}
}
}
// display images
cvShowImage( ORG_IMAGE_DISPLAY, pOrgImage );
cvShowImage( PROCESSED_IMAGE_DISPLAY, pProcessedImage );
} while ( cvWaitKey( 1 ) == -1 );
// release old image
if ( pProcessedImage != NULL )
{
cvReleaseImage( &pProcessedImage );
pProcessedImage = NULL;
}
// before exiting the program,
// destroy the display windows and
// release the cameara capture object.
cvDestroyWindow( ORG_IMAGE_DISPLAY );
cvDestroyWindow( PROCESSED_IMAGE_DISPLAY );
cvReleaseCapture( &pCapture );
return 0;
}

ตัวอย่างการติดต่อกับกล้อง

Posted by Undermine on 05:22 comments (0)

การติดต่อกับกล้องโดย OpenCV นั้นจะใช้ชุดคำสั่งจาก highgui ซึ่งมีตัวอย่างดังนี้



#include
using namespace std;
#include
#include
#define CAMERA_ID 0
#define DISPLAY_WINDOW_NAME "captured image"
int main( int argc, char **argv )
{
// create camera capture object.
CvCapture *pCapture = NULL;
pCapture = cvCreateCameraCapture( CAMERA_ID );
if ( pCapture == NULL )
{
cout << "ERROR: Failed to open camera" << endl;
return EXIT_FAILURE;
}
// create window for displaying captured image.
cvNamedWindow( DISPLAY_WINDOW_NAME );
// capture and display
// until user hit any key of the keyboard
IplImage *pImage = NULL;
do
{
pImage = cvQueryFrame( pCapture );
cvShowImage( DISPLAY_WINDOW_NAME, pImage );
} while ( cvWaitKey( 1 ) == -1 );
// before exiting the program,
// destroy the display window and
// release the cameara capture object.
cvDestroyWindow( DISPLAY_WINDOW_NAME );
cvReleaseCapture( &pCapture );
return 0;
}

ข้อดีของ opencv และ จะเขียนได้อย่างไร

Posted by Undermine on 18:55 comments (0)

ข้อดีของ Opencv

1. เป็น library ที่นำไปใช้ได้เลยครับ algorithm พวกพื้นฐานไม่ต้องมาคิดหรือโค้ดเอง ฟรีด้วยครับ
2. มี source code และ ตัวอย่างให้มาด้วย สามารถนำโค้ดที่มี ใน opencv ไปประยุกต์ใชได้
3. มี yahoo group เป็นที่รวมตัวของคนที่ใช้จากทั่วโลก
4. opencv ถูกพัฒนาจากทีม พัฒนาของ intel และ optimize code สำหรับ cpu 586 ของ intel
คิดว่า น่าจะ รุ่นที่มีพวก คำสั่งพิเศษขึ้นมานะครับ เช่น พวก mmx sse ทำให้เป็น library ที่มีความเร็วในการประมวล
ผลอยู่ในระดับต้นๆ เร็วกว่า ที่มีขายบางตัวด้วยครับ

คำถามยอด ฮิต จะใช้ opencv ดีไหม

Posted by Undermine on 18:52 comments (0)

Q จะเริ่มเล่น Image Processing ใช้ opencv ดีไหมครับ


A คือคำว่า “เล่น image Processing” นี่หมายความกว้างแค่ไหนเหรอครับ

ถ้าจะแค่ลองเปลี่ยนภาพสีเป็น gray scale หรือ จับก้อนสีที่เราสนใจ (connected component)อะไรแบบนี้ ก็ไม่จำเป็นต้องยึดติดกับ openCV หรอกครับ ภาษาทั่วๆไป อย่าง Java เองก็มี library ที่ช่วยในการจัดการกับไฟล์รูปภาพอยู่แล้ว เช่น แปลงไฟล์รูปภาพให้เป็น array ของค่า RGB ของแต่ละ pixel ซึ่งก็น่าจะสะดวกกว่า openCV นะครับ เพราะรู้สึกว่า openCV มันจะต้องใช้กับ C++ มั้ง ถ้าจำไม่ผิด ซึ่ง C++ นี่ก็เป็นภาษาที่ค่อนข้างน่าเหนื่อยใจพอสมควร
แต่ถ้าจะทำอะไรที่มันพิสดารกว่าเปลี่ยนรูปสีเป็น gray scale เช่น การแก้ไขความเบี้ยวของภาพที่เกิดจาก lens distortion อะไรงี้ ก็คงต้องใช้ openCV ล่ะครับ เพราะ openCV มี method สำเร็จรูปไว้จัดการกับเรื่องแบบนี้อยู่แล้ว ส่วน library ของ ภาษาอื่น ผมไม่แน่ใจว่ามันจะมี method เจ๋งๆเหมือน openCV รึเปล่า
สรุปก็คือ ถ้าจะทำอะไรที่ไม่พิสดารมาก ก็ไม่จำเป็นต้องใช้ openCV เพราะมันต้องเขียน C++ ซึ่งเปลืองพลังงานอย่างแรง ภาษาอื่นก็มี library ให้ใช้ แต่ถ้าจะทำอะไรหรูหรา ก็คงต้อง openCV

Creating an OpenCV application with Eclipse on Windows

Posted by Undermine on 18:45 comments (0)


เนื่องจากว่าผมใช้ระบบ Windows เป็นหลัก แต่งานวิจัยผมพัฒนาบน Ubuntu ซึ่งยังยอมรับว่าไม่สามารถใช้ Ubuntu เพียวๆ ได้ เป็นเหตุทำให้งานวิจัยค่อนข้างล่าช้าไปพอสมควร ผมจึงคิดว่าจะมาเขียนโปรแกรมบน Windows แทน และวันนี้ได้ลองผิดลองถูกที่จะใช้ OpenCV กับ Eclipse อยู่นานมาก ขอจดขั้นตอนเก็บไว้หน่อยละกัน
โปรแกรมที่จำเป็นต้องติดตั้ง
  1. MinGW: เป็นชุดคำสั่งรวมพวกไลบรารี่ของภาษา Programming ซึ่งในที่นี้โปรแกรมที่จะติดตั้งก็คือ C/C++ compiler
  2. Eclipse CDT: ดั้งเดิมเป็น IDE สำหรับภาษา Java แต่ตัวนี้ได้ติดตั้งโปรแกรมเสริมให้รองรับการพัฒนาภาษา C/C++
  3. OpenCV for Windows: และแน่นอนที่ขาดไม่ได้ นั่นก็คือ OpenCV ซึ่งเป็นชุดคำสั่งรวมพวกไลบรารี่ของการพัฒนาโปรแกรมทาง Computer Vision หรือ Image Processing

การจัดการปัญหา Memory Leak ใน OpenCV (C/C++)

Posted by Undermine on 18:42 comments (2)


ความรู้ใหม่ได้มาจากเว็บของ Andol ผมนำบางส่วนมาแปลเป็นไทยมาลงไว้กันลืม และเผื่อจะเป็นประโยชน์ต่อหลายๆ คนครับ โดยงานผมจะเกี่ยวข้องการกับวิเคราะห์รูปภาพดังนั้นส่วนที่เอามาคือ เฉพาะส่วน Memory leak ของการประกาศตัวแปร IplImage
ถ้าโค้ดของเรามีดังนี้