Exemple #1
0
 def get_coeffs(self):
     if self.coeffs is not None:
         return self.coeffs
     import varconv
     m, b = varconv.gen_matrix_system(self.image, self.refimage,
                                      self.badpixmask is not None,
                                      self.badpixmask, self.k_side,
                                      self.poly_deg, self.bkgdegree or -1)
     self.coeffs = np.linalg.solve(m, b)
     return self.coeffs
Exemple #2
0
 def test_gen_matrix_system_constantkernel(self):
     deg = 0
     k_side = 3
     n, m = 10, 10
     image = np.random.random((n, m))
     refimage = image.copy()
     mm, b, c = varconv.gen_matrix_system(image, refimage, 0, None,
                                          k_side, deg, -1)
     coeffs = np.linalg.solve(mm, b)
     kc = k_side // 2
     result_kernel = coeffs.reshape((k_side, k_side))
     best_kernel = np.zeros((k_side, k_side))
     best_kernel[kc, kc] = 1.0
     self.assertLess(np.linalg.norm(result_kernel - best_kernel), 1E-10)
Exemple #3
0
 def test_gen_matrix_system_constantkernel(self):
     deg = 0
     k_side = 3
     n, m = 10, 10
     image = np.random.random((n, m))
     refimage = image.copy()
     mm, b = varconv.gen_matrix_system(image, refimage, 0, None,
                                          k_side, deg, -1)
     coeffs = np.linalg.solve(mm, b)
     kc = k_side // 2
     result_kernel = coeffs.reshape((k_side, k_side))
     best_kernel = np.zeros((k_side, k_side))
     best_kernel[kc, kc] = 1.0
     self.assertLess(np.linalg.norm(result_kernel - best_kernel), 1E-10)
Exemple #4
0
 def test_gen_matrix_system_sizes(self):
     deg = 2
     bkg_deg = 0
     k_side = 3
     n, m = 10, 10
     image = np.random.random((n, m))
     refimage = image.copy()
     mm, b = varconv.gen_matrix_system(image, refimage, 0, None,
                                          k_side, deg, bkg_deg)
     pol_dof = (deg + 1) * (deg + 2) // 2
     bkg_dof = (bkg_deg + 1) * (bkg_deg + 2) // 2
     k_size = k_side * k_side
     m_dof = pol_dof * k_size + bkg_dof
     self.assertEqual(mm.shape, (m_dof, m_dof))
     self.assertEqual(b.shape, (m_dof,))
Exemple #5
0
 def test_gen_matrix_system_sizes(self):
     deg = 2
     bkg_deg = 0
     k_side = 3
     n, m = 10, 10
     image = np.random.random((n, m))
     refimage = image.copy()
     mm, b, c = varconv.gen_matrix_system(image, refimage, 0, None,
                                          k_side, deg, bkg_deg)
     pol_dof = (deg + 1) * (deg + 2) / 2
     bkg_dof = (bkg_deg + 1) * (bkg_deg + 2) / 2
     k_size = k_side * k_side
     m_dof = pol_dof * k_size + bkg_dof
     self.assertEqual(mm.shape, (m_dof, m_dof))
     self.assertEqual(b.shape, (m_dof,))
     self.assertEqual(c.shape, (k_size, pol_dof, bkg_dof, n * m))
Exemple #6
0
 def test_convolve2d_adaptive_undoing(self):
     deg = 2
     k_side = 3
     pol_dof = (deg + 1) * (deg + 2) / 2
     kernel = np.random.random((k_side, k_side, pol_dof))
     refimage = np.random.random((10, 10))
     image = varconv.convolve2d_adaptive(refimage, kernel, deg)
     mm, b, c = varconv.gen_matrix_system(image, refimage, 0, None,
                                          k_side, deg, -1)
     coeffs = np.linalg.solve(mm, b)
     result_kernel = coeffs.reshape((k_side, k_side, pol_dof))
     opt_ref = varconv.convolve2d_adaptive(refimage, result_kernel, deg)
     self.assertLess(np.linalg.norm(opt_ref - image, ord=np.inf) /
                     np.linalg.norm(image, ord=np.inf), 1E-8)
     self.assertLess(np.linalg.norm((kernel - result_kernel).flatten(),
                                    ord=np.inf) /
                     np.linalg.norm(kernel.flatten(), ord=np.inf), 1E-8)
