google-research
113 строк · 4.0 Кб
1# coding=utf-8
2# Copyright 2024 The Google Research Authors.
3#
4# Licensed under the Apache License, Version 2.0 (the "License");
5# you may not use this file except in compliance with the License.
6# You may obtain a copy of the License at
7#
8# http://www.apache.org/licenses/LICENSE-2.0
9#
10# Unless required by applicable law or agreed to in writing, software
11# distributed under the License is distributed on an "AS IS" BASIS,
12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13# See the License for the specific language governing permissions and
14# limitations under the License.
15
16"""Removes LHQ images unsuitable for flying."""
17import os
18
19import cv2 as cv
20import numpy as np
21from PIL import Image
22from tqdm import tqdm
23from utils import midas
24
25lhq = 'dataset/LHQ' # source directory
26# assumes source directory contains:
27# 1) directory lhq_256 of LHQ images at size 256x256
28# 2) directory dpt_depth of DPT depth result on lhq_256 (default setting)
29# 2) directory dpt_seg of DPT segmentation result on lhq_256 (default setting)
30lhq = os.path.abspath(lhq) # get abspath for symlink
31output_dir = 'dataset/lhq_processed'
32os.makedirs(os.path.join(output_dir, 'img'), exist_ok=True)
33os.makedirs(os.path.join(output_dir, 'dpt_depth'), exist_ok=True)
34os.makedirs(os.path.join(output_dir, 'dpt_depth-vis'), exist_ok=True)
35os.makedirs(os.path.join(output_dir, 'dpt_sky'), exist_ok=True)
36os.makedirs(os.path.join(lhq, 'dpt_sky_output'), exist_ok=True)
37
38counter = 0
39total = 90000
40
41for c, i in enumerate(tqdm(range(total))): # 90000))):
42path = os.path.join(lhq, 'dpt_depth', '%07d.pfm' % i)
43disp, scale = midas.read_pfm(path)
44
45# normalize the disparity
46max_disp = np.max(disp)
47min_disp = np.min(disp)
48disp_norm = disp / np.max(disp)
49
50seg = np.array(Image.open(os.path.join(lhq, 'dpt_seg', '%07d_seg.png' % i)))
51### remove small contours
52# sky_mask is 0 in the sky region
53sky_mask = 1 - (seg == 3).astype(np.uint8)
54contours, hierarchy = cv.findContours(sky_mask * 255, 1, 2)
55processing_mask = np.ones_like(sky_mask)
56# processing mask is zero for small contours; one otherwise
57for j, cnt in enumerate(contours):
58area = cv.contourArea(cnt)
59if area < 250:
60cv.drawContours(processing_mask, contours, j, (0, 0, 0), cv.FILLED)
61# zero out small contours, and save the intermediate result
62sky_mask_processed = processing_mask * sky_mask
63np.savez(
64os.path.join(lhq, 'dpt_sky_output', '%07d.npz' % i),
65sky_mask=sky_mask_processed,
66)
67contours_processed, _ = cv.findContours(sky_mask_processed * 255, 1, 2)
68
69keep_img = True
70# too many contours --> trees
71if len(contours_processed) > 3:
72keep_img = False
73# check sky region --> too much non-sky region
74if np.mean(sky_mask_processed) > 0.9:
75keep_img = False
76# check upper part of scene
77h, w = sky_mask_processed.shape
78upper = sky_mask_processed[: h // 5, :]
79if np.mean(upper) > 0.4:
80# too much foreground in upper part of image
81keep_img = False
82# check lower part of scene
83lower = sky_mask_processed[-h // 4 :, :]
84if np.mean(lower) < 0.8:
85# too much sky in lower part of image
86keep_img = False
87# check not too many vertical edges
88if np.percentile(np.abs(disp_norm[:, 1:] - disp_norm[:, :-1]), 99) > 0.05:
89keep_img = False
90
91if keep_img:
92cmd = 'ln -s %s %s' % (
93os.path.join(lhq, 'dpt_depth', '%07d.pfm' % i),
94os.path.join(output_dir, 'dpt_depth'),
95)
96os.system(cmd)
97cmd = 'ln -s %s %s' % (
98os.path.join(lhq, 'lhq_256', '%07d.png' % i),
99os.path.join(output_dir, 'img'),
100)
101os.system(cmd)
102cmd = 'ln -s %s %s' % (
103os.path.join(lhq, 'dpt_sky_output', '%07d.npz' % i),
104os.path.join(output_dir, 'dpt_sky'),
105)
106os.system(cmd)
107counter += 1
108if c < 5000:
109disp_out = (disp - np.min(disp)) / (np.max(disp) - np.min(disp))
110im = Image.fromarray((disp_out * 255).astype(np.uint8))
111im.save(os.path.join(output_dir, 'dpt_depth-vis', '%07d.png' % i))
112
113print('kept %d images %0.2f' % (counter, counter / total))
114