[Insight-users] using reader as global variable

john smith mkitkinsightuser at gmail.com
Mon May 30 10:19:19 EDT 2011


Well, I have fixed that but when I am running my program, I do not get any
errors. But when I start debug then an exception of Qt is caught. I can
really understand why? I give you my code.The strange think is that when I
declare my directory without using push_button_File() function, but inside
the z_slice_extract(), I take my results correctly. Couls somebody help me?


#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "canvas.h"

#include "itkImage.h"
#include "itkImageFileReader.h"
#include "itkImageFileWriter.h"
#include "itkExtractImageFilter.h"
#include <itkSize.h>

QString fileName;

  unsigned long size_x;
  unsigned long size_y;
  unsigned long size_z;



MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    ui->graphicsView_inputImage->setScene(scene=new Canvas());


connect(scene,SIGNAL(mousejustpressed(int,int)),this,SLOT(mousejustpressed(int,int)));

    QObject::connect(ui->pushButton_openFile, SIGNAL(clicked()), this,
SLOT(push_button_File()));

    QObject::connect(ui->pushButton_slice_z, SIGNAL(clicked()), this,
SLOT(z_slice_extract()));



}

void MainWindow::push_button_File()
{

  QString fileName = QFileDialog::getOpenFileName(this,
                                   tr("Open File"), QDir::currentPath());


}


void MainWindow::z_slice_extract()
{

 ///unsigned char to display in .png format
  typedef unsigned char        InputPixelType;
  typedef unsigned char        OutputPixelType;

  typedef itk::Image< InputPixelType,  3 >    InputImageType;
  typedef itk::Image< OutputPixelType, 2 >    OutputImageType;

  typedef itk::ImageFileReader< InputImageType  >  ReaderType;
  typedef itk::ImageFileWriter< OutputImageType >  WriterType;

  ReaderType::Pointer reader = ReaderType::New();
  WriterType::Pointer writer = WriterType::New();

  reader->SetFileName( fileName.toStdString()  );
  writer->SetFileName( "2D.png" );


  typedef itk::ExtractImageFilter< InputImageType, OutputImageType >
FilterType;
  FilterType::Pointer filter = FilterType::New();

    reader->Update();
  InputImageType::RegionType inputRegion =
           reader->GetOutput()->GetLargestPossibleRegion();

  InputImageType::SizeType size = inputRegion.GetSize();

  // get the size of the hole 3D image
  unsigned long size_x = size[0];
  unsigned long size_y = size[1];
  unsigned long size_z = size[2];

  // get slices of z coordiante
  size[2] = 0;


  InputImageType::IndexType start = inputRegion.GetIndex();
 // const unsigned int sliceNumber = 90;
  ui->verticalScrollBar_z->setRange(1,size_z);
  unsigned int sliceNumber  =  ui->verticalScrollBar_z->value();
  start[2] = sliceNumber;
 // ui->verticalScrollBar_z->setValue(sliceNumber);


  InputImageType::RegionType desiredRegion;
  desiredRegion.SetSize( size );
  desiredRegion.SetIndex( start );

  filter->SetExtractionRegion( desiredRegion );

  filter->SetInput( reader->GetOutput() );
  writer->SetInput( filter->GetOutput() );

      try
    {
    writer->Update();
    }
    catch( itk::ExceptionObject & err )
    {
    std::cerr << "ExceptionObject caught !" << std::endl;
    std::cerr << err << std::endl;
    }


     scene->addPixmap(QPixmap("2D.png"));
          ui->graphicsView_resultImage->setScene(scene);
    //       ui->graphicsView_resultImage->set;

/**     static QGraphicsPixmapItem* pixmapItem;

     if (!pixmapItem) {
        //    pixmapItem = scene->addPixmap(QPixmap("2D.png"));

          QPixmap tmpmap (QPixmap("2D.png"));
        pixmapItem = scene->addPixmap ( tmpmap.scaled (214,256) );

            ui->graphicsView_inputImage->setScene(scene);
        } else {
            QPixmap tmpmap (QPixmap("2D.png"));
            pixmapItem->setPixmap(tmpmap.scaled ( 214,256));
        }
*/



    // taking the size of the loaded image
    ui->label_4->setText(QString("size x:%1").arg(size_x));
    ui->label_5->setText(QString("size y:%1").arg(size_y));
    ui->label_6->setText(QString("size z:%1").arg(size_z));

    return;
}





void MainWindow::mousejustpressed(int x,int y)
{
    int k1=256-y;


    unsigned char value;

    QImage image(scene->sceneRect().size().toSize(),
    QImage::Format_RGB32);

    QPainter painter(&image);
    scene->render(&painter);

    value=image.pixel(x,k1);


    /**QImage buffer = QImage(1,1, QImage::Format_RGB32);
    QPainter painter(&buffer);
    scene->render(&painter, QRectF(0.0, 0.0, 1.0, 1.0), QRectF(x, y, 1.0,
1.0));
    int value = qGray(buffer.pixel(0,0));
    */

    ui->label->setText(QString("x:%1").arg(x));
    ui->label_2->setText(QString("y:%1").arg(k1));
    ui->label_3->setText(QString("pixel value:%1").arg(value));


}


MainWindow::~MainWindow()
{
    delete ui;
}




2011/5/30 David Doria <daviddoria at gmail.com>

> On Mon, May 30, 2011 at 5:25 AM, john smith <mkitkinsightuser at gmail.com>wrote:
>
>> I am using Qt add in for visual studio 2010 and I have typed this. But
>> still gettting these errors:
>> *1>e:\qt
>> applications\mainwindow_globalreader\mainwindow\mainwindow\mainwindow.h(49):
>> error C2065: 'ImageType' : undeclared identifier
>> 1>e:\qt
>> applications\mainwindow_globalreader\mainwindow\mainwindow\mainwindow.h(50):
>> error C2955: 'itk::ImageFileReader' : use of class template requires
>> template argument list
>> 1>          c:\itk\source\code\io\itkImageFileReader.h(93) : see
>> declaration of 'itk::ImageFileReader'
>> 1>e:\qt
>> applications\mainwindow_globalreader\mainwindow\mainwindow\mainwindow.h(50):
>> fatal error C1903: unable to recover from previous error(s); stopping
>> compilation
>> 1>  mainwindow.cpp*
>>
>> I have include the header fules "itkImage.h" ,"itkImageFileReader.h".Could
>> somebody help me?
>>
>> class MainWindow : public QMainWindow
>> {
>>   ..............
>>
>>
>> private:
>> typedef   itk::ImageFileReader< ImageType >   ReaderType;
>> ReaderType::Pointer reader;
>>
>> };
>>
>>
>> MainWindow::MainWindow(QWidget *parent) :
>>     QMainWindow(parent),
>>     ui(new Ui::MainWindow)
>> {
>> ............
>> ...........
>>       typedef unsigned char        InputPixelType;
>>   typedef itk::Image< InputPixelType,  3 >    InputImageType;
>>   typedef itk::ImageFileReader< InputImageType  >  ReaderType;
>>
>>   ReaderType::Pointer reader = ReaderType::New();
>>
>> }
>>
>
>
> As the error says,  you have not defined ImageType. You need something like
>
> typedef itk::Image<unsigned char, 2> ImageType;
>
> David
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.itk.org/pipermail/insight-users/attachments/20110530/ed062240/attachment.htm>


More information about the Insight-users mailing list