用各自的颜色替换圆点

Replacement of circular spots by respective colors

我这里的objective是将mask_image中的斑点替换成original_image中斑点对应的颜色。我在这里所做的是找到连接的组件并标记它们,但我不知道如何找到相应的标记点并替换它。 我怎样才能把 n 个圆圈放在 n 个对象中并用相应的强度填充它们? 任何帮助将不胜感激。

比如mask图像中(2, 1)的点应该用下图中对应点的颜色来绘制。

遮罩图片http://myfair.software/goethe/images/mask.jpg

原图http://myfair.software/goethe/images/original.jpg

def thresh(img):
    ret , threshold = cv2.threshold(img,5,255,cv2.THRESH_BINARY+cv2.THRESH_OTSU)
    return threshold

def spot_id(img):
    seed_pt = (5, 5)
    fill_color = 0
    mask = np.zeros_like(img)
    kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (3, 3))
    for th in range(5, 255):
        prev_mask = mask.copy()
        mask = cv2.threshold(img, th, 255, cv2.THRESH_BINARY)[1]
        mask = cv2.floodFill(mask, None, seed_pt, fill_color)[1]

        mask = cv2.bitwise_or(mask, prev_mask)

        mask = cv2.morphologyEx(mask, cv2.MORPH_OPEN, kernel)

    #here I labelled them
    n_centers, labels = cv2.connectedComponents(mask)
    label_hue = np.uint8(892*labels/np.max(labels))
    blank_ch = 255*np.ones_like(label_hue)
    labeled_img = cv2.merge([label_hue, blank_ch, blank_ch])
    labeled_img = cv2.cvtColor(labeled_img, cv2.COLOR_HSV2BGR)
    labeled_img[label_hue==0] = 0

    print('There are %d bright spots in the image.'%n_centers)

    cv2.imshow("labeled_img",labeled_img)
    return mask, n_centers

image_thresh = thresh(img_greyscaled)
mask, centers = spot_id(img_greyscaled)

有一种非常简单的方法可以完成此任务。第一个需要采样 mask_image 中每个点中心的值。接下来,扩展此颜色以填充同一图像中的点。

这里是一些使用 PyDIP 的代码(因为我比 OpenCV 更了解它,我是作者),我相信类似的事情可以单独使用 OpenCV 完成:

import PyDIP as dip
import cv2
import numpy as np

# Load the color image shown in the question
original_image = cv2.imread('/home/cris/tmp/BxW25.png')
# Load the mask image shown in the question
mask_image = cv2.imread('/home/cris/tmp/aqf3Z.png')[:,:,0]

# Get a single colored pixel in the middle of each spot of the mask
colors = dip.EuclideanSkeleton(mask_image > 50, 'loose ends away') * original_image

# Spread that color across the full spot
# (dilation and similar operators like this one don't work with color images,
#  so we apply the operation on each channel separately)
for t in range(colors.TensorElements()):
   colors.TensorElement(t).Copy(dip.MorphologicalReconstruction(colors.TensorElement(t), mask_image))

# Save the result
cv2.imwrite('/home/cris/tmp/so.png', np.array(colors))