LEADTOOLS(Leadtoolsアセンブリ)

フレームを表示

GrayscaleCommandクラス








メンバ 
8ビットであるか、12ビットか、16ビットのグレースケールimageへの1-、4-、8-、16-、24-または32ビットimageを変換します。
オブジェクトモデル
構文
public class GrayscaleCommand : RasterCommand, IRasterCommand  
'Declaration
 
Public Class GrayscaleCommand 
   Inherits RasterCommand
   Implements IRasterCommand 
'Usage
 
Dim instance As GrayscaleCommand
public sealed class GrayscaleCommand : IRasterCommand  
@interface LTGrayscaleCommand : LTRasterCommand
public class GrayscaleCommand extends RasterCommand
function Leadtools.ImageProcessing.GrayscaleCommand()
public ref class GrayscaleCommand : public RasterCommand, IRasterCommand  
解説

12と16ビットグレースケールイメージのサポートは、Document/Medical画像化版だけで利用できます。

結果として生じるimageは、8ビットであるか、12ビットか、16ビットグレースケールimageでありえます。一度関数が完了していたら、RasterImage.GrayscaleModeプロパティはグレースケールimageの型を示します。

12ビットまたは16ビットグレースケールに変換するとき、RasterImage.GetLookupTableが使われません。8ビットグレースケールに変換するとき、RasterImage.GetLookupTableは入力ピクセルごとにRGBを取得するのに用いられます。そのRGBトリプルと一致しているグレースケール値が、デスティネーションimageで使われます。

詳細については、「LEADTOOLSによる画像処理入門」を参照してください。

詳細については、「グレースケールイメージ」を参照してください。

サンプル

このサンプルは24ビットimageをロードして、そして8つ、12と、16のビット/ピクセルで、グレースケールimageにそれを変換します

Copy Code  
Imports Leadtools
Imports Leadtools.Codecs
Imports Leadtools.ImageProcessing

      
Public 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/pixel
   Dim image As RasterImage = codecs.Load(srcFileName, 24, CodecsLoadByteOrder.Bgr, 1, 1)

   ' Check the grayscale mode
   Console.WriteLine("Grsyscale mode of original image = {0}", image.GrayscaleMode)

   ' Convert to 8-bit grayscale
   Dim command As GrayscaleCommand = New GrayscaleCommand()
   command.BitsPerPixel = 8
   command.Run(image)

   ' Check the grayscale mode
   Console.WriteLine("Grsyscale mode after grayscale command with 8 bpp = {0}", image.GrayscaleMode)

   ' Save it to disk

   codecs.Options.Jpeg.Save.QualityFactor = 2
   codecs.Save(image, destFileName8, RasterImageFormat.Jpeg, 8)
   image.Dispose()

   ' Load the image again this time grayscale to 12-bits/pixel
   image = 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/pixel
   image = 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 factor
   codecs.Options.Jpeg.Save.QualityFactor = 0
   codecs.Save(image, destFileName16, RasterImageFormat.Jpeg, 16)
   image.Dispose()

   ' Clean Up
   image.Dispose()
End Sub

