简体   繁体   English

如何根据点阵数屏蔽部分图像

[英]How to mask part of an image based on arrays of points

I would like to mask out everything outside of the "V" region defined by the splines I've created. 我想掩盖我创建的样条线定义的“V”区域之外的所有内容。 The result I'm after is a 3D array where the regions outside the "V" are set to 0 or 255. 我得到的结果是一个3D数组,其中“V”以外的区域设置为0或255。

I'm not interested in using fill_between , as I need the region of interest for later processing with CV2. 我对使用fill_between不感兴趣,因为我需要感兴趣的区域以便以后使用CV2进行处理。 Thanks! 谢谢!

在此输入图像描述

Final image should look like this 最终图像应如下所示 在此输入图像描述

Here's what I have: 这就是我所拥有的:

import matplotlib.pyplot as plt
from scipy import misc, interpolate

# Show the image  ---------------- |
f = misc.face()
plt.imshow(f)

# Make the V shape ---------------- |
x1 = [200, 400, 600]
y1 = [0, 300, f.shape[0]]

# Fit spline
tck = interpolate.splrep(x1, y1, k=2)
xx1 = range(min(x1), max(x1))
yy1 = interpolate.splev(xx1, tck)

# Repeat
x2 = [700, 850, 960]
y2 = [f.shape[0], 200, 0]

# Fit spline
tck = interpolate.splrep(x2, y2, k=2)
xx2 = range(min(x2), max(x2))
yy2 = interpolate.splev(xx2, tck)

# Plot splines ---------------- |
plt.plot(xx1, yy1, 'r-', lw=4)
plt.plot(xx2, yy2, 'r-', lw=4)
plt.show()

There must be a better way. 一定会有更好的办法。 But here it is, using interpolation and iterating. 但是这里是使用插值和迭代。

import matplotlib.pyplot as plt
from scipy import misc, interpolate

# Show the image  ---------------- |
im = misc.face().copy()
plt.imshow(f)

# Make the V shape ---------------- |
x1 = [200, 400, 600]
y1 = [0, 300, f.shape[0]]

# Fit spline
tck = interpolate.splrep(x1, y1, k=2)
xx1 = range(min(x1), max(x1))
yy1 = interpolate.splev(xx1, tck)

# Repeat
x2 = [700, 850, 960]
y2 = [f.shape[0], 200, 0]

# Fit spline
tck = interpolate.splrep(x2, y2, k=2)
xx2 = range(min(x2), max(x2))
yy2 = interpolate.splev(xx2, tck)

# Plot splines ---------------- |
plt.plot(xx1, yy1, 'r-', lw=4)
plt.plot(xx2, yy2, 'r-', lw=4)

# Solution - Mask the sides
xx_interp = range(im.shape[0])
yy_interp1 = np.round(np.interp(xx_interp, yy1, xx1)).astype(int)
yy_interp2 = np.round(np.interp(xx_interp, yy2[::-1], xx2[::-1])).astype(int)

for y, x1, x2 in list(zip(xx_interp, yy_interp1, yy_interp2)):
    im[y, :x1, :] = 0
    im[y, x2:, :] = 0

plt.imshow(im);

最终影像

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM