how to test the colour of a pixel?

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
User avatar
togFox
Party member
Posts: 779
Joined: Sat Jan 30, 2021 9:46 am
Location: Brisbane, Oztralia

how to test the colour of a pixel?

Post 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()
Current project:
https://togfox.itch.io/backyard-gridiron-manager
American football manager/sim game - build and manage a roster and win season after season
User avatar
pgimeno
Party member
Posts: 3551
Joined: Sun Oct 18, 2015 2:58 pm

Re: how to test the colour of a pixel?

Post 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.
User avatar
dusoft
Party member
Posts: 510
Joined: Fri Nov 08, 2013 12:07 am
Location: Europe usually
Contact:

Re: how to test the colour of a pixel?

Post 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)
User avatar
BrotSagtMist
Party member
Posts: 614
Joined: Fri Aug 06, 2021 10:30 pm

Re: how to test the colour of a pixel?

Post 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.
obey
User avatar
togFox
Party member
Posts: 779
Joined: Sat Jan 30, 2021 9:46 am
Location: Brisbane, Oztralia

Re: how to test the colour of a pixel?

Post by togFox »

Oh - that simple? I was expecting a far more complicated response!
Current project:
https://togfox.itch.io/backyard-gridiron-manager
American football manager/sim game - build and manage a roster and win season after season
User avatar
zorg
Party member
Posts: 3444
Joined: Thu Dec 13, 2012 2:55 pm
Location: Absurdistan, Hungary
Contact:

Re: how to test the colour of a pixel?

Post 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.
Me and my stuff :3True Neutral Aspirant. Why, yes, i do indeed enjoy sarcastically correcting others when they make the most blatant of spelling mistakes. No bullying or trolling the innocent tho.
User avatar
togFox
Party member
Posts: 779
Joined: Sat Jan 30, 2021 9:46 am
Location: Brisbane, Oztralia

Re: how to test the colour of a pixel?

Post by togFox »

Yeah - performance is something I'll need to check and ensure I don't scan too many pixels too quickly
Current project:
https://togfox.itch.io/backyard-gridiron-manager
American football manager/sim game - build and manage a roster and win season after season
RNavega
Party member
Posts: 251
Joined: Sun Aug 16, 2020 1:28 pm

Re: how to test the colour of a pixel?

Post 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().
Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot], lzralbu and 57 guests