TIFF file read as cv::Mat imported to vtkImageData makes program crash (Qt C++)

20 Views Asked by At

I am using opencv to apply CLAHE to a grayscale image and then vtk for furthur processing. This used to work fine for RAW images. But when same was tried for TIFF image, no operation can be performed on the imported vtk data.

Tried other methods like reading the image as QFile (as was done for RAW) and converting to unsigned char* data input for opencv. Also tried reading as QImage but with same error. Using a C++ loop to convert vtkImageData to Mat as seen in some posts really slows down the program. The code is as follows.

vtkNew<vtkTIFFReader> tiffReader;
tiffReader->SetFileName(filenameDispRaw.toLocal8Bit().constData()); 
// filenameDispRaw will contain tiff image file path, in case raw file is absent
tiffReader->Update();

vtkImageData *tiffImage = vtkImageData::New();
tiffImage = tiffReader->GetOutput();
double ImageWidth = tiffImage->GetDimensions()[0];
double ImageHeight = tiffImage->GetDimensions()[1];

cv::Mat test(ImageHeight, ImageWidth, CV_16UC1);    //, data);
test = cv::imread(filenameDispRaw.toStdString(), CV_16UC1); // directly reading tiff as Mat

cv::Mat inv_tif;
bitwise_not(test, inv_tif);

cv::Ptr<cv::CLAHE> clahe_tif = cv::createCLAHE(histoThreshold, cv::Size(histoTile1,histoTile2));
cv::Mat dst_tif;
clahe_tif->apply(inv_tif, dst_tif);

vtkNew<vtkImageImport> imageImport;
imageImport->SetDataSpacing(1, 1, 1);
imageImport->SetDataOrigin(0, 0, 0);
imageImport->SetWholeExtent(0, ImageWidth -1, 0, ImageHeight - 1, 0, 0);
imageImport->SetDataExtentToWholeExtent();
imageImport->SetDataScalarTypeToUnsignedShort();
imageImport->SetNumberOfScalarComponents(1);
imageImport->SetImportVoidPointer(dst_tif.data);
imageImport->Update();

vtkImageData *imgdataEnhance=vtkImageData::New();
imgdataEnhance = imageImport->GetOutput();

Program crashes in the following line:

double min = imgdataEnhance->GetScalarRange()[0];
double max = imgdataEnhance->GetScalarRange()[1];

Getting CLAHE done is extremely important. I am totally stuck at this point and would appreciate any improvements or alternative methods to get this done from the community members. Please help

0

There are 0 best solutions below