目標學習:1、使用
imread載入圖像。
2、使用
namedWindow創建命名OpenCV窗口。
3、使用
imshow在OpenCV窗口中顯示圖像。
源碼:
1 #include <opencv2/core/core.hpp>
2 #include <opencv2/highgui/highgui.hpp>
3 #include <iostream>
4
5 using namespace cv;
6 using namespace std;
7
8 int main(int argc, char ** argv)
9 {
10 if (2 != argc)
11 {
12 cout << " Usage: display_image ImageToLoadAndDisplay" << endl;
13 return -1;
14 }
15
16 Mat image;
17 image = imread(argv[1], CV_LOAD_IMAGE_COLOR); // Read the file
18
19 if (!image.data) // Check for invalid input
20 {
21 cout << "Could not open or find the image" << std::endl;
22 return -1;
23 }
24
25 namedWindow("Display window", WINDOW_AUTOSIZE); // Create a window for display
26 imshow("Display window", image); // Show our image inside it.
27
28 waitKey(0); // wait for a keystroke in the window
29 return 0;
30 }
說明:
在使用OpenCV 2 的功能之前,幾乎總是要包含
1、
core 部分,定義庫的基本構建塊
2、
highgui模塊,包含輸入輸出操作函數。
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <iostream>
還需要include<iostream>這樣更容易在console上輸出輸入。為了避免數據結構和函數名稱與其他庫沖突,OpenCV有自己的命名空間
cv。當然為了避免在每個關鍵字前都加cv::keyword,可以在頭部導入該命名空間。
using namespace cv;using namespace std;需要在命令行輸入有效的圖像名稱。
if (2 != argc)
{
cout << " Usage: display_image ImageToLoadAndDisplay" << endl;
return -1;
}然后創建
Mat對象用于存儲載入的圖像數據。
Mat image;調用
imread函數載入圖像(圖像名稱為
argv[1]指定的)。第二個參數指定圖像格式。
1、CV_LOAD_IMAGE_UNCHANGED (<0) loads the image as is(including the alpha channel if present)2、CV_LOAD_IMAGE_GRAYSCALE (0) loads the image as an intensity one3、CV_LOAD_IMAGE_COLOR (>0) loads the image in the BGR formatimage = imread(argv[1], CV_LOAD_IMAGE_COLOR); // Read the file
如果第二個參數未指定,那么默認為CV_LOAD_IMAGE_COLOR為了檢查圖像是否正常載入,我們用
namedWindow函數創建一個OpenCV窗口來顯示圖像。需要指定窗口名稱和大小。
第二個參數默認為:WINDOW_AUTOSIZE
1、
WINDOW_AUTOSIZE 只支持QT平臺。
2、
WINDOW_NORMAL QT上支持窗口調整大小。
最后在創建的窗口中顯示圖像
imshow("Display window", image);
結果
編譯執行程序。
./DisplayImage d:\apple.jpg
posted on 2016-07-11 07:58
canaan 閱讀(954)
評論(0) 編輯 收藏 引用 所屬分類:
OPenCV學習