def test_notNumpyArrayCenter(self):
        polarImage, ptSettings = polarTransform.convertToPolarImage(self.shortAxisApexImage, center=(401, 365))
        np.testing.assert_array_equal(ptSettings.center, np.array([401, 365]))
        np.testing.assert_almost_equal(polarImage, self.shortAxisApexPolarImage)

        polarImage, ptSettings = polarTransform.convertToPolarImage(self.shortAxisApexImage, center=(401, 365))
        np.testing.assert_array_equal(ptSettings.center, np.array([401, 365]))
        np.testing.assert_almost_equal(polarImage, self.shortAxisApexPolarImage)
Esempio n. 2
0
def polar_hull(slc):
    """Find the largest convex region using a polar transform."""
    old_dtype = slc.dtype
    # Convert to polar coordinates
    slc = slc.astype('uint8') * 256
    center = center_of_mass(slc)
    center_is_valid = not np.any(np.isnan(center))
    if not center_is_valid:
        log.warning("Unvalid center by center of mass method. "
                    "Defaulting to center of image")
        center = None
    log.debug("Performing polar transform")
    pim, ptSettings = polarTransform.convertToPolarImage(slc, center=center)
    log.debug("Applying anisotropic morphology filters to polar image.")
    pim = pim.astype('bool')
    pim = ndimage.median_filter(pim, size=9)
    pim = closing(pim, selem=np.ones((10, 1)))
    pim = opening(pim, selem=np.ones((1, 10)))
    # Mesh grid in order to compare edge positions
    my, mx = np.mgrid[:pim.shape[0], :pim.shape[1]]
    # Find the outside of the shape in the x direction
    log.debug("Calculating hull boundary")
    edge_r_idx = pim.shape[1] - np.argmax(pim[:, ::-1], axis=1)
    edge_r_idx[edge_r_idx == pim.shape[1]] = 0
    edge_r = mx < edge_r_idx[:, np.newaxis]
    edge_r = ndimage.median_filter(edge_r, size=9)
    log.debug("Performing inverse polar transform")
    hull, uptSetting = polarTransform.convertToCartesianImage(
        edge_r.astype('uint8') * 256, settings=ptSettings)
    return hull.astype(old_dtype)
    def test_polarConversion(self):
        polarImage, ptSettings = polarTransform.convertToPolarImage(
            self.shortAxisApexImage, center=np.array([401, 365]))

        np.testing.assert_array_equal(
            ptSettings.getPolarPointsImage([401, 365]), np.array([0, 0]))
        np.testing.assert_array_equal(
            ptSettings.getPolarPointsImage([[401, 365], [401, 365]]),
            np.array([[0, 0], [0, 0]]))

        np.testing.assert_array_equal(
            ptSettings.getPolarPointsImage((401, 365)), np.array([0, 0]))
        np.testing.assert_array_equal(
            ptSettings.getPolarPointsImage(((401, 365), (401, 365))),
            np.array([[0, 0], [0, 0]]))

        np.testing.assert_array_equal(
            ptSettings.getPolarPointsImage(np.array([401, 365])),
            np.array([0, 0]))
        np.testing.assert_array_equal(
            ptSettings.getPolarPointsImage(np.array([[401, 365], [401, 365]])),
            np.array([[0, 0], [0, 0]]))

        # Fails here
        np.testing.assert_array_equal(
            ptSettings.getPolarPointsImage([[451, 365], [401, 400], [348, 365],
                                            [401, 305]]),
            np.array([[50 * 802 / 543, 0], [35 * 802 / 543, 400],
                      [53 * 802 / 543, 800], [60 * 802 / 543, 1200]]))
Esempio n. 4
0
def generateVerticalLinesBorders():
    polarImage, ptSettings = polarTransform.convertToPolarImage(verticalLinesImage, border='constant', borderVal=128.0)
    saveImage('verticalLinesPolarImageBorders.png', polarImage)

    ptSettings.cartesianImageSize = (500, 500)
    ptSettings.center = np.array([250, 250])
    cartesianImage = ptSettings.convertToCartesianImage(polarImage, border='constant', borderVal=255.0)
    saveImage('verticalLinesCartesianImageBorders2.png', cartesianImage)
