gpt4 book ai didi

python - 如何用Python中区域指定的颜色填充openCV轮廓?

转载 作者:行者123 更新时间:2023-12-02 16:06:42 25 4
gpt4 key购买 nike

我已经对生物细胞进行了分段和二进制化image,并使用openCV提取了轮廓的面积和周长。我试图根据参数q = perimeter / Sqrt(area)用每个单元的颜色图标记和着色,但不知道从哪里开始。基本上,每个单元格都会根据该值具有唯一的颜色。
任何帮助将不胜感激!这是我到目前为止的内容:

> #specify folders
filelocat = '/Users/kate/Desktop/SegmenterTest3/SegmentedCells/'

#process image
img = cv2.imread(str(filelocat) + 'Seg3.png')
image = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
thresh = cv2.threshold(image, 60, 255, cv2.THRESH_BINARY)[1]
kernel = np.ones((20,20), np.uint8)
closing = cv2.morphologyEx(thresh, cv2.MORPH_CLOSE, kernel)
#inverts image so that the objects are white (for analysis)
imagem = cv2.bitwise_not(closing)

#Find contours
cnts = cv2.findContours(imagem.copy(), cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE)
cnts = imutils.grab_contours(cnts)

#calculate moments and extract cell shape info
moment_dict = {}
for index, cnt in enumerate(cnts):
moment_dict[index] = cv2.moments(cnt)

obj_properties = {}
for index, (key, obj_moments) in enumerate(moment_dict.items()):
if obj_moments['m00'] > 1000 and obj_moments['m00'] < 20000:
area = obj_moments['m00']
cx = obj_moments['m10'] / obj_moments['m00']
cy = obj_moments['m01'] / obj_moments['m00']
peri = cv2.arcLength(cnts[index], True)
q = (peri/(math.sqrt(area)))
props = {}
props['q']=q
props['peri']=peri
props['area']=area
props['cx']=cx
props['cy']=cy
obj_properties[key] = props
谢谢您的帮助!!

最佳答案

要解决此问题,您需要收集所有q,以便可以根据观察到的q的范围缩放它们。您可以使用列表理解来做到这一点,如下所示:all_the_q = [v['q'] for k, v in obj_properties.items()]您还需要选择一些颜色图。基于先前评论中的建议,我将其留给读者作为练习。快速了解一下,只需将q缩放到8位RGB即可看到初步结果。
请参阅下面的完整代码。请注意,index中的moment_dictobj_properties词典中的键,因此不需要整个enumerate构造。我完全放弃了enumerate。无论如何,您的过滤循环都会拾取正确的轮廓索引。根据标准选择轮廓后,收集所有q并计算其最小/最大/范围。然后使用它们将单个q缩放到所需的任意比例。在下面的示例中,我将其缩放为绿色组件的8位值。您可以根据需要为红色和蓝色遵循该模式。
请注意,在此图像中,大多数q都在4.0-4.25范围内,有一些离群值在5.50(绘制直方图以查看该分布)。这会使颜色图倾斜,因此大多数单元将以看起来非常相似的颜色进行着色。但是,我希望这有助于您入门。我建议对q应用对数函数,以便在视觉上“分散”分布的下限。

  • 编辑:用matplotlib中的一个替换原始色图。有关所有可能的颜色图选择,请参见https://stackoverflow.com/a/58555688/472566
  • import matplotlib.pyplot as plt
    import math
    import os
    import cv2
    import imutils
    import numpy as np
    # specify folders
    filelocat = '/Users/kate/Desktop/SegmenterTest3/SegmentedCells/'

    # process image
    img = cv2.imread(os.path.join(filelocat, 'Seg3.png'))
    image = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
    thresh = cv2.threshold(image, 60, 255, cv2.THRESH_BINARY)[1]
    kernel = np.ones((20, 20), np.uint8)
    closing = cv2.morphologyEx(thresh, cv2.MORPH_CLOSE, kernel)
    # inverts image so that the objects are white (for analysis)
    imagem = cv2.bitwise_not(closing)

    # Find contours
    cnts = cv2.findContours(imagem.copy(), cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE)
    cnts = imutils.grab_contours(cnts)

    # calculate moments and extract cell shape info
    moment_dict = {}
    for index, cnt in enumerate(cnts):
    moment_dict[index] = cv2.moments(cnt)

    obj_properties = {}
    for index, obj_moments in moment_dict.items():
    if obj_moments['m00'] > 1000 and obj_moments['m00'] < 20000:
    area = obj_moments['m00']
    cx = obj_moments['m10'] / obj_moments['m00']
    cy = obj_moments['m01'] / obj_moments['m00']
    peri = cv2.arcLength(cnts[index], True)
    q = (peri/(math.sqrt(area)))
    props = {}
    props['q'] = q
    props['peri'] = peri
    props['area'] = area
    props['cx'] = cx
    props['cy'] = cy
    obj_properties[index] = props

    all_the_q = [v['q'] for k, v in obj_properties.items()]
    min_q = min(all_the_q)
    max_q = max(all_the_q)
    range_q = max_q - min_q

    # colormapping of q scalars to BGR values
    cmap = plt.cm.get_cmap('terrain')
    for index, prop in obj_properties.items():
    v = (prop['q'] - min_q) / range_q
    r, g, b, a = [int(x) for x in cmap(v, bytes=True)]
    cv2.drawContours(img, cnts, index, (b, g, r), -1)

    cv2.imwrite('colored.png', img)
    cv2.imshow('Biocells', img)
    cv2.waitKey(10000)

    关于python - 如何用Python中区域指定的颜色填充openCV轮廓?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63065234/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com