存储 100,000 个的好方法是什么?整数(代表图像增量数据)?

发布于 2024-10-21 16:47:27 字数 511 浏览 2 评论 0原文

我搜索了论坛并看到了这个问题的一些可能的部分解决方案,但我希望有人帮助将它们放在一起。

我从相机获取帧并对当前帧和前一帧之间的差异进行图像处理。除了来自相机的 RGB 值之外,我还计算每个像素的色相和饱和度,每个像素也是一个 int。所以我的两个问题是:

  1. 存储每次调用didOutputSampleBuffer时的所有这些值的最佳方法是什么?从我读到的内容来看,似乎有这么多值, NSNumber 的开销将是显而易见的,因此使用经典的 C 风格整数数组(长度为 144 x 192 x 5(R,G ,B,H,S) = 138,240。这有意义吗?

  2. 如何将此数组放入 didOutputSampleBuffer 方法的范围内,因为我是在应用程序启动时初始化该数组,而不是在 didOutputSampleBuffer 方法中初始化该数组。论坛上有人提到也许我可以将数组包装在 NSMutableData 中,然后我可以将其存储为属性?

谢谢你的建议, 大学教师

I've searched the forum and seen some possible partial solutions to this question, but I'd like help putting it all together.

I'm getting the frames from the camera and doing image processing on the difference between the current frame and previous frame. In addition to the RGB values from the camera, I'm also calculating Hue and Saturation for each pixel, each of which is also an int. So my 2 questions are:

  1. What is the best way to store all of these values from each call to didOutputSampleBuffer? From what I've been reading, it seems like with this many values, the overhead from NSNumber will be noticable so least memory would be spent using a classic c-style array of ints w/ length 144 x 192 x 5(R,G,B,H,S) = 138,240. Does that make sense?

  2. How do I put this array in the scope of my didOutputSampleBuffer method, because I'm initializing the array upon app launch, not in the didOutputSampleBuffer method. Someone on the forum mentioned perhaps I could wrap the array in NSMutableData and then i could just store it as a property?

Thank you for your advice,
Don

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

你在看孤独的风景 2024-10-28 16:47:27

鉴于图像的大小不会改变,您应该能够创建一个缓冲区来将这些组件存储为交错字节或每个颜色组件平面的几个缓冲区。您可以通过手动使用 malloc()free() 创建此缓冲区并在完成后销毁它来完成此操作。

如果您更喜欢使用引用计数,您可以将这些字节包装在 NSData 实例中,这不会给您的处理增加太多开销。指向已处理缓冲区字节的指针或 NSData 实例都可以用作属性。

请注意,您可能希望为每个组件使用 unsigned char 类型,因为您只为每个颜色组件获取单独的字节。为什么要以不必要的精度浪费内存?

Given that the size of an image won't change, you should be able to create a buffer to store these components as interleaved bytes or a few buffers for each color component plane. You could do this by manually using malloc() and free() to create this buffer and destroy it when done.

If you'd prefer to use reference counting, you could wrap these bytes in an NSData instance, which won't add much overhead to your processing. Either a pointer to your processed buffer bytes or an NSData instance could be used as properties.

Note that you'll probably want to use unsigned char types for each component, because you're only getting back individual bytes for each of the color components. Why waste memory with unnecessary precision?

心房敞 2024-10-28 16:47:27
  1. 是的,这是存储数据的好方法。或者,您可以使用 ac 结构数组(请参阅我的示例)。
  2. 您可以使用全局变量或属性,其中包含 NSMutableData 对象或指向数组的指针。由于您希望以整数而不是原始数据的形式访问数据,因此存储指向数据的指针可能比 NSData 对象更容易。

例子:

// header file
struct PixelData {
    int r, g, b, h, s;
};
@interface TheClass : TheSuperclass {
    struct PixelData *dataPointer;
}
@property struct PixelData *dataPointer;
@end

// implementation file
@implementation TheClass
@synthesize dataPointer;
- (void)didOutputSampleBuffer { // Yes, I know this isn't the full name.
    // parse data
    // store data for pixel at index i:
    struct PixelData *dp = self.dataPointer;
    dp[i].r = r;
    dp[i].g = g;
    dp[i].b = b;
    dp[i].h = h;
    dp[i].s = s;
}
@end
  1. Yes, that is a good way to store the data. Alternatively, you could use a c array of structures (see my example).
  2. You could use a global variable or a property, containing either a NSMutableData object or the pointer to the array. Since you want access to the data as integers and not raw data, storing the pointer to the data would probably be easier than a NSData object.

Example:

// header file
struct PixelData {
    int r, g, b, h, s;
};
@interface TheClass : TheSuperclass {
    struct PixelData *dataPointer;
}
@property struct PixelData *dataPointer;
@end

// implementation file
@implementation TheClass
@synthesize dataPointer;
- (void)didOutputSampleBuffer { // Yes, I know this isn't the full name.
    // parse data
    // store data for pixel at index i:
    struct PixelData *dp = self.dataPointer;
    dp[i].r = r;
    dp[i].g = g;
    dp[i].b = b;
    dp[i].h = h;
    dp[i].s = s;
}
@end
年华零落成诗 2024-10-28 16:47:27

当我处理类似的问题时,我只是将 c 样式数组设置为对象上的 ivar。

这样我就可以向它附加附加属性,例如元数据等。

@interface MyObject : NSObject {

    int *arrayOfInts;
}

@property (readwrite) int *arrayOfInts;

@end

在这种情况下,您仍然必须显式管理内存。

When I was dealing with a similar problem I simply made the c style array an ivar on an object.

This way I could attach additional properties to it like metadata, etc.

@interface MyObject : NSObject {

    int *arrayOfInts;
}

@property (readwrite) int *arrayOfInts;

@end

You still have to explicitly manage the memory in this case.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文