Page 1 of 1

how to test the colour of a pixel?

Posted: Wed Mar 01, 2023 9:42 pm
by togFox
I'm dabbling into artificial intelligence more And more and many techniques is to 'scan ' an image and store pixel information into a 2D table then feed that table into a neural network.

If I draw/ display a graphic on the screen with Love 2D, how might I get RGB values for the pixel at coordinates (50, 50)?

Assume a random image was loaded and displayed with love.graphucs draw()

Re: how to test the colour of a pixel?

Posted: Wed Mar 01, 2023 10:29 pm
by pgimeno
Ironic that you're asking this when in a neighbouring thread someone is asking about love.graphics.captureScreenshot :)

ImageData is the type you need if you want to read pixels.

Re: how to test the colour of a pixel?

Posted: Wed Mar 01, 2023 10:38 pm
by dusoft
togFox wrote: Wed Mar 01, 2023 9:42 pm I'm dabbling into artificial intelligence more And more and many techniques is to 'scan ' an image and store pixel information into a 2D table then feed that table into a neural network.

If I draw/ display a graphic on the screen with Love 2D, how might I get RGB values for the pixel at coordinates (50, 50)?

Assume a random image was loaded and displayed with love.graphucs draw()

Code: Select all

mask = love.image.newImageData('assets/images/example.png')
local r, g, b, a = mask:getPixel(x, y)

Re: how to test the colour of a pixel?

Posted: Thu Mar 02, 2023 12:33 am
by BrotSagtMist
love.graphics.captureScreenshot( callback ) will turn your screen into image data passed to callback.
Your callback should then use https://love2d.org/wiki/ImageData:mapPixel to get the pixel values.
Callback can be a thread stack meaning that you can deciper the data on another thread while the game is running, thats so far the smartest solution.

Re: how to test the colour of a pixel?

Posted: Thu Mar 02, 2023 6:48 am
by togFox
Oh - that simple? I was expecting a far more complicated response!

Re: how to test the colour of a pixel?

Posted: Thu Mar 02, 2023 6:59 am
by zorg
togFox wrote: Thu Mar 02, 2023 6:48 am Oh - that simple? I was expecting a far more complicated response!
Well, if you do want to draw it out first, and then read the image back with captureScreenshot, for whatever convoluted reason, that will have a gigantic delay due to the data going this way: RAM -> VRAM -> RAM.

If you can just use ImageData and manipulate that, you save yourself that hassle.

Re: how to test the colour of a pixel?

Posted: Thu Mar 02, 2023 7:28 am
by togFox
Yeah - performance is something I'll need to check and ensure I don't scan too many pixels too quickly

Re: how to test the colour of a pixel?

Posted: Thu Mar 02, 2023 5:58 pm
by RNavega
An alternative to capturing a screenshot is using a render target, AKA the Canvas object.
Check the samples for how to use it, including its newImageData() function, comparable to captureScreenshot().