[Insight-users] OpenCVImageBridge with RGB images

mini mini.spam at gmx.de
Fri Dec 7 11:58:29 EST 2012


Hi,

i am trying to use the OpenCVImageBrige to convert from itk rgb-image to
opencv Mat, but it does not work well. 
First I try with grayscale images and it works perfect. But when I read a
color image with ITK and want to write or show with OpenCV it runs into
problems. 
The image shows then 9 tiles of the same image in different intensities.
Fiji says that the preferences of the rewritten image has 32Bit per Pixel
RGB.
This problem only occurres when i try to read a color nifti. When i convert
it to color tiff it works well.

Proceedings and results:

read color nifti -> write color tiff -> bad
read gray nifti -> write gray tiff -> good
read color tiff -> write color tiff -> good

Can anyone help me please?

Here is my code snippet without any includes:


typedef unsigned char PixelType;
typedef itk::Image<itk::RGBPixel&lt;PixelType>, 2> ImageType;
typedef itk::OpenCVImageBridge BridgeType;

int main(int argc, char** argv) {

    ImageType::Pointer input = ImageType::New();
    
    typedef itk::ImageFileReader<ImageType> ReaderType;
    ReaderType::Pointer reader = ReaderType::New();
    reader->SetFileName("data/RGB.nii");
    reader->Update();
        
    cv::Mat _cvMat = BridgeType::ITKImageToCVMat(reader->GetOutput());
    IplImage* _cvIPL = BridgeType::ITKImageToIplImage(reader->GetOutput());
        
    //ITK output
    typedef itk::ImageFileWriter< ImageType > WriterType;
    WriterType::Pointer writer = WriterType::New();
    writer->SetFileName("data/output.nii");
    writer->SetInput(reader->GetOutput());
    writer->Update();
        
    
    //opencv output
//    cv::Mat bla;
//    _cvMat.convertTo(bla, CV_8UC1);
//    std::vector<int> compression_params;
//    compression_params.push_back(CV_IMWRITE_PNG_COMPRESSION);
//    compression_params.push_back(9);    
    cv::imwrite("data/alpha.tif", _cvMat);//, compression_params);
    
    cvNamedWindow("IPL", CV_WINDOW_NORMAL);
    cvShowImage("IPL", _cvIPL);
    
    cvNamedWindow("Mat", CV_WINDOW_NORMAL);
    cv::imshow("Mat", _cvMat);
    cv::waitKey(-1);
    
}




--
View this message in context: http://itk-insight-users.2283740.n2.nabble.com/OpenCVImageBridge-with-RGB-images-tp7581894.html
Sent from the ITK Insight Users mailing list archive at Nabble.com.


More information about the Insight-users mailing list