Esempio n. 5
0
def generateVerticalLinesBorders2():
    polarImage, ptSettings = polarTransform.convertToPolarImage(verticalLinesImage, border='nearest')
    saveImage('verticalLinesPolarImageBorders3.png', polarImage)

    ptSettings.cartesianImageSize = (500, 500)
    ptSettings.center = np.array([250, 250])
    cartesianImage = ptSettings.convertToCartesianImage(polarImage, border='nearest')
    saveImage('verticalLinesCartesianImageBorders4.png', cartesianImage)
Esempio n. 6
0
def polartransform_image(img, angle):
    img, _ = polarTransform.convertToPolarImage(img,
                                                initialAngle=angle,
                                                finalAngle=np.pi * 2 + angle,
                                                hasColor=True,
                                                border='nearest')
    img = img.transpose(1, 0, 2)
    return np.clip(img, 0, 1)
Esempio n. 7
0
def generateVerticalLinesPolar4():
    polarImage, ptSettings = polarTransform.convertToPolarImage(
        verticalLinesImage,
        initialRadius=30,
        finalRadius=100,
        initialAngle=2 / 4 * np.pi,
        finalAngle=5 / 4 * np.pi)
    saveImage('verticalLinesPolarImage_scaled3.png', polarImage)
    def test_final_radius(self):
        polarImage, ptSettings = polarTransform.convertToPolarImage(self.shortAxisApexImage,
                                                                    center=np.array([350, 365]))

        np.testing.assert_array_equal(ptSettings.center, np.array([350, 365]))
        self.assertEqual(ptSettings.initialRadius, 0)
        self.assertEqual(ptSettings.finalRadius, 580)
        self.assertEqual(ptSettings.initialAngle, 0.0)
        self.assertEqual(ptSettings.finalAngle, 2 * np.pi)
        self.assertEqual(ptSettings.cartesianImageSize, self.shortAxisApexImage.shape)
        self.assertEqual(ptSettings.polarImageSize, (898, 1600))
Esempio n. 9
0
def generateVerticalLinesPolar2():
    polarImage, ptSettings = polarTransform.convertToPolarImage(
        verticalLinesImage,
        initialRadius=30,
        finalRadius=100,
        initialAngle=2 / 4 * np.pi,
        finalAngle=5 / 4 * np.pi,
        radiusSize=140,
        angleSize=700,
        hasColor=True)
    saveImage('verticalLinesPolarImage_scaled.png', polarImage)
