Results 1 to 5 of 5
Like Tree1Likes
  • 1 Post By galneweinhaw

Thread: LookupOp not giving desired results.

  1. #1
    galneweinhaw is offline Member
    Join Date
    Aug 2013
    Posts
    2
    Rep Power
    0

    Question LookupOp not giving desired results.

    I have a grayscale BufferedImage (TYPE_BYTE_GRAY). I am trying to linearly "scale" the image from having gray values of 0-255 to min-max.

    where 0 <= min < max <= 255

    This is what I've attempted:

    my test uses grayMax = 100, grayMin = 0, src image is all white (255 everywhere). So I am expected a gray image with all values = 100

    Java Code:
    	public static BufferedImage applyCustomGrayscaleNormalization(BufferedImage src, int grayMax, int grayMin) {
    		int w = src.getWidth();
    		int h = src.getHeight();
    		BufferedImage dst = new BufferedImage(w, h, src.getType()); 
    
    		// create table for converting images from 0-255 to grayMin-grayMax
    		
    		// y = m*x + b linear transform
    		float b = grayMin;
    		float m = (grayMax - grayMin)/255.0f;
    		
    		short[] grayDownTable = new short[256];  
    		for (float i = 0; i < 256; i++) {
    			grayDownTable[(int) i] = (short) (m*i + b);  // <-- THE RESULTING TABLE IS AS EXPECTED
    		}	
    
    		
    		BufferedImageOp customGrayscaleOp = new LookupOp(new ShortLookupTable(0, grayDownTable), null);
    		
    		customGrayscaleOp.filter(src, dst);
    		
    		
    		// CHECK RESULTS
    		String temp = Integer.toHexString(dst.getRGB(0, 0)); //<---- temp = "ffa8a8a8" 
    		Color colorCheck = new Color(dst.getRGB(0, 0));
    		int grayCheck = colorCheck.getBlue(); //<---- graycheck = 168
    		
    		return dst;
    	}
    When I check the results of the filter, instead of getting what I thought should be ff646464, i get ffa8a8a8 which is a grayscale value of 168, instead of the 100 I was going for. I don't understand why this happens.

    Thanks for any help!

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,184
    Rep Power
    19

    Default Re: LookupOp not giving desired results.

    Moved from AWT/Swing

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  3. #3
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,184
    Rep Power
    19

    Default Re: LookupOp not giving desired results.

    I don't have any experience with LookupOp, but I would suggest going through the code of that class, which you can find in the src.zip archive in your JDK installation folder. With a bit of perseverance and a little luck, you should be able to figure out where your assumption of what it does differs from the way it was designed to work.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  4. #4
    galneweinhaw is offline Member
    Join Date
    Aug 2013
    Posts
    2
    Rep Power
    0

    Default Re: LookupOp not giving desired results.

    I believe I've discovered the problem, or at least a solution to avoid the problem.... so I wanted to report back and close this.

    The problem wasn't the LookupOp, it was "getRGB(...)" which does some sort of conversion of the raw values based on color spaces and stuff I don't understand.

    Instead of using getRGB, I needed to access the data buffer directly so I could compare apples to apples. This is what is working, in case it helps anyone stumbling upon this thread:

    Java Code:
        
    // CHECK RESULTS, should get 100!
        String temp = Integer.toHexString(dst.getRGB(0, 0)); //<---- temp = "ffa8a8a8"
        Color colorCheck = new Color(dst.getRGB(0, 0));
        int grayCheck = colorCheck.getBlue(); //<---- graycheck = 168
    
        // get the raw data in a 1D array
        byte[] pixels = ((DataBufferByte) dst.getRaster().getDataBuffer()).getData();
        
        grayCheck = pixels[0]; //<---- graycheck = 100, WOOT!
    DarrylBurke likes this.

  5. #5
    gimbal2 is online now Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    3,752
    Rep Power
    5

    Default Re: LookupOp not giving desired results.

    Note that directly accessing the databuffer likely makes it impossible for Java2D to optimize the usage of the image. That's fine for plenty of purposes, but beware of directly accessing the buffer when you are drawing those images you manipulate.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

Similar Threads

  1. Lucene Fuzzy Querying is not giving efficient results
    By samanthkumar.k in forum Lucene
    Replies: 0
    Last Post: 04-03-2013, 05:29 PM
  2. Not getting the desired row displayed
    By trollster in forum JDBC
    Replies: 1
    Last Post: 03-22-2013, 06:17 PM
  3. displaying a desired out put
    By Cubswin in forum New To Java
    Replies: 3
    Last Post: 03-13-2011, 06:05 AM
  4. scale transform giving inaccurate results
    By gib65 in forum AWT / Swing
    Replies: 5
    Last Post: 10-25-2010, 11:55 PM
  5. Replies: 0
    Last Post: 09-30-2010, 05:17 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •