【问题标题】:Flip image vertically in C++ without external libraries like OpenCV在没有像 OpenCV 这样的外部库的情况下在 C++ 中垂直翻转图像
【发布时间】:2023-02-09 03:39:05
【问题描述】:

我有以下接收图像为 void* 的函数。如何在不使用 OpenCV 等外部库的情况下将其颠倒过来。图像的宽度和高度是已知的。

注意:此函数在 Android 上每秒至少调用 30 次,因此需要高效。

PushVideoFrame(void *bytes, int width, int height) {
    if (clientPtr == nullptr) {
        return ErrorCodes::DEVICE_CONNECTION;
    }

     char* data = static_cast< char *>(bytes);

     //////// CODE TO FLIP IMAGE /////////////

    clientPtr->PushVideoFrameAsync(data, width * height * 4)
}

【问题讨论】:

  • 您看过用于翻转的 OpenGL ES 着色器代码吗?我还会检查翻转是否可以作为图像渲染管道的一部分完成(我假设使用 OpenGL ES)。
  • 如果不知道图像数据的编码,这似乎无法回答。例如,它是交错的还是平面的?它以某种方式压缩了吗? ETC
  • 你想要一个嵌套循环复制像素吗?因为你说你不想要图书馆推荐。向我们展示您的尝试。这是预料之中的。 tourHow to Askminimal reproducible example

标签: android c++ image-processing android-ndk


【解决方案1】:

您可以使用额外的内存交换行:

void flipVertically(uint8_t *data, int width, int height) {
    vector<uint8_t> temp(width);
    for (int i = 0; i < height / 2; i++) {
        void *ptr1 = data + i*width;
        void *ptr2 = data + (height-i-1)*width;
        
        // swap(row1, row2) with temp buffer
        memcpy(&temp[0], ptr1, width);
        memcpy(ptr1, ptr2, width);
        memcpy(ptr2, &temp[0], width);
    }
}

【讨论】:

    猜你喜欢
    • 2018-12-30
    • 1970-01-01
    • 1970-01-01
    • 2018-01-19
    • 2011-07-23
    • 2014-04-28
    • 2021-12-27
    • 2011-05-18
    • 2013-02-28
    相关资源
    最近更新 更多