Kan Ouivirach

How-To

How to install OpenCV 2.1 on Ubuntu

by zkan on Jul.17, 2010, under How-To

The new version of OpenCV’s changed the way to install by using cmake. I also have to update myself and find some information regarding it. Recently, I’ve read the tutorial on installing OpenCV 2.1 in Ubuntu which explains very well; so, I follow this tutorial and I would note my installation steps here.

  1. install dependencies as follows (please also see the screen shots below).
    • libcv4
    • libcv-dev
    • libcvaux4
    • libcvaux-dev
    • libhighgui4
    • libhighgui-dev
    • opencv-doc (optional)
    • ffmpeg (optional; if you want to use ffmpeg to do video processing)

    check-opencv-packages01

    check-opencv-packages02

  2. install cmake using the command sudo apt-get install cmake.
  3. run cmake -D WITH_FFMPEG=ON -D CMAKE_INSTALL_PREFIX=${HOME}/opencv/ ..
    • Note that there is a dot at the end of the command above which means to run at the current directory.
    • The options mean to use ffmpeg and to set the OpenCV path in your home directory.
  4. check if ffmpeg is available and the folder to install is correct. See the screen shot below.

    config-checking

  5. run the command make
  6. run the command sudo make install.
  7. The last thing is to is to tell the dynamic linker where the DLLs are; so, add the line: export LD_LIBRARY_PATH=${HOME}/opencv/lib to ~/.bashrc and then relogin.

finish.. :)

Related posts

3 Comments :, more...

การใช้ OpenCV สร้างกราฟ Histogram จากรูป

by zkan on Mar.09, 2010, under How-To, Tutorial

บล็อกนี้ผมขอเสนอวิธีใช้ฟังก์ชั่นจาก OpenCV สร้างกราฟ Histogram จากค่าสีที่ได้จากรูปที่เป็น Grayscale นะครับ ซึ่งก็สามารถนำไปประยุกต์ใช้กับรูปที่เป็น RGB หรือ HSV ได้เหมือนกันครับ

ก่อนอื่นเลย Histogram ของรูป คืออะไร.. มันก็คือกราฟที่เกิดจากการพลอตจำนวนของ pixel ที่ค่าสีนั้นๆ นั่นเอง แกน X คือค่าของสี ส่วนแกน Y คือจำนวนของ pixel (อ่านเพิ่มเติมได้ที่ Image histogram ครับ)

เมื่อเราทราบหลักการของการสร้าง Histogram จากรูปแล้ว เราก็มาดูที่โค้ดกันครับ เริ่มต้นเราก็ประกาศขนาดของ Histogram ครับ ให้มีขนาด 256 ช่อง (ค่าสีปกติจะมีค่าระหว่าง 0-255 ในที่นี้ผมจะให้ช่องหนึ่งคือค่าสีหนึ่งนะครับ ซึ่งค่านี้สามารถเปลี่ยนแปลงได้แล้วแต่งานครับ)

int bins = 256;
int hsize[] = { bins };

ในโค้ดของผมนี้ผมจะสร้าง Histogram ที่เป็น uniform นะครับ เราก็ต้องกำหนดขอบเขตของค่า x ซึ่งต้องกำหนดตาม format ดังนี้ครับ โดย ranges[] จะต้องมีจำนวน dimension เท่ากับจำนวนค่าที่เป็นคู่ที่เราประกาศไว้ (คู่ 0 กับ 255)

float xranges[] = { 0, 255 };
float* ranges[] = { xranges };

เนื่องจากรูปตัวอย่างเป็น RGB เราก็ต้องแปลงให้เป็น Grayscale ก่อนนะครับ โดย image ในที่นี้คือรูปที่เราโหลดมาตั้งแต่ต้นนะครับ

IplImage* gray = cvCreateImage( cvGetSize( image ), 8, 1 );
cvCvtColor( image, gray, CV_BGR2GRAY );

จากนั้นเราต้องสร้างเพลน (plane) เพื่อมาคำนวณกราฟกันครับ ในที่นี้มีแค่ 1

IplImage* planes[] = { gray };

