Skip to content

Commit 0d03704

Browse files
dzenanzjrojasUNC
andauthoredDec 23, 2020
ENH: add cosmic ray correction (#752)
* ENH: add cosmic ray correction * ENH: Add parameter to turn cosmic ray correction on/off Co-authored-by: jrojasUNC <[email protected]>
1 parent 1aa79e5 commit 0d03704

File tree

2 files changed

+63
-1
lines changed

2 files changed

+63
-1
lines changed
 

‎src/PlusDataCollection/Andor/vtkPlusAndorVideoSource.cxx

+55-1
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ void vtkPlusAndorVideoSource::PrintSelf(ostream& os, vtkIndent indent)
4747
os << indent << "CameraIntrinsics: " << cvCameraIntrinsics << std::endl;
4848
os << indent << "DistanceCoefficients: " << cvDistanceCoefficients << std::endl;
4949
os << indent << "UseFrameCorrections: " << UseFrameCorrections << std::endl;
50+
os << indent << "UseCosmicRayCorrection: " << UseCosmicRayCorrection << std::endl;
5051
os << indent << "FlatCorrection: " << flatCorrection << std::endl;
5152
os << indent << "BiasDarkCorrection: " << biasDarkCorrection << std::endl;
5253
os << indent << "BadPixelCorrection: " << badPixelCorrection << std::endl;
@@ -128,6 +129,7 @@ PlusStatus vtkPlusAndorVideoSource::ReadConfiguration(vtkXMLDataElement* rootCon
128129
XML_READ_BOOL_ATTRIBUTE_OPTIONAL(InitializeCoolerState, deviceConfig);
129130
XML_READ_BOOL_ATTRIBUTE_OPTIONAL(RequireCoolTemp, deviceConfig);
130131
XML_READ_BOOL_ATTRIBUTE_OPTIONAL(UseFrameCorrections, deviceConfig);
132+
XML_READ_BOOL_ATTRIBUTE_OPTIONAL(UseCosmicRayCorrection, deviceConfig)
131133

132134
deviceConfig->GetVectorAttribute("HSSpeed", 2, HSSpeed);
133135
deviceConfig->GetVectorAttribute("OutputSpacing", 3, OutputSpacing);
@@ -173,6 +175,7 @@ PlusStatus vtkPlusAndorVideoSource::WriteConfiguration(vtkXMLDataElement* rootCo
173175
deviceConfig->SetAttribute("BadPixelCorrection", badPixelCorrection.c_str());
174176

175177
XML_WRITE_BOOL_ATTRIBUTE(UseFrameCorrections, deviceConfig);
178+
XML_WRITE_BOOL_ATTRIBUTE(UseCosmicRayCorrection, deviceConfig);
176179

177180
return PLUS_SUCCESS;
178181
}
@@ -588,7 +591,7 @@ void vtkPlusAndorVideoSource::CorrectBadPixels(int binning, cv::Mat& cvIMG)
588591
unsigned endX, endY;
589592
int numCellsToCorrect = resolutionCellsToCorrect.size();
590593
for (uint cell : resolutionCellsToCorrect)
591-
{
594+
{
592595
resolutionCellIndexX = cell - frameSize[0] * (cell / frameSize[0]);
593596
resolutionCellIndexY = cell / frameSize[0];
594597
startX = resolutionCellIndexX - 1;
@@ -634,6 +637,38 @@ void vtkPlusAndorVideoSource::CorrectBadPixels(int binning, cv::Mat& cvIMG)
634637
}
635638
}
636639

640+
// ----------------------------------------------------------------------------
641+
void vtkPlusAndorVideoSource::ApplyCosmicRayCorrection(int bin, cv::Mat& floatImage)
642+
{
643+
int kernelSize = 3;
644+
if (bin < 3)
645+
{
646+
kernelSize = 5;
647+
}
648+
649+
// find and subtract background
650+
cv::Mat meanCols, medianImage, diffImage, medianPixels;
651+
cv::reduce(floatImage, meanCols, 0, cv::REDUCE_AVG, CV_32FC1);
652+
ushort background = (ushort)meanCols.at<float>(0, 0);
653+
cv::subtract(floatImage, background, floatImage);
654+
655+
// idenfify cosmice ray indices
656+
cv::medianBlur(floatImage, medianImage, kernelSize);
657+
cv::subtract(floatImage, medianImage, diffImage);
658+
cv::Mat cosmicInd = (diffImage > 50) & (diffImage > 4 * medianImage);
659+
cv::Mat notCosmicInd = ~cosmicInd;
660+
cosmicInd.convertTo(cosmicInd, floatImage.type());
661+
notCosmicInd.convertTo(notCosmicInd, floatImage.type());
662+
cosmicInd /= 255;
663+
notCosmicInd /= 255;
664+
665+
// use maskes to replace cosmic ray indices with values from median image
666+
medianImage.convertTo(medianImage, floatImage.type());
667+
cv::multiply(notCosmicInd, floatImage, floatImage);
668+
cv::multiply(cosmicInd, medianImage, medianPixels);
669+
floatImage += medianPixels;
670+
}
671+
637672
// ----------------------------------------------------------------------------
638673
void vtkPlusAndorVideoSource::ApplyFrameCorrections(int binning)
639674
{
@@ -648,6 +683,12 @@ void vtkPlusAndorVideoSource::ApplyFrameCorrections(int binning)
648683
cv::subtract(floatImage, cvBiasDarkCorrection, floatImage, cv::noArray(), CV_32FC1);
649684
LOG_INFO("Applied constant bias+dark correction");
650685

686+
if(this->UseCosmicRayCorrection)
687+
{
688+
ApplyCosmicRayCorrection(binning, floatImage);
689+
LOG_INFO("Applied cosmic ray correction");
690+
}
691+
651692
// OpenCV's lens distortion correction
652693
cv::undistort(floatImage, result, cvCameraIntrinsics, cvDistanceCoefficients);
653694
LOG_INFO("Applied lens distortion correction");
@@ -939,6 +980,19 @@ bool vtkPlusAndorVideoSource::GetUseFrameCorrections()
939980
return this->UseFrameCorrections;
940981
}
941982

983+
// ----------------------------------------------------------------------------
984+
PlusStatus vtkPlusAndorVideoSource::SetUseCosmicRayCorrection(bool useCosmicRayCorrection)
985+
{
986+
this->UseCosmicRayCorrection = useCosmicRayCorrection;
987+
return PLUS_SUCCESS;
988+
}
989+
990+
// ----------------------------------------------------------------------------
991+
bool vtkPlusAndorVideoSource::GetUseCosmicRayCorrection()
992+
{
993+
return this->UseCosmicRayCorrection;
994+
}
995+
942996
// ----------------------------------------------------------------------------
943997
PlusStatus vtkPlusAndorVideoSource::SetRequireCoolTemp(bool requireCoolTemp)
944998
{

‎src/PlusDataCollection/Andor/vtkPlusAndorVideoSource.h

+8
Original file line numberDiff line numberDiff line change
@@ -239,6 +239,9 @@ class vtkPlusDataCollectionExport vtkPlusAndorVideoSource: public vtkPlusDevice
239239
/*! Applies correction for bad pixels. */
240240
void CorrectBadPixels(int binning, cv::Mat& cvIMG);
241241

242+
/*! Applies cosmic ray correction. */
243+
void ApplyCosmicRayCorrection(int binning, cv::Mat& floatImage);
244+
242245
/*! Applies bias correction for dark current, flat correction and lens distortion. */
243246
void ApplyFrameCorrections(int binning);
244247

@@ -248,6 +251,10 @@ class vtkPlusDataCollectionExport vtkPlusAndorVideoSource: public vtkPlusDevice
248251
PlusStatus SetUseFrameCorrections(bool UseFrameCorrections);
249252
bool GetUseFrameCorrections();
250253

254+
/*! Flag whether to call ApplyCosmicRayCorrection of BLI acquisitions or not. */
255+
PlusStatus SetUseCosmicRayCorrection(bool UseCosmicRayCorrection);
256+
bool GetUseCosmicRayCorrection();
257+
251258
/*! This will be triggered regularly if this->StartThreadForInternalUpdates is true.
252259
* Framerate is controlled by this->AcquisitionRate. This is meant for debugging.
253260
*/
@@ -281,6 +288,7 @@ class vtkPlusDataCollectionExport vtkPlusAndorVideoSource: public vtkPlusDevice
281288
int VSSpeed = 0; // index
282289
int PreAmpGain = 0;
283290
bool UseFrameCorrections = true;
291+
bool UseCosmicRayCorrection = true;
284292

285293
// TODO: Need to handle differet cases for read/acquisiton modes?
286294

0 commit comments

Comments
 (0)
Please sign in to comment.