544 """Find streaks in a masked image.
548 maskedImage : `lsst.afw.image.maskedImage`
549 The image in which to search for streaks.
553 result : `lsst.pipe.base.Struct`
554 Results as a struct with attributes:
557 Lines identified by kernel hough transform.
559 Lines grouped into clusters in rho-theta space.
561 Final result for lines after line-profile fit.
563 2-d boolean mask where detected lines are True.
565 mask = maskedImage.mask
566 detectionMask = (mask.array & mask.getPlaneBitMask(self.config.detectedMaskPlane))
571 badPixelMask = mask.getPlaneBitMask(self.config.badMaskPlanes)
572 badMaskSpanSet = SpanSet.fromMask(mask, badPixelMask).split()
573 for sset
in badMaskSpanSet:
574 sset_dilated = sset.dilated(1)
575 sset_dilated.clippedTo(mask.getBBox()).setMask(mask, mask.getPlaneBitMask(
"BAD"))
576 dilatedBadMask = (mask.array & badPixelMask) > 0
577 self.
edges = initEdges & ~dilatedBadMask
580 if len(self.
lines) == 0:
581 lineMask = np.zeros(detectionMask.shape, dtype=bool)
586 fitLines, lineMask = self.
_fitProfile(clusters, maskedImage)
588 if self.config.onlyMaskDetected:
590 lineMask &= detectionMask.astype(bool)
592 return pipeBase.Struct(
594 lineClusters=clusters,
595 originalLines=self.
lines,
600 def run(self, maskedImage):
601 """Find and mask streaks in a masked image.
603 Finds streaks in the image and modifies maskedImage in place by adding a
604 mask plane with any identified streaks.
608 maskedImage : `lsst.afw.image.Exposure` or `lsst.afw.image.maskedImage`
609 The image in which to search for streaks. The mask detection plane
610 corresponding to `config.detectedMaskPlane` must be set with the
611 detected pixels. The mask will have a plane added with any detected
612 streaks, and with the mask plane name set by
613 self.config.streaksMaskPlane.
617 result : `lsst.pipe.base.Struct`
618 Results as a struct with attributes:
621 Lines identified by kernel hough transform.
623 Lines grouped into clusters in rho-theta space.
625 Final result for lines after line-profile fit.
627 streaks = self.
find(maskedImage)
629 if (self.config.streaksMaskPlane !=
"STREAK")
and \
630 (self.config.streaksMaskPlane
not in maskedImage.mask.getMaskPlaneDict()):
631 maskedImage.mask.addMaskPlane(self.config.streaksMaskPlane)
632 maskedImage.mask.array[streaks.mask] |= maskedImage.mask.getPlaneBitMask(self.config.streaksMaskPlane)
634 return pipeBase.Struct(
636 lineClusters=streaks.lineClusters,
637 originalLines=streaks.originalLines,
659 """Run Kernel Hough Transform on image.
664 2-d image data on which to detect lines.
668 result : `LineCollection`
669 Collection of detected lines, with their detected rho and theta
672 lines = lsst.kht.find_lines(image, self.config.clusterMinimumSize,
673 self.config.clusterMinimumDeviation, self.config.delta,
674 self.config.minimumKernelHeight, self.config.nSigma,
675 self.config.absMinimumKernelHeight)
676 self.log.info(
"The Kernel Hough Transform detected %s line(s)", len(lines))
681 """Group lines that are close in parameter space and likely describe
686 lines : `LineCollection`
687 Collection of lines to group into clusters.
691 result : `LineCollection`
692 Average `Line` for each cluster of `Line`s in the input
699 x = lines.rhos / self.config.rhoBinSize
700 y = lines.thetas / self.config.thetaBinSize
701 X = np.array([x, y]).T
710 kmeans = KMeans(n_clusters=nClusters, n_init=
'auto').fit(X)
711 clusterStandardDeviations = np.zeros((nClusters, 2))
712 for c
in range(nClusters):
713 inCluster = X[kmeans.labels_ == c]
714 clusterStandardDeviations[c] = np.std(inCluster, axis=0)
716 if (clusterStandardDeviations <= 1).all():
721 finalClusters = kmeans.cluster_centers_.T
724 finalRhos = finalClusters[0] * self.config.rhoBinSize
725 finalThetas = finalClusters[1] * self.config.thetaBinSize
727 self.log.info(
"Lines were grouped into %s potential streak(s)", len(finalRhos))
732 """Fit the profile of the streak.
734 Given the initial parameters of detected lines, fit a model for the
735 streak to the original (non-binary image). The assumed model is a
736 straight line with a Moffat profile.
740 lines : `LineCollection`
741 Collection of guesses for `Line`s detected in the image.
742 maskedImage : `lsst.afw.image.maskedImage`
743 Original image to be used to fit profile of streak.
747 lineFits : `LineCollection`
748 Collection of `Line` profiles fit to the data.
749 finalMask : `np.ndarray`
750 2d mask array with detected streaks=1.
752 data = maskedImage.image.array
753 weights = maskedImage.variance.array**-1
754 mask = maskedImage.mask
755 badPixelMask = mask.getPlaneBitMask(self.config.badMaskPlanes)
756 badMask = (mask.array & badPixelMask) > 0
758 weights[~np.isfinite(weights) | ~np.isfinite(data)] = 0
762 finalLineMasks = [np.zeros(data.shape, dtype=bool)]
766 line.sigma = self.config.invSigma**-1
769 if lineModel.lineMaskSize == 0:
772 fit, fitFailure = lineModel.fit(dChi2Tol=self.config.dChi2Tolerance, log=self.log,
773 maxIter=self.config.maxFitIter)
777 if ((abs(fit.rho - line.rho) > 2 * self.config.rhoBinSize)
778 or (abs(fit.theta - line.theta) > 2 * self.config.thetaBinSize)):
780 self.log.debug(
"Streak fit moved too far from initial estimate. Line will be dropped.")
787 lineModel.setLineMask(fit, self.config.maxStreakWidth, self.config.nSigmaMask, logger=self.log)
788 finalModel = lineModel.makeProfile(fit)
790 finalModelMax = abs(finalModel).max()
791 finalLineMask = abs(finalModel) > self.config.footprintThreshold
794 if not finalLineMask.any():
796 fit.modelMaximum = finalModelMax
798 finalLineMasks.append(finalLineMask)
802 self.log.info(
"Streak profile could not be fit for %d out of %d detected lines.", nFitFailures,
804 finalMask = np.array(finalLineMasks).any(axis=0)
806 return lineFits, finalMask