LEADTOOLS Support
Imaging
Imaging SDK Examples
HOW-TO: Remove a Specific Color From an Image
#1
Posted
:
Friday, November 3, 2017 3:16:22 PM(UTC)
Groups: Registered, Tech Support, Administrators
Posts: 163
Was thanked: 9 time(s) in 9 post(s)
Just a quick note this is a follow-up to a previous forum post in which dropping a specific color from an image was covered.
Using CMYK plane separation to remove colors from an image
This post builds on that to demonstrate how other colours can be reduced to be removed in the same step by using the BalanceColorsCommand.
BalanceColorsCommandHere's the input image being processed, with text in various colours. The previous example demonstrated dropping the black text. Here, the red text at the bottom will be reduced to the black plane as well so it is also dropped as part of the operation.
This syntax applies the BalanceColorsCommand with the parameters for red to have no weight, but green and blue to have standard weight.
Code:
RasterCodecs codecs = new RasterCodecs();
RasterImage image = codecs.Load(imagePath);
BalanceColorsCommand bcc = new BalanceColorsCommand(
new BalanceColorCommandFactor(0, 0, 0), // red
new BalanceColorCommandFactor(0, 1, 0), // green
new BalanceColorCommandFactor(0, 0, 1)); // blue
bcc.Run(image);
Note effectively removing the red tints the remaining image cyan.
The same procedure can be used to separate the color planes, then using the AddCommand to recombine them. However, given the cyan plane is effectively an inverse of the black plane, it needs to be removed prior to performing the AddOperation.
Code: ColorSeparateCommand csc = new ColorSeparateCommand(ColorSeparateCommandType.Cmyk);
csc.Run(image);
csc.DestinationImage.RemovePageAt(4); // page 4 is black plane
csc.DestinationImage.RemovePageAt(1); // page 1 is cyan plane
AddCommand ac = new AddCommand(AddCommandType.Add);
ac.Run(csc.DestinationImage);
InvertCommand ic = new InvertCommand();
ic.Run(ac.DestinationImage);
AutoBinarizeCommand abc = new AutoBinarizeCommand();
abc.Run(ac.DestinationImage);
Here's the output final output with the black text and red text subsequently removed programatically and ready for OCR.
Similar programming techniques can be used for other colours as well, however, ones which don't have a direct colour complement that gets split into its own plane may require additional processing.
Edited by user Friday, June 1, 2018 11:56:40 AM(UTC)
| Reason: Updating links to v20
Nick Crook
Developer Support Engineer
LEAD Technologies, Inc.
LEADTOOLS Support
Imaging
Imaging SDK Examples
HOW-TO: Remove a Specific Color From an Image
You cannot post new topics in this forum.
You cannot reply to topics in this forum.
You cannot delete your posts in this forum.
You cannot edit your posts in this forum.
You cannot create polls in this forum.
You cannot vote in polls in this forum.