Just My Life & My Work

安裝好OpenCV之後,接下來就要測試能否運行,我們以最簡單的顯示影像 (Display Image)來做示範。

/**
	Theme: Display Image
	Compiler: Visual Studio 2010 with OpenCV 2.4
	Date: 101/06/01
	Author: HappyMan
	Blog: https://cg2010studio.wordpress.com/
*/
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <iostream>

using namespace cv;
using namespace std;

int main( int argc, char** argv ){
	Mat image;
	if( argc != 2){
		cout <<" Usage: display_image ImageToLoadAndDisplay" << endl;
		image = imread("HappyMan's Girl.jpg", CV_LOAD_IMAGE_COLOR);
		// Read the file
	}
	else{
		image = imread(argv[1], CV_LOAD_IMAGE_COLOR);
	}

	if(! image.data ){// Check for invalid input
		cout <<  "Could not open or find the image" << endl ;
		system("pause");
		return -1;
	}

	namedWindow( "HappyMan - Display window", CV_WINDOW_AUTOSIZE );
	// Create a window for display.
	imshow( "HappyMan - Display window", image );
	// Show our image inside it.

	waitKey(0);
	// Wait for a keystroke in the window
	return 0;
}
imread(const string& filename, int flags=1)

參數:

  • filename – Name of file to be loaded.
  • flags – Flags specifying the color type of a loaded image:
    >0 Return a 3-channel color image
    =0 Return a grayscale image
    <0 Return the loaded image as is.

支援圖檔型態:

  • Windows bitmaps – *.bmp, *.dib (always supported)
  • JPEG files – *.jpeg, *.jpg, *.jpe (see the Notes section)
  • JPEG 2000 files – *.jp2 (see the Notes section)
  • Portable Network Graphics – *.png (see the Notes section)
  • Portable image format – *.pbm, *.pgm, *.ppm (always supported)
  • Sun rasters – *.sr, *.ras (always supported)
  • TIFF files – *.tiff, *.tif (see the Notes section)

namedWindowimshow的第一個參數要設定一樣,否則執行後會跳出兩個視窗。

display_image.h需改為display_image.cpp才能通過編譯。

我已修改sample code,讓原本只能在命令提示字元執行指令:

  • OpenCV2.4test.exe “HappyMan’s Girl.jpg"

能夠直接在編譯後執行,只要將影像檔放在執行檔同資料夾即可。

這裡依然拿我的MV女孩的相片來測試~

參考:Reading and Writing Images and Video

Comments on: "[OpenCV] 顯示影像 (Display Image)" (41)

  1. 未知 的大頭貼

    […] 想要知道是否安裝成功,仍舊可以參考這一篇:顯示影像 (Display Image)。 […]

  2. tom 的大頭貼

    真的感謝大大
    但是VS2010 原來是沒有64BIT V

  3. tom 的大頭貼

    我在用WIN7 64 bit , 我發現解壓後path 係C:\OpenCV2.4\opencv\build; 要將他改這樣? 要改成x64 嗎? 但是vs 是用32bit?
    還有現在有cannot open file “opencv_core240d.lib" error .

    • HappyMan 的大頭貼

      端視你安裝的路徑來設定PATH。

      32bit用x86,64bit用x64。

      「opencv_core240d.lib」注意240是版本2.4.0,
      如果你用2.4.2就是「opencv_core242d.lib」,
      想確認是否有這個檔的話還是前往該資料夾查看喔!

      • tom 的大頭貼

        我全都用了x64, 但是最後都是error lnk1112: module type “x64″ conflicts with target machine type "x86"

        • HappyMan 的大頭貼

          我找到一篇可以幫助你解決這個問題的解答,
          我在StackOverflow的這一篇文章:LNK1112 module machine type ‘X86’ conflicts with target machine type ‘x64’(點選可連結)
          發現之前我研究視同學也有類似的問題,
          也就是電腦是64bit但Visual Studio安裝32bit,
          才會導致這個錯誤喔!

          這個人回答的不錯:
          The error is telling you that the linker has been run with a target that specifies x64 while the module the linker is reading was created for x86. What’s wrong depends on what you are trying to do.

          If you are trying to create an x64 code file then the linker target is correct and the module is wrong. If you are trying to create an x86 code file then the linker target is wrong and the module is correct.

          Whichever item is wrong is the one you have to correct – either by changing the linker target or by changing the module. If the module is wrong you probably ended up compiling it with the x86 version of the compiler rather than the x64 version.

          若要解決這個問題,
          你可能要在64bit的電腦重新安裝64bit的VS,
          或是使用32bit的電腦安裝32bit的VS,
          相信這個問題就能夠明朗化~

  4. tom 的大頭貼

    想請問大大 最後彈出0XC000007B 無法正常啟動是什麼問題?

    • HappyMan 的大頭貼

      這個狀況我沒有遇過,剛google一下,發現跟玩遊戲有關……換台電腦或作業系統(Win XP or Win 7)試試看,搞不好就解決了!

  5. 黃大頭 的大頭貼

    關於第五步
    我有幾個小疑問,不過前提是我有照上面你的步驟做

    1.Include目錄:

    C:\OpenCV2.4\build\include;
    C:\OpenCV2.4\build\include\opencv;

    這部分,\opencv已經是include下的子目錄,為什麼還要再把他include進去

    2.程式庫目錄:

    C:\OpenCV2.4\build\x86\vc10\lib;

    這部分如果我用的是vc2008這樣需要修改哪邊嗎?

    謝謝:)

    • HappyMan 的大頭貼

      你可以試試看拿掉之後會有什麼錯誤訊息,我嘗試過拿掉後不能通過編譯,原因是OpenCV採某種模式去發展程式碼,我想這一定有它的道理,至於為什麼這就要問發展者的構想,我們不懂就先照它的模式走吧~
      原則上VC2008就是vc9,而VC2010就是vc10。其餘設定應該是一樣的。

  6. 黃大頭 的大頭貼

    可以講的再詳細一點嗎 有點沒有頭緒

    感激不盡

  7. 黃大頭 的大頭貼

    我依照第一篇安裝環境步驟再來到這邊直接開新專案貼display.cpp

    但是好像出了一點錯 高手happyman可以幫我解答一下嗎 弄了好久

    我有把完整的影像拍下來了

    以下是縮圖

    http://ppt.cc/Sia8

    麻煩你了

  8. 彥 的大頭貼

    你好大大~想請問~因為我想做kmeans,但是不知道怎麼將讀取影像中灰階強度值寫進陣列中去運算@@~?請問大大能提供一下方法意見嗎?謝謝你><~

  9. mosdeo 的大頭貼

    原來您也改用2.0版的寫法了!請問一下,人臉辨識用1.0版寫法速度比2.0版還要慢,是什麼原因呢?

  10. 未知 的大頭貼

    […] 想要知道是否安裝成功,可以參考這一篇:顯示影像 (Display Image)。 […]

回覆給tom 取消回覆

這個網站採用 Akismet 服務減少垃圾留言。進一步了解 Akismet 如何處理網站訪客的留言資料

標籤雲