谷歌 colab_使用Google Colab在Python中将图像和遮罩拆分为多个部分
谷歌 colab
Data labelers use special annotation tools for objects annotation. For example, the Computer Vision Annotation Tool (CVAT) is widely known in computer vision. Naturally, it is more convenient for labelers to work with high-resolution images. This is especially true when you need to mark a large number of objects.
數據標記器使用特殊的注釋工具進行對象注釋。 例如, 計算機視覺注釋工具(CVAT)在計算機視覺中是眾所周知的。 自然,貼標機使用高分辨率圖像更方便。 當您需要標記大量對象時,尤其如此。
In one of the roof segmentation tasks that I participated in, it was necessary to highlight triangular segments, quadrangular segments, other segments and edges of the roof. An example of such markup is shown in the following figure (white color for edges, red color for triangles, green color for quadrangles, blue color for other polygons):
在我參與的屋頂分割任務之一中,有必要突出顯示三角形的部分,四邊形的部分,其他部分和屋頂的邊緣。 下圖顯示了這種標記的一個示例(邊緣為白色,三角形為紅色,四邊形為綠色,其他多邊形為藍色):
The original images were obtained from Google Earth at 2048x1208 pixels. The masks were annotated by data labelers using CVAT at the same resolution. To train the model, images and masks should be in a lower resolution (from 128x128 to 512x512 pixels). It is well known that image splitting is a technique most often used to slice a large image into smaller parts. Thus, the logical solution was to split the images and their corresponding masks into the parts with the same resolution.
原始圖像是從Google地球以2048x1208像素獲得的。 數據標記人員使用CVAT以相同的分辨率對蒙版進行注釋。 要訓??練模型,圖像和遮罩應使用較低的分辨率(從128x128到512x512像素)。 眾所周知,圖像分割是最常用于將大圖像切成較小部分的技術。 因此,邏輯解決方案是將圖像及其對應的蒙版拆分為具有相同分辨率的部分。
All code for splitting was implemented in Google Colab. Let’s take a closer look. Import libraries:
所有拆分代碼均在Google Colab中實現。 讓我們仔細看看。 導入庫:
import osimport sys
import shutil
import glob
import matplotlib.pyplot as plt
import matplotlib.image as mpimg
from PIL import Image
Mount the Google Drive (with images and masks) to Google Colab:
將Google云端硬盤(帶有圖片和遮罩)安裝到Google Colab:
from google.colab import drivedrive.mount('/content/gdrive')
%cd "gdrive/My Drive/File Folder"
A useful function for creating a new directory and recursively deleting the contents of an existing one:
創建新目錄并遞歸刪除現有目錄內容的有用功能:
def dir_create(path):if (os.path.exists(path)) and (os.listdir(path) != []):
shutil.rmtree(path)
os.makedirs(path)
if not os.path.exists(path):
os.makedirs(path)
The crop function that goes over the original image are adjusted to the original image limit and contain the original pixels:
覆蓋原始圖像的裁剪功能被調整為原始圖像限制并包含原始像素:
def crop(input_file, height, width):img = Image.open(input_file)
img_width, img_height = img.size
for i in range(img_height//height):
for j in range(img_width//width):
box = (j*width, i*height, (j+1)*width, (i+1)*height)
yield img.crop(box)
The function for splitting images and masks into smaller parts (the height and width of the cropping window, and the starting number are taken as input parameters):
將圖像和遮罩分割成較小部分的功能(裁剪窗口的高度和寬度以及起始編號均作為輸入參數):
def split(inp_img_dir, inp_msk_dir, out_dir, height, width,start_num):
image_dir = os.path.join(out_dir, 'images')
mask_dir = os.path.join(out_dir, 'masks')
dir_create(out_dir)
dir_create(image_dir)
dir_create(mask_dir)
img_list = [f for f in
os.listdir(inp_img_dir)
if os.path.isfile(os.path.join(inp_img_dir, f))]
file_num = 0
for infile in img_list:
infile_path = os.path.join(inp_img_dir, infile)
for k, piece in enumerate(crop(infile_path,
height, width), start_num):
img = Image.new('RGB', (height, width), 255)
img.paste(piece)
img_path = os.path.join(image_dir,
infile.split('.')[0]+ '_'
+ str(k).zfill(5) + '.png')
img.save(img_path)
infile_path = os.path.join(inp_msk_dir,
infile.split('.')[0] + '.png')
for k, piece in enumerate(crop(infile_path,
height, width), start_num):
msk = Image.new('RGB', (height, width), 255)
msk.paste(piece)
msk_path = os.path.join(mask_dir,
infile.split('.')[0] + '_'
+ str(k).zfill(5) + '.png')
msk.save(msk_path)
file_num += 1
sys.stdout.write("\rFile %s was processed." % file_num)
sys.stdout.flush()
Let’s set the necessary variables:
讓我們設置必要的變量:
inp_img_dir = ‘./input_dir/images’inp_msk_dir = ‘./input_dir/masks’
out_dir = ‘./output_dir’
height = 512
width = 512
start_num = 1
Let’s form a list of files with original images and masks and split them:
讓我們形成一個包含原始圖像和遮罩的文件列表,并將其分割:
input_images_list = glob.glob(inp_img_dir + ‘/*.jpg’)input_masks_list = glob.glob(inp_msk_dir + ‘/*.png’)
split(inp_img_dir, inp_msk_dir, out_dir, height, width, start_num)
As an example, two original images and masks are shown using the following code:
例如,使用以下代碼顯示兩個原始圖像和蒙版:
for i, (image_path, mask_path) in enumerate(zip(input_images_list,input_masks_list)):
fig, [ax1, ax2] = plt.subplots(1, 2, figsize=(18, 9))
image = mpimg.imread(image_path)
mask = mpimg.imread(mask_path)
ax1.set_title(‘Image ‘ + str(i+1))
ax1.imshow(image)
ax2.imshow(mask)
ax2.set_title(‘Mask ‘ + str(i+1))matplotlib modulematplotlib模塊創建
Using the following function, you can show all parts of the splitted image (divided into 8 parts):
使用以下功能,可以顯示分割圖像的所有部分(分為8個部分):
def image_part_plotter(images_list, offset):fig = plt.figure(figsize=(12, 6))
columns = 4
rows = 2
# ax enables access to manipulate each of subplots
ax = []
for i in range(columns*rows):
# create subplot and append to ax
img = mpimg.imread(images_list[i+offset])
ax.append(fig.add_subplot(rows, columns, i+1))
ax[-1].set_title(“image part number “ + str(i+1))
plt.imshow(img)
plt.show() # Render the plot
Let’s see what we got as result of images and masks splitting. For the first image:
讓我們看看由于圖像和蒙版拆分而得到的結果。 對于第一張圖片:
image_part_plotter(output_images_list, 0)image_part_plotter(output_masks_list, 0)matplotlib modulematplotlib模塊創建 image_part_plotter(output_images_list, 8)
image_part_plotter(output_masks_list, 8)matplotlib modulematplotlib模塊創建
Conclusion
結論
The images and masks splitted using the proposed approach are saved with the same file names in different directories, that is, if the file ‘./output_dir/images/1_00001.png’ is in the folder with images, then the file ‘./output_dir/masks/1_00001.png’ will correspond to it in the directory with masks. After splitting the image and mask, you can apply augmentation to each part of it (for example, change brightness, contrast, rotate or flip). To do this, just add the augmentation function.
使用建議的方法分割的圖像和遮罩以相同的文件名保存在不同的目錄中,也就是說,如果文件“ ./output_dir/images/1_00001.png”位于包含圖像的文件夾中,則文件“ ./ output_dir / masks / 1_00001.png'將在帶有掩碼的目錄中與之對應。 分割圖像和遮罩后,可以對圖像的每個部分應用增強(例如,更改亮度,對比度,旋轉或翻轉)。 為此,只需添加增強功能。
Pillow (PIL Fork)
枕頭(PIL前叉)
Computer Vision Annotation Tool (CVAT)
計算機視覺注釋工具(CVAT)
This notebook provides recipes for loading and saving data from external sources
本筆記本提供了從外部源加載和保存數據的方法
翻譯自: https://towardsdatascience.com/images-and-masks-splitting-into-multiple-pieces-in-python-with-google-colab-2f6b2ddcb322
谷歌 colab
總結
以上是生活随笔為你收集整理的谷歌 colab_使用Google Colab在Python中将图像和遮罩拆分为多个部分的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 退税操作流程2022,有以下八个步骤
- 下一篇: 美国人口普查年收入比赛_训练网络对收入进