การทำ 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
ถ้าโค้ดของเรามีดังนี้

OpenCV on Linux again with G++

Posted by Undermine on 17:36 comments (0)


After a long way try to develop C program with OpenCV library on Ubuntu Linux using NetBeans and Eclispe IDE, no succeed at all. There are always some problems occurs and the project cannot move on further so everything has to be move back onto Windows to be developed. But now all these thing can be developed in Linux by  using traditional GNU C Compiler (GCC) that most of Linux users know it very well. This is the complete procedure from the beginning(install) till the end(compile-build).
Part A. Install OpenCV 2.0.0
1. Install prerequisites
Enter this line into Linux Terminal:

Installing OpenCV from the cvs version for Ubuntu 5

Posted by Undermine on 17:33 comments (0)


First, there are a few dependencies to sort out. You will need to install the following packages - using Synaptic.

  • cvs - so you can get the files
    • If you don't use cvs normally, type touch ~/.cvpass to create an empty password file (or else you'll get error messages)
  • gcc - so you can compile them
  • g++ - "
  • libgtk2.0-dev
  • Various image libraries - libimlib2 and libimlib2-dev get most of them. OpenCV requires libpng, libjpeg and libtiff with the -dev versions so check they are there too...

Installing OpenCV for Ubuntu 6.06 (Dapper) UPDATED

Posted by Undermine on 17:31 comments (0)


You might want to use this distribution because it has longer support, some packages seem to be more stable than later ones and for me at least it runs faster than 7.04!
Note that there is an older section (struck out), which I have kept as we recently had a case where it was necessary to install the older version of ffmpeg!
  1. Install subversion and all the basic compilation packages such as g++ and gcc etc.
  2. Download ffmpeg with: svn checkout svn://svn.mplayerhq.hu/ffmpeg/trunk ffmpeg and then
    • ./configure --enable-shared --prefix=/opt/ffmpeg (the prefix bit is optional but I prefer to store special development packages like this one on their own directory so it is simpler to locate, configure, remove)

Installing OpenCV for Ubuntu 7.04 (Feisty)

Posted by Undermine on 17:30 comments (0)


You might want to use this distribution because it has corrected bugs from previous ones, supports your hardware better, etc.
Thanks to Leeds University for their guide on using OpenCV
  1. Install subversion and all the basic compilation packages such as g++ and gcc etc.
  2. Download ffmpeg with: svn checkout svn://svn.mplayerhq.hu/ffmpeg/trunk ffmpeg and then
    • ./configure --enable-shared --prefix=/opt/ffmpeg (the prefix bit is optional but I prefer to store special development packages like this one on their own directory so it is simpler to locate, configure, remove)
    • make
    • sudo make install

Installing OpenCV for Ubuntu 8.04

Posted by Undermine on 17:29 comments (0)


We have tried the officially supported version of this, i.e. runnning KDE 3.x instead of 4.x
Note that installation on version 7.10 follows the same procedure, but I never got round documenting it
Things seem to be getting easier!
Thanks to Leeds University for their guide on using OpenCV
  1. sudo apt-get install build-essential to get compiler, ld, make, ....
  2. Install the other necessary packages: sudo apt-get install libgtk2.0-dev libavcodec-dev libavformat-dev libjpeg62-dev libtiff4-dev
  3. Now download and untar version 1.0.0 of OpenCV from http://sourceforge.net/project/showfiles.php?group_id=22870&package_id=16948
  4. Now do

เริ่มต้นกับ OpenCV ภาค 2 : Revenge of the Linux

Posted by Undermine on 17:26 comments (0)


เนื่องจากวิธีในครั้งที่แล้ว สามารถทำได้บนเครื่องบางเครื่องเท่านั้น (เฉพาะเครื่องผมที่ใช้ Ubuntu 9.04 อยู่) แต่ปรากฎว่าพอลองเอาไปทำกับ Linux Mint 7 ของเพื่อนผม พบว่ามีปัญหากับ package SVN มาก Cmake ไม่ออกซักที ก็เลยโหลด binary ไฟล์มาคอมไพล์โดยตรงเลย
1. install compiler, open the Terminal and type this command:
sudo apt-get install build-essential
2. install related dependencies:
sudo apt-get install libgtk2.0-dev libavcodec-dev libavformat-dev libjpeg62-dev libtiff4-dev

เริ่มต้นกับ OpenCV บน linux

Posted by Undermine on 17:23 comments (0)


OpenCV เป็นไลบรารี่ภาษา C ตัวหนึ่ง  ที่ถูกสร้างขึ้นมาเพื่องาน Image Processing โดยที่สามารถทำงานร่วมกับ IDE ทั่วไป และสามารถทำงานได้บนทุกแพลตฟอร์ม ไม่ว่าจะเป็นวินโดว์ ลินุกซ์ หรือ MacOS
สิ่งที่มันทำได้ (โดยคร่าวๆ)
- ทำงาน Image Processing ได้เกือบทุกประเภท ไม่ว่าจะลด noise / edge detection / hough transform / Feature Detection (ถ้าให้อธิบายแบบชาวบ้านก็คือ ทำทุกอย่างที่ Photoshop ทำได้)
- สามารถทำงานได้กับทั้งภาพนิ่ง วีดีโอ (ไฟล์ทุกสกุลที่ codec สามารถอ่านได้) และทำงานกับกล้องแบบ real-time ได้

การติดตั้งโปรแกร OpenCV 2.0 สำหรับ VC2008 (ตอนที่ 1)