Esempio n. 10
0
def cart2polar(image):
    # 128x128x? -> 180x103x?
    if image.shape[0] != 128 or image.shape[1] != 128:
        print('ERROR! wrong image size: ' + str(image.shape))
        return None
    polarImage, _ = pt.convertToPolarImage(image,
                                           hasColor=True,
                                           finalRadius=64,
                                           radiusSize=103,
                                           angleSize=180)
    return polarImage
    def test_3d_support_rgb(self):
        polarImage, ptSettings = polarTransform.convertToPolarImage(self.verticalLinesAnimated, hasColor=True)

        np.testing.assert_array_equal(ptSettings.center, np.array([128, 128]))
        self.assertEqual(ptSettings.initialRadius, 0)
        self.assertEqual(ptSettings.finalRadius, 182)
        self.assertEqual(ptSettings.initialAngle, 0.0)
        self.assertEqual(ptSettings.finalAngle, 2 * np.pi)
        self.assertEqual(ptSettings.cartesianImageSize, self.verticalLinesAnimated.shape[-3:-1])
        self.assertEqual(ptSettings.polarImageSize, (1024, 256))

        np.testing.assert_almost_equal(polarImage, self.verticalLinesAnimatedPolar)
    def test_defaultCenter(self):
        polarImage, ptSettings = polarTransform.convertToPolarImage(self.shortAxisApexImage)

        np.testing.assert_array_equal(ptSettings.center, np.array([400, 304]))
        self.assertEqual(ptSettings.initialRadius, 0)
        self.assertEqual(ptSettings.finalRadius, 503)
        self.assertEqual(ptSettings.initialAngle, 0.0)
        self.assertEqual(ptSettings.finalAngle, 2 * np.pi)
        self.assertEqual(ptSettings.cartesianImageSize, self.shortAxisApexImage.shape)
        self.assertEqual(ptSettings.polarImageSize, (800, 1600))

        np.testing.assert_almost_equal(polarImage, self.shortAxisApexPolarImage_centerMiddle)
    def test_3d_support_grayscale(self):
        polarImage, ptSettings = polarTransform.convertToPolarImage(self.horizontalLinesAnimated)

        np.testing.assert_array_equal(ptSettings.center, np.array([512, 384]))
        self.assertEqual(ptSettings.initialRadius, 0)
        self.assertEqual(ptSettings.finalRadius, 640)
        self.assertEqual(ptSettings.initialAngle, 0.0)
        self.assertEqual(ptSettings.finalAngle, 2 * np.pi)
        self.assertEqual(ptSettings.cartesianImageSize, self.horizontalLinesAnimated.shape[0:2])
        self.assertEqual(ptSettings.polarImageSize, (1024, 2048))

        np.testing.assert_almost_equal(polarImage, self.horizontalLinesAnimatedPolar)
    def test_3d_support_rgb_multithreaded(self):
        polarImage, ptSettings = polarTransform.convertToPolarImage(self.verticalLinesAnimated, useMultiThreading=True)

        np.testing.assert_array_equal(ptSettings.center, np.array([128, 128]))
        self.assertEqual(ptSettings.initialRadius, 0)
        self.assertEqual(ptSettings.finalRadius, 182)
        self.assertEqual(ptSettings.initialAngle, 0.0)
        self.assertEqual(ptSettings.finalAngle, 2 * np.pi)
        self.assertEqual(ptSettings.cartesianImageSize, self.verticalLinesAnimated.shape[0:2])
        self.assertEqual(ptSettings.polarImageSize, (256, 1024))

        np.testing.assert_almost_equal(polarImage, self.verticalLinesAnimatedPolar)
    def test_settings(self):
        polarImage1, ptSettings1 = polarTransform.convertToPolarImage(self.verticalLinesImage,
                                                                      initialRadius=30, finalRadius=100,
                                                                      initialAngle=2 / 4 * np.pi,
                                                                      finalAngle=5 / 4 * np.pi, radiusSize=140,
                                                                      angleSize=700)

        polarImage, ptSettings = polarTransform.convertToPolarImage(self.verticalLinesImage, settings=ptSettings1)

        np.testing.assert_array_equal(ptSettings.center, np.array([128, 128]))
        self.assertEqual(ptSettings.initialRadius, 30)
        self.assertEqual(ptSettings.finalRadius, 100)
        self.assertEqual(ptSettings.initialAngle, 2 / 4 * np.pi)
        self.assertEqual(ptSettings.finalAngle, 5 / 4 * np.pi)
        self.assertEqual(ptSettings.cartesianImageSize, self.verticalLinesImage.shape[0:2])
        self.assertEqual(ptSettings.polarImageSize, (140, 700))

        np.testing.assert_almost_equal(polarImage, self.verticalLinesPolarImage_scaled)

        polarImage2 = ptSettings1.convertToPolarImage(self.verticalLinesImage)
        np.testing.assert_almost_equal(polarImage2, self.verticalLinesPolarImage_scaled)
    def test_IFRadius(self):
        polarImage, ptSettings = polarTransform.convertToPolarImage(self.verticalLinesImage, initialRadius=30,
                                                                    finalRadius=100)

        np.testing.assert_array_equal(ptSettings.center, np.array([128, 128]))
        self.assertEqual(ptSettings.initialRadius, 30)
        self.assertEqual(ptSettings.finalRadius, 100)
        self.assertEqual(ptSettings.initialAngle, 0.0)
        self.assertEqual(ptSettings.finalAngle, 2 * np.pi)
        self.assertEqual(ptSettings.cartesianImageSize, self.verticalLinesImage.shape[0:2])
        self.assertEqual(ptSettings.polarImageSize, (99, 1024))

        np.testing.assert_almost_equal(polarImage, self.verticalLinesPolarImage_scaled2)
    def test_default_horizontalLines(self):
        polarImage, ptSettings = polarTransform.convertToPolarImage(self.horizontalLinesImage)

        np.testing.assert_array_equal(ptSettings.center, np.array([512, 384]))
        self.assertEqual(ptSettings.initialRadius, 0)
        # sqrt(512^2 + 384^2) maximum distance = 640
        self.assertEqual(ptSettings.finalRadius, 640)
        self.assertEqual(ptSettings.initialAngle, 0.0)
        self.assertEqual(ptSettings.finalAngle, 2 * np.pi)
        self.assertEqual(ptSettings.cartesianImageSize, self.horizontalLinesImage.shape)
        self.assertEqual(ptSettings.polarImageSize, (1024, 2048))

        np.testing.assert_almost_equal(polarImage, self.horizontalLinesPolarImage)
    def test_default(self):
        polarImage, ptSettings = polarTransform.convertToPolarImage(self.shortAxisApexImage,
                                                                    center=np.array([401, 365]))

        np.testing.assert_array_equal(ptSettings.center, np.array([401, 365]))
        self.assertEqual(ptSettings.initialRadius, 0)
        self.assertEqual(ptSettings.finalRadius, 543)
        self.assertEqual(ptSettings.initialAngle, 0.0)
        self.assertEqual(ptSettings.finalAngle, 2 * np.pi)
        self.assertEqual(ptSettings.cartesianImageSize, self.shortAxisApexImage.shape)
        self.assertEqual(ptSettings.polarImageSize, (1600, 802))

        np.testing.assert_almost_equal(polarImage, self.shortAxisApexPolarImage)
    def test_3d_support_grayscale_multithreaded(self):
        polarImage, ptSettings = polarTransform.convertToPolarImage(self.horizontalLinesAnimated,
                                                                    useMultiThreading=True)

        np.testing.assert_array_equal(ptSettings.center, np.array([512, 384]))
        self.assertEqual(ptSettings.initialRadius, 0)
        self.assertEqual(ptSettings.finalRadius, 640)
        self.assertEqual(ptSettings.initialAngle, 0.0)
        self.assertEqual(ptSettings.finalAngle, 2 * np.pi)
        self.assertEqual(ptSettings.cartesianImageSize, self.horizontalLinesAnimated.shape[-2:])
        self.assertEqual(ptSettings.polarImageSize, (2048, 1024))

        np.testing.assert_almost_equal(polarImage, self.horizontalLinesAnimatedPolar)
    def test_borders2(self):
        polarImage, ptSettings = polarTransform.convertToPolarImage(
            self.verticalLinesImage, hasColor=True, border='nearest')

        np.testing.assert_almost_equal(polarImage,
                                       self.verticalLinesPolarImageBorders3)

        ptSettings.cartesianImageSize = (500, 500)
        ptSettings.center = np.array([250, 250])
        cartesianImage = ptSettings.convertToCartesianImage(polarImage,
                                                            border='nearest')

        np.testing.assert_almost_equal(
            cartesianImage, self.verticalLinesCartesianImageBorders4)
    def test_IFRadiusAngle(self):
        polarImage, ptSettings = polarTransform.convertToPolarImage(self.verticalLinesImage, initialRadius=30,
                                                                    finalRadius=100, initialAngle=2 / 4 * np.pi,
                                                                    finalAngle=5 / 4 * np.pi, hasColor=True)

        np.testing.assert_array_equal(ptSettings.center, np.array([128, 128]))
        self.assertEqual(ptSettings.initialRadius, 30)
        self.assertEqual(ptSettings.finalRadius, 100)
        self.assertEqual(ptSettings.initialAngle, 2 / 4 * np.pi)
        self.assertEqual(ptSettings.finalAngle, 5 / 4 * np.pi)
        self.assertEqual(ptSettings.cartesianImageSize, self.verticalLinesImage.shape[-3:-1])
        self.assertEqual(ptSettings.polarImageSize, (384, 99))

        np.testing.assert_almost_equal(polarImage, self.verticalLinesPolarImage_scaled3)
    def test_borders(self):
        polarImage, ptSettings = polarTransform.convertToPolarImage(
            self.verticalLinesImage, border='constant', borderVal=128.0)

        np.testing.assert_almost_equal(polarImage,
                                       self.verticalLinesPolarImageBorders)

        ptSettings.cartesianImageSize = (500, 500)
        ptSettings.center = np.array([250, 250])
        cartesianImage = ptSettings.convertToCartesianImage(polarImage,
                                                            border='constant',
                                                            borderVal=255.0)

        np.testing.assert_almost_equal(
            cartesianImage, self.verticalLinesCartesianImageBorders2)
    def test_default(self):
        polarImage, ptSettings = polarTransform.convertToPolarImage(
            self.shortAxisApexImage, center=np.array([401, 365]))

        cartesianImage = ptSettings.convertToCartesianImage(polarImage)

        np.testing.assert_array_equal(ptSettings.center, np.array([401, 365]))
        self.assertEqual(ptSettings.initialRadius, 0)
        self.assertEqual(ptSettings.finalRadius, 543)
        self.assertEqual(ptSettings.initialAngle, 0.0)
        self.assertEqual(ptSettings.finalAngle, 2 * np.pi)
        self.assertEqual(ptSettings.cartesianImageSize, (608, 800))
        self.assertEqual(ptSettings.polarImageSize,
                         self.shortAxisApexPolarImage.shape[-2:])

        assert_image_approx_equal_average(cartesianImage,
                                          self.shortAxisApexImage, 5)
