安裝好OpenCV之後,接下來就要測試能否運行,我們以最簡單的顯示影像 (Display Image)來做示範。
- 環境設定參考:Visual Studio 2010 安裝 OpenCV 2.4
- 範例程式:C:\OpenCV2.4\samples\cpp\tutorial_code\introduction\display_image\display_image.cpp
/**
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)
namedWindow和imshow的第一個參數要設定一樣,否則執行後會跳出兩個視窗。
display_image.h需改為display_image.cpp才能通過編譯。
我已修改sample code,讓原本只能在命令提示字元執行指令:
- OpenCV2.4test.exe “HappyMan’s Girl.jpg"
能夠直接在編譯後執行,只要將影像檔放在執行檔同資料夾即可。
這裡依然拿我的MV女孩的相片來測試~

Comments on: "[OpenCV] 顯示影像 (Display Image)" (41)
[…] 想要知道是否安裝成功,仍舊可以參考這一篇:顯示影像 (Display Image)。 […]
讚讚
真的感謝大大
但是VS2010 原來是沒有64BIT V
讚讚
我在用WIN7 64 bit , 我發現解壓後path 係C:\OpenCV2.4\opencv\build; 要將他改這樣? 要改成x64 嗎? 但是vs 是用32bit?
還有現在有cannot open file “opencv_core240d.lib" error .
讚讚
端視你安裝的路徑來設定PATH。
32bit用x86,64bit用x64。
「opencv_core240d.lib」注意240是版本2.4.0,
如果你用2.4.2就是「opencv_core242d.lib」,
想確認是否有這個檔的話還是前往該資料夾查看喔!
讚讚
我全都用了x64, 但是最後都是error lnk1112: module type “x64″ conflicts with target machine type "x86"
讚讚
我找到一篇可以幫助你解決這個問題的解答,
我在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,
相信這個問題就能夠明朗化~
讚讚
想請問大大 最後彈出0XC000007B 無法正常啟動是什麼問題?
讚讚
這個狀況我沒有遇過,剛google一下,發現跟玩遊戲有關……換台電腦或作業系統(Win XP or Win 7)試試看,搞不好就解決了!
讚讚
關於第五步
我有幾個小疑問,不過前提是我有照上面你的步驟做
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這樣需要修改哪邊嗎?
謝謝:)
讚讚
你可以試試看拿掉之後會有什麼錯誤訊息,我嘗試過拿掉後不能通過編譯,原因是OpenCV採某種模式去發展程式碼,我想這一定有它的道理,至於為什麼這就要問發展者的構想,我們不懂就先照它的模式走吧~
原則上VC2008就是vc9,而VC2010就是vc10。其餘設定應該是一樣的。
讚讚
#include
#include
我把上面這兩段改成
#include
#include
一堆錯誤訊息不見了
但是變成這樣
http://ppt.cc/SHR8
include目錄那邊我也有修改過了
請問是怎麼了:P?
讚讚
訊息顯示沒有那個檔喔!還是用原本的路徑和include來執行會順利些~照理說依照我的環境設定執行我的程式碼,應該會沒有問題才是呢!
讚讚
可以講的再詳細一點嗎 有點沒有頭緒
感激不盡
讚讚
Visual Studio 2010 安裝 OpenCV 2.4這篇文章中的第五步,「Include目錄」和「程式庫目錄」這兩個路徑你可能沒有設定好。
讚讚
我依照第一篇安裝環境步驟再來到這邊直接開新專案貼display.cpp
但是好像出了一點錯 高手happyman可以幫我解答一下嗎 弄了好久
我有把完整的影像拍下來了
以下是縮圖
http://ppt.cc/Sia8
麻煩你了
讚讚
看起來是環境的路徑沒有設定好,才會出現「無法解析的符號」這類錯誤訊息,你可以再試試看!
讚讚
你好大大~想請問~因為我想做kmeans,但是不知道怎麼將讀取影像中灰階強度值寫進陣列中去運算@@~?請問大大能提供一下方法意見嗎?謝謝你><~
讚讚
這一篇應該可以幫助你:轉換影像為灰階 (Transform Image to Gray Level)。
這篇有教你如何存取BGR的值,讓你可以再做後續應用喔~
讚讚
原來您也改用2.0版的寫法了!請問一下,人臉辨識用1.0版寫法速度比2.0版還要慢,是什麼原因呢?
讚讚
你說的1.0版和2.0版是只OpenCV的版本?
通常是因為演算法和資料量的差異,
才使得執行效果和效能不同。
讚讚
[…] 想要知道是否安裝成功,可以參考這一篇:顯示影像 (Display Image)。 […]
讚讚