แล้วเราก็คำนวณค่าของ Histogram ดังนี้ โดยค่า 1 ตัวแรกหมายถึง จำนวน dimension ครับ ส่วน CV_HIST_ARRAY หมายถึงให้ type เป็นชนิด array และค่า 1 ตัวสุดท้ายหมายถึงว่าเป็น uniform ครับ

CvHistogram* hist = cvCreateHist( 1, hsize, CV_HIST_ARRAY, ranges, 1 );
cvCalHist( planes, hist, 0, NULL );

ในการคำนวณโดยใช้ cvCalHist นั้น เราเซตค่าที่ 3 ว่าให้เป็น 1 แปลว่า ถ้าสมมุติว่าเราวนลูปอ่านค่าสีจากรูปมาเรื่อยๆ เราสามารถบวกค่า pixel เพิ่มได้ในกราฟ Histogram อันเดิมครับ แต่ถ้าเป็น 0 ก็แปลว่าไม่ต้องบวกเพิ่ม ส่วนค่าสุดท้าย ถ้าไม่ใช่ NULL เราจะนำแค่จุด pixel ที่ไม่ใช่ 0 และมีการ mask ไว้ในรูปมาคำนวณครับ

ขั้นตอนต่อไป เราก็ต้องสร้างรูปขึ้นมาเพื่อแสดงผลครับ ในที่นี้เรากำหนดให้มีความสูงแค่ 50 พอครับ

IplImage* imgHistogram = cvCreateImage( cvGetSize( bins, 50 ), 8, 1 );
cvRectangle( imgHistogram, cvPoint( 0, 0 ), cvPoint( 256, 50 ), CV_RGB( 255, 255, 255 ), -1 ); // ค่าสุดท้ายคือค่า thickness ครับ ถ้าเป็น -1 แสดงว่าให้เป็นค่าสูงสุดเลย (เพื่อที่ว่าเราจะระบายสีขาวให้เต็มสีเหลี่ยมเลยครับ)

ขั้นตอนสุดท้ายเราก็วาดกราฟครับ และแสดงผล วิธีการก็คือดึงค่าออกมาทีละค่านั่นเอง แต่เนื่องจากเราเซตความสูงไว้ที่ 50 เราจะต้อง normalize ด้วยนะครับ แต่การที่เราจะ normalize ได้เราต้องมีค่าสูงสุดของ Histogram ก่อนครับ หาได้ดังนี้

float max_value = 0, min_value = 0;
cvGetMinMaxHistValue( hist, &min_value, $max_value );

และสุดท้ายจริงๆ ก็ดังนี้ครับ

cvNamedWindow( "histogram", 1 );
for( int i = 0; i < bins; i++ ) {
  float value = cvQueryHistValue_1D( hist, i );
  int normalized = cvRound( value * 50 / max_value );
  cvLine( imgHistogram, cvPoint( i, 50 ), cvPoint( i, 50 - normalized ), CV_RGB( 0, 0, 0 ) );
}
cvShowImage( "histogram", imgHistogram );

หวังว่าจะเป็นจุดอ้างอิงสำหรับผู้ที่เริ่มต้นสนใจในด้าน Image Processing นะครับ

Source code: hist.cc

credit: Isaias Gonzalez (siderevs at gmail dot com)

Related posts

2 Comments :, , , , more...

เก็บโน้ต TomBoy ของคุณให้ sync กับเครื่องอื่น โดยใช้ Dropbox

by zkan on Feb.04, 2010, under How-To

เรื่องของเรื่องก็คือ ผมอยากจะเปลี่ยนไปใช้ Ubuntu เต็มตัว และใช้ Windows เฉพาะเล่นเกม (DotA) และเรื่องที่สำคัญอย่างหนึ่งก็คือผมเก็บโน้ตของผมไว้ใน MS OneNote และ sync กันระหว่างเครื่องโดยใช้ LiveMesh ซึ่งถ้าผมเปลี่ยนไปใช้ Ubuntu ผมก็ต้องการ feature แบบนี้เช่นกัน

