FLTK + ITK questions

64 views
Skip to first unread message

Ibrahim A

unread,
Aug 17, 2016, 1:16:19 PM8/17/16
to fltk.general
Dear all:
I am trying to build modified examples inspired by Luis Ibáñez's tutorial "ITK and Graphical User Interface" found here:
I changed the first example to read a color image and converted it to a grayscale image (the original was about smoothing). This part works fine. I like to know how I can display the output image in the displayed window when pressing the Run button. I tried different things but nothing worked.

In the second example, I am not sure where to put the code in the pages 18-21 in the above pdf file. Currently, I put it in two separate files fltkProgressBar.h  fltkProgressBar.cxx ( I am not cpp expert) . I also don't know where to put the itk  #include statements in the fl file. With the code that I listed bellow in example 2, I got this error:
-----------------------------------------------------------------------------------------------------------
 [ 14%] Generating myGUI.h
Scanning dependencies of target myGUI
[ 28%] Building CXX object CMakeFiles/myGUI.dir/itkGUI.cxx.o
In file included from /home/ibr/ZmyProjects/itk/itkGUI/example2/src/itkGUI.cxx:2:0:
./myGUI.h:20:3: error: ‘fltk’ does not name a type
   fltk::ProgressBar *progressBar;
   ^
/home/ibr/ZmyProjects/itk/itkGUI/example2/src/itkGUI.cxx: In function ‘int main()’:
/home/ibr/ZmyProjects/itk/itkGUI/example2/src/itkGUI.cxx:36:9: error: ‘class myGUI’ has no member named ‘progressBar’
     gui.progressBar->Observe( smoother );
         ^
make[2]: *** [CMakeFiles/myGUI.dir/itkGUI.cxx.o] Error 1
make[1]: *** [CMakeFiles/myGUI.dir/all] Error 2
make: *** [all] Error 2
-----------------------------------------------------------------------------------------------------------

Your suggestion is appreciated. Here are the file contents. I also attached the complete examples with this post.

####################################################################
#                                                    First example                                                          #  
####################################################################

-------------------------------------------------------------------
itkGUI.cxx
-------------------------------------------------------------------
// ===========  FLTK ===================
#include "myGUI.h"
// ===========  ITK ===================
#include "itkImage.h"
#include "itkImageFileReader.h"
#include "itkImageFileWriter.h"
#include "itkCastImageFilter.h"
#include "itkRGBToLuminanceImageFilter.h"

int main(  ){  
  //****************************  ITK  ********************************  
  //  read 2D RGB PNG image and convert it to gray scale.

  //  Initialization
  const unsigned int Dimension = 2;
  typedef unsigned char                           ComponentType;
  typedef itk::RGBPixel< ComponentType >          InputPixelType;
  typedef itk::Image< InputPixelType, Dimension > InputImageType;

  // Reading
  typedef itk::ImageFileReader< InputImageType >  ReaderType;
        ReaderType::Pointer reader = ReaderType::New();
        reader->SetFileName( "../img.png" );

  typedef unsigned char                             OutputPixelType;
  typedef itk::Image< OutputPixelType, Dimension >  OutputImageType;

  // Convert to gray scale
  typedef itk::RGBToLuminanceImageFilter< InputImageType, OutputImageType >  FilterType;
  FilterType::Pointer filter = FilterType::New();
        filter->SetInput( reader->GetOutput() );

        //  Writing
  typedef itk::ImageFileWriter< OutputImageType > WriterType;
        WriterType::Pointer writer = WriterType::New();
        writer->SetFileName( "../out.png" );
        writer->SetInput( filter->GetOutput() );
        writer->Update();
   //****************************  FLTK  ********************************
    myGUI gui;
    gui.Show();
    Fl::run();
    return 0;
}

