←Select platform

RemapHueCommand Class

Summary
Uses a lookup table to change an image's hue values. The saturation and value tables change S and V values only if a particular hue value is marked as non-zero in the Mask property. It is used for all resolutions, including 48 and 64-bit images.
Syntax
C#
Objective-C
C++/CLI
Java
Python
public class RemapHueCommand : RasterCommand 
@interface LTRemapHueCommand : LTRasterCommand 
public class RemapHueCommand 
    extends RasterCommand 
public ref class RemapHueCommand : public RasterCommand   
class RemapHueCommand(RasterCommand): 
Remarks
  • This command can be used to change a range of colors to another range of colors. For example, it could be used to change all red pixels to any color, where a red pixel can have any brightness (V) and any amount of white (saturation). A red pixel in this case would be RGB(x,y,y) where 0 <= x <(LookUpTableLength - 1) and 0 <= y < x or in the HSV color space HSV(0,x,x) where 0<=x<=(LookUpTableLength - 1)
  • The Mask lookup table identifies which values in the HueTable are valid. If a Mask value is 0, then the corresponding value in the HueTable is ignored. If a Mask value is non-zero, then the corresponding value in the HueTable is used. For example, if a pixel has a hue value of 240 and Mask[240] is nonzero, then the hue value of 240 is replaced with HueTable[240]. Traditionally, hue ranges from 0 to 359. For the lookup table, the range of 0 to 359 is remapped to a range of 0.. LookUpTableLength - 1. For example, if LookUpTableLength = 256
  • This command supports 48 and 64-bit colored images.
  • This command does not support signed data images.
  • This command does not support 32-bit grayscale images.

For more information, refer to Changing Brightness and Contrast.

Remap Hue Function - Before

Remap Hue Function - Before

Remap Hue Function - After

Remap Hue Function - After

View additional platform support for this Remap Hue function.

Example

Run the RemapHueCommand on an image and change all green hues (and hues near green).

C#
Java
using Leadtools; 
using Leadtools.Codecs; 
using Leadtools.ImageProcessing.Color; 
 
 
public int INCREMENT_S1(int x, int Length) 
{ 
   return ((x + 1) % Length); 
} 
 
public int DECREMENT_S1(int x, int Length) 
{ 
   return ((x + (Length - 1)) % Length); 
} 
 
public int ADD_S1(int x, int y, int Length) 
{ 
   return ((x + y) % Length); 
} 
 
public void RemapHueCommandCommandExample() 
{ 
   // Load an image 
   RasterCodecs codecs = new RasterCodecs(); 
   codecs.ThrowExceptionsOnInvalidImages = true; 
 
   RasterImage image = codecs.Load(Path.Combine(LEAD_VARS.ImagesDir, "cannon.jpg")); 
 
   // Prepare the command 
   int[] MaskTable; 
   int[] HueTable; 
   RasterHsvColor hsvRef; 
   int HueGreen, HueChange; 
   int Change; 
   int i, Count; 
   int Length; 
 
   if (image.BitsPerPixel >= 48) 
      Length = 0x10000; 
   else if (!(image.BitsPerPixel == 16 || image.BitsPerPixel == 12)) 
      Length = 256; 
   else if (image.GetLookupTable() != null && image.UseLookupTable) 
      Length = 256; 
   else 
      Length = (1 << image.BitsPerPixel); 
 
   //Allocate tables 
   MaskTable = new int[Length]; 
   HueTable = new int[Length]; 
 
   //Initialize tables 
   for (i = 0; i < Length; i++) 
   { 
      MaskTable[i] = 0; 
      HueTable[i] = i; 
   } 
 
   //Get the hue for green 
   hsvRef = RasterHsvColor.FromRasterColor(new RasterColor(0, 255, 0)); 
 
   HueGreen = hsvRef.H; 
 
   //Obtain new hue   
   hsvRef = RasterHsvColor.FromRasterColor(new RasterColor(255, 128, 0)); 
   Change = (int)hsvRef.H - (int)HueGreen; 
   HueChange = (Change > 0) ? (int)Change : (int)(Change + Length - 1); 
   HueGreen *= (Length - 1) / 255; 
   HueChange *= (Length - 1) / 255; 
 
   //Set values in HueTable, MaskTable  
   HueTable[HueGreen] = (HueTable[HueGreen] + HueChange); 
   MaskTable[HueGreen] = 1; 
 
   //set the hues near green (+/- 15) 
   Count = (15 * (Length - 1)) / 255; 
   for (i = INCREMENT_S1(HueGreen, Length); Count > 0; i = INCREMENT_S1(i, Length), Count--) 
   { 
      HueTable[i] = ADD_S1(HueTable[i], HueChange, Length); 
      MaskTable[i] = 1; 
   } 
 
   Count = (15 * (Length - 1)) / 255; 
   for (i = DECREMENT_S1(HueGreen, Length); Count > 0; i = DECREMENT_S1(i, Length), Count--) 
   { 
      HueTable[i] = ADD_S1(HueTable[i], HueChange, Length); 
      MaskTable[i] = 1; 
   } 
 
   RemapHueCommand command = new RemapHueCommand(MaskTable, HueTable, null, null, Length); 
   command.Run(image); 
   codecs.Save(image, Path.Combine(LEAD_VARS.ImagesDir, "Result.jpg"), RasterImageFormat.Jpeg, 24); 
 
} 
 
