1

我正在尝试使用 ARtoolkit,但使用的是静态图像而不是视频流。我需要能够加载图像、识别标记并定位它们。我正在使用 SDL 加载图像。我能够从加载的图像中获取每个像素的 RGB 值,但我不确定如何格式化数据以供 ARToolkit 使用。

ARToolkit 将其图像存储为 ARUint8*(无符号字符*)类型。我对这种格式的工作方式感到困惑。现在我在主循环中有这段代码,它在程序执行时连续运行。此代码(应该)打印出帧中每个像素的 RGB 值。

ARUint8* dataPtr;
dataPtr = arVideoGetImage(); // Get a new frame from the webcam

int width, height;  
if (arVideoInqSize(&width, &height) == 0) // if width and height could be obtained
{
    for (int y = 0; y < height; y++)
    {
        for (int x = 0; x < width; x++)
        {
            printf("pixel %i, %i: %i, %i, %i\n", x, y, dataPtr[(y * 320) + x], dataPtr[(y * 320) + x + 1], dataPtr[(y * 320) + x + 2]);
        }
    }
}

典型输出:

pixel 5, 100: 0, 0, 0
pixel 6, 100: 178, 3, 0
pixel 7, 100: 0, 0, 177
etc...

它似乎可以正确访问 RGB 值,但我不确定如何将图像数据(从 SDL 格式)复制到这种新格式中。

4

1 回答 1

2

弄清楚了。发布答案以防其他人需要它。

在 Windows 上,ARToolkit 默认为 dataPtr 数组的 BGRA。以下函数将加载图像(使用 SDL)并返回指向 ARUint8(包含图像数据)的指针。

ARUint8* loadImage(char* filename, int* w, int* h)
{
    SDL_Surface* img = IMG_Load(filename);
    if (!img)
    {
        printf("Image '%s' failed to load. Error: %s\n", filename, IMG_GetError());
        return NULL;
    }
    *w = img->w; // Assign width and height to the given pointers
    *h = img->h;

    ARUint8* dataPtr = (ARUint8*)calloc(img->w * img->h * 4, sizeof(ARUint8)); // Allocate space for image data

    // Write image data to the dataPtr variable
    for (int y = 0; y < img->h; y++)
    {
        for (int x = 0; x < img->w; x++)
        {
            Uint8 r, g, b;
            SDL_GetRGB(getpixel(img, x, y), img->format, &r, &g, &b); // Get the RGB values
            int i = ((y * img->w) + x) * 4; // Figure out index in array
            dataPtr[i] = b;
            dataPtr[i + 1] = g;
            dataPtr[i + 2] = r;
            dataPtr[i + 3] = 0; // Alpha
        }
    }
    SDL_FreeSurface(img);
    return dataPtr;
}

getpixel 函数是从这里借用的:http: //sdl.beuc.net/sdl.wiki/Pixel_Access

这个功能让我可以使用照片而不是来自网络摄像头的视频。

于 2014-05-26T21:59:02.480 回答