Posted by Undermine on 17:19 comments (0)

การติดตั้งโปรแกร OpenCV 2.0 สำหรับ Microsoft Visual C++ 2008

OpenCV เป็นหนึ่งในผลงานอันยอดเยี่ยมของ Intel ถูกพัฒนาขึ้นเพื่อใช้เป็นเครื่องมือสำหรับนักพัฒนาโปรแกรมทางด้าน Image Processing และ Computer Vision

บทความนี้ขอนำเสนอขั้นตอนการติดตั้ง OpenCV 2.0 สำหรับ Microsoft Visual C++ 2008 ซึ่งทำงานบนระบบปฏิบัติการ Windows เพื่อให้ง่ายต่อการทำความเข้าใจ จะขออธิบายขั้นต่อนอย่างๆอย่างละเอียดด้วยรูปภาพพร้อมคำอธิบาย

ขั้นตอนการติดตั้ง
1. ดาวน์โหลดตัวโปรแกรม OpenVC 2.0 จาก http://sourceforge.net/projects/opencvlibrary/


การติดตั้งโปรแกร OpenCV 2.0 สำหรับ VC2008 (ตอนที่ 2)

Posted by Undermine on 17:18 comments (0)

จากตอนที่ 1 เราได้ทำการติดตั้ง OpenCV 2.0 ไปเรียนร้อยแล้ว แต่ยังไมาสามารถนำมาใช้ได้ การติดตั้ง OpenCV 2.0 จะต้องทำการ Make file ใหม่ ซึ่งแตกต่างจาก OpenCV 0.x และ 1.x

ในตอนที่ 2 นี้จะอธิบายถึงขั้นตอนการติดตั้งโปรแกรม CMake ซึ่งจะใช้เป็นตัว Make file สำหรับ OpenCV 2.0 และเช่นเดิม เพื่อให้ง่ายต่อการติดตั้งโปรแกรมจะขออธิบายขั้นตอนต่างๆอย่างละเอียดด้วยรูปภาพพร้อมคำอธิบาย

ขั้นตอนการติดตั้ง

1. ดาวน์โหลดตัวโปรแกรม CMake 2.8 จาก http://www.cmake.org/cmake/resources/software.html
หรือคลิกที่รูปภาพ เพื่อเข้าไปยังหน้าเว็บสำหรับดาวน์โหลด


การติดตั้งโปรแกร OpenCV 2.0 สำหรับ VC2008 (ตอนที่ 4)

Posted by Undermine on 17:17 comments (0)


มาว่ากันต่อถึงการติดตั้ง OpenCV 2.0 เพื่อให้สามารถใช้ได้กับโปรแกรม Microsoft Visual C++ 2008 รายละเอียดในตอนนี้จะขออธิบายถึงการกำหนดให้ Visual C++ 2008 สามารถดึงความสามารถของ OpenCV 2.0 มาใช้ได้ รวมไปถึงการสร้างโปรเจ็คใหม่ การคอมไพล์ และการสั่งรันเพื่อดูผลการทำงาน

ขั้นตอน

1. เริ่มจากการสั่งรันโปรแกรม Visual C++ 2008 จากนั้นให้คลิกที่ Tools -> Options... ตามรูป


2. ตอนนี้จะมีหน้าต่าง Options ปรากฎขึ้นมา ให้ทำการกำหนดค่าต่างๆตามรูป (ยังไม่ต้องคลิกปุ่ม OK)

การติดตั้งโปรแกร OpenCV 2.0 สำหรับ VC2008 (ตอนที่ 3)

Posted by Undermine on 17:16 comments (0)


ลากยาวกันมาจนถึงตอนที่ 3 แล้วครับ ในสองตอนที่ผ่านมา (ตอนที่ 1 และ ตอนที่ 2) เราได้ทำการติดตั้ง OpenCV2.0 และ CMake2.8 ไปเรียบร้อยแล้ว ในตอนนี้จะขออธิบายถึงขั้นตอนการใช้โปรแกรม CMake และ Visual C++ 2008 เพื่อทำการสร้างไฟล์ต่างๆ และสุดท้ายจะจบด้วยการตั้งค่าเครื่องคอมพิวเตอร์เพื่อให้สามารถทำงานได้อย่างถูกต้องตามลำดับครับ

ขั้นตอนการติดตั้ง

1. เริ่มจากเปิดโปรแกรม CMake โดยการคลิกที่ Start -> All Programes -> CMake 2.8 -> CMacke (cmake-gui) จากนั้นให้คลิกขวาแล้วเลือก Run as administrator


2. หลังจากสั่งเปิดโปรแกรม CMacke (cmake-gui) จะมีหน้าต่างปรากฎขึ้นดังรูป

X-Y Coordinate System on OpenCV

Posted by Undermine on 17:09 comments (0)


In case that I would forgot which axis is X or Y……. This is just a memo
cvcoordinate
Note : I take this coordinate by observing the result from the local maxima function. So it should be the coordinate in the general case
However in case of  camera coordinate in the space. The coordinate axis is the difference issue (see in Sinisa Kolaric blog).

การติดตั้ง OpenCV บน Ubuntu

Posted by Undermine on 08:34 comments (0)


1. โหลด OpenCV จากที่นี่





2. untar ไฟล์ คลิกขวาที่ opencv-1.1pre1.tar.gz แล้วเลือก Extract Here





3. รัน Terminal คลิกที่ Applications | Accessories | Terminal