label_save_path = '/data/gaofen/gaofen3/v1/{}/labels'.format(image_set) wwtool.mkdir_or_exist(label_save_path) # print(os.listdir(label_path)) for idx, label_file in enumerate(os.listdir(label_path)): print(idx, label_file) file_name = label_file.split('.xml')[0] label_file = os.path.join(label_path, file_name + '.xml') image_file = os.path.join(image_path, file_name + '.tif') img = imread(image_file) objects = wwtool.rovoc_parse(label_file) bboxes = np.array([obj['bbox'] for obj in objects]) subimages = wwtool.split_image(img, subsize=1024, gap=200) subimage_coordinates = list(subimages.keys()) bboxes_ = bboxes.copy() for subimage_coordinate in subimage_coordinates: objects = [] bboxes_[:, 0] = bboxes[:, 0] - subimage_coordinate[0] bboxes_[:, 1] = bboxes[:, 1] - subimage_coordinate[1] cx_bool = np.logical_and(bboxes_[:, 0] >= 0, bboxes_[:, 0] < 1024) cy_bool = np.logical_and(bboxes_[:, 1] >= 0, bboxes_[:, 1] < 1024) subimage_bboxes = bboxes_[np.logical_and(cx_bool, cy_bool)] if len(subimage_bboxes) == 0:
def split_image(self, image_fn): if not image_fn.endswith('.jpg'): return image_file = os.path.join(self.image_path, image_fn) shp_file = os.path.join(self.merged_shp_path, image_fn.replace('jpg', 'shp')) geo_file = os.path.join(self.geo_path, image_fn.replace('jpg', 'png')) file_name = os.path.splitext(os.path.basename(image_file))[0] if not os.path.exists(shp_file): return img = cv2.imread(image_file) geo_info = rio.open(geo_file) objects = self.shp_parser(shp_file, geo_info) masks = np.array([obj['segmentation'] for obj in objects]) subimages = wwtool.split_image(img, subsize=self.subimage_size, gap=self.gap) subimage_coordinates = list(subimages.keys()) if masks.shape[0] == 0: return mask_centroids = [] for obj in objects: geometry = obj['converted_polygon'].centroid geo = geojson.Feature(geometry=geometry, properties={}) coordinate = geo.geometry["coordinates"] coordinate[0], coordinate[1] = abs(coordinate[0]), abs(coordinate[1]) mask_centroids.append(coordinate) mask_centroids = np.array(mask_centroids) mask_centroids_ = mask_centroids.copy() for subimage_coordinate in subimage_coordinates: objects = [] mask_centroids_[:, 0] = mask_centroids[:, 0] - subimage_coordinate[0] mask_centroids_[:, 1] = mask_centroids[:, 1] - subimage_coordinate[1] cx_bool = np.logical_and(mask_centroids_[:, 0] >= 0, mask_centroids_[:, 0] < subimage_size) cy_bool = np.logical_and(mask_centroids_[:, 1] >= 0, mask_centroids_[:, 1] < subimage_size) subimage_masks = masks[np.logical_and(cx_bool, cy_bool)] subimage_masks_ = [] for subimage_mask in subimage_masks: subimage_mask_np = np.array(subimage_mask) subimage_mask_np[0::2] = subimage_mask_np[0::2] - subimage_coordinate[0] subimage_mask_np[1::2] = subimage_mask_np[1::2] - subimage_coordinate[1] subimage_masks_.append(subimage_mask_np.tolist()) subimage_masks = subimage_masks_ if len(subimage_masks) == 0: continue img = subimages[subimage_coordinate] if np.mean(img) == 0: continue label_save_file = os.path.join(self.label_save_path, '{}__{}_{}.txt'.format(file_name, subimage_coordinate[0], subimage_coordinate[1])) image_save_file = os.path.join(self.image_save_path, '{}__{}_{}.png'.format(file_name, subimage_coordinate[0], subimage_coordinate[1])) cv2.imwrite(image_save_file, img) for subimage_mask in subimage_masks: subimage_objects = dict() subimage_objects['mask'] = subimage_mask subimage_objects['label'] = 'building' objects.append(subimage_objects) wwtool.simpletxt_dump(objects, label_save_file, encode='mask')
def split_image(self, image_fn): if not image_fn.endswith('.jpg'): return image_file = os.path.join(self.image_path, image_fn) shp_file = os.path.join(self.roof_shp_path, image_fn.replace('jpg', 'shp')) geo_file = image_file file_name = os.path.splitext(os.path.basename(image_file))[0] if not os.path.exists(shp_file): return img = cv2.imread(image_file) geo_info = rio.open(geo_file) objects = self.shp_parser(shp_file, geo_info, coord='pixel', ignore_file=None, merge_flag=True, connection_mode='floor') masks = np.array([obj['segmentation'] for obj in objects]) subimages = wwtool.split_image(img, subsize=self.subimage_size, gap=self.gap) subimage_coordinates = list(subimages.keys()) if masks.shape[0] == 0: return mask_centroids = [] for obj in objects: geometry = obj['converted_polygon'].centroid geo = geojson.Feature(geometry=geometry, properties={}) coordinate = geo.geometry["coordinates"] coordinate[0], coordinate[1] = abs(coordinate[0]), abs( coordinate[1]) mask_centroids.append(coordinate) mask_centroids = np.array(mask_centroids) mask_centroids_ = mask_centroids.copy() for subimage_coordinate in subimage_coordinates: objects = [] mask_centroids_[:, 0] = mask_centroids[:, 0] - subimage_coordinate[0] mask_centroids_[:, 1] = mask_centroids[:, 1] - subimage_coordinate[1] cx_bool = np.logical_and(mask_centroids_[:, 0] >= 0, mask_centroids_[:, 0] < subimage_size) cy_bool = np.logical_and(mask_centroids_[:, 1] >= 0, mask_centroids_[:, 1] < subimage_size) subimage_masks = masks[np.logical_and(cx_bool, cy_bool)] subimage_masks_ = [] for subimage_mask in subimage_masks: if wwtool.mask2polygon(subimage_mask).area < 5: continue subimage_mask_np = np.array(subimage_mask) subimage_mask_np[ 0::2] = subimage_mask_np[0::2] - subimage_coordinate[0] subimage_mask_np[ 1::2] = subimage_mask_np[1::2] - subimage_coordinate[1] subimage_masks_.append(subimage_mask_np.tolist()) subimage_masks = subimage_masks_ # cut the polygons by image boundary subimage_masks = wwtool.clip_mask(subimage_masks, image_size=(1024, 1024)) # judge whether to drop this subimage drop_flag = self.drop_subimage(subimages, subimage_coordinate, subimage_masks, show=self.show) if drop_flag: continue img = subimages[subimage_coordinate] label_save_file = os.path.join( self.label_save_path, '{}_{}__{}_{}.txt'.format(self.sub_imageset_fold, file_name, subimage_coordinate[0], subimage_coordinate[1])) image_save_file = os.path.join( self.image_save_path, '{}_{}__{}_{}.png'.format(self.sub_imageset_fold, file_name, subimage_coordinate[0], subimage_coordinate[1])) cv2.imwrite(image_save_file, img) for subimage_mask in subimage_masks: subimage_objects = dict() subimage_objects['mask'] = subimage_mask subimage_objects['label'] = 'building' objects.append(subimage_objects) wwtool.simpletxt_dump(objects, label_save_file, encode='mask')
frame_id += 1 continue print(scene_name, video_name, frame_id, image_name) objects = stanford_compus_parse.stanford_compus_parse( scene_name, video_name, frame_id) if objects == []: frame_id += 1 continue bboxes = np.array( [wwtool.xyxy2cxcywh(obj['bbox']) for obj in objects]) labels = np.array([obj['label'] for obj in objects]) subimages = wwtool.split_image(img, subsize=subimage_size, gap=gap, expand_boundary=True) subimage_coordinates = list(subimages.keys()) bboxes_ = bboxes.copy() labels_ = labels.copy() if bboxes_.shape[0] == 0: frame_id += 1 continue for subimage_coordinate in subimage_coordinates: objects = [] bboxes_[:, 0] = bboxes[:, 0] - subimage_coordinate[0] bboxes_[:, 1] = bboxes[:, 1] - subimage_coordinate[1] cx_bool = np.logical_and(bboxes_[:, 0] >= 0, bboxes_[:, 0] < subimage_size)
# print(os.listdir(label_path)) for idx, label_file in enumerate(os.listdir(label_path)): print(idx, label_file) file_name = label_file.split('.txt')[0] label_file = os.path.join(label_path, file_name + '.txt') image_file = os.path.join(image_path, file_name + '.jpg') img = imread(image_file) objects = wwtool.nwpu_parse(label_file) bboxes = np.array( [wwtool.xyxy2cxcywh(obj['bbox']) for obj in objects]) labels = np.array([obj['label'] for obj in objects]) subimages = wwtool.split_image(img, subsize=subimage_size, gap=gap) subimage_coordinates = list(subimages.keys()) bboxes_ = bboxes.copy() labels_ = labels.copy() if bboxes_.shape[0] == 0: continue for subimage_coordinate in subimage_coordinates: objects = [] bboxes_[:, 0] = bboxes[:, 0] - subimage_coordinate[0] bboxes_[:, 1] = bboxes[:, 1] - subimage_coordinate[1] cx_bool = np.logical_and(bboxes_[:, 0] >= 0, bboxes_[:, 0] < subimage_size) cy_bool = np.logical_and(bboxes_[:, 1] >= 0,
from wwtool import split_image, show_image from skimage.io import imread if __name__ == '__main__': # img = np.random.rand(5292, 3371) img_path = '/home/jwwangchn/data/GF/v0/tif' save_path = '/home/jwwangchn/data/GF/v1/vis' for image_name in os.listdir(img_path): mmcv.mkdir_or_exist( os.path.join(save_path, image_name.split('.png')[0])) # img = imread(os.path.join(img_path, image_name)) img = cv2.imread(os.path.join(img_path, image_name)) print("{} has the shape {}".format(image_name, img.shape)) subimages = split_image(img, 1024, 0, mode='keep_all') subimage_coordinates = list(subimages.keys()) print("start point: ", subimage_coordinates) for subimage_coordinate in subimage_coordinates: print("coordinate: {}".format(subimage_coordinate)) img = subimages[subimage_coordinate] if np.mean(img) == 0: continue save_name = os.path.join( save_path, image_name.split('.png')[0], "{}__{}_{}.png".format( image_name.split('.png')[0], subimage_coordinate[0], subimage_coordinate[1])) cv2.imwrite(save_name, img) # show_image(img, wait_time=100, save_name=save_name)
import os import cv2 import numpy as np import mmcv from wwtool import split_image, show_image from skimage.io import imread if __name__ == '__main__': # img = np.random.rand(5292, 3371) img_path = '/data/gaofen/v0/tif' save_path = '/data/gaofen/v1/vis' for image_name in os.listdir(img_path): mmcv.mkdir_or_exist(os.path.join(save_path, image_name.split('.tif')[0])) img = imread(os.path.join(img_path, image_name)) print("{} has the shape {}".format(image_name, img.shape)) subimages = split_image(img, np.minimum(img.shape[0], img.shape[1])//2, 0, mode='drop_boundary') subimage_coordinates = list(subimages.keys()) print("start point: ", subimage_coordinates) for subimage_coordinate in subimage_coordinates: print("coordinate: {}".format(subimage_coordinate)) img = subimages[subimage_coordinate] if np.mean(img) == 0: continue save_name = os.path.join(save_path, image_name.split('.tif')[0], "{}__{}_{}.png".format(image_name.split('.tif')[0], subimage_coordinate[0], subimage_coordinate[1])) cv2.imwrite(save_name, img) # show_image(img, wait_time=100, save_name=save_name)