Image Processing Computer Science

Retinex Color Channels Demonstration

This Image Processing Reference section displays the code for an example program that demonstrates the retinex theory concepts of color contrast. The retinex color theory posits that, under various conditions, it is possible for people to perceive colors that are not even there.

In the example below, three images are generated from the original. Each generated image contains only white and one of the three fundamental image colors: red, green, and blue. Despite this, each of the red, green, and blue images appears to contain its complementary color: red(cyan), green(magenta), and blue(yellow). The effect is most pronounced in the red image, which appears to contain bright cyan, particularly in Aristole's clothing. The effect is less pronoounced in the blue image and still less in the green image. This is probably because our eyes are less sensitive to yellow and magenta than cyan.

RetinexChannels.php

<?php

  function CreateRetinexImage($qSourceImage) {
    $iW  = ImageSX($qSourceImage);
    $iH  = ImageSY($qSourceImage);
    // Create three new images: One for each color
    $qImageRedGray		= ImageCreateTrueColor($iW, $iH);
    $qImageGreenGray	= ImageCreateTrueColor($iW, $iH);
    $qImageBlueGray		= ImageCreateTrueColor($iW, $iH);
    // Create the images
    for ($j = 0; $j < $iH; ++$j) {
      for ($i = 0; $i < $iW; ++$i) {
		// Extract the color channels
        $qColor  = ImageColorAt($qSourceImage, $i, $j);
        $iRed    = (($qColor >> 16) & 0xFF);
        $iGreen  = (($qColor >> 8) & 0xFF);
        $iBlue   = ($qColor & 0xFF);
        $iColor = 0;
        // Alternate between the monochrome colors and white for each image
        if ((($i + $j) % 2) == 0) {
          $qColorRedGray		= ($iRed << 16);
          $qColorGreenGray		= ($iGreen << 8);
          $qColorBlueGray		= $iBlue;
        } else {
          if ($iBlue > $iGreen) {
            $qColorRedGray  = (($iBlue << 16) + ($iBlue << 8) + $iBlue);
          } else {
            $qColorRedGray  = (($iGreen << 16) + ($iGreen << 8) + $iGreen);
          }
          if ($iRed > $iBlue) {
            $qColorGreenGray = (($iRed << 16) + ($iRed << 8) + $iRed);
          } else {
            $qColorGreenGray = (($iBlue << 16) + ($iBlue << 8) + $iBlue);
          }
          if ($iRed > $iGreen) {
            $qColorBlueGray  = (($iRed << 16) + ($iRed << 8) + $iRed);
          } else {
            $qColorBlueGray  = (($iGreen << 16) + ($iGreen << 8) + $iGreen);
          }
        }
        ImageSetPixel($qImageRedGray, $i, $j, $qColorRedGray);
        ImageSetPixel($qImageGreenGray, $i, $j, $qColorGreenGray);
        ImageSetPixel($qImageBlueGray, $i, $j, $qColorBlueGray);
      }
    }
    // Create the image used to display the contrast images.
    $qDestImage    = ImageCreateTrueColor(2*$iW, 2*$iH);
    ImageCopyMerge($qDestImage, $qSourceImage, 0, 0, 0, 0, $iW, $iH, 100);
    ImageCopyMerge($qDestImage, $qImageRedGray, $iW, 0, 0, 0, $iW, $iH, 100);
    ImageCopyMerge($qDestImage, $qImageGreenGray, $iW, $iH, 0, 0, $iW, $iH, 100);
    ImageCopyMerge($qDestImage, $qImageBlueGray, 0, $iH, 0, 0, $iW, $iH, 100);
	// Deallocate the extra images that were created
    ImageDestroy($qImageRedGray);
    ImageDestroy($qImageGreenGray);
    ImageDestroy($qImageBlueGray);
    return $qDestImage;
  }

  $qOriginalImage = ImageCreateFromPng("TheSchoolOfAthens_Raphael_400x286.png");

  $qRetinexImage = CreateRetinexImage($qOriginalImage);

  header('Content-type: image/png');
  imagepng($qRetinexImage);
  imagedestroy($qOriginalImage);
  imagedestroy($qRetinexImage);
?>
 
 

Output

 
 

© 2007–2022 XoaX.net LLC. All rights reserved.