开发者

Seam Carving – Accessing pixel data in cocoa

I want to implement the seam carving algorithm by Avidan/Shamir. After the energy computing stage which can be implemented using a core image filter, I need to compute the seams with the lowest energy which can't be implemen开发者_StackOverflow社区ted as a core image filter for it uses dynamic programming (and you don't have access to previous computations in opengl shading language).

So i need a way to access the pixel data of an image efficiently in objective-c cocoa.

Pseudo code omitting boundary checks:

for y in 0..lines(image) do:
  for x in 0..columns(image) do:
    output[x][y] = value(image, x, y) + 
                     min{ output[x-1][y-1]; output[x][y-1]; output[x+1][y-1] }


The best way to get access to the pixel values for an image, is to create a CGBitmapContextRef with CGBitmapContextCreate. The important part about this is that when you create the context, you get to pass the pointer in that will be used as the backing store for the bitmap's data. Meaning that data will hold the pixel values and you can do what ever you want with them.

So the steps should be:

  1. Allocate a buffer with malloc or another suitable allocator.
  2. Pass that buffer as the first parameter to CGBitmapContextCreate.
  3. Draw your image into the returned CGBitmapContextRef.
  4. Release the context.
  5. Now you have your original data pointer that is filled with pixels in the format specified in the call to CGBitmapContextCreate.
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