Preprocessing RGB image Masks to Segmentation Masks

You can access my work in all areas on my website www.bulentsiyah.com
Search for a command to run...

You can access my work in all areas on my website www.bulentsiyah.com
No comments yet. Be the first to comment.
Softtech, Türkiye'nin lider teknoloji şirketlerinden biri olarak her yıl alanında uzman isimlerin görüşlerini içeren Teknoloji Raporu yayımlamaktadır. Bu yılki rapor, üretken yapay zeka teknolojileri odağında hazırlanmış olup; insanı yapay zekadan ay...
Merhaba👋, günümüzde yapay zeka, hayatın her alanında hızla yer buluyor ve artık hepimizin gündeminde. Bu önemli teknolojiyi daha derinlemesine anlatmak ve uygulamalarından bahsetmek adına, 9-10 Kasım tarihlerinde Koç Üniversitesi, Türkiye İş Bankası...
I who work in the field of Computer Vision have always been curious about creatures that have different abilities from us. Computer Vision is an interdisciplinary scientific field that deals with how computers can gain meaning from digital images or ...

Bilgisayarlı Görü (Computer Vision) alanında çalışmalar yapan biri olarak aslında bizden farklı görme yeteneklerine sahip canlıları merak ettim. Bilgisayarlı Görü, bilgisayarların dijital görüntülerden veya videolardan nasıl bir anlam kazanabileceği...

Merhaba 👋, bir alıntı ile başlayayım: Hayatınız iki yoldan değişir: tanıştığınız insanlar ve okuduğunuz kitaplar vasıtasıyla. Eğer yeni kitaplar okumazsanız ne olur biliyor musunuz? Değişmessiniz. Ve değişmiyorsanız büyümüyorsunuz demektir. Bu kadar...

Hi, with this kernel, I will transfer your RGB color image masks to pre-process and make it ready for the semantic segmentation model.
The first step in training our segmentation model is to prepare the dataset. We would need the input RGB images and the corresponding segmentation images. If you want to make your own dataset, a tool like labelme or GIMP can be used to manually generate the ground truth segmentation masks. Assign each class a unique ID. In the segmentation images, the pixel value should denote the class ID of the corresponding pixel. You can examine for Deep Learning based Semantic Segmentation example

If you have RGB color image masks (like Aerial Semantic Segmentation Drone Dataset RGB_color_masks Folder ) , you can follow the steps below. First of all, our goal is to obtain a pixel-based class image from RGB color image masks as follows.RGB color image maskspixel-based class image masks


The dataset has RGB colored segmentation masks, and there are 24 classes in the dataset. The dataset has a file named class_dict.csv and it contains the name of the classes and their respective RGB values. The contents of the file are as follows:
| name | r | g | b |
| unlabeled | 0 | 0 | 0 |
| paved-area | 128 | 64 | 128 |
| dirt | 130 | 76 | 0 |
| grass | 0 | 102 | 0 |
| gravel | 112 | 103 | 87 |
| water | 28 | 42 | 168 |
| rocks | 48 | 41 | 30 |
| pool | 0 | 50 | 89 |
| vegetation | 107 | 142 | 35 |
| roof | 70 | 70 | 70 |
| wall | 102 | 102 | 156 |
| window | 254 | 228 | 12 |
| door | 254 | 148 | 12 |
| fence | 190 | 153 | 153 |
| fence-pole | 153 | 153 | 153 |
| person | 255 | 22 | 96 |
| dog | 102 | 51 | 0 |
| car | 9 | 143 | 150 |
| bicycle | 119 | 11 | 32 |
| tree | 51 | 51 | 0 |
| bald-tree | 190 | 250 | 190 |
| ar-marker | 112 | 150 | 146 |
| obstacle | 2 | 135 | 115 |
| conflicting | 255 | 0 | 0 |
import os,re
from PIL import Image
import numpy as np
import pandas as pd
import cv2
import matplotlib.pyplot as plt
%matplotlib inline
def atoi(text) :
return int(text) if text.isdigit() else text
def natural_keys(text) :
return [atoi(c) for c in re.split('(\d+)', text)]
read_csv = pd.read_csv('/kaggle/input/semantic-drone-dataset/class_dict_seg.csv',index_col=False,skipinitialspace=True)
read_csv.head()
def segmantation_images(path,new_path, debug_test_num):
filenames = []
for root, dirnames, filenames in os.walk(path):
filenames.sort(key = natural_keys)
rootpath = root
#print(filenames)
count = 0
for item in filenames:
if debug_test_num !=0:
if debug_test_num <= count:
break
count = count + 1
if os.path.isfile(path+item):
f, e = os.path.splitext(item)
image_rgb = Image.open(path+item)
image_rgb = np.asarray(image_rgb)
new_image = np.zeros((image_rgb.shape[0],image_rgb.shape[1],3)).astype('int')
for index, row in read_csv.iterrows():
new_image[(image_rgb[:,:,0]==row.r)&
(image_rgb[:,:,1]==row.g)&
(image_rgb[:,:,2]==row.b)]=np.array([index+1,index+1,index+1]).reshape(1,3)
new_image = new_image[:,:,0]
output_filename = new_path+f+'.png'
cv2.imwrite(output_filename,new_image)
print('writing file: ',output_filename)
else:
print('no file')
print("number of files written: ",count)
debug_test_num = 5 # 5 samples are enough just to see it working. If you set 0, you can do all the datasets.
segmantation_images("/kaggle/input/semantic-drone-dataset/RGB_color_image_masks/RGB_color_image_masks/", "", debug_test_num=debug_test_num)
from PIL import Image
input_image = "/kaggle/input/semantic-drone-dataset/RGB_color_image_masks/RGB_color_image_masks/"+"002.png"
output_image = "002.png"
fig, axs = plt.subplots(1, 2, figsize=(16, 8), constrained_layout=True)
img_orig = Image.open(input_image)
axs[0].imshow(img_orig)
axs[0].grid(False)
output_image = Image.open(output_image)
out_array = np.asarray(output_image)
axs[1].imshow(out_array)
axs[1].grid(False)
