[Insight-users] Get pixel Data

Sergio Vera sergio.vera at alma3d.com
Thu Feb 17 05:20:55 EST 2011


Hello John
You are almost there.


First you have created a FileReader, but you don't have actually read the
image. You have to call the Update method in the file reader so that the
filter starts doint its job.
Once the file is read, you use the image at the output of the filter, and
read it's contents. Notice that you don't need to create a new image, you
need to use the output image from the filter.

Your code should look like this, modifications shown in bold font (there may
be errors in this code as I didnt compiled it):

#include "itkImage.h"
#include "itkImageFileReader.h"

#include "itkIndex.h"

int main(int argc, char *argv[])
{
  if( argc < 2 )
    {
    std::cerr << "Usage: " << std::endl;
    std::cerr << argv[0] << " inputImageFile" << std::endl;
    return EXIT_FAILURE;
    }

  typedef itk::Image<  unsigned short, 2 >         ImageType;
  typedef itk::ImageFileReader<ImageType> ReaderType;

  ReaderType::Pointer reader= ReaderType::New();
  reader->SetFileName(argv[1]);

*  reader->Update() // a try... catch block needed here*

  //////////////////

  //  image object can be created
*  // ImageType::Pointer image = ImageType::New(); // no doing this, you
read from another image*
*  ImageType::Pointer image = reader->GetOutput();
*

  ImageType::IndexType pixelIndex;

  pixelIndex[0] = 17;   // x position
  pixelIndex[1] = 19;   // y position


  ImageType::PixelType   pixelValue = image->GetPixel( pixelIndex );


  std::cout << image->GetPixel(pixelIndex);

///////////////////////

  return EXIT_SUCCESS;
}


On Thu, Feb 17, 2011 at 11:05 AM, john smith <mkitkinsightuser at gmail.com>wrote:

> Hello,
>
> I use visualstudio2010 and cmake.I am trying to read an image with the
> Command  Prompt window (for example a .png image) and then get the data
> value of a pixel using the get()  method. Finally, I want to take the value
> of the pixel on the Command Prompt window. So, I have created an imageReader
> object and an IndexType object as it is shown in the following code. As
> structures separately, the two objects seem to be correct, but I don't know
> how to connect them together to get my data value. Could someone help me
> with this problem?
>
> Thanks
> -----------------------code------------------------------
> --------------------------------------------------------
> #include "itkImage.h"
> #include "itkImageFileReader.h"
>
> #include "itkIndex.h"
>
> int main(int argc, char *argv[])
> {
>   if( argc < 2 )
>     {
>     std::cerr << "Usage: " << std::endl;
>     std::cerr << argv[0] << " inputImageFile" << std::endl;
>     return EXIT_FAILURE;
>     }
>
>   typedef itk::Image<  unsigned short, 2 >         ImageType;
>   typedef itk::ImageFileReader<ImageType> ReaderType;
>
>   ReaderType::Pointer reader= ReaderType::New();
>   reader->SetFileName(argv[1]);
>
>
>   //////////////////
>
>   //  image object can be created
>   ImageType::Pointer image = ImageType::New();
>
>
>   ImageType::IndexType pixelIndex;
>
>   pixelIndex[0] = 17;   // x position
>   pixelIndex[1] = 19;   // y position
>
>
>   ImageType::PixelType   pixelValue = image->GetPixel( pixelIndex );
>
>
>   std::cout << image->GetPixel(pixelIndex);
>
> ///////////////////////
>
>   return EXIT_SUCCESS;
> }
>
>
> ------------------CMakeLists.txt------------------
> -------------------------------------------------------
> cmake_minimum_required(VERSION 2.6)
>
> PROJECT(ImageFileReader)
>
> FIND_PACKAGE(ITK REQUIRED)
> INCLUDE(${ITK_USE_FILE})
>
>
> ADD_EXECUTABLE(myproject myproject.cxx)
> TARGET_LINK_LIBRARIES(myproject ITKIO ITKCommon)
>
> _____________________________________
> Powered by www.kitware.com
>
> Visit other Kitware open-source projects at
> http://www.kitware.com/opensource/opensource.html
>
> Kitware offers ITK Training Courses, for more information visit:
> http://www.kitware.com/products/protraining.html
>
> Please keep messages on-topic and check the ITK FAQ at:
> http://www.itk.org/Wiki/ITK_FAQ
>
> Follow this link to subscribe/unsubscribe:
> http://www.itk.org/mailman/listinfo/insight-users
>
>


-- 
Sergio Vera

 Alma IT Systems
 C/ Vilana, 4B, 4º 1ª
 08022 Barcelona
 T. (+34) 932 380 592
 www.alma3d.com
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.itk.org/pipermail/insight-users/attachments/20110217/d68d53e7/attachment.htm>


More information about the Insight-users mailing list