OpenCV: Pseudo Coloring

Download the Project folder and the source code [2.62 Mb].



**UPDATE**
Please Change the Code from:


ptr_d[3*x] = (int) (temp * 255/180);
to

ptr_d[3*x] = (int) (temp * 255/60);
in line 108, as show below. This improves the upper end of thermal image.





Sample Code:


//______________________________________________________________________________________
// OpenCV Pseudo Coloring of Images
// Author: Bharath Prabhuswamy
//______________________________________________________________________________________
//______________________________________________________________________________________

#include "cv.h"
#include "highgui.h"
#include <stdio.h>
#include <math.h>

int main()
{
        IplImage* img  = cvLoadImage("thermal.jpg");
 int flag;
 
 /* Always check if the program can find a file */
 if( !img ) 
   return -1;

 IplImage* gray = cvCreateImage( cvGetSize(img), 8, 1 );
 IplImage* disp = cvCreateImage( cvGetSize(img), 8, 3 );
 cvZero(disp);

 /*Convert the image to gray*/
 cvCvtColor(img,gray,CV_RGB2GRAY);

 uchar* ptr_d;
 uchar* ptr_g;
 int temp;

 printf("Select an option for Colouring -\n");
 printf("1.MRI Scan\n");
 printf("2.Thermal Image\n");
 scanf("%d",&flag);

 /*Check if the user selects a valid option */
 if(flag !=1 && flag !=2)
 {
  printf("Invalid Input\n");
  printf("Application Exit\n");
  return -1;
 }
  
 for( int y=0; y<gray->height; y++) 
 {
  ptr_g = (uchar*) (gray->imageData + y * gray->widthStep);
  ptr_d = (uchar*) (disp->imageData + y * disp->widthStep);
   
  for(int x=0; x<img->width; x++ ) 
  {
   /*The option is MRI */
   if(flag ==1)
   {
 //Colour pixels that Black and fade to Blue
    if(ptr_g[x] < 25)                          
    {
     temp = ptr_g[x];
     ptr_d[3*x+2] = 0;
     ptr_d[3*x+1] = 0;
     ptr_d[3*x] = (int) (temp * 255/25);
    }
    else
    {
     temp = ptr_g[x] - 25;
//Colour pixels that nearly Grey to Blue and then fade to Green 
     if(temp >=0 && temp < 115)     
                                        //
     {
      ptr_d[3*x+1] = (int) (temp * 255/115);
      ptr_d[3*x] = 255 -(int) (temp * 255/115);
     }
//Starting From Green,Colour pixels that nearly White to Red 
     else if(temp >=116 && temp <= 230)   
     {
      temp = ptr_g[x] - 115;
      ptr_d[3*x+2] = (int) (temp * 255/115);
      ptr_d[3*x+1] = 255 -(int) (temp * 255/115);
     }
    }
   }
   else
   {
//Colour pixels that Black and fade to Purple
    if(ptr_g[x] < 15)      
    {
     temp = ptr_g[x];
     ptr_d[3*x+2] = (int)(temp * 90/15);
     ptr_d[3*x+1] = 0;
     ptr_d[3*x] = (int)(temp * 90/15);
    }
    else
    {
     temp = ptr_g[x] - 15;
//Colour pixels that nearly Grey to Purple and fade to Yellow
     if(temp >=0 && temp < 180)   
     {
      ptr_d[3*x+2] = (int)(90 + (temp * 155/180));
      ptr_d[3*x+1] = (int)(temp * 255/180);
      ptr_d[3*x] = (int)(90 - (temp * 90/180));
     }
//Starting From Yellow,Colour pixels that nearly White to White itself 
     else if(temp >=180 && temp <= 240) 
     {
      temp = ptr_g[x] - 180;
      ptr_d[3*x+2] = 255;
      ptr_d[3*x+1] = 255;
      ptr_d[3*x] = (int) (temp * 255/60);
     }
    }

   }
  }
  
 }

 /* create a window to show the coloured image */
 cvNamedWindow( "Display", 1 );
 cvShowImage( "Display",disp);
 
 /* Save the coloured image if needed*/
 //cvSaveImage("output.jpg",disp);
 cvWaitKey(0);

 cvDestroyWindow( "Display" );
    cvReleaseImage( &img );

    return 0;
}




Images:


2 comments:

  1. Bharath, Good work on this. I'm trying to learn from your code.
    For the thermal image, how did you decide that the pixel values should be temp*155/180 or temp*90/15 .

    or are these generic thermal pseudo colouring values? if so do you have a source where you got these values from? thanks

    ReplyDelete
  2. The idea actually was to have WHITE for brighter regions of the thermal image.
    This was altered when writing the program.

    It happened because I did this by trail and error, trying to come out with a combination which would look familiar.

    ReplyDelete