def reconstruct(self, ratio, sr_image): """ Reconstruct the SR image by the given ratio. :param ratio: reconstruct ratio :type ratio: float :param sr_image: original SR image :type sr_image: L{sr_image.SRImage} :return: reconstructed SR image :rtype: L{sr_image.SRImage} """ # import matplotlib.pyplot as plt sr_dataset = SRDataSet.from_sr_image(sr_image) reconstructed_sr_image = sr_image construct_level = 1 # int(math.log(ratio, ALPHA) + 0.5) r = ALPHA for level in xrange(construct_level): reconstructed_sr_image = self._reconstruct(r, reconstructed_sr_image, sr_dataset) reconstructed_sr_image = sr_image_util.back_project( reconstructed_sr_image, sr_image, 3, level + 1) new_sr_dataset = SRDataSet.from_sr_image(reconstructed_sr_image) # plt.imshow(reconstructed_sr_image) # plt.show() sr_dataset.merge(new_sr_dataset) sys.stdout.write("\rReconstructing %.2f%%" % (float(level + 1) / construct_level * 100)) sys.stdout.flush() return reconstructed_sr_image
def reconstruct(self, ratio, sr_image): """Reconstruct the SR image by the given ratio. @param ratio: reconstruct ratio @type ratio: float @param sr_image: original SR image @type sr_image: L{sr_image.SRImage} @return: reconstructed SR image @rtype: L{sr_image.SRImage} """ sr_dataset = SRDataSet.from_sr_image(sr_image) reconstructed_sr_image = sr_image construct_level = int(math.log(ratio, ALPHA) + 0.5) r = ALPHA for level in range(construct_level): reconstructed_sr_image = self._reconstruct(r, reconstructed_sr_image, sr_dataset) reconstructed_sr_image = sr_image_util.back_project( reconstructed_sr_image, sr_image, 3, level + 1) new_sr_dataset = SRDataSet.from_sr_image(reconstructed_sr_image) sr_dataset.merge(new_sr_dataset) sys.stdout.write("\rReconstructing %.2f%%" % (float(level + 1) / construct_level * 100)) sys.stdout.flush() return reconstructed_sr_image
class TestSRDataset(unittest.TestCase): def setUp(self): self.sr_dataset = SRDataSet(np.array([[1, 1], [2, 2]]), np.array([[2, 2], [3, 3]]), neighbors=1) def test_add(self): self.sr_dataset.add(np.array([[3, 3], [4, 4]]), np.array([[5, 5], [6, 6]])) expected_low_res_patches = np.array([[1, 1], [2, 2], [3, 3], [4, 4]]) expected_high_res_patches = np.array([[2, 2], [3, 3], [5, 5], [6, 6]]) self.assertTrue( np.array_equal(expected_low_res_patches, self.sr_dataset.low_res_patches)) self.assertTrue( np.array_equal(expected_high_res_patches, self.sr_dataset.high_res_patches)) def test_query(self): low_res_patches = np.array([[1, 1]]) high_res_patches = self.sr_dataset.query(low_res_patches) expected_high_res_patches = np.array([[[2, 2]]]) self.assertTrue( np.array_equal(expected_high_res_patches, high_res_patches)) def test_from_sr_image(self): sr_image = sr_image_factory.SRImageFactory.create_sr_image_from( "test_data/babyface_4.png") sr_dataset = SRDataSet.from_sr_image(sr_image) low_res_patches = sr_dataset.low_res_patches high_res_patches = sr_dataset.high_res_patches self.assertEqual(np.shape(low_res_patches), np.shape(high_res_patches))
class TestSRDataset(unittest.TestCase): def setUp(self): self.sr_dataset = SRDataSet(np.array([[1, 1], [2, 2]]), np.array([[2, 2], [3, 3]]), neighbors=1) def test_add(self): self.sr_dataset.add(np.array([[3, 3], [4, 4]]), np.array([[5, 5], [6, 6]])) expected_low_res_patches = np.array([[1, 1], [2, 2], [3, 3], [4, 4]]) expected_high_res_patches = np.array([[2, 2], [3, 3], [5, 5], [6, 6]]) self.assertTrue(np.array_equal(expected_low_res_patches, self.sr_dataset.low_res_patches)) self.assertTrue(np.array_equal(expected_high_res_patches, self.sr_dataset.high_res_patches)) def test_query(self): low_res_patches = np.array([[1, 1]]) high_res_patches = self.sr_dataset.query(low_res_patches) expected_high_res_patches = np.array([[[2, 2]]]) self.assertTrue(np.array_equal(expected_high_res_patches, high_res_patches)) def test_from_sr_image(self): sr_image = sr_image_factory.SRImageFactory.create_sr_image_from("test_data/babyface_4.png") sr_dataset = SRDataSet.from_sr_image(sr_image) low_res_patches = sr_dataset.low_res_patches high_res_patches = sr_dataset.high_res_patches self.assertEqual(np.shape(low_res_patches), np.shape(high_res_patches))
def test_from_sr_image(self): sr_image = sr_image_factory.SRImageFactory.create_sr_image_from( "test_data/babyface_4.png") sr_dataset = SRDataSet.from_sr_image(sr_image) low_res_patches = sr_dataset.low_res_patches high_res_patches = sr_dataset.high_res_patches self.assertEqual(np.shape(low_res_patches), np.shape(high_res_patches))
class TestSRDataset(unittest.TestCase): def setUp(self): self.sr_dataset = SRDataSet(np.array([[1, 1], [2, 2]]), np.array([[2, 2], [3, 3]])) def test_add(self): self.sr_dataset.add(np.array([[3, 3], [4, 4]]), np.array([[5, 5], [6, 6]])) expected_low_res_patches = np.array([[1, 1], [2, 2], [3, 3], [4, 4]]) expected_high_res_patches = np.array([[2, 2], [3, 3], [5, 5], [6, 6]]) self.assertTrue(np.array_equal(expected_low_res_patches, self.sr_dataset.low_res_patches)) self.assertTrue(np.array_equal(expected_high_res_patches, self.sr_dataset.high_res_patches)) def test_query(self): low_res_patches = np.array([[1, 1]]) high_res_patches = self.sr_dataset.query(low_res_patches, neighbors=1) expected_high_res_patches = np.array([[2, 2]]) self.assertTrue(np.array_equal(expected_high_res_patches, high_res_patches))
def reconstruct(self, ratio, sr_image): """Reconstruct the SR image by the given ratio. @param ratio: reconstruct ratio @type ratio: float @param sr_image: original SR image @type sr_image: L{sr_image.SRImage} @return: reconstructed SR image @rtype: L{sr_image.SRImage} """ sr_dataset = SRDataSet.from_sr_image(sr_image) reconstructed_sr_image = sr_image r = ratio ** (1.0/DEFAULT_RECONSTRUCT_LEVEL) for level in range(DEFAULT_RECONSTRUCT_LEVEL): reconstructed_sr_image = self._reconstruct(r, reconstructed_sr_image, sr_dataset) new_sr_dataset = SRDataSet.from_sr_image(reconstructed_sr_image) sr_dataset.merge(new_sr_dataset) return reconstructed_sr_image
def reconstruct(self, ratio, sr_image): """Reconstruct the SR image by the given ratio. @param ratio: reconstruct ratio @type ratio: float @param sr_image: original SR image @type sr_image: L{sr_image.SRImage} @return: reconstructed SR image @rtype: L{sr_image.SRImage} """ sr_dataset = SRDataSet.from_sr_image(sr_image) reconstructed_sr_image = sr_image construct_level = int(math.log(ratio, ALPHA) + 0.5) r = ALPHA for level in range(construct_level): reconstructed_sr_image = self._reconstruct(r, reconstructed_sr_image, sr_dataset) reconstructed_sr_image = sr_image_util.back_project(reconstructed_sr_image, sr_image, 3, level+1) new_sr_dataset = SRDataSet.from_sr_image(reconstructed_sr_image) sr_dataset.merge(new_sr_dataset) sys.stdout.write("\rReconstructing %.2f%%" % (float(level+1) / construct_level * 100)) sys.stdout.flush() return reconstructed_sr_image
def setUp(self): self.sr_dataset = SRDataSet(np.array([[1, 1], [2, 2]]), np.array([[2, 2], [3, 3]]), neighbors=1)
def setUp(self): self.sr_dataset = SRDataSet(np.array([[1, 1], [2, 2]]), np.array([[2, 2], [3, 3]]))
def test_from_sr_image(self): sr_image = sr_image_factory.SRImageFactory.create_sr_image_from("test_data/babyface_4.png") sr_dataset = SRDataSet.from_sr_image(sr_image) low_res_patches = sr_dataset.low_res_patches high_res_patches = sr_dataset.high_res_patches self.assertEqual(np.shape(low_res_patches), np.shape(high_res_patches))