Exemple #7
0
 def test_convolve2d_adaptive_undoing(self):
     deg = 2
     k_side = 3
     pol_dof = (deg + 1) * (deg + 2) // 2
     kernel = np.random.random((k_side, k_side, pol_dof))
     refimage = np.random.random((10, 10))
     image = varconv.convolve2d_adaptive(refimage, kernel, deg)
     mm, b = varconv.gen_matrix_system(image, refimage, 0, None,
                                          k_side, deg, -1)
     coeffs = np.linalg.solve(mm, b)
     result_kernel = coeffs.reshape((k_side, k_side, pol_dof))
     opt_ref = varconv.convolve2d_adaptive(refimage, result_kernel, deg)
     self.assertLess(np.linalg.norm(opt_ref - image, ord=np.inf) /
                     np.linalg.norm(image, ord=np.inf), 1E-8)
     self.assertLess(np.linalg.norm((kernel - result_kernel).flatten(),
                                    ord=np.inf) /
                     np.linalg.norm(kernel.flatten(), ord=np.inf), 1E-8)
Exemple #8
0
    def test_convolve2d_adaptive_cameraman(self):
        from PIL import Image
        this_dir = os.path.abspath(os.path.dirname(__file__))
        cameraman_path = os.path.join(this_dir, "cameraman.tif")
        refimage = np.array(Image.open(cameraman_path), dtype='float64')

        # degrade reference
        deg = 2
        k_side = 3
        pol_dof = (deg + 1) * (deg + 2) / 2
        kernel = np.random.random((k_side, k_side, pol_dof))
        image = varconv.convolve2d_adaptive(refimage, kernel, deg)

        mm, b, c = varconv.gen_matrix_system(image, refimage, 0, None,
                                             k_side, deg, -1)
        coeffs = np.linalg.solve(mm, b)
        result_kernel = coeffs.reshape((k_side, k_side, pol_dof))

        opt_ref = varconv.convolve2d_adaptive(refimage, result_kernel, deg)
        self.assertLess(np.linalg.norm(opt_ref - image, ord=np.inf) /
                        np.linalg.norm(image, ord=np.inf), 1E-8)
Exemple #9
0
    def test_convolve2d_adaptive_cameraman(self):
        from PIL import Image
        this_dir = os.path.abspath(os.path.dirname(__file__))
        cameraman_path = os.path.join(this_dir, "cameraman.tif")
        refimage = np.array(Image.open(cameraman_path), dtype='float64')

        # degrade reference
        deg = 2
        k_side = 3
        pol_dof = (deg + 1) * (deg + 2) // 2
        kernel = np.random.random((k_side, k_side, pol_dof))
        image = varconv.convolve2d_adaptive(refimage, kernel, deg)

        mm, b = varconv.gen_matrix_system(image, refimage, 0, None,
                                             k_side, deg, -1)
        coeffs = np.linalg.solve(mm, b)
        result_kernel = coeffs.reshape((k_side, k_side, pol_dof))

        opt_ref = varconv.convolve2d_adaptive(refimage, result_kernel, deg)
        self.assertLess(np.linalg.norm(opt_ref - image, ord=np.inf) /
                        np.linalg.norm(image, ord=np.inf), 1E-8)
Exemple #10
0
    def make_system(self):
        import varconv

        # Check here for types
        if self.image_data.dtype != np.float64:
            img64 = self.image_data.astype('float64')
        else:
            img64 = self.image_data
        if self.refimage_data.dtype != np.float64:
            ref64 = self.refimage_data.astype('float64')
        else:
            ref64 = self.refimage_data

        c_bkgdegree = -1 if self.bkgdegree is None else self.bkgdegree
        m, b, conv = varconv.gen_matrix_system(img64, ref64,
                                               self.badpixmask is not None,
                                               self.badpixmask,
                                               self.k_side, self.poly_deg,
                                               c_bkgdegree)
        coeffs = np.linalg.solve(m, b)
        poly_dof = (self.poly_deg + 1) * (self.poly_deg + 2) / 2
        k_dof = self.k_side * self.k_side * poly_dof
        ks = self.k_side
        self.kernel = coeffs[:k_dof].reshape((ks, ks, self.poly_dof))
        opt_conv = varconv.convolve2d_adaptive(ref64, self.kernel,
                                               self.poly_deg)
        if self.bkgdegree is not None:
            self.background = self.coeffstobackground(coeffs[k_dof:])
            self.optimal_image = opt_conv + self.background
        else:
            self.background = np.zeros(self.image.shape)
            self.optimal_image = opt_conv

        if self.badpixmask is not None:
            self.optimal_image = np.ma.array(self.optimal_image,
                                             mask=self.badpixmask)

        self.difference = self.image - self.optimal_image