Public NotInheritable Class LEAD_VARS
Public Const ImagesDir As String = "C:\Users\Public\Documents\LEADTOOLS Images"
End Class
using Leadtools;
using Leadtools.Codecs;
using Leadtools.ImageProcessing;

      
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/pixel
   RasterImage image = codecs.Load(srcFileName, 24, CodecsLoadByteOrder.Bgr, 1, 1);

   // Check the grayscale mode
   Console.WriteLine("Grsyscale mode of original image = {0}", image.GrayscaleMode);

   // Convert to 8-bit grayscale
   GrayscaleCommand command = new GrayscaleCommand();
   command.BitsPerPixel = 8;
   command.Run(image);

   // Check the grayscale mode
   Console.WriteLine("Grsyscale mode after grayscale command with 8 bpp = {0}", image.GrayscaleMode);

   // Save it to disk

   codecs.Options.Jpeg.Save.QualityFactor = 2;
   codecs.Save(image, destFileName8, RasterImageFormat.Jpeg, 8);
   image.Dispose();

   // Load the image again this time grayscale to 12-bits/pixel
   image = 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/pixel
   image = 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 factor
   codecs.Options.Jpeg.Save.QualityFactor = 0;
   codecs.Save(image, destFileName16, RasterImageFormat.Jpeg, 16);
   image.Dispose();

   // Clean Up
   codecs.Dispose();
}
RasterCommandExamples.prototype.GrayscaleCommandExample = function ( )
{
    Tools.SetLicense ( ) ;
    with (Leadtools) {
        with (Leadtools.Codecs) {
            with (Leadtools.ImageProcessing) {
                var codecs = new RasterCodecs();

                var srcFileName = "Assets\\Image1.cmp";
                var destFileName8 = "Image1_grayscale8.jpg";
                var destFileName12 = "Image1_grayscale12.jpg";
                var destFileName16 = "Image1_grayscale16.jpg";
                var image;
                var command;
                var loadFile;
                // Load the source image from disk as 24-bits/pixel
                return Tools.AppInstallFolder().getFileAsync(srcFileName).then(function (file) {
                    loadFile = file;
                    return codecs.loadAsync(LeadStreamFactory.create(loadFile), 24, CodecsLoadByteOrder.bgr, 1, 1)
                })
            .then(function (img) {
                image = img;

                // Check the grayscale mode
                console.info("Grsyscale mode of original image = ", image.grayscaleMode);

                // Convert to 8-bit grayscale
                command = new GrayscaleCommand();
                command.bitsPerPixel = 8;
                command.run(image);

                // Check the grayscale mode
                console.info("Grsyscale mode after grayscale command with 8 bpp = ", image.grayscaleMode);

                // Save it to disk
                codecs.options.jpeg.save.qualityFactor = 2;
                return Tools.AppLocalFolder().createFileAsync(destFileName8)
            })
            .then(function (saveFile) {
                return codecs.saveAsync(image, LeadStreamFactory.create(saveFile), RasterImageFormat.jpeg, 8)
            })
            .then(function () {
                image.close();

                // Load the image again this time grayscale to 12-bits/pixel
                return codecs.loadAsync(LeadStreamFactory.create(loadFile), 24, CodecsLoadByteOrder.bgr, 1, 1)
            })
                .then(function (img) {
                    image = img;
                    command.bitsPerPixel = 12;
                    command.run(image);
                    console.info("Grsyscale mode after grayscale command with 12 bpp = ", image.grayscaleMode);
                    codecs.options.jpeg.save.qualityFactor = 2;
                    return Tools.AppLocalFolder().createFileAsync(destFileName12)
                })
            .then(function (saveFile) {
                codecs.saveAsync(image, LeadStreamFactory.create(saveFile), RasterImageFormat.jpeg, 12)
            })
            .then(function () {
                image.close();

                // Load the image again this time grayscale to 16-bits/pixel
                return codecs.loadAsync(LeadStreamFactory.create(loadFile), 24, CodecsLoadByteOrder.bgr, 1, 1)
            })
            .then(function (img) {
                image = img;
                command.bitsPerPixel = 16;
                command.run(image);
                console.info("Grsyscale mode after grayscale command with 16 bpp = ", image.grayscaleMode);

                // When saving 16-bit jpegs, you must use lossless quality factor
                codecs.options.jpeg.save.qualityFactor = 0;
                return Tools.AppLocalFolder().createFileAsync(destFileName16)
            })
            .then(function (saveFile) {
                return codecs.saveAsync(image, LeadStreamFactory.create(saveFile), RasterImageFormat.Jpeg, 16)
            })
            .then(function () {
                image.close();
                codecs.close();
            });
            }
        }
    }
}
using Leadtools;
using Leadtools.Codecs;
using Leadtools.ImageProcessing;

      
public async Task GrayscaleCommandExample()
{
   RasterCodecs codecs = new RasterCodecs();
   string srcFileName = @"Assets\Image1.cmp";
   string destFileName8 = @"Image1_grayscale8.jpg";
   string destFileName12 = @"Image1_grayscale12.jpg";
   string destFileName16 = @"Image1_grayscale16.jpg";

   // Load the source image from disk as 24-bits/pixel
   StorageFile loadFile = await Tools.AppInstallFolder.GetFileAsync(srcFileName);
   RasterImage image = await codecs.LoadAsync(LeadStreamFactory.Create(loadFile), 24, CodecsLoadByteOrder.Bgr, 1, 1);

   // Check the grayscale mode
   Debug.WriteLine("Grsyscale mode of original image = {0}", image.GrayscaleMode);

   // Convert to 8-bit grayscale
   GrayscaleCommand command = new GrayscaleCommand();
   command.BitsPerPixel = 8;
   command.Run(image);

   // Check the grayscale mode
   Debug.WriteLine("Grsyscale mode after grayscale command with 8 bpp = {0}", image.GrayscaleMode);

   // Save it to disk

   codecs.Options.Jpeg.Save.QualityFactor = 2;
   StorageFile saveFile = await Tools.AppLocalFolder.CreateFileAsync(destFileName8);
   await codecs.SaveAsync(image, LeadStreamFactory.Create(saveFile), RasterImageFormat.Jpeg, 8);
   image.Dispose();

   // Load the image again this time grayscale to 12-bits/pixel
   image = await codecs.LoadAsync(LeadStreamFactory.Create(loadFile), 24, CodecsLoadByteOrder.Bgr, 1, 1);
   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;
   saveFile = await Tools.AppLocalFolder.CreateFileAsync(destFileName12);
   await codecs.SaveAsync(image, LeadStreamFactory.Create(saveFile), RasterImageFormat.Jpeg, 12);
   image.Dispose();

   // Load the image again this time grayscale to 16-bits/pixel
   image = await codecs.LoadAsync(LeadStreamFactory.Create(loadFile), 24, CodecsLoadByteOrder.Bgr, 1, 1);
   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 factor
   codecs.Options.Jpeg.Save.QualityFactor = 0;
   saveFile = await Tools.AppLocalFolder.CreateFileAsync(destFileName16);
   await codecs.SaveAsync(image, LeadStreamFactory.Create(saveFile), RasterImageFormat.Jpeg, 16);
   image.Dispose();

   // Clean Up
   image.Dispose();
}
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 mode
   Debug.WriteLine("Grsyscale mode of original image = {0}", image.GrayscaleMode);

   // Convert to 8-bit grayscale
   GrayscaleCommand command = new GrayscaleCommand();
   command.BitsPerPixel = 8;
   command.Run(image);

   // Check the grayscale mode
   Debug.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 disk
   codecs.Options.Jpeg.Save.QualityFactor = 2;
   codecs.Save(image, destStream8, RasterImageFormat.Jpeg, 8);
   image.Dispose();

   // this time grayscale to 12-bits/pixel
   image = 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/pixel
   image = 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 factor
   codecs.Options.Jpeg.Save.QualityFactor = 0;
   codecs.Save(image, destStream16, RasterImageFormat.Jpeg, 16);
   image.Dispose();

   // Clean Up
   temp.Dispose();
}
Imports Leadtools
Imports Leadtools.Codecs
Imports Leadtools.ImageProcessing
Imports Leadtools.Windows.Media

Public 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 mode
   Debug.WriteLine("Grsyscale mode of original image = {0}", image.GrayscaleMode)

   ' Convert to 8-bit grayscale
   Dim command As GrayscaleCommand = New GrayscaleCommand()
   command.BitsPerPixel = 8
   command.Run(image)

   ' Check the grayscale mode
   Debug.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 disk
   codecs.Options.Jpeg.Save.QualityFactor = 2
   codecs.Save(image, destStream8, RasterImageFormat.Jpeg, 8)
   image.Dispose()

   ' this time grayscale to 12-bits/pixel
   image = 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/pixel
   image = 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 factor
   codecs.Options.Jpeg.Save.QualityFactor = 0
   codecs.Save(image, destStream16, RasterImageFormat.Jpeg, 16)
   image.Dispose()

   ' Clean Up
   temp.Dispose()
End Sub
必要条件

ターゲットプラットホーム

参照

参照

GrayscaleCommandメンバ
Leadtools.ImageProcessing名前空間
Leadtools.Drawing.RasterPaintCallbacks
カスタム描画を実装すること