Color finding question. [Java]

Discussion in 'Programming General' started by Dynamic [Dynamic], Aug 17, 2008.

Color finding question. [Java]
  1. Unread #1 - Aug 17, 2008 at 12:49 AM
  2. Dynamic [Dynamic]
    Joined:
    Aug 16, 2008
    Posts:
    27
    Referrals:
    0
    Sythe Gold:
    0

    Dynamic [Dynamic] Member

    Color finding question. [Java]

    Well I need a color fining method, a quick one. :p It also must be able to go to other colors, so let's say the first color it finds is at point 2, 2. And I want it to find the one at 100, 100. It must find point 2, 2, but then find other points like 100, 100.

    I made a method myself, but it's made slow. So that's why I'm asking for help. :nuts:

    Here's the method if you want to see it:
    Code:
    import java.awt.AWTException;
    import java.awt.Color;
    import java.awt.Dimension;
    import java.awt.Robot;
    import java.awt.Toolkit;
    
    
    public class FindColor {
    
    	public static int MaxHeight;
    	public static int MaxWidth;
    	public static int CurrentHeight;
    	public static int CurrentWidth;
    	public static long startTime = System.currentTimeMillis();
    	public static boolean foundColor;
    	public static Color acolor;
    	public static Robot robot;
    
    	public static void main(String[] args) throws AWTException {
    		robot = new Robot();
    		Dimension dimension = Toolkit.getDefaultToolkit().getScreenSize();
    		MaxHeight = dimension.height;
    		MaxWidth = dimension.width;
    		acolor = new Color(0, 84, 227);
    		while (!foundColor) {
    			Search_Screen();
    		}
    	}
    
    	public static void Search_Screen() {
    		if (!robot.getPixelColor(CurrentHeight, CurrentWidth).equals(acolor)) {
    			if (CurrentHeight < MaxHeight) {
    				CurrentHeight++;
    				return;
    			} else if (CurrentHeight >= MaxHeight) {
    				CurrentHeight = 0;
    				if (CurrentWidth < MaxWidth) {
    					CurrentWidth++;
    				}
    				return;
    			}
    			System.out.println("Color not found!");
    			System.out.println(totalTime());
    		} else {
    			robot.mouseMove(CurrentHeight, CurrentWidth);
    			foundColor = true;
    			System.out.println("Color found!");
    			System.out.println(totalTime());
    		}
    	}
    
    	public static String totalTime() {
    		long millis = System.currentTimeMillis() - startTime;
    		long hours = millis / 3600000;
    		millis -= hours * 3600000;
    		long minutes = millis / 60000;
    		millis -= minutes * 60000;
    		long seconds = millis / 1000;
    		return ("" + hours + ":" + minutes + ":" + seconds);
    	}
    
    }
    Took 20 seconds to look over at all the colors on my computer screen... Now that's slow. :(
     
  3. Unread #2 - Aug 17, 2008 at 11:44 AM
  4. robotzindisguise
    Joined:
    Oct 8, 2007
    Posts:
    1,067
    Referrals:
    5
    Sythe Gold:
    1

    robotzindisguise Guru

    Color finding question. [Java]

    This code will scan the entire screen for the color, which is overkill.
    You should scan only the part of the computer screen where the color will occur.
    If your system's screen resolution is say 1080x800, you are searching approx 900,000 pixels. The RuneScape screen consists of 170k pixels.
    To scan only the RuneScape screen would still be overkill, if you want code that executes fast, consider the SIZE and SHAPE of the object you're looking for, not just color, and the area of the screen you expect it to appear in. Say you are looking for a small square object that's 5x5 pixels, you can speed up your algoritm by scanning only 1 in every 5 pixels, in BOTH the x and y axis, and scanning only the area of interest.
    To find the 5x5 square using this technique, would involve scanning at most just 170 pixels, and it's no exaggeration, would give the same result over 5000 times quicker than the code above.
     
  5. Unread #3 - Aug 17, 2008 at 7:10 PM
  6. Dynamic [Dynamic]
    Joined:
    Aug 16, 2008
    Posts:
    27
    Referrals:
    0
    Sythe Gold:
    0

    Dynamic [Dynamic] Member

    Color finding question. [Java]

    Wow. I'd never thought of talking to you. Your my hero. =0

    Your tip is VERY good. Glad I am getting advice from an expert color bot maker. 5000x faster is very satisfying, and this could even be better than just searching for each square.

    Thanks very much.
     
  7. Unread #4 - Aug 17, 2008 at 9:03 PM
  8. SuF
    Joined:
    Jan 21, 2007
    Posts:
    14,212
    Referrals:
    28
    Sythe Gold:
    1,234
    Discord Unique ID:
    203283096668340224
    <3 n4n0 Two Factor Authentication User Community Participant Spam Forum Participant Sythe's 10th Anniversary

    SuF Legend
    Pirate Retired Global Moderator

    Color finding question. [Java]

    Wow robot, thats very smart... a little jump into how your bots are soo good D:..



    Also get pixel color is slow... if you take a screenshot of the screen then search that it is faster i hear... search it up on here and you might find an example. D:
     
  9. Unread #5 - Aug 18, 2008 at 3:03 AM
  10. Swan
    Joined:
    Jan 23, 2007
    Posts:
    4,957
    Referrals:
    0
    Sythe Gold:
    0
    Sythe's 10th Anniversary Member of the Month Winner

    Swan When They Cry...
    Retired Global Moderator

    Color finding question. [Java]

    I'm unsure how this would be done in java, but whenever I've been searching for a colour or colours, I generally get a picture of the screen or target area and load it in to a byte array of RGB values. Maybe you could give that a bit of thought.
     
  11. Unread #6 - Aug 18, 2008 at 7:33 AM
  12. Dynamic [Dynamic]
    Joined:
    Aug 16, 2008
    Posts:
    27
    Referrals:
    0
    Sythe Gold:
    0

    Dynamic [Dynamic] Member

    Color finding question. [Java]

    ScreenCapture? Yeah, I've seen that being made, but I don't know how to use it and I'm not sure it goes past the first color it finds... Theres an example on google, I just have to go fish it out.
     
  13. Unread #7 - Aug 18, 2008 at 11:11 AM
  14. robotzindisguise
    Joined:
    Oct 8, 2007
    Posts:
    1,067
    Referrals:
    5
    Sythe Gold:
    1

    robotzindisguise Guru

    Color finding question. [Java]

    No problem.
    I'm not sure if it would be much quicker, but if you want to save the on-screen image into memory and scan it instead, with Java you can use the BufferedImage object:
    Code:
    // determine current screen size
    Toolkit toolkit = Toolkit.getDefaultToolkit();
    Dimension screenSize = toolkit.getScreenSize();
    Rectangle screenRect = new Rectangle(screenSize);
    // save on screen image into memory
    BufferedImage image = bot.createScreenCapture(screenRect);
    or to just capture part of the screen:
    Code:
    // capture 500x500 rectangle, with top-left coordinate, (0,0)
    Robot robot = new Robot();
    Rectangle captureSize = new Rectangle(0, 0, 500, 500);
    BufferedImage image = robot.createScreenCapture(captureSize);

    To get it to go beyond the first color, what I'd do is decide the maximum number of colors you want to search for, say 10(the number you use should depend on how many you are expecting or hoping to find) , change your search from a void method into a method that scans more than one pixel at a time, the whole target area, and returns an array of Point objects. This way you save on memory too, by using local variables to store the current position of your scan, instead of globals. Inside the method, create an array of Point objects and use an integer to count how many Points you've found containing that color so far. Then replace the foundColor = true block of code, with something like
    Code:
    points[numColors] = new Point(currentWidth,currentHeight);
    numColors++;
    if(numColors>=10) return points;
    
     
  15. Unread #8 - Aug 18, 2008 at 4:38 PM
  16. SuF
    Joined:
    Jan 21, 2007
    Posts:
    14,212
    Referrals:
    28
    Sythe Gold:
    1,234
    Discord Unique ID:
    203283096668340224
    <3 n4n0 Two Factor Authentication User Community Participant Spam Forum Participant Sythe's 10th Anniversary

    SuF Legend
    Pirate Retired Global Moderator

    Color finding question. [Java]

    Yes, using the capture screen way is noticibly faster than using getpixel color. Let me look around for my color finding method I made which you could use to help ya.



    EDT: I found like 5 that are only half done... I must have lost my completed one... :(
     
  17. Unread #9 - Aug 19, 2008 at 7:26 AM
  18. robotzindisguise
    Joined:
    Oct 8, 2007
    Posts:
    1,067
    Referrals:
    5
    Sythe Gold:
    1

    robotzindisguise Guru

    Color finding question. [Java]

    Performing say 170 getPixelColors should be faster than the screen capture operation alone (in standard mode, the part of the RS Applet containing the player window, is 170k pixels). Even though getting colors from a screencapture is faster, on the whole performing an intelligent scan should be the best option, depending on the kind of size/shape of object and the size of the area you are scanning. It's a trade-off.

    It would be a slightly wasteful use of memory, but definitely would save you time if you were doing a more brute-force pixel search.
     
  19. Unread #10 - Aug 19, 2008 at 7:45 AM
  20. SuF
    Joined:
    Jan 21, 2007
    Posts:
    14,212
    Referrals:
    28
    Sythe Gold:
    1,234
    Discord Unique ID:
    203283096668340224
    <3 n4n0 Two Factor Authentication User Community Participant Spam Forum Participant Sythe's 10th Anniversary

    SuF Legend
    Pirate Retired Global Moderator

    Color finding question. [Java]

    Well, if you grab a small screen shot of only a small part of rs that you are looking it, it might be faster, only if you pick and choose where to grab it.
     
  21. Unread #11 - Aug 19, 2008 at 3:20 PM
  22. robotzindisguise
    Joined:
    Oct 8, 2007
    Posts:
    1,067
    Referrals:
    5
    Sythe Gold:
    1

    robotzindisguise Guru

    Color finding question. [Java]

    You are right.
    If you use BufferedImage, then use Color c = new Color(image.getRGB(x,y)), it in-lines the color codes, and is actually a bit faster.
    You definitely still want to streamline the scan, because it has a more dramatic effect on speed. The screen capture approach in most cases should be about 10-30% quicker if you take an image of JUST the area of the screen you are interested in (taking the whole screen will be slower, in most cases). Just experimented with it, and on my machine, using an economical screen capture together with an economical scan, is faster, except in cases where you can reduce the number of pixels in the area scanned to, 1 in every 50, or so (that's like 1 every 10 px in x-axis, and 1 every 5 in y axis), or below, in which case it's faster to avoid screen capture.
     
  23. Unread #12 - Aug 19, 2008 at 5:35 PM
  24. Dynamic [Dynamic]
    Joined:
    Aug 16, 2008
    Posts:
    27
    Referrals:
    0
    Sythe Gold:
    0

    Dynamic [Dynamic] Member

    Color finding question. [Java]

    Yeah, I got around with your idea, the bot is finding cows perfectly. Using a method that searches 10x10. It's less than 1 second... Anyways, with your idea I also made a cluster, so it searches for a certain cow color, then it searches for other cow colors by that cow color, if they are there it goes ahead and moves the mouse over there...

    Now I just have to make mouse splines, inCombat detection, randoms(pain in the fucking anus). And a nice sexy GUI...
     
  25. Unread #13 - Aug 19, 2008 at 5:39 PM
  26. SuF
    Joined:
    Jan 21, 2007
    Posts:
    14,212
    Referrals:
    28
    Sythe Gold:
    1,234
    Discord Unique ID:
    203283096668340224
    <3 n4n0 Two Factor Authentication User Community Participant Spam Forum Participant Sythe's 10th Anniversary

    SuF Legend
    Pirate Retired Global Moderator

    Color finding question. [Java]

    Randoms can be easy to do, if you notice something very specific about the random... If you don't have somthing speficif and are searching for a picture constantly it sucks.... Incombat is easy... either look for the health bar above your head or the thing on the HUD.
     
  27. Unread #14 - Aug 19, 2008 at 7:35 PM
  28. Dynamic [Dynamic]
    Joined:
    Aug 16, 2008
    Posts:
    27
    Referrals:
    0
    Sythe Gold:
    0

    Dynamic [Dynamic] Member

    Color finding question. [Java]

    Randoms are easy, but painful. I wish I had members so I could make a members bot...

    HP Bar sucks, it's very slow... What is the HUD you are talking about?

    Oh, lol. I can get all my randoms from SRL and convert them to Java.

    But anyone know how to make your own L&F or Theme of a GUI, like RID has it's own and so does AutoFighter.org AutoFighter.
     
  29. Unread #15 - Aug 19, 2008 at 9:13 PM
  30. SuF
    Joined:
    Jan 21, 2007
    Posts:
    14,212
    Referrals:
    28
    Sythe Gold:
    1,234
    Discord Unique ID:
    203283096668340224
    <3 n4n0 Two Factor Authentication User Community Participant Spam Forum Participant Sythe's 10th Anniversary

    SuF Legend
    Pirate Retired Global Moderator

    Color finding question. [Java]

    Well that thing that tells you your hp in the hud... could be used to tell if you are being hurt.... D: Or figure out about how much hp you have like 10%
     
  31. Unread #16 - Aug 19, 2008 at 10:04 PM
  32. Morphis
    Joined:
    Aug 2, 2008
    Posts:
    734
    Referrals:
    0
    Sythe Gold:
    0

    Morphis Apprentice
    Banned

    Color finding question. [Java]

    SuF is talking about the little orb full of red liquid on runescape in the top right corner of the screen. Just make it search that orb for signs of yellow which is the text color when you get low health, the lowest is red though. So just have it if it does not find it sleep or do nothing, else eat.
     
  33. Unread #17 - Aug 19, 2008 at 10:05 PM
  34. Dynamic [Dynamic]
    Joined:
    Aug 16, 2008
    Posts:
    27
    Referrals:
    0
    Sythe Gold:
    0

    Dynamic [Dynamic] Member

    Color finding question. [Java]

    Yeah, I'll actually make a color animation detector, much faster. Randoms I was thinking use a UserNameOnScreen? Things like School and Maze should be easy, I'll just make a simple mini map color reader, if it's like mostly gray, it's Maze, if it has a pattern of desks, it's school...
     
  35. Unread #18 - Aug 19, 2008 at 10:53 PM
  36. Dynamic [Dynamic]
    Joined:
    Aug 16, 2008
    Posts:
    27
    Referrals:
    0
    Sythe Gold:
    0

    Dynamic [Dynamic] Member

    Color finding question. [Java]

    OK, here is some speed results, I am debugging. Basically searches my whole screen with different pixel skips and stuff.

    1x1:
    So it searches my whole screen in 23 seconds, not so good...

    2x2:
    6 seconds! That's 3 times faster. Much faster for just a skip of 2 pixels! But we can do faster.

    4x4:
    Wow! 2 seconds, barely noticeable. This thing is the bomb.

    8x8:
    Yeah... 1 seconds, that is our goal. That is the whole screen in 1 second, imagine just searching the runescape applet... The possibilities.
     
  37. Unread #19 - Aug 20, 2008 at 8:57 PM
  38. SuF
    Joined:
    Jan 21, 2007
    Posts:
    14,212
    Referrals:
    28
    Sythe Gold:
    1,234
    Discord Unique ID:
    203283096668340224
    <3 n4n0 Two Factor Authentication User Community Participant Spam Forum Participant Sythe's 10th Anniversary

    SuF Legend
    Pirate Retired Global Moderator

    Color finding question. [Java]

    Is the updated code above?

    I say never search the entire thing. Search the game area, your inventory, the map, the conversaion area or parts of them... searching the entire thing is a waste.
     
  39. Unread #20 - Aug 20, 2008 at 10:34 PM
  40. Dynamic [Dynamic]
    Joined:
    Aug 16, 2008
    Posts:
    27
    Referrals:
    0
    Sythe Gold:
    0

    Dynamic [Dynamic] Member

    Color finding question. [Java]

    Well I need a function that find the applet... I was thinking making it through color so it'd be kind of cool, the bot finds the applet by itself and stuff... Having huge trouble with this bad boy, but I'll be finished with it soon.
     
< How hard would a program like this be [If possible] | The Annoyer Version 1 >

Users viewing this thread
1 guest


 
 
Adblock breaks this site