谷歌 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.
數(shù)據(jù)標(biāo)記器使用特殊的注釋工具進(jìn)行對(duì)象注釋。 例如, 計(jì)算機(jī)視覺注釋工具(CVAT)在計(jì)算機(jī)視覺中是眾所周知的。 自然,貼標(biāo)機(jī)使用高分辨率圖像更方便。 當(dāng)您需要標(biāo)記大量對(duì)象時(shí),尤其如此。
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):
在我參與的屋頂分割任務(wù)之一中,有必要突出顯示三角形的部分,四邊形的部分,其他部分和屋頂?shù)倪吘墶?下圖顯示了這種標(biāo)記的一個(gè)示例(邊緣為白色,三角形為紅色,四邊形為綠色,其他多邊形為藍(lán)色):
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像素獲得的。 數(shù)據(jù)標(biāo)記人員使用CVAT以相同的分辨率對(duì)蒙版進(jìn)行注釋。 要訓(xùn)??練模型,圖像和遮罩應(yīng)使用較低的分辨率(從128x128到512x512像素)。 眾所周知,圖像分割是最常用于將大圖像切成較小部分的技術(shù)。 因此,邏輯解決方案是將圖像及其對(duì)應(yīng)的蒙版拆分為具有相同分辨率的部分。
All code for splitting was implemented in Google Colab. Let’s take a closer look. Import libraries:
所有拆分代碼均在Google Colab中實(shí)現(xiàn)。 讓我們仔細(xì)看看。 導(dǎo)入庫:
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:
創(chuàng)建新目錄并遞歸刪除現(xiàn)有目錄內(nèi)容的有用功能:
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:
覆蓋原始圖像的裁剪功能被調(diào)整為原始圖像限制并包含原始像素:
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):
將圖像和遮罩分割成較小部分的功能(裁剪窗口的高度和寬度以及起始編號(hào)均作為輸入?yún)?shù)):
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:
讓我們?cè)O(shè)置必要的變量:
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:
讓我們形成一個(gè)包含原始圖像和遮罩的文件列表,并將其分割:
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:
例如,使用以下代碼顯示兩個(gè)原始圖像和蒙版:
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模塊創(chuàng)建
Using the following function, you can show all parts of the splitted image (divided into 8 parts):
使用以下功能,可以顯示分割圖像的所有部分(分為8個(gè)部分):
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:
讓我們看看由于圖像和蒙版拆分而得到的結(jié)果。 對(duì)于第一張圖片:
image_part_plotter(output_images_list, 0)image_part_plotter(output_masks_list, 0)matplotlib modulematplotlib模塊創(chuàng)建 image_part_plotter(output_images_list, 8)
image_part_plotter(output_masks_list, 8)matplotlib modulematplotlib模塊創(chuàng)建
Conclusion
結(jié)論
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'將在帶有掩碼的目錄中與之對(duì)應(yīng)。 分割圖像和遮罩后,可以對(duì)圖像的每個(gè)部分應(yīng)用增強(qiáng)(例如,更改亮度,對(duì)比度,旋轉(zhuǎn)或翻轉(zhuǎn))。 為此,只需添加增強(qiáng)功能。
Pillow (PIL Fork)
枕頭(PIL前叉)
Computer Vision Annotation Tool (CVAT)
計(jì)算機(jī)視覺注釋工具(CVAT)
This notebook provides recipes for loading and saving data from external sources
本筆記本提供了從外部源加載和保存數(shù)據(jù)的方法
翻譯自: https://towardsdatascience.com/images-and-masks-splitting-into-multiple-pieces-in-python-with-google-colab-2f6b2ddcb322
谷歌 colab
總結(jié)
以上是生活随笔為你收集整理的谷歌 colab_使用Google Colab在Python中将图像和遮罩拆分为多个部分的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 退税操作流程2022,有以下八个步骤
- 下一篇: websocket python爬虫_p