예제 #1
0
    def testFunc(self):
        vars = (1, 2, 1)
        self.assertEqual(feval(self.func, vars), 4)

        xopt = nelderMeadModify(self.func, np.array([2.1]), args=(
            2,
            8,
        ))
        self.assertEqual(xopt[0], 2)
예제 #2
0
    def deblendDonut(self, iniGuessXY, magRatio):
        """
        
        Get the deblended donut image.
        
        Arguments:
            iniGuessXY {[float]} -- Initial guess of (x, y) position of neighboring star.
            magRatio {[float]} -- Initial guess of magnitude ratio between neighboring star 
                                  and bright star.
        
        Returns:
            [float] -- Deblended donut image and pixel x, y position.
        """

        # Deblended image
        imgDeblend = []

        # Postion of centroid

        # Get the initial guess of brightest donut
        realcx, realcy, realR, imgBinary = self.getCenterAndR_ef(checkEntropy=True)

        # Remove the salt and pepper noise noise of resImgBinary
        imgBinary = binary_opening(imgBinary).astype(float)
        imgBinary = binary_closing(imgBinary).astype(float)

        # Check the image quality
        if (not realcx):
            return imgDeblend, realcx, realcy

        # Get the binary image by adaptive threshold
        adapcx, adapcy, adapR, adapImgBinary = self.getCenterAndR_adap()

        # Calculate the system error by only taking the background signal
        bg1D = self.image.flatten()
        bgImgBinary1D = adapImgBinary.flatten()
        background = bg1D[bgImgBinary1D==0]
        bgPhist, pgCen = np.histogram(background, bins=256)
        sysError = pgCen[0]

        # Remove the system error
        noSysErrImage = self.image - sysError
        noSysErrImage[noSysErrImage<0] = 0

        # Get the residure map
        resImgBinary = adapImgBinary - imgBinary

        # Compensate the zero element for subtraction
        resImgBinary[np.where(resImgBinary<0)] = 0

        # Remove the salt and pepper noise noise of resImgBinary
        resImgBinary = binary_opening(resImgBinary).astype(float)

        # Calculate the shifts of x and y
        x0 = int(iniGuessXY[0] - realcx)
        y0 = int(iniGuessXY[1] - realcy)

        xoptNeighbor = nelderMeadModify(self.__funcResidue, np.array([x0, y0]), 
                                        args=(imgBinary, resImgBinary), step=15)

        # Shift the main donut image to fitted position of neighboring star 
        fitImgBinary = shift(imgBinary, [int(xoptNeighbor[0][1]), int(xoptNeighbor[0][0])])

        # Handle the numerical error of shift. Regenerate a binary image.
        fitImgBinary[fitImgBinary > 0.5] = 1
        fitImgBinary[fitImgBinary < 0.5] = 0

        # Get the overlap region between main donut and neighboring donut
        imgOverlapBinary = imgBinary + fitImgBinary
        imgOverlapBinary[imgOverlapBinary < 1.5] = 0
        imgOverlapBinary[imgOverlapBinary > 1.5] = 1

        # Get the overall binary image
        imgAllBinary = imgBinary + fitImgBinary
        imgAllBinary[imgAllBinary > 1] = 1

        # Get the reference image for the fitting
        imgRef = noSysErrImage*imgAllBinary

        # Calculate the magnitude ratio of image
        imgMainDonut = noSysErrImage*imgBinary
        imgFit = shift(imgMainDonut, [int(xoptNeighbor[0][1]), int(xoptNeighbor[0][0])])

        xoptMagNeighbor = minimize_scalar(self.__funcMag, bounds = (0, 1), method="bounded",
                                          args=(imgMainDonut, imgOverlapBinary, imgFit, imgRef, xoptNeighbor[0]))

        imgDeblend = imgMainDonut - xoptMagNeighbor.x*imgFit*imgOverlapBinary

        # Repair the boundary of image
        imgDeblend = self.__repairBoundary(imgOverlapBinary, imgBinary, imgDeblend)

        # Calculate the centroid position of donut
        realcy, realcx = center_of_mass(imgBinary)

        return imgDeblend, realcx, realcy
