{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"pygments_lexer":"ipython3","nbconvert_exporter":"python","version":"3.6.4","file_extension":".py","codemirror_mode":{"name":"ipython","version":3},"name":"python","mimetype":"text/x-python"}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"from pathlib import Path\nimport numpy as np\nimport pandas as pd\nimport scipy.ndimage as ndi\nimport cv2\nimport matplotlib.pyplot as plt\nimport os\n\ndef get_mask(rle, img_width, img_heigth):\n    array = np.fromiter(rle.split(), dtype=np.uint)\n    array = array.reshape((-1, 2)).T\n    array[0] = array[0] - 1\n    starts, lenghts = array\n    mask_decompressed = np.concatenate([np.arange(s, s + l, dtype=np.uint) for s, l in zip(starts, lenghts)])\n    mask_img = np.zeros(img_width * img_heigth, dtype=np.uint8)\n    mask_img[mask_decompressed] = 1\n    mask_img = mask_img.reshape((img_width, img_heigth))\n    return mask_img\n\nos.mkdir(\"./my_train_label\") # 创建新的标签文件夹\ntrain = pd.read_csv(\"../input/sartorius-cell-instance-segmentation/train.csv\") # 读取train.csv文件\ntrain_ids = train['id'].unique().tolist() # 获取样本图片即原始图的名字\nfor my_id in train_ids:\n    annotations = train[train[\"id\"] == my_id]['annotation'].tolist()\n    mask_img = np.zeros((520,704), dtype=np.uint8)\n    for annotation in annotations:\n        now_mask = get_mask(annotation,520,704)\n        mask_img = mask_img + now_mask\n    cv2.imwrite(\"./my_train_label/\" + my_id+\".png\",mask_img*255,[int(cv2.IMWRITE_PNG_COMPRESSION),0]) # 将得到的mask存储到my_train_label文件下名字与原图相同","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2021-12-03T08:18:56.993479Z","iopub.execute_input":"2021-12-03T08:18:56.993753Z","iopub.status.idle":"2021-12-03T08:19:43.422510Z","shell.execute_reply.started":"2021-12-03T08:18:56.993725Z","shell.execute_reply":"2021-12-03T08:19:43.421720Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"execution":{"iopub.status.busy":"2021-12-03T08:18:33.037667Z","iopub.execute_input":"2021-12-03T08:18:33.038077Z","iopub.status.idle":"2021-12-03T08:18:33.041493Z","shell.execute_reply.started":"2021-12-03T08:18:33.038046Z","shell.execute_reply":"2021-12-03T08:18:33.040781Z"},"trusted":true},"execution_count":null,"outputs":[]}]}