目標(biāo)學(xué)習(xí):1、使用
imread載入圖像。
2、使用
namedWindow創(chuàng)建命名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 部分,定義庫的基本構(gòu)建塊
2、
highgui模塊,包含輸入輸出操作函數(shù)。
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <iostream>
還需要include<iostream>這樣更容易在console上輸出輸入。為了避免數(shù)據(jù)結(jié)構(gòu)和函數(shù)名稱與其他庫沖突,OpenCV有自己的命名空間
cv。當(dāng)然為了避免在每個關(guān)鍵字前都加cv::keyword,可以在頭部導(dǎo)入該命名空間。
using namespace cv;using namespace std;需要在命令行輸入有效的圖像名稱。
if (2 != argc)
{
cout << " Usage: display_image ImageToLoadAndDisplay" << endl;
return -1;
}然后創(chuàng)建
Mat對象用于存儲載入的圖像數(shù)據(jù)。
Mat image;調(diào)用
imread函數(shù)載入圖像(圖像名稱為
argv[1]指定的)。第二個參數(shù)指定圖像格式。
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
如果第二個參數(shù)未指定,那么默認(rèn)為CV_LOAD_IMAGE_COLOR為了檢查圖像是否正常載入,我們用
namedWindow函數(shù)創(chuàng)建一個OpenCV窗口來顯示圖像。需要指定窗口名稱和大小。
第二個參數(shù)默認(rèn)為:WINDOW_AUTOSIZE
1、
WINDOW_AUTOSIZE 只支持QT平臺。
2、
WINDOW_NORMAL QT上支持窗口調(diào)整大小。
最后在創(chuàng)建的窗口中顯示圖像
imshow("Display window", image);
結(jié)果
編譯執(zhí)行程序。
./DisplayImage d:\apple.jpg
posted on 2016-07-11 07:58
canaan 閱讀(982)
評論(0) 編輯 收藏 引用 所屬分類:
OPenCV學(xué)習(xí)