Loading...
Logo
Processing Forum

Increase contrast of an image

in General Discussion  •  Other  •  1 year ago  
Hello, 

I have a few gray scale images which I display in my sketch. 
I would like to increase the contrast of the images. I know how to control hue/brightness, but I would like to somehow raise the contrast of the image. Like in the OpenCV library:  http://ubaa.net/shared/processing/opencv/opencv_contrast.html

Is there any library or way to do so? This would be great. Thanks for your help!!!

Replies(1)

this works for colour images as well and show how to iterate through the pixel values. you can modify this example to adjust hue or saturation if you want. please make sure you place an test.jpg image inside your data folder so this works.

Copy code

  1. PImage img;
  2. PImage imgEnhanced;

  3. void setup() 
  4. {
  5.   //make this big enough so you can see all of your image
  6.   size(200, 200);
  7.   
  8.   //things we only do once in the program should be here
  9.   //put a test.jpg image in a folder called data inside the sketch folder
  10.   img = loadImage("test.jpg");
  11.   imgEnhanced = new PImage(img.width, img.height);  
  12. }

  13. void draw() 
  14. {
  15.   background(0);
  16.   
  17.   //just use the mouse positions to work out
  18.   //contrast and brightness values
  19.   //up down is brightness
  20.   //left right is contrast
  21.   
  22.   //contrast - this probably doesn't need to be bigger than 5 or 10
  23.   float contrast = 5f * ( mouseX / (float)width); //value should go from 0 to 5
  24.   println("contrast: " + contrast);
  25.   
  26.   //brightness - this is additive and we want to make it both brigher and darker so -128 to +128
  27.   float bright = 255 * ( mouseY / (float)width  - 0.5); //value should go from -128 to +128
  28.   println("contrast: " + contrast);
  29.   
  30.   //call out contrast and brightness function
  31.   ContrastAndBrightness(img,imgEnhanced, contrast,bright); 
  32.   
  33.   //display the image
  34.   image(imgEnhanced, 0,0); 
  35. }

  36. //image processing function to enhance contrast
  37. //this doesn't make sense without also adjusting the brightness at the same time
  38. void ContrastAndBrightness(PImage input, PImage output,float cont,float bright)
  39. {
  40.    int w = input.width;
  41.    int h = input.height;
  42.    
  43.    //our assumption is the image sizes are the same
  44.    //so test this here and if it's not true just return with a warning
  45.    if(w != output.width || h != output.height)
  46.    {
  47.      println("error: image dimensions must agree");
  48.      return;
  49.    }
  50.    
  51.    //this is required before manipulating the image pixels directly
  52.    input.loadPixels();
  53.    output.loadPixels();
  54.       
  55.    //loop through all pixels in the image
  56.    for(int i = 0; i < w*h; i++)
  57.    {  
  58.        //get color values from the current pixel (which are stored as a list of type 'color')
  59.        color inColor = input.pixels[i];
  60.        
  61.        //slow version for illustration purposes - calling a function inside this loop
  62.        //is a big no no, it will be very slow, plust we need an extra cast
  63.        //as this loop is being called w * h times, that can be a million times or more!
  64.        //so comment this version and use the one below
  65.        int r = (int) red(input.pixels[i]);
  66.        int g = (int) green(input.pixels[i]);
  67.        int b = (int) blue(input.pixels[i]);
  68.        
  69.        //here the much faster version (uses bit-shifting) - uncomment to try
  70.        //int r = (inColor >> 16) & 0xFF; //like calling the function red(), but faster
  71.        //int g = (inColor >> 8) & 0xFF;
  72.        //int b = inColor & 0xFF;      
  73.        
  74.        //apply contrast (multiplcation) and brightness (addition)
  75.        r = (int)(r * cont + bright); //floating point aritmetic so convert back to int with a cast (i.e. '(int)');
  76.        g = (int)(g * cont + bright);
  77.        b = (int)(b * cont + bright);
  78.        
  79.        //slow but absolutely essential - check that we don't overflow (i.e. r,g and b must be in the range of 0 to 255)
  80.        //to explain: this nest two statements, sperately it would be r = r < 0 ? 0 : r; and r = r > 255 ? 255 : 0;
  81.        //you can also do this with if statements and it would do the same just take up more space
  82.        r = r < 0 ? 0 : r > 255 ? 255 : r;
  83.        g = g < 0 ? 0 : g > 255 ? 255 : g;
  84.        b = b < 0 ? 0 : b > 255 ? 255 : b;
  85.        
  86.        //and again in reverse for illustration - calling the color function is slow so use the bit-shifting version below
  87.        output.pixels[i] = color(r ,g,b);
  88.        //output.pixels[i]= 0xff000000 | (r << 16) | (g << 8) | b; //this does the same but faster
  89.    
  90.    }
  91.    
  92.    //so that we can display the new image we must call this for each image
  93.    input.updatePixels();
  94.    output.updatePixels();
  95. }