【发布时间】:2011-12-01 14:26:39
【问题描述】:
我想知道是否有任何 C++ 或 C 库允许通过坐标获取 png-image。例如: 我打开了 png 文件,我只需要获取它的 parf,它从
开始x = 5
y = 5
w = 10
h = 10
and so on
那么什么库允许做这样的操作呢? 提前感谢
【问题讨论】:
我想知道是否有任何 C++ 或 C 库允许通过坐标获取 png-image。例如: 我打开了 png 文件,我只需要获取它的 parf,它从
开始x = 5
y = 5
w = 10
h = 10
and so on
那么什么库允许做这样的操作呢? 提前感谢
【问题讨论】:
使用Magic++:
#include <Magick++.h>
using namespace Magick;
void main()
{
Image image;
image.read( "in.png" );
// Crop the image to specified size (width, height, xOffset, yOffset)
image.crop( Geometry(10, 10, 5, 5) );
// Write the image to a file
image.write( "out.png" );
}
【讨论】:
大多数图书馆都会为您做到这一点。在 OpenCV 中:
标题:
#include <cv.h>
#include <highgui.h>
代码:
IplImage *im = cvLoadImage("input.png");
cvSetImageROI(im, cvRect(x, y, w, h));
cvShowImage("output.png", im);
【讨论】:
就像@misha 所说,大多数库都有这个功能。 boost.GIL 库也可以做到:
#include <boost/gil/gil_all.hpp>
// I need this bugfix to compile against libpng 1.5, your mileage may vary
#define int_p_NULL (int*)NULL
// done with the fix
#include <boost/gil/extension/io/png_dynamic_io.hpp>
int main()
{
boost::gil::rgb8_image_t img;
png_read_image("in.png", img);
png_write_view("out.png", subimage_view(const_view(img), 5, 5, 10, 10));
}
编译时只需要-lpng。
【讨论】: