CImg - 如何转换交错的原始数据?

时间:2018-01-12 17:16:50

标签: cimg

我刚开始使用CImg库,我想用原始数据测试它,但是我遇到了很大的问题,弄清楚为什么我无法获得正确的结果。

我的RGB24原始文件格式为RGB交错(RGBRGBRGB ...),而CImg按照here的说明存储数据。

我写的(最后)测试代码就是这个,我收集了所有可能的4个! permute_axes 方法

的组合= 24
#ifndef _USE_MATH_DEFINES
#define _USE_MATH_DEFINES
#endif

#ifndef _GNU_SOURCE
#define _GNU_SOURCE
#endif

#ifdef cimg_display
#undef cimg_display
#define cimg_display 0
#endif

#include "CImg.h"

#include <iostream>
#include <iomanip>
#include <sstream>
#include <cstdint>
#include <cstdlib>
#include <thread>
#include <vector>
#include <array>
#include <cmath>

#include <unistd.h>
#include <sys/file.h>
#include <fcntl.h>

using namespace std;
using namespace cimg_library;

#define MODE_700 (mode_t) S_IRWXU // 00700
#define MODE_400 (mode_t) S_IRUSR // 00400
#define MODE_200 (mode_t) S_IWUSR // 00200
#define MODE_100 (mode_t) S_IXUSR // 00100
#define MODE_070 (mode_t) S_IRWXG // 00070
#define MODE_040 (mode_t) S_IRGRP // 00040
#define MODE_020 (mode_t) S_IWGRP // 00020
#define MODE_010 (mode_t) S_IXGRP // 00010
#define MODE_007 (mode_t) S_IRWXO // 00007
#define MODE_004 (mode_t) S_IROTH // 00004
#define MODE_002 (mode_t) S_IWOTH // 00002
#define MODE_001 (mode_t) S_IXOTH // 00001

#define MODE_777 (mode_t) MODE_700 | MODE_070 | MODE_007

int main(int argc, const char *argv[]) 
{      
    //uint8_t buffer[960 * 1280 * 3];
    CImg <uint8_t> image(960, 1280, 1, 3);
    // CImg <float> gaussk(7,7,3); 
    uint8_t buffer[960 * 1280 * 3] = {0};
    float var = 1.5*1.5;
    // int32_t xtmp;
    // int32_t ytmp;
    int32_t fd = -1;

    stringstream sstr;  

    cout << "image size is: " << unsigned(image.size()) << endl;
    printf("opening %s\n", argv[1]);
    fd = open(argv[1], O_RDONLY);
    if(fd == -1)
    {
        perror("open: ");
        return -1;
    }

    cout << "Reading..." << endl;
    // read(fd, image.data(), image.size());
    read(fd, buffer, sizeof(buffer));
    close(fd);
    fd = -1;

    for(uint8_t i = 0; i < 24; i++)
    {
        sstr << "blurred_" << unsigned(i) << ".raw";
        image.assign(buffer, 960, 1280, 1, 3);

        switch(i)
        {
            case 0:
                image.permute_axes("xyzc");
                break;
            case 1:
                image.permute_axes("xycz");
                break;
            case 2:
                image.permute_axes("xzyc");
                break;
            case 3:
                image.permute_axes("xzcy");
                break;
            case 4:
                image.permute_axes("xcyz");
                break;
            case 5:
                image.permute_axes("xczy");
                break;
            case 6:
                image.permute_axes("yxzc");
                break;
            case 7:
                image.permute_axes("yxcz");
                break;
            case 8:
                image.permute_axes("yzxc");
                break;
            case 9:
                image.permute_axes("yzcx");
                break;
            case 10:
                image.permute_axes("ycxz");
                break;
            case 11:
                image.permute_axes("yczx");
                break;
            case 12:
                image.permute_axes("zxyc");
                break;
            case 13:
                image.permute_axes("zxcy");
                break;
            case 14:
                image.permute_axes("zyxc");
                break;
            case 15:
                image.permute_axes("zycx");
                break;
            case 16:
                image.permute_axes("zcxy");
                break;
            case 17:
                image.permute_axes("zcyx");
                break;
            case 18:
                image.permute_axes("cxyz");
                break;
            case 19:
                image.permute_axes("cxzy");
                break;
            case 20:
                image.permute_axes("cyxz");
                break;
            case 21:
                image.permute_axes("cyzx");
                break;
            case 22:
                image.permute_axes("czxy");
                break;
            case 23:
                image.permute_axes("czyx");
                break;
        }
        image.blur(2.5);
        // image.save("blurred.bmp");
        cout << "Writing " << sstr.str() << endl;
        fd = open(sstr.str().c_str(), O_RDWR | O_CREAT, MODE_777);
        write(fd, image.data(), image.size());
        close(fd);
        sstr.str("");
    }

    return 0;
}

但是输出模糊帧是错误的,大部分时间都是灰度级的。

根据头文件 CImg.h ,这种转换应该是可能的,这也是我在阅读this博客文章后所理解的内容。

在我开始编写自己的函数以尝试解决此问题之前,处理原始文件并使用CImg进行此类转换的正确方法是什么?

1 个答案:

答案 0 :(得分:3)

所以你有交错数据,并希望它非交错,能够让CImg函数与它一起使用。

那么为什么你没有完全按照重要

所做的博客文章的建议
CImg result(dataPointer, spectrum, width, height, depth, false);
result.permute_axes("yzcx");

代码

image.assign(buffer, 3, 960, 1280, 1); // note the 3 being first
image.permute_axes("yzcx");
image.blur(2.5);
// image.save("blurred.bmp");
//now convert back to interleaved
image.permute_axes("cxyz");
fd = open(sstr.str().c_str(), O_RDWR | O_CREAT, MODE_777);
write(fd, image.data(), image.size());