-------------------------------------------------------------------
myGUI.fl
-------------------------------------------------------------------
code_name {.cxx}
class myGUI {open
} {
  Function {myGUI()} {open
  } {
    Fl_Window myWin {open
      xywh {4028 486 590 440} type Single hide
      code0 {\#include "itkProcessObject.h"}
    } {
      Fl_Button {} {
        label Quit
        callback {Quit();}
        xywh {345 315 120 40}
      }
      Fl_Box {} {
        label {FLTK and ITK} selected
        xywh {135 185 305 100} labelfont 9 labelsize 20
      }
      Fl_Button {} {
        label Run
        xywh {140 315 105 40}
      }
    }
  }
  Function {~myGUI()} {} {
    code {} {}
  }
  Function {Quit()} {} {
    code {myWin->hide();} {}
  }
  Function {Show()} {} {
    code {myWin->show();} {}
  }
  Function {Run()} {open
  } {
    code {} {}
  }
}
-------------------------------------------------------------------
CMakeLists.txt
-------------------------------------------------------------------
CMAKE_MINIMUM_REQUIRED(VERSION 3.6.1)
PROJECT(itkGUI)

find_package(ITK REQUIRED)
include(${ITK_USE_FILE})

FIND_PACKAGE(FLTK REQUIRED NO_MODULE)  
include_directories(${FLTK_INCLUDE_DIRS}) 
link_directories(${FLTK_LIBRARY_DIRS}) 
add_definitions(${FLTK_DEFINITIONS}) 

FLTK_WRAP_UI( myGUI myGUI.fl ) # create variable myGUI_FLTK_UI_SRCS, myGUI.cxx, and myGUI.h files

ADD_LIBRARY(myGUI itkGUI.cxx ${myGUI_FLTK_UI_SRCS}) 


ADD_EXECUTABLE( itkGUI itkGUI.cxx )  

TARGET_LINK_LIBRARIES( itkGUI fltk myGUI ${ITK_LIBRARIES} ) 

####################################################################
#                                                    second example                                                          #  
####################################################################

-------------------------------------------------------------------
itkGUI.cxx
-------------------------------------------------------------------
// ===========  FLTK ===================
#include "myGUI.h"
#include <FL/Fl.H>
#include <FL/Fl_Slider.H>
#include "fltkProgressBar.h"
// ===========  ITK ===================
#include "itkImage.h"
#include "itkImageFileReader.h"
#include "itkImageFileWriter.h"
#include "itkCurvatureFlowImageFilter.h"


int main(  ){  
  //****************************  ITK  ********************************  

  //  Initialization
  typedef itk::Image< float, 2 > ImageType;
  typedef itk::ImageFileReader< ImageType > ReaderType;
  typedef itk::CurvatureFlowImageFilter<ImageType,ImageType> SmootherFilterType;
 

   // Reading
   ReaderType::Pointer reader = ReaderType::New();
        reader->SetFileName( "../img.png" );

   // filtering
   SmootherFilterType::Pointer smoother = SmootherFilterType::New();
   smoother->SetInput( reader->GetOutput() );
   //smoother->SetNumberOfIterations( 7 );
   //smoother->SetTimeStep( 0.5 );       

   //****************************  FLTK  ********************************
    myGUI gui;
    gui.Show();

    gui.progressBar->Observe( smoother );
    gui.SetFilter( smoother );

    //Fl::run();
    return 0;

}
-------------------------------------------------------------------
fltkProgressBar.h
-------------------------------------------------------------------
#include "itkCommand.h"
#include <FL/Fl_Slider.H>

namespace fltk {
      class ProgressBar : public Fl_Slider {
            public:
                typedef itk::MemberCommand< ProgressBar > RedrawCommandType;
                ProgressBar(int x, int y, int w, int h, char * label=0);
                void ProcessEvent( itk::Object * , const itk::EventObject & );
                void Observe( itk::Object *caller );
           private:
                RedrawCommandType::Pointer m_RedrawCommand;
       };
} // end namespace fltk
-------------------------------------------------------------------
fltkProgressBar.cxx
-------------------------------------------------------------------
#include "fltkProgressBar.h"
#include "itkProcessObject.h"
#include <FL/Fl.H>
namespace fltk {
      ProgressBar::ProgressBar(int x, int y, int w, int h, char * label): Fl_Slider( x, y, w, h, label ){
     m_RedrawCommand = RedrawCommandType::New();
     m_RedrawCommand->SetCallbackFunction( this, & ProgressBar::ProcessEvent);
      }
      void ProgressBar::ProcessEvent( itk::Object * caller, const itk::EventObject & event){
     if( typeid( itk::ProgressEvent ) == typeid( event ) )  {
     ::itk::ProcessObject::Pointer process =  dynamic_cast< ::itk::ProcessObject *>( caller );
     this->value( process->GetProgress() );
     this->redraw();
     Fl::check();
     }
      }
      void ProgressBar::Observe( itk::Object * caller )   {
           caller->AddObserver(
           itk::ProgressEvent(),
           m_RedrawCommand.GetPointer() );
      }
} // end namespace fltk
-------------------------------------------------------------------
myGUI.fl
-------------------------------------------------------------------
# data file for the Fltk User Interface Designer (fluid)
version 1.0304
header_name {.h}
code_name {.cxx}
class myGUI {open
} {
  decl {itk::ProcessObject * m_Filter;} {private local
  }
  Function {myGUI()} {open
  } {
    Fl_Window myWin {open
      xywh {3882 145 590 440} type Single hide
      code0 {\#include "itkProcessObject.h"}
    } {
      Fl_Button {} {
        label Quit
        callback {Quit();}
        xywh {345 315 120 40}
      }
      Fl_Box {} {
        label {FLTK and ITK}
        xywh {135 185 305 100} labelfont 9 labelsize 20
      }
      Fl_Button {} {
        label Run
        xywh {140 315 105 40}
      }
      Fl_Progress progressBar {
        label progressBar selected
        xywh {180 265 245 35}
        code0 {\#include "itkProcessObject.h"}
        class {fltk::ProgressBar}
      }
    }
  }
  Function {~myGUI()} {open
  } {
    code {} {}
  }
  Function {Quit()} {open
  } {
    code {myWin->hide();} {}
  }
  Function {Hide()} {open
  } {
    code {myWin->hide();} {}
  }
  Function {Show()} {open
  } {
    code {myWin->show();} {}
  }
  Function {SetFilter(itk::ProcessObject * po)} {open
  } {
    code {myWin->show();} {}
    code {m_Filter=po;} {}
  }
  Function {Run()} {open
  } {
    code {this->Hide();} {}
    code {m_Filter->Update();} {}
  }
}
-------------------------------------------------------------------
CMakeLists.txt
-------------------------------------------------------------------
CMAKE_MINIMUM_REQUIRED(VERSION 3.6.1)
PROJECT(itkGUI)

find_package(ITK REQUIRED)
include(${ITK_USE_FILE})

FIND_PACKAGE(FLTK REQUIRED NO_MODULE)  
include_directories(${FLTK_INCLUDE_DIRS}) 
link_directories(${FLTK_LIBRARY_DIRS}) 
add_definitions(${FLTK_DEFINITIONS}) 

FLTK_WRAP_UI( myGUI myGUI.fl ) # create variable myGUI_FLTK_UI_SRCS, myGUI.cxx, and myGUI.h files

ADD_LIBRARY(myGUI itkGUI.cxx ${myGUI_FLTK_UI_SRCS}) 


ADD_EXECUTABLE( itkGUI itkGUI.cxx )  

TARGET_LINK_LIBRARIES( itkGUI fltk myGUI ${ITK_LIBRARIES} )  

TARGET_LINK_LIBRARIES( itkGUI fltk myGUI ${ITK_LIBRARIES} ) 
example1.zip
example2.zip

Ibrahim A

unread,
Aug 18, 2016, 6:58:35 AM8/18/16
to fltk.general
I tried simple image view example and it worked (attached is the complete example). When I tried putting the code in fluid as in the figure bellow:

Where in but2 I called Run();. I get this error :
-------------------------------------------------------------------
 [ 14%] Generating myGUI.h
Scanning dependencies of target myGUI
[ 28%] Building CXX object CMakeFiles/myGUI.dir/itkGUI.cxx.o
In file included from /home/ibr/ZmyProjects/itk/itkGUI/src/itkGUI.cxx:2:0:
./myGUI.h:15:20: error: expected ‘)’ before ‘,’ token
   Fl_JPEG_Image(img, "../out.jpg"); 
                    ^
make[2]: *** [CMakeFiles/myGUI.dir/itkGUI.cxx.o] Error 1
make[1]: *** [CMakeFiles/myGUI.dir/all] Error 2
make: *** [all] Error 2
-------------------------------------------------------------------

Any suggestion?

Here is the working example (no GUI): 
-------------------------------------------------------------------
imgView.cxx
-------------------------------------------------------------------
//Simple example to display a png image
#include <FL/Fl.H>
#include <FL/Fl_Window.H>
#include <FL/Fl_PNG_Image.H>
#include <FL/Fl_Box.H>
int main() {
    Fl_Window     win(960,600);                 // make a window with the image size
    Fl_Box        box(10,10,960-20,600-20);     // widget that will contain the image
    Fl_PNG_Image img("../img.png");      // load png image into memory
    box.image(img);                             // attach jpg image to box
    win.show();
    return(Fl::run());
}
-------------------------------------------------------------------
CMakeLists.txt
-------------------------------------------------------------------
CMAKE_MINIMUM_REQUIRED(VERSION 3.6.1)
PROJECT(imgView)

FIND_PACKAGE(FLTK REQUIRED NO_MODULE)  
include_directories(${FLTK_INCLUDE_DIRS}) 
link_directories(${FLTK_LIBRARY_DIRS}) 
add_definitions(${FLTK_DEFINITIONS}) 

FIND_PACKAGE(PNG REQUIRED)
include_directories(${PNG_INCLUDE_DIRS})

ADD_EXECUTABLE( imgView imgView.cxx )  

TARGET_LINK_LIBRARIES( imgView fltk   libfltk_images.a  ${PNG_LIBRARIES} )
imgView.zip
Message has been deleted

Ibrahim A

unread,
Aug 18, 2016, 5:42:18 PM8/18/16
to fltk.general
Hey, it is me again :D. No more error messages but I didn't get the result I want. The box didn't update with the resulted image. 
Here is what I did:
1-The following tree was created which contains three folders for the source, the data and the build files :
   # cmake 
    itkGUI/src/CMakeLists.txt
   # Contains the main function 
    itkGUI/src/itkGUI.cxx
   # Class for the image filtering using ITK 
    itkGUI/src/myFilter.h
    itkGUI/src/myFilter.cxx
   # The FLTK FLUID file 
    itkGUI/src/myGUI.fl
    
    # source image 
    itkGUI/data/imgS.png
    # source image 
    itkGUI/data/imgT.png
    # for building
    itkGUI/build/ 
2- Using FLUID, a GUI interface was created as in the following figure ( click to enlarge the figure):

The idea is: when clicking the Run button, it calls the constructor function of the filtering class which converting the image from RGB to grayscale and save the result to a file "../data/imgO.png" then load this file into the box "imgO" in the right.
The program works. The resulted image was created and saved to a file when the button clicked. I tried to upload it using this code but nothing happened:
    Fl_PNG_Image img("../data/imgO.png"); // resulted image , I also tried the complete path but nothing changed
    this->imgO->image(img); //"imgO" is a Box,
    this->myWin->show();    //"myWin" is the main window       
my questions:
1- I noticed that the image data is included in the generated myGUI.cxx file, is there a way to put only a link to the image file instead then add it later to the binary file?
2- How can use the short path e.g. ../src/myFilter.h instead of the complete path   
3- What is the correct code to update the box with the resulted image? the previous code didn't work
Your help is appreciated :)
I attached the complete example (some paths needed to be changed in the fl file). I also list here all the contents:
-------------------------------------------------------------------
itkGUI.cxx
-------------------------------------------------------------------
#include <iostream>
#include  "myGUI.h"

// Main function for the program
int main( ){
    std::cout << "program started" <<std::endl;
    myGUI gui;
    gui.Show();
    Fl::run();
   return 0;
}//main
-------------------------------------------------------------------
myFilter.h
-------------------------------------------------------------------
#ifndef myFilter_H_
#define myFilter_H_
class myFilter {
public:
myFilter(int x);
virtual ~myFilter();
};
#endif /* myFilter_H_ */
-------------------------------------------------------------------
myFilter.cxx
-------------------------------------------------------------------
#include "itkImage.h"
#include "itkImageFileReader.h"
#include "itkRGBToLuminanceImageFilter.h"
#include "itkCastImageFilter.h"
#include "itkImageFileWriter.h"

#include "myFilter.h"
#include <iostream>
myFilter::myFilter(int x) {
    std::cout << "class myFilter started"<< std::endl;
      // Reading
      typedef itk::RGBPixel< unsigned char >          InputPixelType;
      typedef itk::Image< InputPixelType, 2 > InputImageType;
      typedef itk::ImageFileReader< InputImageType >  ReaderType;
          ReaderType::Pointer reader = ReaderType::New();
          reader->SetFileName( "../data/imgS.png" );
      // Convert to gray scale
      typedef itk::Image< unsigned char, 2 >  OutputImageType;
      typedef itk::RGBToLuminanceImageFilter< InputImageType, OutputImageType >  FilterType;
      FilterType::Pointer filter = FilterType::New();
          filter->SetInput( reader->GetOutput() );
      //  Writing
      typedef itk::ImageFileWriter< OutputImageType > WriterType;
          WriterType::Pointer writer = WriterType::New();
          writer->SetFileName( "../data/imgO.png" );
          writer->SetInput( filter->GetOutput() );
          writer->Update();
}

myFilter::~myFilter() {
    std::cout << "class myFilter deleted"<< std::endl;
}
-------------------------------------------------------------------
myGUI.fl
-------------------------------------------------------------------
# data file for the Fltk User Interface Designer (fluid)
version 1.0304
header_name {.h}
code_name {.cxx}
class myGUI {open} {
  decl {//} {public local
  }
  Function {myGUI()} {open  } {
    code {printf("myGUI Class created\\n");} {}
    Fl_Window myWin {
      label {ITK FLTK GUI } open
      xywh {360 165 635 440} type Single hide
      code0 {\#include "/home/ibr/ZmyProjects/itk/itkGUI/src/myFilter.h"}
      code1 {\#include <FL/Fl_PNG_Image.H>}
    } {
      Fl_Box lbl {
        label {ITK - FLTK  Image Filtering}
        xywh {135 6 340 48} labelfont 11 labelsize 20
      }
      Fl_Button but_exit {
        label Exist
        callback {Quit();}
        xywh {180 335 100 50} color 88
      }
      Fl_Button but_run {
        label Run
        callback {Run();}
        xywh {325 335 100 50} color 71
      }
      Fl_Box imgS {
        label imgS selected
        image {../data/imgS.png} xywh {35 53 270 232} box BORDER_BOX color 0 selection_color 1 labelcolor 7
      }
      Fl_Box imgO {
        label imgO
        xywh {330 53 270 232} box BORDER_BOX color 0 selection_color 1 labelcolor 7
      }
    }
  }
  Function {~myGUI()} {open  } {
    code {printf("myGUI Class deleted !\\n");} {}
  }
  Function {Run()} {open  } {
    code {
          myFilter(1);// calling the constructor, we can put any integer here
          Fl_PNG_Image img("../data/imgO.png");
          this->imgO->image(img);
          this->myWin->show();
  }
  Function {Show()} {open  } {
    code {this->myWin->show();} {}
  }
  Function {Quit()} {open
  } {
    code {myWin->hide();} {}
  }
}
-------------------------------------------------------------------
CMakeLists.txt
-------------------------------------------------------------------
CMAKE_MINIMUM_REQUIRED(VERSION 3.6.1)
PROJECT(itkGUI)

find_package(ITK REQUIRED)
include(${ITK_USE_FILE})

FIND_PACKAGE(FLTK REQUIRED NO_MODULE)  
include_directories(${FLTK_INCLUDE_DIRS}) 
link_directories(${FLTK_LIBRARY_DIRS}) 
add_definitions(${FLTK_DEFINITIONS}) 

FIND_PACKAGE(PNG REQUIRED)
include_directories(${PNG_INCLUDE_DIRS})

FLTK_WRAP_UI( myGUI myGUI.fl ) # create variable myGUI_FLTK_UI_SRCS, myGUI.cxx, and myGUI.h files

ADD_LIBRARY(myGUI itkGUI.cxx  ${myGUI_FLTK_UI_SRCS}) 

ADD_EXECUTABLE( itkGUI itkGUI.cxx myFilter.cxx )  

TARGET_LINK_LIBRARIES( itkGUI fltk myGUI libfltk_images.a  ${ITK_LIBRARIES} ${PNG_LIBRARIES} ) 
itkGUI.zip

Ibrahim A

unread,
Aug 18, 2016, 5:59:43 PM8/18/16
to fltk.general
Sorry, I just read MacArthur, Ian (Leonardo, UK) reply. I reattached all the files without the contents of the build folder. 
example1.zip
example2.zip
imgView.zip
itkGUI.zip

Ibrahim A

unread,
Aug 18, 2016, 6:05:58 PM8/18/16
to fltk.general
Just putting this reply here (it was in different post) and commenting
MacArthur, Ian (Leonardo, UK) 
Aug 18 (10 hours ago)
> I tried simple image view example and it worked (attached is the 
> complete example). 

The zip file was stripped away when it arrived here, so I can't comment on your code. 

Did you put an executable file into the zip? 

If so, please do not do that - many (all?) email filters and some firewalls will automatically strip away any zip file that contains an executable, for security reasons. 

Note also that many (most?) developers will not run arbitrary binaries that are posted to mail lists, since they are very likely to be malicious! 

Better just to put the source code in, then it is less likely to be stripped away by the mail filters and people have the option of building your code to see what it does for themselves, if they judge it prudent to do so. 

Thanks, I didn't know that. Done! please see my previous post. 

> Where in but2 I called Run();. I get this error : 
> ------------------------------------------------------------------- 
>  [ 14%] Generating myGUI.h 
> Scanning dependencies of target myGUI 
> [ 28%] Building CXX object CMakeFiles/myGUI.dir/itkGUI.cxx.o 
> In file included from 
> /home/ibr/ZmyProjects/itk/itkGUI/src/itkGUI.cxx:2:0: 
> ./myGUI.h:15:20: error: expected ‘)’ before ‘,’ token 
>    Fl_JPEG_Image(img, "../out.jpg"); 

>                     ^ 
> make[2]: *** [CMakeFiles/myGUI.dir/itkGUI.cxx.o] Error 1 
> make[1]: *** [CMakeFiles/myGUI.dir/all] Error 2 
> make: *** [all] Error 2 
> ------------------------------------------------------------------- 

> Any suggestion? 

Without seeing the code for myGUI.h, line 15, character position 20, it's hard to say, but it looks like you may have typed something invalid there. 

It complains about:- 

     Fl_JPEG_Image(img, "../out.jpg"); 

Perhaps you meant something like:- 

     Fl_JPEG_Image img ("../out.jpg"); 

Or something. 
You are right, I figured this out and solved this in the previous post, still the image was not updated.  
In any case, you need to study the errors the compiler reports, and fix them, as that will surely lead you to clarity! 







Selex ES Ltd
Registered Office: Sigma House, Christopher Martin Road, Basildon, Essex SS14 3EL
A company registered in England & Wales.  Company no. 02426132
********************************************************************
This email and any attachments are confidential to the intended
recipient and may also be privileged. If you are not the intended
recipient please delete it from your system and notify the sender.
You should not copy it or use it for any purpose nor disclose or
distribute its contents to any other person.
********************************************************************
Auto Generated Inline Image 1
Auto Generated Inline Image 2

Ibrahim A

unread,
Aug 20, 2016, 12:04:38 PM8/20/16
to fltk.general
I found that:
- I have to add this code: 
   Fl::check();
  after
   this->imgO->redraw();
- I also found that if the image is not loaded there is no error message which means I need to check it manually e.g:
  printf(" height is:%d \\n",img.h()); 

Now the code works :) Thanks to everyone support!

Ibrahim A

unread,
Aug 20, 2016, 12:40:01 PM8/20/16
to fltk.general
I created a post in my webpage that includes all the working examples. I hope it will help beginners who work with the same environment. 

Reply all
Reply to author
Forward
0 new messages