制作RGB蒙版。 cv2.error:/io/opencv/modules/core/src/arithm.cpp:241:错误:(-215)(mtype == 0 || mtype == 1)

时间:2018-03-11 21:26:13

标签: python python-3.x mask scikit-image cv2

我进行了超像素分割,其中有50个不同的区域。

现在我想为每个区域蒙版分配不同的RGB颜色。

由于我有50个片段,我希望获得50种不同的颜色,如下所示:

color_segment=['red','gree,'blue','orange',.....,'magenta'] # 50 different colors

1)我的代码: 此代码通过仅为每个区域分配白色掩码(值255)

def access_pixel_superpixel(image):

    image = cv2.imread(image)
    segments = slic(img_as_float(image), n_segments=50, sigma=5)
    # show the output of SLIC
    fig = plt.figure("Superpixels")
    ax = fig.add_subplot(1, 1, 1)
    ax.imshow(mark_boundaries(img_as_float(cv2.cvtColor(image, cv2.COLOR_BGR2RGB)), segments))
    plt.axis("off")
    plt.show()
    plt.cla()
    plt.close()
    #p=255
    for (i, segVal) in enumerate(np.unique(segments)):
        # construct a mask for the segment
        print("[x] inspecting segment %d" % (i))
        mask = np.zeros(image.shape[:2], dtype="uint8")
        #import ipdb;ipdb.set_trace()
        mask[segments == segVal] = 255



        # show the masked region
        cv2.imshow("Mask", mask)

        cv2.imshow("Applied", cv2.bitwise_and(image, image, mask=mask))
        cv2.waitKey(0)

2)第二次尝试:

此代码仅适用于灰度颜色掩码。只有一个通道从0到255不等。 def access_pixel_superpixel(image):

image = cv2.imread(image)
segments = slic(img_as_float(image), n_segments=50, sigma=5)
# show the output of SLIC
fig = plt.figure("Superpixels")
ax = fig.add_subplot(1, 1, 1)
ax.imshow(mark_boundaries(img_as_float(cv2.cvtColor(image, cv2.COLOR_BGR2RGB)), segments))
plt.axis("off")
plt.show()
plt.cla()
plt.close()
p=255
for (i, segVal) in enumerate(np.unique(segments)):
    # construct a mask for the segment
    print("[x] inspecting segment %d" % (i))
    mask = np.zeros(image.shape[:2], dtype="uint8")
    #import ipdb;ipdb.set_trace()
    mask[segments == segVal] = p
    p -=5



    # show the masked region
    cv2.imshow("Mask", mask)

    cv2.imshow("Applied", cv2.bitwise_and(image, image, mask=mask))
    cv2.waitKey(0)

3)我想得到但不起作用 为每个蒙版分配不同的RGB颜色

color_segment = ['red','gree,'blue','orange',.....,'magenta']#50种不同的颜色

我尝试了红色[255,0,0]如下:

def display_mask_segments(image):

image = cv2.imread(image)
segments = slic(img_as_float(image), n_segments=50, sigma=5)
# show the output of SLIC
fig = plt.figure("Superpixels")
ax = fig.add_subplot(1, 1, 1)
ax.imshow(mark_boundaries(img_as_float(cv2.cvtColor(image, cv2.COLOR_BGR2RGB)), segments))
plt.axis("off")
plt.show()
plt.cla()
plt.close()

for (i, segVal) in enumerate(np.unique(segments)):
    # construct a mask for the segment
    print("[x] inspecting segment %d" % (i))
    mask = np.zeros(image.shape[0:2], dtype="uint8")
    #import ipdb;ipdb.set_trace()
    mask[segments == segVal] = [255,0,0] # red color



    # show the masked region
    cv2.imshow("Mask", mask)

    cv2.imshow("Applied", cv2.bitwise_and(image, image, mask=mask))
    cv2.waitKey(0)

但是我收到以下错误:

 mask[segments == segVal] = [255,0,0]
ValueError: NumPy boolean array indexing assignment cannot assign 3 input values to the 1034 output values where the mask is true 

4)将蒙版形状更改为RGB: 我将我的蒙版更改为RGB形状,以便它可以接收RGB值,例如 [255,0,0]如下:

mask = np.zeros(image.shape, dtype="uint8")
mask[segments == segVal] = [255,0,0]

但是我在以下地址收到以下错误:

cv2.imshow("Applied", cv2.bitwise_and(image, image, mask=mask))

是:

    cv2.imshow("Applied", cv2.bitwise_and(image, image, mask=mask))
cv2.error: /io/opencv/modules/core/src/arithm.cpp:241: error: (-215) (mtype == 0 || mtype == 1) && _mask.sameSize(*psrc1) in function binary_op

5)可视化:

我得到:

resulted

我在找什么:

Looking_for

我的问题是如何制作RGB蒙版?

谢谢

0 个答案:

没有答案