半个镜像c++

mirror half an image c++

所以我试图镜像一个图像,虽然只有一半,所以我的图像(斑马)只有两个屁股或两个头,这真的无关紧要。 我设法用下面的代码对图片的一半进行了灰度化。 我应该怎么做才能镜像而不是变灰?

int main()
{
    sf::Image image;
    image.loadFromFile("../Images/zebra.bmp"); 
    sf::Color greyscale(0, 0, 0, 255);
    sf::Color newPixelColor;

    sf::RenderWindow window(sf::VideoMode(image.getSize().x, image.getSize().y), "Image manipulation");
    sf::Texture imageTexture;
    imageTexture.loadFromImage(image);

    sf::RectangleShape picture;

    picture.setSize(sf::Vector2f((float)image.getSize().x, (float)image.getSize().y));
    picture.setTexture(&imageTexture);

    sf::Event event;

    while (window.isOpen())
    {
        while (window.pollEvent(event))
        {
            if (event.type == sf::Event::Closed)
            {
                window.close();
            }
        }

        if (sf::Keyboard::isKeyPressed(sf::Keyboard::Space))
        {
            for (int x = 0; x < image.getSize().x; x++) {
                for (int y = 0; y < image.getSize().y; y++) {
                    if(x > (image.getSize().x) / 2) {
                        int newPixel = image.getPixel(x, y).r;
                        newPixel = image.getPixel(x, y).g;
                        newPixel = image.getPixel(x, y).b;

                        newPixelColor.r = newPixel;
                        newPixelColor.g = newPixel;
                        newPixelColor.b = newPixel;

                        image.setPixel(x, y, newPixelColor);
                    }
                }
            }
            imageTexture.loadFromImage(image);
        }
        window.clear();
        window.draw(picture);
        window.display();
    }
    return 0;
}`

对于那些想要答案的人来说。用 x 改变 y,用 y 改变 x,你就会得到我想要的!

int halfHeight = image.getSize().y / 2;

for (int y = 0; y < halfHeight; y++)
{
    for (int x = 0; x < image.getSize().x; x++)
    {
        image.setPixel(x, image.getSize().y - 1 - y, image.getPixel(x, y));
    }
}

SFML 并非真正用于图像处理或逐像素计算(外部着色器)。有更多优化的库,例如 OpenCV。

如果你想绘制图像的一部分,镜像它们等,你应该只依赖于使用 sf::Sprite 或(可能更好)sf::VertexArray。您可以简单地操纵纹理矩形或纹理坐标来镜像或调整您要显示的部分。

对于这个例子,我使用 this public domain image of a Zebra

我的示例代码是不言自明的,所以我认为它不需要很多额外的解释:

#include <SFML/Graphics.hpp>
#include <vector>

int main()
{
    sf::RenderWindow window({1280, 853}, "Zebra");
    sf::Texture texZebra;
    texZebra.loadFromFile("zebra.jpg");

    sf::VertexArray zebra(sf::Quads);

    // Every vertex is defined by a world coordinate, color, and texture coordinate

    // Define the first quad (left half)
    zebra.append({{0, 0},     sf::Color::White, {0, 0}});
    zebra.append({{640, 0},   sf::Color::White, {640, 0}});
    zebra.append({{640, 853}, sf::Color::White, {640, 853}});
    zebra.append({{0, 853},   sf::Color::White, {0, 853}});

    // Define the right quad (right half)
    zebra.append({{640, 0},     sf::Color::White, {640, 0}});
    zebra.append({{1280, 0},   sf::Color::White, {0, 0}});
    zebra.append({{1280, 853}, sf::Color::White, {0, 853}});
    zebra.append({{640, 853},   sf::Color::White, {640, 853}});

    while (window.isOpen()) {
        sf::Event event;
        while (window.pollEvent(event)) {
            if (event.type == sf::Event::Closed)
                window.close();
        }

        sf::RenderStates state;
        state.texture = &texZebra;
        window.draw(zebra, state);
        window.display();
    }
}

一旦 运行,您将得到 window 显示原始图像的左半部分,在中间镜像: