Converts a 1-bit, 4-bit, 8-bit, 16-bit, 24-bit, or 32-bit image to an 8-bit, 12-bit, or 16-bit grayscale image.
public class GrayscaleCommand : RasterCommand Public Class GrayscaleCommandInherits Leadtools.Imageprocessing.RasterCommandImplements Leadtools.Imageprocessing.IRasterCommand
public sealed class GrayscaleCommand : Leadtools.Imageprocessing.IRasterCommand @interface LTGrayscaleCommand : LTRasterCommand public class GrayscaleCommand extends RasterCommand function Leadtools.ImageProcessing.GrayscaleCommand() public ref class GrayscaleCommand : public Leadtools.Imageprocessing.RasterCommand, Leadtools.Imageprocessing.IRasterCommand Support for 12 and 16-bit grayscale images is available only in the Document/Medical Imaging editions.
The resulting image can be an 8-bit, 12-bit, or 16-bit grayscale image. Once the function is complete, the RasterImage.GrayscaleMode property will indicate the type of grayscale image.
When converting to 12-bit or 16-bit grayscale, the RasterImage.GetLookupTable is not used. When converting to 8-bit grayscale, the RasterImage.GetLookupTable is used to get the RGB for each input pixel. The grayscale value corresponding to that RGB triple is used in the destination image.
For more information, refer to Introduction to Image Processing With LEADTOOLS.
For more information, refer to Grayscale Images.
This example will load a 24-bit image then convert it to a grayscale image with 8, 12 and 16 bits/pixel
using Leadtools;using Leadtools.Codecs;using Leadtools.ImageProcessing;using LeadtoolsExamples.Common;public void GrayscaleCommandExample(){RasterCodecs codecs = new RasterCodecs();string srcFileName = Path.Combine(ImagesPath.Path, "Image1.cmp");string destFileName8 = Path.Combine(ImagesPath.Path, "Image1_grayscale8.jpg");string destFileName12 = Path.Combine(ImagesPath.Path, "Image1_grayscale12.jpg");string destFileName16 = Path.Combine(ImagesPath.Path, "Image1_grayscale16.jpg");// Load the source image from disk as 24-bits/pixelRasterImage image = codecs.Load(srcFileName, 24, CodecsLoadByteOrder.Bgr, 1, 1);// Check the grayscale modeConsole.WriteLine("Grsyscale mode of original image = {0}", image.GrayscaleMode);// Convert to 8-bit grayscaleGrayscaleCommand command = new GrayscaleCommand();command.BitsPerPixel = 8;command.Run(image);// Check the grayscale modeConsole.WriteLine("Grsyscale mode after grayscale command with 8 bpp = {0}", image.GrayscaleMode);// Save it to diskcodecs.Options.Jpeg.Save.QualityFactor = 2;codecs.Save(image, destFileName8, RasterImageFormat.Jpeg, 8);image.Dispose();// Load the image again this time grayscale to 12-bits/pixelimage = codecs.Load(srcFileName, 24, CodecsLoadByteOrder.Bgr, 1, 1);command.BitsPerPixel = 12;command.Run(image);Console.WriteLine("Grsyscale mode after grayscale command with 12 bpp = {0}", image.GrayscaleMode);codecs.Options.Jpeg.Save.QualityFactor = 2;codecs.Save(image, destFileName12, RasterImageFormat.Jpeg, 12);image.Dispose();// Load the image again this time grayscale to 16-bits/pixelimage = codecs.Load(srcFileName, 24, CodecsLoadByteOrder.Bgr, 1, 1);command.BitsPerPixel = 16;command.Run(image);Console.WriteLine("Grsyscale mode after grayscale command with 16 bpp = {0}", image.GrayscaleMode);// When saving 16-bit jpegs, you must use lossless quality factorcodecs.Options.Jpeg.Save.QualityFactor = 0;codecs.Save(image, destFileName16, RasterImageFormat.Jpeg, 16);image.Dispose();// Clean Upcodecs.Dispose();}
Imports LeadtoolsImports Leadtools.CodecsImports Leadtools.ImageProcessingPublic Sub GrayscaleCommandExample()Dim codecs As RasterCodecs = New RasterCodecs()Dim srcFileName As String = Path.Combine(LEAD_VARS.ImagesDir, "Image1.cmp")Dim destFileName8 As String = Path.Combine(LEAD_VARS.ImagesDir, "Image1_grayscale8.jpg")Dim destFileName12 As String = Path.Combine(LEAD_VARS.ImagesDir, "Image1_grayscale12.jpg")Dim destFileName16 As String = Path.Combine(LEAD_VARS.ImagesDir, "Image1_grayscale16.jpg")' Load the source image from disk as 24-bits/pixelDim image As RasterImage = codecs.Load(srcFileName, 24, CodecsLoadByteOrder.Bgr, 1, 1)' Check the grayscale modeConsole.WriteLine("Grsyscale mode of original image = {0}", image.GrayscaleMode)' Convert to 8-bit grayscaleDim command As GrayscaleCommand = New GrayscaleCommand()command.BitsPerPixel = 8command.Run(image)' Check the grayscale modeConsole.WriteLine("Grsyscale mode after grayscale command with 8 bpp = {0}", image.GrayscaleMode)' Save it to diskcodecs.Options.Jpeg.Save.QualityFactor = 2codecs.Save(image, destFileName8, RasterImageFormat.Jpeg, 8)image.Dispose()' Load the image again this time grayscale to 12-bits/pixelimage = codecs.Load(srcFileName, 24, CodecsLoadByteOrder.Bgr, 1, 1)command.BitsPerPixel = 12command.Run(image)Console.WriteLine("Grsyscale mode after grayscale command with 12 bpp = {0}", image.GrayscaleMode)codecs.Options.Jpeg.Save.QualityFactor = 2codecs.Save(image, destFileName12, RasterImageFormat.Jpeg, 12)image.Dispose()' Load the image again this time grayscale to 16-bits/pixelimage = codecs.Load(srcFileName, 24, CodecsLoadByteOrder.Bgr, 1, 1)command.BitsPerPixel = 16command.Run(image)Console.WriteLine("Grsyscale mode after grayscale command with 16 bpp = {0}", image.GrayscaleMode)' When saving 16-bit jpegs, you must use lossless quality factorcodecs.Options.Jpeg.Save.QualityFactor = 0codecs.Save(image, destFileName16, RasterImageFormat.Jpeg, 16)image.Dispose()' Clean Upimage.Dispose()End SubPublic NotInheritable Class LEAD_VARSPublic Const ImagesDir As String = "C:\Users\Public\Documents\LEADTOOLS Images"End Class
using Leadtools;using Leadtools.Codecs;using Leadtools.Examples;using Leadtools.ImageProcessing;using Leadtools.Windows.Media;public void GrayscaleCommandExample(RasterImage image, Stream destStream8, Stream destStream12, Stream destStream16){RasterCodecs codecs = new RasterCodecs();RasterImage temp = image.Clone();// Check the grayscale modeDebug.WriteLine("Grsyscale mode of original image = {0}", image.GrayscaleMode);// Convert to 8-bit grayscaleGrayscaleCommand command = new GrayscaleCommand();command.BitsPerPixel = 8;command.Run(image);// Check the grayscale modeDebug.WriteLine("Grsyscale mode after grayscale command with 8 bpp = {0}", image.GrayscaleMode);Debug.Assert(image.GrayscaleMode == RasterGrayscaleMode.OrderedInverse || image.GrayscaleMode == RasterGrayscaleMode.OrderedNormal);// Save it to diskcodecs.Options.Jpeg.Save.QualityFactor = 2;codecs.Save(image, destStream8, RasterImageFormat.Jpeg, 8);image.Dispose();// this time grayscale to 12-bits/pixelimage = temp.Clone();command.BitsPerPixel = 12;command.Run(image);Debug.WriteLine("Grsyscale mode after grayscale command with 12 bpp = {0}", image.GrayscaleMode);codecs.Options.Jpeg.Save.QualityFactor = 2;codecs.Save(image, destStream12, RasterImageFormat.Jpeg, 12);image.Dispose();// this time grayscale to 16-bits/pixelimage = temp.Clone();command.BitsPerPixel = 16;command.Run(image);Debug.WriteLine("Grsyscale mode after grayscale command with 16 bpp = {0}", image.GrayscaleMode);// When saving 16-bit jpegs, you must use lossless quality factorcodecs.Options.Jpeg.Save.QualityFactor = 0;codecs.Save(image, destStream16, RasterImageFormat.Jpeg, 16);image.Dispose();// Clean Uptemp.Dispose();}
Imports LeadtoolsImports Leadtools.CodecsImports Leadtools.ImageProcessingImports Leadtools.Windows.MediaPublic Sub GrayscaleCommandExample(ByVal image As RasterImage, ByVal destStream8 As Stream, ByVal destStream12 As Stream, ByVal destStream16 As Stream)Dim codecs As RasterCodecs = New RasterCodecs()Dim temp As RasterImage = image.Clone()' Check the grayscale modeDebug.WriteLine("Grsyscale mode of original image = {0}", image.GrayscaleMode)' Convert to 8-bit grayscaleDim command As GrayscaleCommand = New GrayscaleCommand()command.BitsPerPixel = 8command.Run(image)' Check the grayscale modeDebug.WriteLine("Grsyscale mode after grayscale command with 8 bpp = {0}", image.GrayscaleMode)Debug.Assert(image.GrayscaleMode = RasterGrayscaleMode.OrderedInverse OrElse image.GrayscaleMode = RasterGrayscaleMode.OrderedNormal)' Save it to diskcodecs.Options.Jpeg.Save.QualityFactor = 2codecs.Save(image, destStream8, RasterImageFormat.Jpeg, 8)image.Dispose()' this time grayscale to 12-bits/pixelimage = temp.Clone()command.BitsPerPixel = 12command.Run(image)Debug.WriteLine("Grsyscale mode after grayscale command with 12 bpp = {0}", image.GrayscaleMode)codecs.Options.Jpeg.Save.QualityFactor = 2codecs.Save(image, destStream12, RasterImageFormat.Jpeg, 12)image.Dispose()' this time grayscale to 16-bits/pixelimage = temp.Clone()command.BitsPerPixel = 16command.Run(image)Debug.WriteLine("Grsyscale mode after grayscale command with 16 bpp = {0}", image.GrayscaleMode)' When saving 16-bit jpegs, you must use lossless quality factorcodecs.Options.Jpeg.Save.QualityFactor = 0codecs.Save(image, destStream16, RasterImageFormat.Jpeg, 16)image.Dispose()' Clean Uptemp.Dispose()End Sub
Raster .NET | C API | C++ Class Library | JavaScript HTML5
Document .NET | C API | C++ Class Library | JavaScript HTML5
Medical .NET | C API | C++ Class Library | JavaScript HTML5
Medical Web Viewer .NET
