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

问题描述 投票:0回答:1

我想掩盖我创建的样条线定义的“V”区域之外的所有内容。我得到的结果是一个3D数组,其中“V”以外的区域设置为0或255。

我对使用fill_between不感兴趣,因为我需要感兴趣的区域以便以后用CV2处理。谢谢!

enter image description here

最终图像应该像这个enter image description here

这就是我所拥有的:

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()
python image-processing cv2
1个回答
0
投票

肯定有更好的办法。但在这里,使用插值和迭代。

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);

Final Image

© www.soinside.com 2019 - 2024. All rights reserved.