예제 #3
0
    def deblendDonut(self, imgToDeblend, iniGuessXY):
        """Deblend the donut image.

        Parameters
        ----------
        imgToDeblend : numpy.ndarray
            Image to deblend.
        iniGuessXY : list[tuple]
            The list contains the initial guess of (x, y) positions of
            neighboring stars as [star 1, star 2, etc.].

        Returns
        -------
        numpy.ndarray
            Deblended donut image.
        float
            Position x of donut in pixel.
        float
            Position y of donut in pixel.

        Raises
        ------
        ValueError
            Only support to deblend single neighboring star.
        """

        # Check the number of neighboring star
        if len(iniGuessXY) != 1:
            raise ValueError(
                "Only support to deblend single neighboring star.")

        # Get the initial guess of the brightest donut
        imgBinary = self._centroidFind.getImgBinary(imgToDeblend)
        realcx, realcy, realR = self._centroidFind.getCenterAndRfromImgBinary(
            imgBinary)

        # Check the image quality
        if not realcx:
            return np.array([]), realcx, realcy

        # Remove the salt and pepper noise
        imgBinary = binary_opening(imgBinary).astype(float)
        imgBinary = binary_closing(imgBinary).astype(float)

        # Get the binary image by the adaptive threshold method
        imgBinaryAdapt = self._getImgBinaryAdapt(imgToDeblend)

        # Calculate the system error by only taking the background signal
        bg1D = imgToDeblend.flatten()
        bgImgBinary1D = imgBinaryAdapt.flatten()
        background = bg1D[bgImgBinary1D == 0]
        bgPhist, binEdges = np.histogram(background, bins=256)
        sysError = np.mean(binEdges[0:2])

        # Remove the system error
        noSysErrImage = imgToDeblend - sysError
        noSysErrImage[noSysErrImage < 0] = 0

        # Get the residure map
        resImgBinary = imgBinaryAdapt - imgBinary

        # Compensate the zero element for subtraction
        resImgBinary[np.where(resImgBinary < 0)] = 0

        # Remove the salt and pepper noise noise of resImgBinary
        resImgBinary = binary_opening(resImgBinary).astype(float)

        # Calculate the shifts of x and y
        # Only support to deblend single neighboring star at this moment
        starXyNbr = iniGuessXY[0]
        x0 = int(starXyNbr[0] - realcx)
        y0 = int(starXyNbr[1] - realcy)

        xoptNeighbor = nelderMeadModify(
            self._funcResidue,
            np.array([x0, y0]),
            args=(imgBinary, resImgBinary),
            step=15,
        )

        # Shift the main donut image to fitted position of neighboring star
        fitImgBinary = shift(
            imgBinary, [int(xoptNeighbor[0][1]),
                        int(xoptNeighbor[0][0])])

        # Handle the numerical error of shift. Regenerate a binary image.
        fitImgBinary[fitImgBinary > 0.5] = 1
        fitImgBinary[fitImgBinary < 0.5] = 0

        # Get the overlap region between main donut and neighboring donut
        imgOverlapBinary = imgBinary + fitImgBinary
        imgOverlapBinary[imgOverlapBinary < 1.5] = 0
        imgOverlapBinary[imgOverlapBinary > 1.5] = 1

        # Get the overall binary image
        imgAllBinary = imgBinary + fitImgBinary
        imgAllBinary[imgAllBinary > 1] = 1

        # Get the reference image for the fitting
        imgRef = noSysErrImage * imgAllBinary

        # Calculate the magnitude ratio of image
        imgMainDonut = noSysErrImage * imgBinary
        imgFit = shift(imgMainDonut,
                       [int(xoptNeighbor[0][1]),
                        int(xoptNeighbor[0][0])])

        xoptMagNeighbor = minimize_scalar(
            self._funcMag,
            bounds=(0, 1),
            method="bounded",
            args=(imgMainDonut, imgOverlapBinary, imgFit, imgRef,
                  xoptNeighbor[0]),
        )

        imgDeblend = imgMainDonut - xoptMagNeighbor.x * imgFit * imgOverlapBinary

        # Repair the boundary of image
        imgDeblend = self._repairBoundary(imgOverlapBinary, imgBinary,
                                          imgDeblend)

        # Calculate the centroid position of donut
        realcy, realcx = center_of_mass(imgBinary)

        return imgDeblend, realcx, realcy
