Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Is this possible?

edited April 2014 in Questions Posts: 22

Is it possible to find the x,y coordinates of a certain color on the screen? Or a perhaps a group with the same color and find the center of that? I tried using image:get(), but it only gets 1 pixel, so that doesn't work. Can anyone help me? Thanks in advance.

Tagged:

Comments

  • Posts: 723

    You could could use image:get() on each pixel I turn by using a couple of loops.

    for x=1,WIDTH do
    for y=1,HEIGHT do
    --do your pixel check
    end
    end
    
  • dave1707dave1707 Mod
    Posts: 7,514

    @Leviathan Actually r,g,b,a=image:get(x,y) does work. You just have to go thru all of the x,y values for that image to find the color you want.

  • Posts: 22

    @dave1707 Really? Wow, that's cool. But when I put Width/2 and Height/2 for height. It doesn't work. It only reads that pixel. So I'm still confused on what your saying. Can you give an example?

  • dave1707dave1707 Mod
    Posts: 7,514

    @Leviathan Here an example. This will print only 100 color values because there could be thousands.


    function setup() img=readImage("Planet Cute:Character Boy") w=img.width -- width of image h=img.height -- height of image count=0 -- set count to 0 for x=1,w do -- loop for width for y=1,h do -- loop for height r,g,b,a=img:get(x,y) -- get pixel color if r>0 and g>0 and b>0 and a>0 then -- check value above 0 print(r,g,b,a) count=count+1 if count>100 then -- only print 100 values break end end end if count>100 then -- only do 100 times break end end end function draw() background(40, 40, 50) sprite(img,WIDTH/2,HEIGHT/2) -- show image end
  • Posts: 22

    @dave1707 hey thank you! I like how you used the break tool. Forgot about that in the old lua programmer on ios. Sweet thank you! This all makes sense.

  • Posts: 1,976

    You could also add a third parameter to skip over a few pixels in between, good for speeding things up. Using image.get can be very slow and make a noticable difference if you use it enough.


    function setup() img=readImage("Planet Cute:Character Boy") w=img.width -- width of image h=img.height -- height of image count=0 -- set count to 0 skip = 5 -- New line: how many pixels to jump at a time in the for loops for x=1,w,skip do -- loop for width, notice the new third parameter for y=1,h,skip do -- loop for height, notice the new third parameter r,g,b,a=img:get(x,y) -- get pixel color if r>0 and g>0 and b>0 and a>0 then -- check value above 0 print(r,g,b,a) count=count+1 if count>100 then -- only print 100 values break end end end if count>100 then -- only do 100 times break end end end function draw() background(40, 40, 50) sprite(img,WIDTH/2,HEIGHT/2) -- show image end
  • IgnatzIgnatz Mod
    Posts: 5,396

    @Leviathan - if you want to find the whole filled area, you can use SkyTheCoder's suggestion to find the first pixel (assuming the colour only occurs in one place of course), and then use a recursive loop to find all the neighbouring cells with the same colour. I used one in this post (see the recursion part at bottom)

    https://coolcodea.wordpress.com/2013/03/30/18-lessons-from-a-simple-board-game-part-1/

    (NB in the code, Wordpress mangles > signs into &gt and < into &lt)

    (It can be done more efficiently using a table, but I can't find my code for that right now).

  • Posts: 22

    @Ignatz I'll take a look at that, thank you!

Sign In or Register to comment.