c++ - 垂直翻转图像

标签 c++ image-processing

从 openGL 检索缓冲区后,我正在尝试垂直翻转图像。它似乎使用以下代码输出不正确的图像:

const int width = 100;
const int height = width;
const int components = 3;
unsigned char pixels[width * height * components];
glReadPixels(0, 0, width, height, GL_RGB, GL_UNSIGNED_BYTE, pixels);
unsigned char flipPixels[width * height * components];
for (int i = 0; i < width; ++i) {
    for (int j = 0; j < height; ++j) {
        for (int k = 0; k < components; ++k) {
            flipPixels[i + j * width + k] = pixels[(height) * (width) - ((j+1) * width) + i + k];
        }
    }
}

我知道我只能迭代一半的高度并达到同样的效果,但我想通过遍历图像的完整高度来实现它。我似乎无法弄清楚代码有什么问题。任何帮助将不胜感激。

最佳答案

我不确定图像是如何存储的,但是你的索引 ik 被赋予了相同的步幅,这是可疑的。也许您需要 i * componentsj * width * components。之后,垂直反转你只需要将 j 更改为 (height - j - 1)

flipPixels[(i + j * width) * components + k] = pixels[(i + (height - 1 - j) * width) * components + k];

关于c++ - 垂直翻转图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26706036/

相关文章:

c++ - 在 MFC dll 中包含 boost mutex 头文件时断言

c++ - 在 VS2010 项目中更改输出目录不起作用

image - Abbyy Finereader 命令行用法/python 用法?

c++ - 我无法使用 VS2010 运行 openCV2.3.1,因为找不到 opencv_core231d.dll

python - 通过 keras 正确 reshape MNIST 图像

c++ - 获取时间c++

c++ - 我可以使用 std::pair,但重命名 .first 和 .second 成员名称吗?

Java - JNA 和共享库,在 Linux 上从 .jar 启动时出现 UnsatisfiedLinkError

image-processing - 使用 Hadoop MapReduce 进行图像处理

python - 值错误 : cannot reshape array of size 230 into shape (3, 600,800)