예제 #4
0
    def deblendDonut(self, iniGuessXY):
        """Get the deblended donut image.

        Parameters
        ----------
        iniGuessXY : tuple or list
            Initial guess of (x, y) position of neighboring star.

        Returns
        -------
        numpy.ndarray
            Deblended donut image.
        float
            Position x in pixel.
        float
            Position y in pixel.
        """

        # Deblended image
        imgDeblend = []

        # Postion of centroid

        # Get the initial guess of brightest donut
        realcx, realcy, realR, imgBinary = self.getCenterAndR_ef(checkEntropy=True)

        # Remove the salt and pepper noise noise of resImgBinary
        imgBinary = binary_opening(imgBinary).astype(float)
        imgBinary = binary_closing(imgBinary).astype(float)

        # Check the image quality
        if (not realcx):
            return imgDeblend, realcx, realcy

        # Get the binary image by adaptive threshold
        adapcx, adapcy, adapR, adapImgBinary = self.getCenterAndR_adap()

        # Calculate the system error by only taking the background signal
        bg1D = self.getImg().flatten()
        bgImgBinary1D = adapImgBinary.flatten()
        background = bg1D[bgImgBinary1D == 0]
        bgPhist, binEdges = np.histogram(background, bins=256)
        sysError = np.mean(binEdges[0:2])

        # Remove the system error
        noSysErrImage = self.getImg() - sysError
        noSysErrImage[noSysErrImage < 0] = 0

        # Get the residure map
        resImgBinary = adapImgBinary - imgBinary

        # Compensate the zero element for subtraction
        resImgBinary[np.where(resImgBinary < 0)] = 0

        # Remove the salt and pepper noise noise of resImgBinary
        resImgBinary = binary_opening(resImgBinary).astype(float)

        # Calculate the shifts of x and y
        x0 = int(iniGuessXY[0] - realcx)
        y0 = int(iniGuessXY[1] - realcy)

        xoptNeighbor = nelderMeadModify(self._funcResidue, np.array([x0, y0]),
                                        args=(imgBinary, resImgBinary), step=15)

        # Shift the main donut image to fitted position of neighboring star
        fitImgBinary = shift(imgBinary, [int(xoptNeighbor[0][1]), int(xoptNeighbor[0][0])])

        # Handle the numerical error of shift. Regenerate a binary image.
        fitImgBinary[fitImgBinary > 0.5] = 1
        fitImgBinary[fitImgBinary < 0.5] = 0

        # Get the overlap region between main donut and neighboring donut
        imgOverlapBinary = imgBinary + fitImgBinary
        imgOverlapBinary[imgOverlapBinary < 1.5] = 0
        imgOverlapBinary[imgOverlapBinary > 1.5] = 1

        # Get the overall binary image
        imgAllBinary = imgBinary + fitImgBinary
        imgAllBinary[imgAllBinary > 1] = 1

        # Get the reference image for the fitting
        imgRef = noSysErrImage*imgAllBinary

        # Calculate the magnitude ratio of image
        imgMainDonut = noSysErrImage*imgBinary
        imgFit = shift(imgMainDonut, [int(xoptNeighbor[0][1]), int(xoptNeighbor[0][0])])

        xoptMagNeighbor = minimize_scalar(
            self._funcMag, bounds=(0, 1), method="bounded",
            args=(imgMainDonut, imgOverlapBinary, imgFit, imgRef, xoptNeighbor[0]))

        imgDeblend = imgMainDonut - xoptMagNeighbor.x*imgFit*imgOverlapBinary

        # Repair the boundary of image
        imgDeblend = self._repairBoundary(imgOverlapBinary, imgBinary, imgDeblend)

        # Calculate the centroid position of donut
        realcy, realcx = center_of_mass(imgBinary)

        return imgDeblend, realcx, realcy