为什么修复失败以及如何修复?

发布于 2025-01-15 15:10:50 字数 2212 浏览 0 评论 0原文

我有一个 100x100 numpy 数组,大约是 47% nan。我想修复 nan 较小的连接区域。这是输入数组,nans 是白色的。

输入

以下是 nan(白色):

m_nans = np.isnan(input).astype(np.uint8)

nans

这里我选择面积低于任意阈值的 nans 的连接区域:

n,nans_labelled,stats,centroids = cv2.connectedComponentsWithStats(m_nans)
labels = np.where(stats[1:, 4] < 500)[0] + 1
m_nans_small = np.isin(nans_labelled, labels).astype(np.uint8)

nans 连接小

现在我想要修复上面的区域,我们从半径 3 开始。

inpainted = cv2.inpaint(input, m_nans_small, 3, cv2.INPAINT_NS)

inpaint ns r3

这些是修复区域:

nans inpainted

然后我尝试将半径增加到 10,结果没有任何结果修复了。然后我尝试扩大面具,这导致南被修复。考虑到较高的半径只会导致 nan 被修复,我再次尝试将所有 nan 屏蔽(m_nans)。对于我真正感兴趣的修复区域 m_nans_small,结果是相同的。然后我尝试使用 m_nans_small 尝试尽可能小的半径 (1),虽然这会修复更多区域,但它无法完全修复感兴趣的区域。与我想要修复的区域的面积相比,半径 1 也太小了。

inpaint ns r1

有趣的是,如果我尝试递归修复 - 修复上面用与原始蒙版相交的图像中的 nan 蒙版的图像 - 没有明显的改进,即使使用最小半径。这表明修补不会因为缺少油漆而失败。即使完全被有效像素包围的两个像素区域也无法修复。

m_nans_small_remaining = np.isnan(inpainted) & m_nans_small 
inpainted2 = cv2.inpaint(inpainted, m_nans_small_remaining, 1, cv2.INPAINT_NS)

第二次修复ns r1 使用剩余的小连接 nans

最后我尝试修复扩张的 m_nans。半径为 1 时,它几乎可以修复任何内容,但由于半径较小,结果很差,并且会覆盖良好的数据。它的半径为 50,用 nan 修复所有内容。

我知道修复失败的原因,但我想确切地知道。然后我想知道如何解决它。

I have a 100x100 numpy array which is about 47% nans. I would like to inpaint the smaller connected regions of nan. Here is the input array, nans are white.

input

Here are the nans (white):

m_nans = np.isnan(input).astype(np.uint8)

nans

Here I select connected regions of nans with areas below an arbitrary threshold:

n,nans_labelled,stats,centroids = cv2.connectedComponentsWithStats(m_nans)
labels = np.where(stats[1:, 4] < 500)[0] + 1
m_nans_small = np.isin(nans_labelled, labels).astype(np.uint8)

nans connected small

Now I would like to inpaint the regions above, lets start with radius 3.

inpainted = cv2.inpaint(input, m_nans_small, 3, cv2.INPAINT_NS)

inpaint ns r3

And these are the inpainted regions:

nans inpainted

Then I tried increasing the radius to 10 which resulted in nothing being inpainted. Then I tried dilating the mask, which led to nans being inpainted. Thinking higher radius caused only nans to be inpainted, I tried again with all nans masked (m_nans). The result was identical for the regions I was actually interested in inpainting, m_nans_small. Then I tried the smallest radius possible (1) with m_nans_small and while that inpaints more regions, it fails to fully inpaint the region of interest. A radius of 1 is also too small compared to the area of the regions I want to inpaint.

inpaint ns r1

Interestingly, if I try to inpaint recursively - inpaint the image above masked with the nans from that image intersected with the original mask - there is no noticeable improvement, even with the smallest radius. This suggests that inpainting does not fail from a lack of paint. Even two-pixel regions completely surrounded by valid pixels fail to inpaint.

m_nans_small_remaining = np.isnan(inpainted) & m_nans_small 
inpainted2 = cv2.inpaint(inpainted, m_nans_small_remaining, 1, cv2.INPAINT_NS)

second inpaint ns r1 using remaining small connected nans

At last I tried inpainting a dilated m_nans. With a radius of 1 it inpaints almost anything, but the result is shoddy due to the small radius, and it overwrites good data. With a radius of 50 it inpaints everything with nans.

I have some idea why inpainting fails, but I would like to know for sure. Then I would like to know how to fix it.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文