ITK/Examples/ImageProcessing/RGBToLuminanceImageFilter: Difference between revisions

From KitwarePublic
< ITK‎ | Examples
Jump to navigationJump to search
(Add text to viewports)
(Deprecated content that is moved to sphinx)
 
(One intermediate revision by one other user not shown)
Line 1: Line 1:
<div class="floatcenter">[[File:ITK_Examples_Baseline_ImageProcessing_TestRGBToLuminanceImageFilter.png]]</div>
{{warning|1=The media wiki content on this page is no longer maintained.  The examples presented on the https://itk.org/Wiki/*  pages likely require ITK version 4.13 or earlier releases.   In many cases, the examples on this page no longer conform to the best practices for modern ITK versions.
==RGBToLuminanceImageFilter.cxx==
}}
<source lang="cpp">
#include "itkImage.h"
#include "itkImageFileReader.h"
#include "itkRGBToLuminanceImageFilter.h"


#include "QuickView.h"
[https://itk.org/ITKExamples[ITK Sphinx Examples]]
 
typedef itk::Image<itk::RGBPixel<unsigned char>, 2> RGBImageType;
typedef itk::Image<unsigned char, 2>  ScalarImageType;
 
static void CreateImage(RGBImageType* const image);
 
int main(int argc, char *argv[])
{
  RGBImageType::Pointer image;
 
  std::string imageName;
  if(argc < 2)
    {
    //std::cerr << "Required: filename" << std::endl;
    //return EXIT_FAILURE;
    image = RGBImageType::New();
    CreateImage(image);
    imageName = "Generated image";
    }
  else
  {
    typedef itk::ImageFileReader<RGBImageType> ReaderType;
    ReaderType::Pointer reader = ReaderType::New();
    reader->SetFileName(argv[1]);
    reader->Update();
    image = reader->GetOutput();
    imageName = argv[1];
  }
 
  typedef itk::RGBToLuminanceImageFilter<RGBImageType, ScalarImageType> LuminanceFilterType;
  LuminanceFilterType::Pointer luminanceFilter = LuminanceFilterType::New();
  luminanceFilter->SetInput(image);
  luminanceFilter->Update();
 
  QuickView viewer;
  viewer.AddImage(
    image.GetPointer(),
    true,
    imageName);
  viewer.AddImage(
    luminanceFilter->GetOutput(),
    true,
    "Luminance");
  viewer.Visualize();
 
  return EXIT_SUCCESS;
}
 
 
void CreateImage(RGBImageType* const image)
{
  // Create an image with 2 connected components
  itk::Index<2> corner = {{0,0}};
 
  itk::Size<2> size;
  unsigned int NumRows = 200;
  unsigned int NumCols = 300;
  size[0] = NumRows;
  size[1] = NumCols;
 
  itk::ImageRegion<2> region(corner, size);
 
  image->SetRegions(region);
  image->Allocate();
 
  RGBImageType::PixelType pixel;
  pixel.SetRed(200);
  pixel.SetGreen(50);
  pixel.SetBlue(50);
  // Make a square
  for(unsigned int r = 40; r < 100; r++)
    {
    for(unsigned int c = 40; c < 100; c++)
      {
      itk::Index<2> pixelIndex;
      pixelIndex[0] = r;
      pixelIndex[1] = c;
 
      image->SetPixel(pixelIndex, pixel);
      }
    }
}
</source>
 
{{ITKCMakeLists|BinaryThinningImageFilter}}

Latest revision as of 13:57, 31 May 2019

Warning: The media wiki content on this page is no longer maintained. The examples presented on the https://itk.org/Wiki/* pages likely require ITK version 4.13 or earlier releases. In many cases, the examples on this page no longer conform to the best practices for modern ITK versions.

[ITK Sphinx Examples]