Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

documentation on pixel access #31

Open
ghost opened this issue Nov 28, 2015 · 2 comments
Open

documentation on pixel access #31

ghost opened this issue Nov 28, 2015 · 2 comments

Comments

@ghost
Copy link

ghost commented Nov 28, 2015

what is the syntax for getting the value of a pixel?

I have loaded an image and have tried:

width, length = myimage:size() --this works

but there does not appear to be a method in Image.lua that gives pixel access, does one need to be written? something like, Image:get_pixel(x,y) that returns a value and/or Image:get_pixels(x,y,w,h) that returns a table of values indexed by column, row.

or can MagickGetImagePixels be used?

@soumith
Copy link
Collaborator

soumith commented Nov 28, 2015

We really wrote this in the context of torch, and in that case getting a pixel is pretty direct (access the tensor location). If you want direct pixel access in Lua, you might have to interface a few functions

@ghost
Copy link
Author

ghost commented Nov 28, 2015

sorry I am a very basic developer, are you saying that I need to add a declaration to http://www.graphicsmagick.org/api/pixel_cache.html#acquireonepixel or http://www.graphicsmagick.org/api/pixel_cache.html#getonepixel ? is that done in the Image.lua file at the top between the [[ and ]]

i suppose I could use a series of methods, clone():crop():format():tostring() I could get one pixel as a string containing raw bytes...

I tried adding declarations for PixelPacket and the getOnePixel() but I don't really know what I am doing with that.

I did manage to add declarations for

double PixelGetRed( const PixelWand *wand );
double PixelGetGreen( const PixelWand *wand );
double PixelGetBlue( const PixelWand *wand );

and then wrote this function:

--get pixel(s)
function Image:getPixel(x, y)
  -- Create PixelWand:
   local pixelwand = ffi.gc(clib.NewPixelWand(), function(pixelwand)
      -- Collect:
      clib.DestroyPixelWand(pixelwand)
   end)
   local r,g,b
   r = clib.PixelGetRed(pixelwand)
   g = clib.PixelGetGreen(pixelwand)
   b = clib.PixelGetBlue(pixelwand)
    print(r)
   return r,g,b
end

but of course this doesn't return anything other than 0 because no x,y coordinate is set anywhere. it is probably better to use the pixelPacket approach anyway. not sure where to go from here...

can anyone help?
torch is not an option for my project but this library is mostly working for me, except this one thing. pixel access appears to be a basic feature in all the image libraries I've seen. could I request this feature to be added? it would probably be trivial for the maintainers or anyone familiar with declarations to add a method that interfaces with getonepixel, and one that interfaces with getpixels ( http://www.graphicsmagick.org/api/pixel_cache.html#getpixels ), this library already uses getImagePixels... in the toTensor() method... with this feature your library would be useful to many other users who are looking to switch from imagemagick in a lua environment...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant