【问题标题】:Replacement of circular spots by respective colors用各自的颜色替换圆形斑点
【发布时间】:2019-02-11 18:59:30
【问题描述】:

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

例如,如果遮罩图中(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)

【问题讨论】:

  • 我看不出你在哪里使用轮廓。或者你为什么要使用轮廓。
  • 我的错误..它应该是斑点,我现在纠正了它
  • 不要使用轮廓。你在这里不需要它们。
  • 你能告诉我如何用相应专色的颜色替换整个专色网格
  • 你不能把第二张图片叠加在第一张上,让黑色背景变得透明吗?

标签: python image-processing connected-components


【解决方案1】:

有一种非常简单的方法可以完成这项任务。第一个需要对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))

【讨论】:

  • 看起来不错。但是打开似乎也不适用于彩色图像以将斑点放大到相同的大小......所有斑点的大小应该相同。
  • @ChinmayAthavale:我发布了错误的图片,我现在已经修复了。
  • 还有一个疑问..有什么方法可以标记斑点并访问它们,以便我可以找到单个斑点强度并按标签打印
  • 如果您使用的是 PyDIP,您可以使用dip.Labeldip.MeasurementTool.Measure 在此处获取输出图像中每个点的平均 RGB 值。您还可以使用骨架的输出(单像素掩码),找到每个设置像素的坐标(您将获得每个点的一组坐标),然后简单地读取这些坐标处的输入图像。简单地对这些像素坐标进行排序就会给你一个标签。
  • 你能给我这个opencv代码吗?我无法搜索类似的 opencv 函数,也无法安装 PyDIP,因为它不在 pypi 上
猜你喜欢
  • 1970-01-01
  • 2018-08-02
  • 2010-12-31
  • 1970-01-01
  • 2014-09-07
  • 1970-01-01
  • 1970-01-01
  • 2017-07-01
  • 1970-01-01
相关资源
最近更新 更多