Esempio n. 24
0
def generateHorizontalLinesAnimatedPolar():
    frameSize = 40
    ptSettings = None
    polarFrames = []

    for frame in horizontalLinesAnimated:
        # Call convert to polar image on each frame, uses the assumption that individual 2D image works fine based on
        # other tests
        if ptSettings:
            polarFrame = ptSettings.convertToPolarImage(frame)
        else:
            polarFrame, ptSettings = polarTransform.convertToPolarImage(frame)

        polarFrames.append(polarFrame)

    polarImage3D = np.stack(polarFrames, axis=0)
    saveVideo('horizontalLinesAnimatedPolar.avi', polarImage3D)
Esempio n. 25
0
def invertImageRadial(image, isCentroid):
    #imagePolar is the polar image and can be saved, the function takes in unpolarized image, centre of image, the radial and angular bounds, then the dimensions of polar image (radius followed by angle)
    imagePolar, bla = polarTransform.convertToPolarImage(
        image, (60, 60), 0, 59, 0, pi2, 120, 720)
    #the following lines add along radius strips, to extract distribution
    sumAtRadius = []
    for i in range(120):
        sum = 0
        for j in range(720):
            sum += imagePolar[j][i]
        average = sum / 720
        sumAtRadius.append(average)
    #if the distribution is the reference, then the values are normalized to its max value
    if isCentroid:
        global maxValue
        maxValue = np.amax(sumAtRadius)
    sumAtRadius = np.divide(sumAtRadius, maxValue)
    return sumAtRadius
    def test_default2(self):
        polarImage1, ptSettings = polarTransform.convertToPolarImage(
            self.shortAxisApexImage,
            center=np.array([401, 365]),
            radiusSize=2000,
            angleSize=4000)

        cartesianImage = ptSettings.convertToCartesianImage(polarImage1)
        ptSettings.polarImageSize = self.shortAxisApexPolarImage.shape[0:2]
        polarImage = ptSettings.convertToPolarImage(cartesianImage)

        np.testing.assert_array_equal(ptSettings.center, np.array([401, 365]))
        self.assertEqual(ptSettings.initialRadius, 0)
        self.assertEqual(ptSettings.finalRadius, 543)
        self.assertEqual(ptSettings.initialAngle, 0.0)
        self.assertEqual(ptSettings.finalAngle, 2 * np.pi)
        self.assertEqual(ptSettings.cartesianImageSize, (608, 800))
        self.assertEqual(ptSettings.polarImageSize,
                         self.shortAxisApexPolarImage.shape[0:2])

        assert_image_equal(polarImage, self.shortAxisApexPolarImage, 10)
Esempio n. 27
0
def generateVerticalLinesPolar3():
    polarImage, ptSettings = polarTransform.convertToPolarImage(verticalLinesImage, initialRadius=30,
                                                                finalRadius=100)
    saveImage('verticalLinesPolarImage_scaled2.png', polarImage)
Esempio n. 28
0
def generateVerticalLinesPolar():
    polarImage, ptSettings = polarTransform.convertToPolarImage(verticalLinesImage)
    saveImage('verticalLinesPolarImage.png', polarImage)
Esempio n. 29
0
def generateShortAxisPolar2():
    polarImage, ptSettings = polarTransform.convertToPolarImage(shortAxisApexImage)
    saveImage('shortAxisApexPolarImage_centerMiddle.png', polarImage)
Esempio n. 30
0
def generateShortAxisPolar():
    polarImage, ptSettings = polarTransform.convertToPolarImage(shortAxisApexImage, center=[401, 365])
    saveImage('shortAxisApexPolarImage.png', polarImage)