Converting grayscale - RGB to other colours

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Sodrohu
    New Member
    • Jun 2009
    • 2

    Converting grayscale - RGB to other colours

    Currently I got a thermal image mapping program which converts grayscale pictures to RGB. Now, what I need to do is to convert those 3 RGB colors into other three colours. The choice of colours are white, black, red, green and blue.

    To explain more, for the thermal mapping using grayscale converted to RGB, red denotes hot areas, green-zero, and blue-cold. What I want to do is to make a program that enables you to choose the colours from the five I mentioned above to represent the hot, zero and cold areas. This is the program, using Microsoft visual Studio C++ .

    This program reads from the text file map24 and puts it in the RGB variable.
    Code:
    int readmappingFile()
    {
    	FILE *fp;	
    	int i;
    
    	fp = fopen("C:\\map24.txt", "r");
    
    	if(fp != NULL)
    	{
    		for(i=0; i<256; i++)
    		{
    			fscanf(fp, "%d %d %d", &R[i], &G[i], &B[i]);
    		}
    	}	
    	
    	return OK;
    }
    This one deals with the RGB conversion.

    Code:
    int temperatureMappingCore()
    {
    	byte index;
    	int w, h;
    	int gWStep, cWStep;
    	
    	gWStep = inImg->widthStep;
    	cWStep = outImg->widthStep;
    	
    	for(h=0; h<inImg->height; h++)
    	{
    		for(w=0; w<inImg->width; w++)
    		{
    			index = (byte)inImg->imageData[h*gWStep + w];			
    			outImg->imageData[h*cWStep + w*3] = B[index];
    			outImg->imageData[h*cWStep + w*3 + 1] = G[index];
    			outImg->imageData[h*cWStep + w*3 + 2] = R[index];
    		}		
    	}		
    
    	return OK;
    }
    Can anyone help?
  • newb16
    Contributor
    • Jul 2008
    • 687

    #2
    If I understood you, given three colors ( c1, c2, c3 ) you want to map grayscale to RGB so that for grayscale pixel with value (g) in range [0..127] resulting rgb value is between (color1) and (color2) for each component, where r1 is red component of color c1:
    R = r1+ (r2-r1)*g/128 and the same for G and B components, and for g in range [128..255]: R = r2+(r3-r2)*(g-128)/128. Then you can either generate the mapping file for 256 values of grayscale or convert in on the fly.

    Comment

    • Sodrohu
      New Member
      • Jun 2009
      • 2

      #3
      Uh, not really. But I've solved that one already so thanks.

      Comment

      Working...