Kan Ouivirach

Tag: OpenCV

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...

การจัดการปัญหา 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...

OpenCV 2.0 released

by zkan on Oct.06, 2009, under Tools

Great news for me!

One thing I mentioned in the previous blog was that, to install OpenCV with ffmpeg, the soft links need to be created under /usr/include/ffmpeg. However, for this new version of OpenCV, we don’t need to do it anymore. :)

Wanna try? >> Click <<

Related posts

3 Comments :, more...

Creating an OpenCV application with Eclipse on Windows

by zkan on Sep.28, 2009, under How-To

เนื่องจากว่าผมใช้ระบบ 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

(continue reading…)

Related posts

6 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...