Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

#include "itkAdvancedImageToImageMetric.h"
#include "itkKernelFunctionBase2.h"
#include <mutex>
#include <vector>


Expand Down Expand Up @@ -504,6 +505,8 @@ class ITK_TEMPLATE_EXPORT ParzenWindowHistogramImageToImageMetric
bool m_UseExplicitPDFDerivatives{ true };
bool m_UseFiniteDifferenceDerivative{ false };
double m_FiniteDifferencePerturbation{ 1.0 };

std::mutex m_Mutex{};
};

} // end namespace itk
Expand Down
32 changes: 16 additions & 16 deletions Common/CostFunctions/itkParzenWindowHistogramImageToImageMetric.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ ParzenWindowHistogramImageToImageMetric<TFixedImage, TMovingImage>::InitializeHi
*/
m_JointPDF = JointPDFType::New();
m_JointPDF->SetRegions(JointPDFSizeType{ m_NumberOfMovingHistogramBins, m_NumberOfFixedHistogramBins });
m_JointPDF->Allocate();
m_JointPDF->AllocateInitialized();

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note to myself: It might not be sufficient to zero-initialize the values here in InitializeHistograms(). Maybe ComputePDFs should (also) do m_JointPDF->FillBuffer(0.0) before calling m_Threader->SetSingleMethodAndExecute.


if (this->GetUseDerivative())
{
Expand Down Expand Up @@ -1046,6 +1046,21 @@ ParzenWindowHistogramImageToImageMetric<TFixedImage, TMovingImage>::ThreadedComp
m_ParzenWindowHistogramGetValueAndDerivativePerThreadVariables[threadId].st_NumberOfPixelsCounted =
numberOfPixelsCounted;

{
const std::lock_guard<std::mutex> lockGuard(m_Mutex);

/** Accumulate joint histogram. */
const ImageBufferRange imageBufferRange(*m_JointPDF);

auto pdfValueIteratorOfThread = ImageBufferRange(*jointPDF).cbegin();

for (PDFValueType & pdfValue : imageBufferRange)
{
pdfValue += *pdfValueIteratorOfThread;
++pdfValueIteratorOfThread;
}
}

} // end ThreadedComputePDFs()


Expand Down Expand Up @@ -1075,21 +1090,6 @@ ParzenWindowHistogramImageToImageMetric<TFixedImage, TMovingImage>::AfterThreade
/** Compute alpha. */
m_Alpha = 1.0 / static_cast<double>(Superclass::m_NumberOfPixelsCounted);

/** Accumulate joint histogram. */
const ImageBufferRange imageBufferRange(*m_JointPDF);

std::fill(imageBufferRange.begin(), imageBufferRange.end(), PDFValueType{});

for (const auto & perThreadStruct : m_ParzenWindowHistogramGetValueAndDerivativePerThreadVariables)
{
auto pdfValueIteratorPerThread = ImageBufferRange(*perThreadStruct.st_JointPDF).cbegin();

for (PDFValueType & pdfValue : imageBufferRange)
{
pdfValue += *pdfValueIteratorPerThread;
++pdfValueIteratorPerThread;
}
}
} // end AfterThreadedComputePDFs()


Expand Down
Loading