3

我想剪切一个图像并将其保存在一个新图像中。写入文件的新图像是正确的,但其打印的像素是错误的。为什么?

Magick::Image cutout(Magick::Image & image, Point const & pos,
    size_t width, size_t height)
{
    size_t const BufferSize = width*height*3*sizeof(char);
    std::unique_ptr<char[]> buffer(new char[BufferSize]);
    image.write(pos.x, pos.y, width, height, "RGB",
            Magick::CharPixel, buffer.get());
    Magick::Image res(width, height, "RGB",
            Magick::CharPixel, buffer.get());
    return res;
}

Magick::Image cutoutDigit(Magick::Image & image,
        FixesType const & fixes, Point const & pos)
{
    Point lt = transform(fixes, pos);
    Point rb = transform(fixes,
            Point(pos.x+DigitWidth, pos.y+DigitHeight));
    auto newImage = cutout(image, lt, rb.x-lt.x, rb.y-lt.y);
    Magick::Geometry g(DigitWidth,DigitHeight);
    g.aspect(true);
    newImage.resize(g);
    return newImage;
}

void print(Magick::PixelPacket * pixels, size_t columns, size_t rows)
{
    for ( size_t i = 0; i < rows; ++i) {
        for ( size_t j = 0; j < columns; ++j) {
            if (pixels->red < 0x80) {
                std::cout << '#';
                } else {
                std::cout << ' ';
            }
            //std::cout << std::hex << std::setfill('0') << std::setw(2) << (pixels-    >green & 0xff);
            ++pixels;
        }
        std::cout << std::endl; 
    }
}

char recognition(Magick::Image & image)
{
    Magick::Pixels view(image);
    Magick::PixelPacket * pixels = view.get(0,0,DigitWidth,DigitHeight);

    print(pixels,image.columns(),image.rows());

    return judge(is_connected(pixels, 2, {0,3,4})
        | is_connected(pixels, 1, {0,3})
        | is_connected(pixels, 5, {3,4}));
}

int main()
{
    ...
    auto i(cutoutDigit(image,fixes,Point(130+c*126, 130+r*156)));
    std::cout << recognition(i) << ' ';
    i.write("new。gif");
    ...
}

有没有其他解决方案我可以剪切图像并且图像不会改变?

4

0 回答 0