2 回答

TA贡献1852条经验 获得超1个赞
如果我正确理解了您的问题,则应采取更详细的方法来获得所需的结果。
首先,使用简单阈值的方法会产生嘈杂的方法。
我使用了您的样本的修改图像:
如果你应用你的阈值,那么可能会出现这样的结果:
在这里可以使用更精细的阈值:
image3 = cv2.inRange(image, np.array([10, 10, 10]), np.array([255, 255, 255]))
结果会创建一个二进制图像(类似于您想要的输出,除了上面的条带):
为了摆脱条带,我会(虽然这只是一种不是完美的方法)使用一些东西来找到由白色区域创建的角落,然后用它用黑色绘制它上面的所有区域:
ind = np.where(image3 == 255)
max_x = np.max(ind[1])
max_y = ind[0][np.argmax(ind[1])]
image3[:max_y, :max_x] = 255
结果将是这样的:

TA贡献1878条经验 获得超4个赞
无论如何,这不是完美的答案。但这可能会有所帮助。
我重新创建了图像,如下所示:
然后我读了它并按照你的路径首先将其设为二进制(稍作修改以减少噪音):
import numpy as np
from matplotlib import pyplot as plt
img = plt.imread("sample.jpg")
img2 = img.copy()
img2[img2.sum(-1) > 30] = 255
img2[img2.sum(-1) <= 30] = 0
这是修改后的结果:
选项1
这可能不是你问的,但它类似于评论中讨论的解决方案之一,我认为它部分正确:
i, j = np.where(img2.sum(-1) > 0) # find all white coordinates
i, j = (i[j.argmax()], j[j.argmax()]) # the corner white point into the black
img2[:i, :j] = 255 # paint whine all the left-above rectangle from this point
这是最终结果:
这是一个不完美但非常简单的纯 numpy 解决方案。
选项 2
在这个解决方案中,我们需要一些简单的微积分和线性代数。在二维空间中取两个点并在它们之间画一条线。那么,边界线的作用是什么?
point2 = (i, j) # same i and j from OPTION1 (coordinates of the top-right corner)
point1 = (img2.shape[0], img2[-1].sum(-1).argmin()) # the bottom-right white corner.
a = (point2[1] - point1[1]) / (point2[0] - point1[0])
c = point1[1] - a * point1[0]
f = lambda x: int(a * x + c)
现在,画线左侧的所有区域:
for i in range(img2.shape[0]):
img2[:i, :f(i)+1] = 255
结果如下:
添加回答
举报