0

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...

vicjusar
  • 1
  • 1
  • you know about numpy and opencv. browse their tutorials. if you have already identified the set of colors to target, you just need to "quantify" the image according to the color of each pixel. you can use multiple calls of `cv.inRange` for that. or look for questions that ask about "quantization" or "posterization" of images. the idea is the same, except those things replace colors with "a similar color", while you do that with a label instead. this is basically "nearest neighbor" lookup. – Christoph Rackwitz Jun 09 '23 at 11:48
  • Thanks Christoph, (God level profile by the way ;-D). I'm just junior in this task and I needed to read your feedback 4 times to have an idea about what to do. I'll try your proposal. – vicjusar Jun 09 '23 at 14:08
  • Couple of good suggestions in this thread: https://stackoverflow.com/questions/16992713/translate-every-element-in-numpy-array-according-to-key – Nick ODell Jun 09 '23 at 21:46
  • Thansk Nick!, I already visited such question prior posting mine and realised taht those solution replaced each single numbre of tehe RGB tuple. My intent here is to replace the complete RGB tuple by the corresponding one from the dic. – vicjusar Jun 13 '23 at 15:08
  • Hi @ChristophRackwitz, I tried your proposal by using cv.inRange and I was able to extract the masks for the colors of my dic. Unfortunatelly still not able to replace the colors as I do not know how to place the original colors fro the new ones. – vicjusar Jun 13 '23 at 19:44
  • CODE: img= cv2.cvtColor(cv2.imread(test_path), cv2.COLOR_BGR2RGB) mask1 = cv2.inRange(img, (255,128,0), (255,128,0)) mask2 = cv2.inRange(img, (239,89,191), (239,89,191)) mask = cv2.bitwise_or(mask1, mask2) target = cv2.bitwise_and(img,img, mask=mask) cv2.imshow('mask both',mask) cv2.imshow('target',target) – vicjusar Jun 13 '23 at 19:52
  • 1
    Important information for answering this question is how many elements are in the dictionary, and if there are any colors in the input image that are not in the dictionary, and if similar input colors map to the same output color or similar output colors, and how precise the mapping needs to be applied. – Cris Luengo Jun 19 '23 at 12:21
  • 1
    Basically, the way to make this faster is to not use a dictionary. To know with what to replace the dictionary, we need to know more about the task. – Cris Luengo Jun 19 '23 at 12:24
  • Thanks for the reply and the interest Cris (God profile as well...). The dictionary is made by 55 sets of tuples where either the key and the value are tuples. I use the Key tuple to mathc the pic pixel and them I replace it by the value tuple. Dic abstract: replacemet_dic = {(255, 0, 0): (1, 1, 1), (200, 0, 0): (2, 2, 2), (150, 0, 0): (3, 3, 3), (128, 0, 0): (4, 4, 4), (182, 89, 6): (5, 5, 5), (150, 50, 4): (6, 6, 6), (90, 30, 1): (7, 7, 7), (90, 30, 30): (8, 8, 8), (204, 153, 255): (9, 9, 9), ....} – vicjusar Jun 20 '23 at 14:46

0 Answers0