วันนี้ผมได้วิธีง่ายๆ มาและอยากแบ่งปันก็คือใช้ Tomboy Notes และ Dropbox นั่นเอง ถึงแม้ว่า Tomboy จะดีไม่เท่า OneNote ก็ตาม แต่ผมก็ยังคงไม่มีปัญหา เพราะเดิมที ไม่ได้ใช้ความสามารถของ OneNote มากสักเท่าไหร่

วิธีทำให้โน้ตของเรา sync กับ Dropbox ก็ให้ไปที่ Tomboy แล้ว Edit -> Preferences -> Synchronization และ ใช้ Service เป็น Local Folder จากนั้นก็เซต Folder Path ให้อยู่ภายใต้โฟลเดอร์ของ Dropbox และตามด้วยชื่อโฟลเดอร์ที่เราต้องการ เช่น ~/Dropbox/Tomboy หลังจากนั้น เราก็เขียนโน้ตตามปรกติ ถ้าเราต้องการให้ sync กัน ให้เลือกที่ Tools -> Synchronize Notes

เป็นอันเสร็จสิ้น :)

credit: http://www.starryhope.com/linux/2009/synchronize-tomboy-notes-with-dropbox/

Related posts

4 Comments :, , more...

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

by zkan on Dec.01, 2009, under How-To

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

ถ้าโค้ดของเรามีดังนี้

1
2
3
4
IplImage* img = cvCreateImage( ... );  // ประกาศตัวแปร img และได้จองหน่วยความจำไว้
img = xfunction( ... );  // ส่งค่า IplImage* กลับ
…
cvReleaseImage( &img );  // ลบหน่วยความจำตัวใหม่ที่เกิดจาก xfunction

แบบนี้แสดงว่าเกิด Memory leak ที่ xfunction ครับ คือตอนแรกเราจองหน่วยความจำให้กับตัวแปร img และต่อมาได้เรียกฟังก์ชั่น xfunction ซึ่งฟังก์ชั่นนี้ จะจองหน่วยความจำไว้อีกที่หนึ่งสำหรับค่าที่จะส่งกลับ ทำให้ตัวแปร img ไปชี้ที่หน่วยความจำใหม่ และหน่วยความจำเก่าที่ได้จองไว้ตอนแรกยังคงอยู่ (ถ้าใช้ภาษา Java คิดว่าไม่น่าจะมีปัญหาตรงนี้ครับ)

วิธีแก้ก็ง่ายมากครับ แค่แทนที่จะใช้ cvCreateImage() เราก็เปลี่ยนให้ชี้ไปที่ NULL แทน ดังนี้ครับ

1
2
3
4
IplImage* img = NULL;
img = xfunction( ... );
…
cvReleaseImage( &img );

สรุปคือ ถ้าใช้ภาษา C/C++ ต้องระวังเป็นอย่างมากครับ ไม่งั้นโปรแกรมอาจจะใช้แรมไปจนหมดเครื่องแน่ๆ ต้องหมั่นตรวจสอบว่าเราจองหน่วยความจำไว้ที่ไหน และพยายามลบทิ้งถ้าไม่ได้ใช้แล้วนะครับ ถ้ายิ่งทำงานเกี่ยวกับการวิเคราห์ไฟล์วีดีโอด้วยแล้วยิ่งต้องระวัง

อ่านรายละเอียดทั้งหมดได้ที่: OpenCV memory leaking management in C/C++

Related posts

Leave a Comment :, , more...

Mount USB drive on Ubuntu 9.10 (Karmic Koala)

by zkan on Oct.31, 2009, under How-To

I write the solution for a problem that USB drive cannot be mounted automatically on Ubuntu 9.10.

Firstly, install gnome-mount.

sudo apt-get install gnome-mount

Secondly, go to System > Preferences > Startup Applications. And add this command.

gnome-mount -p xxx (change xxx to the volume label)

Finally, log out the system and it should work then. However, the problem still occurs after unmount and unplug the USB drive. To fix it, log out the system and then plug in the USB drive again. It is not a very good solution by the way. :(

Also, find the bug report here.

Related posts

3 Comments :, , , , more...

Looking for something?

Use the form below to search the site:

Still not finding what you're looking for? Drop a comment on a post or contact us so we can take care of it!

Visit our friends!

A few highly recommended friends...