static class LEAD_VARS 
{ 
   public const string ImagesDir = @"C:\LEADTOOLS23\Resources\Images"; 
} 
 
import static org.junit.Assert.assertTrue; 
 
import java.io.File; 
import java.io.IOException; 
 
import org.junit.*; 
import org.junit.runner.JUnitCore; 
import org.junit.runner.Result; 
import org.junit.runner.notification.Failure; 
 
import leadtools.*; 
import leadtools.codecs.*; 
import leadtools.imageprocessing.color.*; 
 
 
public int INCREMENT_S1(int x, int Length) { 
    return ((x + 1) % Length); 
} 
 
public int DECREMENT_S1(int x, int Length) { 
    return ((x + (Length - 1)) % Length); 
} 
 
public int ADD_S1(int x, int y, int Length) { 
    return ((x + y) % Length); 
} 
 
public void remapHueCommandCommandExample() { 
    final String LEAD_VARS_IMAGES_DIR = "C:\\LEADTOOLS23\\Resources\\Images"; 
 
    // Load an image 
    RasterCodecs codecs = new RasterCodecs(); 
    codecs.setThrowExceptionsOnInvalidImages(true); 
 
    RasterImage image = codecs.load(combine(LEAD_VARS_IMAGES_DIR, "cannon.jpg")); 
 
    // Prepare the command 
    int length; 
    if (image.getBitsPerPixel() >= 48) 
        length = 0x10000; 
    else if (!(image.getBitsPerPixel() == 16 || image.getBitsPerPixel() == 12)) 
        length = 256; 
    else if (image.getLookupTable() != null && image.getUseLookupTable()) 
        length = 256; 
    else 
        length = (1 << image.getBitsPerPixel()); 
 
    // Allocate tables 
    int[] maskTable = new int[length]; 
    int[] hueTable = new int[length]; 
 
    // Initialize tables 
    for (int i = 0; i < length; i++) { 
        maskTable[i] = 0; 
        hueTable[i] = i; 
    } 
 
    // Get the hue for green 
    RasterHsvColor hsvRef = RasterHsvColor.fromRasterColor(new RasterColor(0, 255, 0)); 
    int hueGreen = hsvRef.getH(); 
 
    // Obtain new hue 
    hsvRef = RasterHsvColor.fromRasterColor(new RasterColor(255, 128, 0)); 
    int change = (int) hsvRef.getH() - (int) hueGreen; 
    int hueChange = (change > 0) ? (int) change : (int) (change + length - 1); 
    hueGreen *= (length - 1) / 255; 
    hueChange *= (length - 1) / 255; 
 
    // Set values in HueTable, MaskTable 
    hueTable[hueGreen] = (hueTable[hueGreen] + hueChange); 
    maskTable[hueGreen] = 1; 
 
    // Set the hues near green (+/- 15) 
    int count = (15 * (length - 1)) / 255; 
    for (int i = INCREMENT_S1(hueGreen, length); count > 0; i = INCREMENT_S1(i, length), count--) { 
        hueTable[i] = ADD_S1(hueTable[i], hueChange, length); 
        maskTable[i] = 1; 
    } 
 
    count = (15 * (length - 1)) / 255; 
    for (int i = DECREMENT_S1(hueGreen, length); count > 0; i = DECREMENT_S1(i, length), count--) { 
        hueTable[i] = ADD_S1(hueTable[i], hueChange, length); 
        maskTable[i] = 1; 
    } 
 
    RemapHueCommand command = new RemapHueCommand(); 
    command.setMask(maskTable); 
    command.setHueTable(hueTable); 
    command.setSaturationTable(null); 
    command.setValueTable(null); 
    command.setLookUpTableLength(length); 
    command.run(image); 
    codecs.save(image, combine(LEAD_VARS_IMAGES_DIR, "Result.jpg"), RasterImageFormat.JPEG, 24); 
 
    System.out.println("Command run and image saved to " + combine(LEAD_VARS_IMAGES_DIR, "Result.jpg")); 
    assertTrue("Command run and image saved", (new File(combine(LEAD_VARS_IMAGES_DIR, "Result.jpg")).exists())); 
} 
Requirements

Target Platforms

Help Version 23.0.2024.3.3
Products | Support | Contact Us | Intellectual Property Notices
© 1991-2024 LEAD Technologies, Inc. All Rights Reserved.

Leadtools.ImageProcessing.Color Assembly

Products | Support | Contact Us | Intellectual Property Notices
© 1991-2023 LEAD Technologies, Inc. All Rights Reserved.