镜像一半图像C ++

时间:2018-10-02 12:51:36

标签: c++ sfml

所以我试图镜像一个图像,尽管只有一半,所以我的图像(斑马)只有两个has头或两个头,这真的没关系。 我设法用下面的代码对图片的一半进行灰度处理。 我应该如何镜像而不是将其变成灰色?

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;
}`

2 个答案:

答案 0 :(得分: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));
    }
}

答案 1 :(得分:0)

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();
    }
}

运行后,您将获得一个窗口,该窗口显示原始图像的左半部分,并在中间镜像:

Example Screenshot