I'm in front of the task to replace the pixels of .png mask files to be used for a semantic segmentation. Files are 1208x1920x3
I made the correspondence between the label original pixel and the class number in dic as:
replacement_dic = {(xxx,yyy,zzz):(cl1,cl1,cl1),....}
I made the code por pixel replacement as follows but takes 20 second per file (Intel® Core™ i7-12700H de 12.ª gen) => too much...
def replace_pixels(path, replacement_dict)
label = cv2.cvtColor(cv2.imread(path),cv2.COLOR_BRG2RGB
hight,width,_ = label.shape
for i in range(height):
for in in range(width):
pixel = tuple(label[i,j])
if pixel in replacement_dic:
label[i,j] = replacement_dic[pixel]
else:
label[i,j] = [0,0,0]
return label
for i in renge(len(all_labels):
new_label = replace_pixeles(all_labels[i], replacement_dic)
cv2.imwrite(all_labels[i], new_label)
Q1: Any idea to make it faster?
Q2: The replacemente class in case of not matachín with dict ([0,0,0]) is well defined or should have to be a tuple as the items in the dic (0,0,0).
Q3: A scheme proposal to do the same in a CNN preprocesing pipe line would be appreciated!
I tried to use tensorflow in a pipeline but there were to much details that I do not kwo who to manage yet...