在 C++ 中打开和显示图像?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20168797/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Opening and Displaying an Image in C++?
提问by DeeVu
Basically I am teaching myself C++ and part of the program function will be to open and close an image specified. How would I go about doing this? Or what resource would I use?
基本上我是在自学 C++,程序功能的一部分是打开和关闭指定的图像。我该怎么做呢?或者我会使用什么资源?
Thanks!
谢谢!
回答by deeiip
In c++ (without any extra library) you may open an image. But there will be nothing particularly useful except a bunch of binary data. then you have to use your own decoder If you use opencvyou can write to open an image and display it:
在 C++(没有任何额外的库)中,您可以打开一个图像。但是除了一堆二进制数据之外,没有什么特别有用的。那么你必须使用你自己的解码器如果你使用opencv你可以写打开一个图像并显示它:
Mat m("fileName");
imshow("windowName",m);
To do the same with a general purpose library like qt you can use this code :
要对像 qt 这样的通用库执行相同操作,您可以使用以下代码:
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QGraphicsScene scene;
QGraphicsView view(&scene);
QGraphicsPixmapItem item(QPixmap("c:\test.png"));
scene.addItem(&item);
view.show();
return a.exec();
}
To learn more about imageviewer widget go here. Or you may have a look at hereto display as graphics view.
回答by peterh - Reinstate Monica
For a crossplatform, opensource and very good library you can use libmagick++.
对于跨平台、开源和非常好的库,您可以使用 libmagick++。
回答by Iman
modified Hello World sample from OpenCV 2 Computer Vision Application Programming Cookbookrunning in VS 2012 win32 Console app
在 VS 2012 win32 控制台应用程序中运行的OpenCV 2 计算机视觉应用程序编程手册中修改的 Hello World 示例
or official OpenCV (Open Source) sample
warning: opencv-2.4.10.exe Win installer is 360 MBwhich have many advance features and have sample code, Doc and built binaries in Python and Java too x86 and 64 in it too
警告:opencv-2.4.10.exe Win 安装程序是360 MB,它具有许多高级功能,并且在 Python 和 Java 中也有示例代码、Doc 和内置二进制文件,x86 和 64 位也在其中
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
int main()
{
// read an image
cv::Mat image= cv::imread("img.jpg");
// create image window named "My Image"
cv::namedWindow("My Image");
// show the image on window
cv::imshow("My Image", image);
// wait key for 5000 ms
cv::waitKey(5000);
return 0;
}