예제 #1
0
    def test_read_resize_write_larger_values(self):
        '''
        Check if the number different pixels between images is not more than were changed.
        '''

        imgs = np.zeros([10, 32, 64])
        perc_pixels_changed = (10 * 20) / imgs[0].size
        for i in range(10):
            start_r, end_r = i, i + 10
            start_c, end_c = i * 3, i * 3 + 20
            imgs[i, start_r:end_r, start_c:end_c] = 255

        with TemporaryDirectory() as tmp_dir:
            fpaths = [os.path.join(tmp_dir, '0{}.png'.format(i)) for i in range(10)]
            write_fpaths = [os.path.splitext(fpath)[0] + '_write.png' for fpath in fpaths]

            for fpath, img in zip(fpaths, imgs):
                cv2.imwrite(fpath, img)

            for read_fpath, write_fpath in zip(fpaths, write_fpaths):
                read_resize_write(read_fpath, write_fpath, 64, 128, 0.0)

            for read_fpath, write_fpath in zip(fpaths, write_fpaths):
                original_img = cv2.cvtColor(cv2.imread(read_fpath), cv2.COLOR_BGR2GRAY)
                resized_img = cv2.cvtColor(cv2.imread(write_fpath), cv2.COLOR_BGR2GRAY)
                unresized_img = cv2.resize(resized_img, (64, 32))
                diff = original_img - unresized_img
                perc_pixels_diff = np.sum(diff != 0) / diff.size
                self.assertTrue(perc_pixels_diff < perc_pixels_changed) 
예제 #2
0
    def test_read_resize_write_over_tol(self):
        '''
        Files not written if desired and actual aspect ratios over aspect_ratio_tol. 
        '''

        img = np.uint8(np.random.uniform(0, 255, size = (64, 32)))

        with TemporaryDirectory() as tmp_dir:
            read_fpath = os.path.join(tmp_dir, 'dataset' 'test_img.png')
            write_fpath = os.path.join(tmp_dir, 'dataset_resize', 'test_img_resize.png')
            cv2.imwrite(read_fpath, img)

            read_resize_write(read_fpath, write_fpath, 32, 64, aspect_ratio_tol = 1.0)
            self.assertTrue('dataset_resize' not in os.listdir(tmp_dir))
예제 #3
0
    def test_read_resize_write_ValueError(self):
        '''
        Negative aspect_ratio_tol raises ValueError.
        '''

        imgs = [np.uint8(np.random.uniform(0, 255, size = (64, 128))) for _ in range(10)]

        with TemporaryDirectory() as tmp_dir:
            fpaths = [os.path.join(tmp_dir, '0{}.png'.format(i)) for i in range(10)]
            write_fpaths = [os.path.splitext(fpath)[0] + '_write.png' for fpath in fpaths]

            for fpath, img in zip(fpaths, imgs):
                cv2.imwrite(fpath, img)

            with self.assertRaises(ValueError):
                read_resize_write(fpaths[0], write_fpaths[0], 32, 64, -10.0)
예제 #4
0
    def test_read_resize_write_n_files(self):
        '''
        Number of files in directory equals number of files resized and written.
        '''

        imgs = [np.uint8(np.random.uniform(0, 255, size = (64, 128))) for _ in range(10)]

        with TemporaryDirectory() as tmp_dir:
            fpaths = [os.path.join(tmp_dir, '0{}.png'.format(i)) for i in range(10)]
            write_fpaths = [os.path.splitext(fpath)[0] + '_write.png' for fpath in fpaths]

            for fpath, img in zip(fpaths, imgs):
                cv2.imwrite(fpath, img)

            for read_fpath, write_fpath in zip(fpaths, write_fpaths):
                read_resize_write(read_fpath, write_fpath, 32, 64, 0.0)
            
            for fpath in write_fpaths:
                self.assertTrue(os.path.split(fpath)[1] in os.listdir(tmp_dir))
예제 #5
0
    def test_read_resize_write_shape(self):
        '''
        Ouput shapes are equal to the desired shape.
        '''

        imgs = [np.uint8(np.random.uniform(0, 255, size = (64, 128))) for _ in range(10)]

        with TemporaryDirectory() as tmp_dir:
            fpaths = [os.path.join(tmp_dir, '0{}.png'.format(i)) for i in range(10)]
            write_fpaths = [os.path.splitext(fpath)[0] + '_write.png' for fpath in fpaths]

            for fpath, img in zip(fpaths, imgs):
                cv2.imwrite(fpath, img)

            for read_fpath, write_fpath in zip(fpaths, write_fpaths):
                read_resize_write(read_fpath, write_fpath, 32, 64, 0.0)

            for fpath in write_fpaths:
                resized_img = cv2.imread(fpath)
                self.assertCountEqual(resized_img.shape[:2], [32, 64])