Search code examples
vtkdicombrightnesscontrast

How to restore brightness and contrast in VTK?


I am developing a C++ application, using Qt + VTK. Actually, I have a QVTKWidget, used for displaying a DICOM image. I have used a vtkImageViewer2 with a vtkInteractorStyleImage, so I can change brightness and contrast of the image by clicking on it with the mouse, while the left button is pressed.

However, I wish to enable a button for restore brightness and contrast at their original values. I have done some research, but I can't find any function regarding it, in the documentation, nor any saved brightness and contrast value that I can change at run-time.

I hope someone can help. Thank you.


Solution

  • Following this example, I've been able to put this code togheter:

    void MainWindow::on_luminosityResetButton_clicked()
    {
        vtkSmartPointer<vtkImageShiftScale> shiftScaleFilter = 
        vtkSmartPointer<vtkImageShiftScale>::New();
        shiftScaleFilter->SetOutputScalarTypeToUnsignedChar();
        #if VTK_MAJOR_VERSION <= 5
            shiftScaleFilter->SetInputConnection(imageData->GetProducerPort());
        #else
            shiftScaleFilter->SetInputData(imageData);
        #endif
        shiftScaleFilter->SetShift(0);
        shiftScaleFilter->SetScale(0);
        shiftScaleFilter->Update();
    
        // Create actors
        vtkSmartPointer<vtkImageSliceMapper> originalSliceMapper = vtkSmartPointer<vtkImageSliceMapper>::New();
        #if VTK_MAJOR_VERSION <= 5
            originalSliceMapper->SetInputConnection(imageData->GetProducerPort());
        #else
            originalSliceMapper->SetInputData(imageData);
        #endif
    
        vtkSmartPointer<vtkImageSlice> originalSlice = vtkSmartPointer<vtkImageSlice>::New();
        originalSlice->SetMapper(originalSliceMapper);
    
        vtkSmartPointer<vtkImageSliceMapper> shiftScaleMapper = vtkSmartPointer<vtkImageSliceMapper>::New();
        shiftScaleMapper->SetInputConnection(shiftScaleFilter->GetOutputPort());
    
        vtkSmartPointer<vtkImageSlice> shiftScaleSlice = vtkSmartPointer<vtkImageSlice>::New();
        shiftScaleSlice->SetMapper(shiftScaleMapper);
        shiftScaleSlice->GetProperty()->SetInterpolationTypeToNearest();
    
        renderer->AddViewProp(originalSlice);
    
        vtkSmartPointer<vtkRenderer> shiftScaleRenderer =
            vtkSmartPointer<vtkRenderer>::New();
        shiftScaleRenderer->AddViewProp(shiftScaleSlice);
    
        ui->qvtkWidget->update();
    }
    

    Now it works (brightness and contrast gain their original values), but I have not totally understood the logic behind that. So, I don't mark my own answer as the correct one (hoping to obtain a more pertinent answer), but I post it anyway. I saw this same issue (unanswered) elsewhere, and I hope that it can be useful to others.