在本教程中,您将学习
1、加载一张图片(using cv::imread ;cvLoadImage)
2、创建一个窗口(using cv::namedWindow)
3、在窗口中显示图片(using cv::imshow)
代码:
1 #include "stdafx.h"
2 #include <opencv\cv.h>
3 #include <opencv\highgui.h>
4 #include <stdio.h>
5 #include <opencv2\opencv.hpp>
6 #include <string>
7
8 int _tmain(int argc, _TCHAR* argv[])
9 {
10 std::string filename = "d:\\lena.jpg";
11 cv::Mat image;//创建图像存储对象
12 image = cv::imread(filename);//加载图像
13 if (!image.data)
14 {
15 printf("No image data\n");
16 return -1;
17 }
18 std::cout << image.cols << std::endl;
19 cv::namedWindow("Display Image", CV_WINDOW_AUTOSIZE);//创建窗口
20 cv::imshow("Display Image", image);//显示
21 cv::waitKey(0);
22 system("pause");
23 return 0;
24 }
另外一种方法:
1 #include "stdafx.h"
2 #include <opencv\cv.h>
3 #include <opencv\highgui.h>
4
5 using namespace std;
6 int _tmain(int argc, _TCHAR* argv[])
7 {
8 IplImage * test;
9 test = cvLoadImage("d:\\lena.jpg");//图片路径
10 cvNamedWindow("test_demo", 1);
11 cvShowImage("test_demo", test);
12 cvWaitKey(0);
13 system("pause");
14 return 0;
15 }
图片显示结果:
创建一张图片
1 #include "stdafx.h"
2 #include <opencv\cv.h>
3 #include <opencv\highgui.h>
4
5
6 int _tmain(int argc, _TCHAR* argv[])
7 {
8 cvNamedWindow("test_demo", CV_WINDOW_AUTOSIZE);
9 IplImage *img = cvCreateImage(cvSize(340, 280), IPL_DEPTH_8U, 1);
10 CvFont font;
11 double hScale = 1.0;
12 double vScale = 1.0;
13 int lineWidth = 1;
14 cvInitFont(&font, CV_FONT_HERSHEY_SIMPLEX | CV_FONT_ITALIC, hScale, vScale, 0, lineWidth);
15 cvPutText(img, "Hello world!", cvPoint(100, 100), &font, cvScalar(255, 255, 0));
16 cvShowImage("test_demo", img);
17 cvWaitKey();
18 system("pause");
19 return 0;
20 }