コード例 #1
0
class TestORCTOptimization(unittest.TestCase):
    def __init__(self, methodName: str = ...) -> None:
        super().__init__(methodName)
        self.datasetUtils = DataUtils()
        self.evaluation = Evaluation()

    def opt_func(self, X, twoComplement):
        n_particles = X.shape[0]  # number of particles
        costs = []
        for x in X:
            x = np.reshape(x, [2, 2])
            filtered = compute_orct2(compute_orct1(twoComplement, x), x)
            filtered = (filtered + 255) / 2

            def inverseFunction(data):
                data = data.astype('float32') * 2 - 255
                data = compute_orct2inverse(data, x)
                data = compute_orct1inverse(data, x)
                return data

            sampleFunctionReverse = inverseFunction
            psnr, ssim, jpeg2000CompressionRatioAfter, jpeg2000CompressionRatioBefore = self.evaluation.evaluate(
                filtered, twoComplement, sampleFunctionReverse)
            cost = np.abs(
                (1 / psnr) * (1 / ssim) * 1 / (jpeg2000CompressionRatioAfter))
            costs.append(cost)
        return np.array(costs)

    def test_orct12_optimization(self):
        bayer = self.datasetUtils.readCFAImages()

        twoComplement = self.datasetUtils.twoComplementMatrix(bayer)
        twoComplement = twoComplement.astype("float32")

        options = {'c1': 0.5, 'c2': 0.1, 'w': 0.9}
        optimizer = GlobalBestPSO(n_particles=10,
                                  dimensions=4,
                                  options=options)

        costFunction = partial(self.opt_func, twoComplement=twoComplement)
        cost, pos = optimizer.optimize(costFunction, iters=30)

        pass

    def test_orct12_(self):
        pos = np.asarray([0.39182592, 0.23747258, 0.51497874, -0.08751142])
        x = np.reshape(pos, [2, 2])
        # x = np.asarray([[0.05011018, -0.53709484],
        #                 [-1.1104253, -0.30699651]])
        bayer = self.datasetUtils.readCFAImages()

        twoComplement = self.datasetUtils.twoComplementMatrix(bayer)
        twoComplement = twoComplement.astype("float32")

        filtered = compute_orct2(compute_orct1(twoComplement, x), x)

        filtered = (filtered + 255) / 2

        def inverseFunction(data):
            data = data.astype('float32') * 2 - 255
            data = compute_orct2inverse(data, x)
            data = compute_orct1inverse(data, x)
            return data

        sampleFunctionReverse = inverseFunction
        self.evaluation.evaluate(filtered, twoComplement,
                                 sampleFunctionReverse)
        pass

    def test_ocrtOptimizedWithDataset(self):
        pos = np.asarray([0.39182592, 0.23747258, 0.51497874, -0.08751142])
        x = np.reshape(pos, [2, 2])
        rgbImages = self.datasetUtils.loadKodakDataset()
        cfaImages, image_size = self.datasetUtils.convertDatasetToCFA(
            rgbImages)
        bayer = cfaImages[2, :, :]

        twoComplement = self.datasetUtils.twoComplementMatrix(bayer)
        twoComplement = twoComplement.astype("float32")

        filtered = compute_orct2plus3(compute_orct1(twoComplement, x), x)

        filtered = (filtered + 255) / 2

        def inverseFunction(data):
            data = data.astype('float32') * 2 - 255
            data = compute_orct2plus3inverse(data, x)
            data = compute_orct1inverse(data, x)
            return data

        sampleFunctionReverse = inverseFunction
        self.evaluation.evaluate(filtered, bayer, sampleFunctionReverse)
        pass

    def test_multiObjOptimization(self):
        algorithm = NSGA2(pop_size=10,
                          n_offsprings=10,
                          sampling=get_sampling("real_random"),
                          crossover=get_crossover("real_sbx", prob=0.9,
                                                  eta=15),
                          mutation=get_mutation("real_pm", eta=20),
                          eliminate_duplicates=True)
        termination = get_termination("n_gen", 5)
        bayer = self.datasetUtils.readCFAImages()
        twoComplement = self.datasetUtils.twoComplementMatrix(bayer)
        twoComplement = twoComplement.astype("float32")
        problem = MyProblem(twoComplement)
        res = minimize(problem,
                       algorithm,
                       termination,
                       save_history=True,
                       verbose=True)

        # Objective Space
        res.F = 1 / res.F
        plot = Scatter(title="Objective Space")
        plot.add(res.F)
        plot.show()
        print("Best filter{}".format(np.reshape(res.opt[-1].X, [2, 2])))
