在c++中通过坐标获取png

Getting png by the coordinates in C++

本文关键字:坐标 获取 png c++      更新时间:2023-10-16

我想知道是否有任何c++或C的库,允许通过坐标获得png-image。例如:我已经打开了png文件,我只需要得到从

开始的部分。
x = 5
y = 5
w = 10
h = 10
and so on

那么哪个库允许这样的操作呢?Thanks on advance

Using 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说的,大多数库都有这个功能。的提振。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