Analyzing captured image

I need to search in an image for the number and position of pixels of a certain color.
As the screen changes quickly and I need to make sure that I get a consistent state, I thought of first capturing the image and then analyze it for the pixels of the certain color.

The color matching part - with some pre-captured test image - was easy:
set width to imageSize(testImage).x
	
// loop over whole width of the (first line) of testImage, to find points of the relevant TestColor.
	repeat width-1 times
		set point to (repeatIndex(),1) 
		set currentPointColor to imageColorAtLocation(testImage,point)
		
		if currentPointColor equals TestColor then
			insert point.x into PointList
		end if
	end repeat
This works great, if testImage is an image I have already in the suite.

But how do I get an up-to-date image to analyze? If I capture it with CapureScreen, I'm not able to work with it afterwards, as something like
set testImage to file (ImagePath & "xyz.tiff") // load image xyz...
fails with STFileException File /xzy.tiff couldn't be read using the defaultStringEncoding (UTF8)

How can I access such a dynamically created image?

Or should I use some standard search functionality of Eggplant instead? It is important that I can provide the pixel color as value, not as an pre-captured image...

Thanks,
Andre

Comments

  • SenseTalkDougSenseTalkDoug ForumAdmin admin
    I would suggest you use the colorAtLocation() function to read the colors directly from a region of the screen. So your code might look something like this:
    -- locate the topLeft and bottomRight corners of area to search
    set topLeft to imageLocation("topLeft")
    set bottomRight to imageLocation("bottomRight")
    
    -- check each pixel in the search area for matching colors
    repeat with y=topLeft.y to bottomRight.y
      repeat with x = topLeft.x to bottomRight.x
        if colorAtLocation(x,y) equals TestColor then
          insert (x,y) into PointList
        end if
      end repeat
    end repeat
    
    Will that do what you want?
  • anvoianvoi Member
    Doug,

    Yes, that's basically what I want to do.
    As the image on the SuT screen rapidly changes, I fear that I get already some changed content for analyzing the last pixels compared to the content I used to analyze first place (it's approx. 600-800 pixels I need to compare). This would lead to an inconsistent result. That's why I thought capturing the image and analyze afterwards would be the better solution -- but can't get that to work.

    Thanks,
    Andre
  • JonathanOSXJonathanOSX ForumAdmin admin
    For the captured image approach don't try to accessing it as a file container. Just use the path.
    capturescreen (name:"/tmp/MyScreenShot", rect:(0,0,100,100))
    put imageColorAtLocation("/tmp/MyScreenShot.tiff", (50,50))
    
  • anvoianvoi Member
    That was too easy... it works fine... thank you!
    Andre :D
Sign In or Register to comment.