コード例 #2
0
class TestORCT(unittest.TestCase):

    def __init__(self, methodName: str = ...) -> None:
        super().__init__(methodName)
        self.datasetUtils = DataUtils()
        self.evaluation = Evaluation()
        self.precisionFloatingPoint = 0

    def test_orct1(self):
        bayer = cv2.imread("../Data/image.bmp")
        bayer = np.sum(bayer, axis=2).astype('float64')
        orct1Filtered = compute_orct1(bayer, precisionFloatingPoint=self.precisionFloatingPoint)
        pass

    def test_orct2(self):
        bayer = cv2.imread("../Data/image.bmp")
        bayer = np.sum(bayer, axis=2).astype('float64')
        orct2Filtered = compute_orct2(bayer, precisionFloatingPoint=self.precisionFloatingPoint)
        pass

    def test_orct23PlusReversible(self):
        bayer = cv2.imread("../Data/image.bmp")
        bayer = np.sum(bayer, axis=2).astype('float64')
        orct23Filtered = compute_orct2plus3(bayer, precisionFloatingPoint=self.precisionFloatingPoint)
        orct23FilteredInversed = compute_orct2plus3inverse(orct23Filtered, precisionFloatingPoint=self.precisionFloatingPoint)
        print("PSNR: {}".format(self.evaluation.calculate_psnr(bayer, np.round(orct23FilteredInversed))))

    def test_orct1Reversible(self):
        bayer = cv2.imread("../Data/image.bmp")
        bayer = np.sum(bayer, axis=2).astype('float64')
        orct1Filtered = compute_orct1V2(bayer, precisionFloatingPoint=self.precisionFloatingPoint)
        orct1FilteredInversed = compute_orct1inverseV2(orct1Filtered, precisionFloatingPoint=self.precisionFloatingPoint)
        print("PSNR: {}".format(self.evaluation.calculate_psnr(bayer, np.round(orct1FilteredInversed))))

    def test_orct123Reversible(self):
        bayer = cv2.imread("../Data/image.bmp")
        bayer = np.sum(bayer, axis=2).astype('float64')
        orct1Filtered = compute_orct1V2(bayer, precisionFloatingPoint=self.precisionFloatingPoint)
        orct23Filtered = compute_orct2plus3(orct1Filtered, precisionFloatingPoint=self.precisionFloatingPoint)
        orct23FilteredNormalized = copy.deepcopy(orct23Filtered)
        orct23FilteredNormalized[orct23FilteredNormalized == 0] = -256
        orct23FilteredNormalized = (orct23FilteredNormalized + 256) / 2
        orct23FilteredNormalized = np.ceil(orct23FilteredNormalized)
        orct23FilteredNormalized = orct23FilteredNormalized * 2 - 256
        orct23FilteredNormalized[orct23FilteredNormalized == -256] = 0
        orct23Filtered = orct23FilteredNormalized
        orct23FilteredInversed = compute_orct2plus3inverse(orct23Filtered, precisionFloatingPoint=self.precisionFloatingPoint)
        orct1FilteredInversed = compute_orct1inverseV2(orct23FilteredInversed, precisionFloatingPoint=self.precisionFloatingPoint)

        print("PSNR: {}".format(self.evaluation.calculate_psnr(np.round(orct23FilteredInversed), np.round(orct1Filtered))))
        print("PSNR: {}".format(self.evaluation.calculate_psnr(bayer, np.round(orct1FilteredInversed))))

    def test_orct12(self):
        bayer = self.datasetUtils.readCFAImages()

        bayer = bayer.astype("float64")

        orct1Res = compute_orct1(bayer, precisionFloatingPoint=self.precisionFloatingPoint)
        filtered = compute_orct2(orct1Res, precisionFloatingPoint=self.precisionFloatingPoint)
        # filtered = (filtered)/2

        filtered[filtered == 0] = -256
        filtered = (filtered + 256) / 2

        def inverseFunction(data):
            data = data.astype('float32')
            data = data * 2 - 256
            data[data == -256] = 0
            data = compute_orct2inverse(data, precisionFloatingPoint=self.precisionFloatingPoint)
            data = compute_orct1inverse(data, precisionFloatingPoint=self.precisionFloatingPoint)
            return data

        sampleFunctionReverse = inverseFunction
        self.evaluation.evaluate(filtered, bayer, sampleFunctionReverse, precisionFloatingPoint=self.precisionFloatingPoint)
        pass

    def test_orct123Plus(self):
        bayer = self.datasetUtils.readCFAImages()

        bayer = bayer.astype("float32")

        orct_1 = compute_orct1V2(bayer, precisionFloatingPoint=self.precisionFloatingPoint)
        filtered = compute_orct2plus3(orct_1, precisionFloatingPoint=self.precisionFloatingPoint)

        filtered[filtered == 0] = -256
        filtered = (filtered + 256) / 2
        filtered = np.ceil(filtered)

        def inverseFunction(data):
            data = data.astype('float32')
            data = data * 2 - 256
            data[data == -256] = 0
            data = compute_orct2plus3inverse(data, precisionFloatingPoint=self.precisionFloatingPoint)
            data = compute_orct1inverseV2(data, precisionFloatingPoint=self.precisionFloatingPoint)
            return np.round(data)

        sampleFunctionReverse = inverseFunction
        self.evaluation.evaluate(filtered, bayer, sampleFunctionReverse, precisionFloatingPoint=self.precisionFloatingPoint, roundingMethod="ceil")

        pass

    def test_ocrtShahedWithDataset(self):
        rgbImages = self.datasetUtils.loadArri()
        cfaImages, image_size = self.datasetUtils.convertDatasetToCFA(rgbImages)
        psnrs = []
        ssims = []
        jpeg2000CompressionRatioAfters = []
        JpegLsCompressionRatios = []
        compressionRatioLZWs = []
        compressionRatiojpeg2000LossyAfters = []

        # filtered = (filtered + 128)
        # filtered[:, 1::2] = filtered[:, 1::2] / 2 + 128

        def inverseFunction(data):
            data = data.astype('float32') * 2 - 256

            data = compute_orct1inverse(data, precisionFloatingPoint=self.precisionFloatingPoint)
            return data

        sampleFunctionReverse = inverseFunction

        for bayer in cfaImages:
            bayer = bayer.astype("float32")

            filtered = compute_orct2(compute_orct1(bayer, precisionFloatingPoint=self.precisionFloatingPoint), precisionFloatingPoint=self.precisionFloatingPoint)
            test_sample = filtered
            filtered = (filtered + 256) / 2

            psnr, ssim, jpeg2000CompressionRatioAfter, JpegLsCompressionRatio, compressionRatioLZWAfter, compressionRatiojpeg2000LossyAfter = self.evaluation.evaluate(filtered, bayer,
                                                                                                                                                                       sampleFunctionReverse,
                                                                                                                                                                       precisionFloatingPoint=self.precisionFloatingPoint)

            psnrs.append(psnr)
            ssims.append(ssim)
            jpeg2000CompressionRatioAfters.append(jpeg2000CompressionRatioAfter)
            JpegLsCompressionRatios.append(JpegLsCompressionRatio)
            compressionRatioLZWs.append(compressionRatioLZWAfter)
            compressionRatiojpeg2000LossyAfters.append(compressionRatiojpeg2000LossyAfter)
        pd.DataFrame({"psnr": psnrs, "ssim": ssims, "jpeg200CompressionRatio (bpp)": jpeg2000CompressionRatioAfters,
                      "JpegLsCompressionRatio": JpegLsCompressionRatios,
                      "compressionRatioLZW": compressionRatioLZWs,
                      "compressionRatiojpeg2000Lossy": compressionRatiojpeg2000LossyAfters}).to_excel("resultsShahedMethod.xlsx")

    def test_ocrtNewMethodWithDataset(self):
        psnrs = []
        ssims = []
        jpeg2000CompressionRatioAfters = []
        JpegLsCompressionRatiosAfters = []
        compressionRatioLZWsAfters = []
        jpeg2000CompressionRatioBefores = []
        JpegLsCompressionRatiosBefores = []
        compressionRatioLZWsBefores = []
        datasetName = []
        nameOfdatasets = ["Akademie", "Arri exterior", "Color test chart", "Face", "Kodak", "Lake locked", "Lake pan", "Night Odeplatz", "Nikon D40", "Nikon D90", "Nikon D7000", "Siegestor",
                          "Pool interior"]
        for nameOfdataset in nameOfdatasets:
            print(nameOfdataset)
            if nameOfdataset in ["Kodak", "Nikon D90", "Nikon D7000", "Nikon D40"]:
                self.precisionFloatingPoint = 0
                bias = 256
            else:
                self.precisionFloatingPoint = 4
                bias = 65536
            rgbImages = self.datasetUtils.loadOtherDataset(nameOfdataset)
            cfaImages, image_size = self.datasetUtils.convertDatasetToCFA(rgbImages)

            def inverseFunction(data):
                data = data.astype('float32')
                data = data * 2 - bias
                data = compute_orct2plus3inverse(data, precisionFloatingPoint=self.precisionFloatingPoint)
                data = compute_orct1inverseV2(data, precisionFloatingPoint=self.precisionFloatingPoint)
                return np.round(data)

            sampleFunctionReverse = inverseFunction

            for bayer in cfaImages:
                bayer = bayer.astype("float32")

                filtered = compute_orct2plus3(compute_orct1(bayer, precisionFloatingPoint=self.precisionFloatingPoint), precisionFloatingPoint=self.precisionFloatingPoint)
                test_sample = filtered
                filtered = (filtered + bias) / 2
                filtered = np.ceil(filtered-np.min(filtered))

                psnr, ssim, jpeg2000CompressionRatioAfter, JpegLsCompressionRatioAfter, compressionRatioLZWAfter, jpeg2000CompressionRatioBefore, JpegLsCompressionRatioBefore, compressionRatioLZWBefore = self.evaluation.evaluate(
                    filtered, bayer,
                    sampleFunctionReverse,
                    precisionFloatingPoint=self.precisionFloatingPoint,
                    roundingMethod="ceil")
                datasetName.append(nameOfdataset)
                psnrs.append(psnr)
                ssims.append(ssim)
                jpeg2000CompressionRatioAfters.append(jpeg2000CompressionRatioAfter)
                JpegLsCompressionRatiosAfters.append(JpegLsCompressionRatioAfter)
                compressionRatioLZWsAfters.append(compressionRatioLZWAfter)
                jpeg2000CompressionRatioBefores.append(jpeg2000CompressionRatioBefore)
                JpegLsCompressionRatiosBefores.append(JpegLsCompressionRatioBefore)
                compressionRatioLZWsBefores.append(compressionRatioLZWBefore)

        pd.DataFrame({"Image set name": datasetName,
                      "psnr": psnrs,
                      "ssim": ssims,
                      "jpeg200-LS After": jpeg2000CompressionRatioAfters,
                      "jpeg-Ls After": JpegLsCompressionRatiosAfters,
                      "LZW After": compressionRatioLZWsAfters,
                      "jpeg200-LS Before": jpeg2000CompressionRatioBefores,
                      "jpeg-Ls Before": JpegLsCompressionRatiosBefores,
                      "LZW Before": compressionRatioLZWsBefores}).to_excel("resultsAll2.xlsx")

    def test_simpleORCT(self):
        bayer = np.array([[145, 77, 142, 73], [76, 67, 72, 62], [127, 67, 125, 65], [65, 54, 65, 57],
                          [145, 75, 142, 73], [46, 61, 72, 62], [117, 47, 105, 65], [87, 31, 53, 17]])
        bayer = bayer.astype("float32")
        data = compute_orct2plus3(bayer)
        data2 = compute_orct2plus3inverse(data)
        pass