使用 C++ 或 C 处理图像
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4906736/
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
Working with images in C++ or C
提问by mohammad
The first thing is that I am a beginner. Okay?
首先,我是初学者。好的?
I've read related answers and questions, but please help me with this problem:
我已经阅读了相关的答案和问题,但请帮助我解决这个问题:
How can I open an JPEG image file in C++, convert it to a grayscale image, get its histogram, resize it to a smaller image, crop a particular area of it, or show a particular area of it?
如何在 C++ 中打开 JPEG 图像文件、将其转换为灰度图像、获取其直方图、将其调整为较小的图像、裁剪其特定区域或显示其特定区域?
For these tasks, is C or C++ faster in general?
对于这些任务,一般来说 C 还是 C++ 更快?
What libraries are simplest and fastest? The running time is very important.
哪些库最简单、最快?运行时间非常重要。
Thanks.
谢谢。
回答by ayush
here is an example using magicklibrary.
这是一个使用魔法库的例子。
program which reads an image, crops it, and writes it to a new file (the exception handling is optional but strongly recommended):
读取图像、裁剪图像并将其写入新文件的程序(异常处理是可选的,但强烈推荐):
#include <Magick++.h>
#include <iostream>
using namespace std;
using namespace Magick;
int main(int argc,char **argv)
{
// Construct the image object. Seperating image construction from the
// the read operation ensures that a failure to read the image file
// doesn't render the image object useless.
Image image;
try {
// Read a file into image object
image.read( "girl.jpeg" );
// Crop the image to specified size (width, height, xOffset, yOffset)
image.crop( Geometry(100,100, 100, 100) );
// Write the image to a file
image.write( "x.jpeg" );
}
catch( Exception &error_ )
{
cout << "Caught exception: " << error_.what() << endl;
return 1;
}
return 0;
}
回答by templatetypedef
There are many good libraries for working with images in C and C++, none of which is clearly superior to all others. OpenCVwiki, project pagehas great support for some of these tasks, while ImageMagickwiki, project pageis good at others. The JPEG group has its own implementation of JPEG processing functions as well. These are probably good resources to start from; the API documentation can guide you more specifically on how to use each of these.
在 C 和 C++ 中有许多用于处理图像的好库,其中没有一个明显优于所有其他库。OpenCV wiki, project page对其中一些任务有很大的支持,而 ImageMagick wiki, project page擅长其他任务。JPEG 组也有自己的 JPEG 处理功能实现。这些可能是开始的好资源;API 文档可以更具体地指导您如何使用其中的每一个。
As for whether C or C++ libraries are bound to be faster, there's no clear winner between the two. After all, you can always compile a C library in C++. That said, C++ libraries tend to be a bit trickier to pick up because of the language complexity, but much easier to use once you've gotten a good feel for the language. (I am a bit biased toward C++, so be sure to consider the source). I'd recommend going with whatever language you find easier for the task; neither is a bad choice here, especially if performance is important.
至于 C 或 C++ 库是否一定会更快,两者之间没有明显的赢家。毕竟,您始终可以用 C++ 编译 C 库。也就是说,由于语言的复杂性,C++ 库往往有点难以掌握,但是一旦您对该语言有了良好的感觉,就会更容易使用。(我有点偏向 C++,所以一定要考虑来源)。我建议使用您认为更容易完成任务的任何语言;两者都不是一个糟糕的选择,尤其是在性能很重要的情况下。
Best of luck with your project!
祝你的项目好运!
回答by florianbaethge
well for basic image manipulations you could also try Qt's QImage class (and other). This gives you basic functionality for opening, scaling, resizing, cropping, pixel manipulations and other tasks.
对于基本的图像操作,您还可以尝试 Qt 的 QImage 类(和其他)。这为您提供了打开、缩放、调整大小、裁剪、像素操作和其他任务的基本功能。
Otherwise you could as already said use ImageMagick or OpenCV. OpenCV provides a lot of examples with it for many image manipulation/image recognition tasks...
否则你可以像已经说过的那样使用 ImageMagick 或 OpenCV。OpenCV 为许多图像处理/图像识别任务提供了大量示例......
Hope it helps...
希望能帮助到你...
回答by xcramps
回答by Agnius Vasiliauskas
If running time is really important thing then you must consider image processing library which offloads processing job to GPU chip, such as:
如果运行时间真的很重要,那么您必须考虑将处理工作卸载到 GPU 芯片的图像处理库,例如: