By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,034 Members | 820 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,034 IT Pros & Developers. It's quick & easy.

Convert image from RGB to LMS

P: 8
I need to convert an image from RGB format to LMS format using matrix's and an array of double to hold the LMS values as they're created. I need to take each pixel (R, G, and B) and and multiply it by the combined matrix shown below. Any ideas? I also know that I need to convert the byte value: (val + 256) % 256.

{0.3811,0.5783,0.0402}
{0.1967,0.7244,0.0782}
{0.0241,0.1288,0.8444}

Thanks for your help!
Dec 5 '06 #1
Share this Question
Share on Google+
1 Reply


P: 8
Here's more info to help with understanding what I'm doing. It's the same thing from above with with some added stuff to it. Thanks again for your help!


I need to convert an image from RGB format to LMS format using matrix's and an array of double to hold the LMS values as they're created. I need to take each pixel (R, G, and B) and and multiply it by the combined matrix shown below. Any ideas? I also know that I need to convert the byte value: (val + 256) % 256.

{0.3811,0.5783,0.0402}
{0.1967,0.7244,0.0782}
{0.0241,0.1288,0.8444}

Then, after I convert it to RGB, I need to go back to LMS. I need to create an array of byte to hold the RGB values as they are created. Take each pixel (L, M, and S) and multiply it by combined matrix as shown below. I have to remember to clamp the value between 0 and 255 before assigning into RGB which is a byte array.

{4.4679, -3.5873, 0.1193}
{-1.2186, 2.3809, -0.1624)
{0.0497, -0.2439, 1.2045)

Here's some of my code that I've already written:

Expand|Select|Wrap|Line Numbers
  1. public static void main(String args[]) throws IOException
  2.         {
  3.         DataInputStream rd = new DataInputStream(System.in);   
  4.         char c1=(char)rd.readUnsignedByte();
  5.         char c2=(char)rd.readUnsignedByte();
  6.         int width=getNumber(rd);
  7.         int height=getNumber(rd);
  8.         int colorValue=getNumber(rd);
  9.         System.out.println("P6" + " " + width + " "+ height + " 255");
  10.         byte image[] = new byte[width*height*3];
  11.         double lms[] = new double[width*height*3];
  12.         rd.read(image);
  13.  
  14.         double A[][] = {{0.3811,0.5783,0.0402},{0.1967,0.7244,0.0782},{0.0241,0.1288,0.8444}};
  15.         double D[][]=new double[3][1];
  16.         double V[][]=new double[3][1];
  17.  
  18.         for(int i=0; i < image.length; i = i+3) 
  19.             {
  20.             D[0][0] = image[i];
  21.             D[1][0] = image[i+1];
  22.             D[2][0] = image[i+2];
  23.             V = mult(A, D);
  24.             lms[i] = V[0][0];
  25.             lms[i+1] = V[1][0];
  26.             lms[i+2] = V[2][0];
  27.             }
  28.  
  29.         System.out.print(V);
  30.     }
  31.  public static double [][] mult(double A[][], double D[][])
  32.         {
  33.         double V[][] = new double[A.length][D[0].length];
  34.         for(int i = 0; i<A.length; i++)
  35.             {
  36.             for(int j = 0; j<D[0].length; j++)
  37.                 {
  38.                 for(int k=0; k<A[0].length; k++)
  39.                     {
  40.                     V[i][j] = V[i][j] + A[i][k]*D[k][j];
  41.                     }
  42.                 }
  43.             }
  44.         return V;
  45.         }
  46.  
  47. }
  48.  
Thanks for your help!
Dec 5 '06 #2

Post your reply

Sign in to post your reply or Sign up for a free account.