33 this->SetNumberOfIndexedInputs(2);
34 this->SetNumberOfIndexedOutputs(2);
71 if (!paImage || !usImage) {
73 throw std::invalid_argument(
"One of the images was NULL.");
78 MITK_ERROR <<
"Mismatching image dimensions detected in the motion " 79 "compensation filter.";
80 throw std::invalid_argument(
"Both images must have dimension 3.");
84 for (
unsigned int i = 0; i < paImage->GetDimension(); ++i) {
85 if (paImage->GetDimensions()[i] != usImage->GetDimensions()[i]) {
86 MITK_ERROR <<
"Mismatching image dimensions detected in the motion " 87 "compensation filter.";
88 throw std::invalid_argument(
89 "Both images must have the same length in each dimension.");
102 for (
unsigned int i = 0; i < usOutput->GetDimension(); ++i) {
103 if (usOutput->GetDimensions()[i] != usInput->GetDimensions()[i]) {
113 output->Initialize(input);
115 output->SetImportVolume(accessor.
GetData());
124 if (m_BatchSize == 0) {
130 for (
unsigned int i = 0; i < paInput->GetDimensions()[
IMAGE_DIMENSION - 1]; ++i) {
138 if (i % batch == 0) {
141 m_UsRes = m_UsMat.clone();
142 m_PaRes = m_PaMat.clone();
145 cv::calcOpticalFlowFarneback(m_UsRef, UsMatRescaled, m_Flow, m_PyrScale,
146 m_Levels, m_WinSize, m_Iterations, m_PolyN,
147 m_PolySigma, m_Flags);
152 cv::remap(m_PaMat, m_PaRes, m_Map, cv::noArray(), cv::INTER_LINEAR);
153 cv::remap(m_UsMat, m_UsRes, m_Map, cv::noArray(), cv::INTER_LINEAR);
164 cv::Mat
map(flow.size(), flow.type());
166 for (
int y = 0; y <
map.rows; ++y) {
167 for(
int x = 0; x <
map.cols; ++x) {
168 cv::Point2f f = flow.at<cv::Point2f>(y,x);
169 map.at<cv::Point2f>(y,x) = cv::Point2f(x + f.x, y + f.y);
178 if (m_MaxValue == m_MinValue) {
183 return MAX_MATRIX*(mat.clone() - m_MinValue) / (m_MaxValue - m_MinValue);
192 slice->Initialize(input->GetPixelType(),
IMAGE_DIMENSION - 1, input->GetDimensions());
193 slice->SetVolume(accessor.GetData());
196 m_ImageToOpenCVFilter->SetImage(slice);
197 return m_ImageToOpenCVFilter->GetOpenCVMat();
203 m_OpenCVToImageFilter->SetOpenCVMat(mat);
204 m_OpenCVToImageFilter->Update();
208 output->SetSlice(accessor.
GetData(), i);
213 MITK_INFO <<
"Start motion compensation.";
227 this->m_MaxValue = usInput->GetStatistics()->GetScalarValueMax();
228 this->m_MinValue = usInput->GetStatistics()->GetScalarValueMin();
233 MITK_INFO <<
"Motion compensation accomplished.";
mitk::Image::Pointer GetPaOutput()
Wrapper which gets the photoacoustic image out of the correct output.
cv::Mat GetMatrix(const mitk::Image::Pointer input, unsigned int i)
Extract a 2d slice as OpenCV matrix.
void PerformCorrection(mitk::Image::Pointer paInput, mitk::Image::Pointer usInput, mitk::Image::Pointer paOutput, mitk::Image::Pointer usOutput)
This method performs the actual motion compensation.
mitk::Image::Pointer GetPaInput()
Wrapper which gets the photoacoustic image out of the correct input.
mitk::Image::Pointer GetUsInput()
Wrapper which gets the ultrasonic image out of the correct input.
virtual void SetInput(const InputImageType *image)
cv::Mat FitMatrixToChar(cv::Mat mat)
Rescale matrix such that the values lie between 0 and 255.
void SetPaInput(mitk::Image::Pointer)
Wrapper which sets the photoacoustic image as the correct input.
PhotoacousticMotionCorrectionFilter()
mitk::Image::Pointer GetUsOutput()
Wrapper which gets the ultrasonic image out of the correct output.
void InitializeOutputIfNecessary(mitk::Image::Pointer paInput, mitk::Image::Pointer usInput, mitk::Image::Pointer paOutput, mitk::Image::Pointer usOutput)
Assure that the output images have the same dimensions as the input images.
cv::Mat ComputeFlowMap(cv::Mat)
Compute the remapping map from an optical flow.
void SetUsInput(mitk::Image::Pointer)
Wrapper which sets the ultrasonic image as the correct input.
~PhotoacousticMotionCorrectionFilter() override
void InsertMatrixAsSlice(cv::Mat mat, mitk::Image::Pointer output, unsigned int i)
Insert a OpenCV matrix as a slice into an image.
void InitializeOutput(mitk::Image::Pointer input, mitk::Image::Pointer output)
Copy the image data from the input image to the output image.
void CheckInput(mitk::Image::Pointer paImage, mitk::Image::Pointer usImage)
Validate the input images.
MITKMATCHPOINTREGISTRATION_EXPORT ResultImageType::Pointer map(const InputImageType *input, const RegistrationType *registration, bool throwOnOutOfInputAreaError=false, const double &paddingValue=0, const ResultImageGeometryType *resultGeometry=nullptr, bool throwOnMappingError=true, const double &errorValue=0, mitk::ImageMappingInterpolator::Type interpolatorType=mitk::ImageMappingInterpolator::Linear)
InputImageType * GetInput(void)
OutputType * GetOutput()
Get the output data of this image source object.
void GenerateData() override
Apply OpenCV algorithm to compensate motion in a 2d image time series.
ImageReadAccessor class to get locked read access for a particular image part.
const void * GetData() const
Gives const access to the data.