Маркировка границ суперпикселей изображения в питоне

Я новичок в Python и работаю над проектом обработки изображений, в котором мне нужно разделить любое случайное изображение на суперпиксели и отметить границы сформированных суперпикселей. Код, который я использовал:

def Superpixel(image, path, szExtension, display, title):
    iPixels = 4
    segment_array_store = [] # Blank array to append the superpixels arrays generated
    #slic function is pre-defined fucntion in python and is used to obtain suerpixels for any image
    fStart = time.time() * 1000
    segments = slic(img_as_float(image), compactness = 1000.0, n_segments = iPixels) # Need square superpixels that is why compactness is large 
    fEnd = time.time() * 1000
    fTotal = fEnd - fStart
    if display == 'true':
        #---------- Code to plot the segmented image on command window ------------#
        fig = plt.figure("Superpixels")
        ax = fig.add_subplot(1, 1, 1)
        ax.set_title(title)
        ax.imshow(mark_boundaries(img_as_float(cv2.cvtColor(image, cv2.COLOR_BGR2RGB)), segments, color = (0, 1, 0)))
        plt.axis("off")
        plt.show()
        #--------------------------------------------------------------------------#
    # Loop to access each superpixel individually and also saving it.
    for (i, segVal) in enumerate(np.unique(segments)):
        if display == 'true': # Construct a mask for the segment
            print "[x] inspecting segments: %d" % (i)
        mask = np.zeros(image.shape[:2], dtype = "uint8")
        mask[segments == segVal] = 255
        # Save the array of each segment into variable segment_array
        segment_array = (cv2.bitwise_and(image, image, mask = mask) )
        # Append the array of each segment into one bulk array segment_array_store
        segment_array_store.append(segment_array)
        # Save the segment's array as an image of size same as the size of input image
        cv2.imwrite(path + str(i) + szExtension, segment_array_store[i])
    return i, fTotal # Return the number of superpixels created to the line from where the function was called

я использую slic функция для разделения изображения на суперпиксели и mark_boundaries отметить границы сформированных сегментов. Но проблема в том, что функция slic делит мое изображение на четыре сегмента, но mark_boundaries не отмечает все четыре сегмента. То есть:

OUTPUT:

[x] inspecting segment 1
[x] inspecting segment 2
[x] inspecting segment 3
[x] inspecting segment 4

РЕЗУЛЬТАТ ИЗОБРАЖЕНИЯ:

Почему это так? Почему не все четыре строки приходят? Как я могу исправить это? я делаю какую-либо ошибку в маркировке границ?

0 ответов

Другие вопросы по тегам