【问题标题】:Outline a region in a graph在图中勾勒出一个区域
【发布时间】:2014-08-23 17:26:57
【问题描述】:

我有两个使用 matplotlib 绘制的 2D numpy 数组(尺寸相同)。我绘制的第一个数组是灰度颜色图。第二个代表一个光圈,但它是一个不规则的形状(一些像素被勾勒出来,它是一组形成轮廓的水平和垂直线)。我不确定如何要求它绘制第二个数组。该数组由三个数字(0、1 和 3)组成,我只需要勾勒出一个值 (3) 的像素,但我需要轮廓来包含这些像素的区域,而不是单独的像素。我需要所有像素的内部保持透明,这样我才能通过它看到灰度颜色图。

有谁知道如何做到这一点?

【问题讨论】:

  • 我刚刚注意到您不接受我在下面的回答。如果你有充分的理由,那没有什么错。但是,只是为了了解更多,如果您对原因发表评论,那将非常有趣。这样我就可以了解我的答案有什么问题,并可能对其进行改进,以便更好地回答您的问题!
  • @DrV,答案本身没有问题,但我无法使用您提供的代码重现您的结果。我试图确定原因。我也无法使您的代码适应我的目的,所以我想也许一旦我确实找到它来回答我的问题,我会重新接受它作为答案。不过,我确实计划重新接受它,因为这是一个非常好的答案。

标签: python numpy matplotlib plot


【解决方案1】:

如果我理解正确的话,这是一个有趣的问题。为了确定您的意思,您想在像素值为 3 的所有连续区域周围画一条带有某种颜色的线。

我认为没有现成的功能,但我们不要让它阻止我们。我们需要创建自己的函数。

我们可以从创建需要勾画的区域的布尔地图开始:

import numpy as np
import matplotlib.pyplot as plt

# our image with the numbers 1-3 is in array maskimg
# create a boolean image map which has trues only where maskimg[x,y] == 3
mapimg = (maskimg == 3)

# a vertical line segment is needed, when the pixels next to each other horizontally
#   belong to diffferent groups (one is part of the mask, the other isn't)
# after this ver_seg has two arrays, one for row coordinates, the other for column coordinates 
ver_seg = np.where(mapimg[:,1:] != mapimg[:,:-1])

# the same is repeated for horizontal segments
hor_seg = np.where(mapimg[1:,:] != mapimg[:-1,:])

# if we have a horizontal segment at 7,2, it means that it must be drawn between pixels
#   (2,7) and (2,8), i.e. from (2,8)..(3,8)
# in order to draw a discountinuous line, we add Nones in between segments
l = []
for p in zip(*hor_seg):
    l.append((p[1], p[0]+1))
    l.append((p[1]+1, p[0]+1))
    l.append((np.nan,np.nan))

# and the same for vertical segments
for p in zip(*ver_seg):
    l.append((p[1]+1, p[0]))
    l.append((p[1]+1, p[0]+1))
    l.append((np.nan, np.nan))

# now we transform the list into a numpy array of Nx2 shape
segments = np.array(l)

# now we need to know something about the image which is shown
#   at this point let's assume it has extents (x0, y0)..(x1,y1) on the axis
#   drawn with origin='lower'
# with this information we can rescale our points
segments[:,0] = x0 + (x1-x0) * segments[:,0] / mapimg.shape[1]
segments[:,1] = y0 + (y1-y0) * segments[:,1] / mapimg.shape[0]

# and now there isn't anything else to do than plot it
plt.plot(segments[:,0], segments[:,1], color=(1,0,0,.5), linewidth=3)

让我们通过生成一些数据并显示它来测试它:

image = np.cumsum(np.random.random((20,20))-.5, axis=1)
maskimg = np.zeros(image.shape, dtype='int')
maskimg[image > 0] = 3

x0 = -1.5
x1 =  1.5
y0 = 2.3
y1 = 3.8

plt.figure()
plt.imshow(maskimg, origin='lower', extent=[x0,x1,y0,y1], cmap=plt.cm.gray, interpolation='nearest')
plt.axis('tight')

之后我们运行上面的过程,得到:

如果需要,代码可以变得更密集,但现在 cmets 占用了大量空间。对于大图像,通过寻找连续路径来优化图像片段创建可能是明智的。这将使要绘制的点数最多减少三倍。但是,这样做需要一些不同的代码,这不像这个那么清楚。 (如果有 cmets 提出要求和适当数量的赞成票,我会添加它:)

【讨论】:

  • 如果掩码不包含任何段,l 将为空并且失败 ;-) 感谢出色的 sn-p!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-09-01
  • 1970-01-01
  • 2013-02-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-02-26
相关资源
最近更新 更多