将一个云2D图像填充到一个连续地图中。

huangapple go评论105阅读模式
英文:

Filling a cloud 2D image into a continous map

问题

我有以下图片:

将一个云2D图像填充到一个连续地图中。

我希望获得类似以下的效果(我没有完全做得很好):

将一个云2D图像填充到一个连续地图中。

我该如何使用Python来实现这个效果?我的初始图片是一个包含0和255值的2D numpy数组。

英文:

I have the following image:

将一个云2D图像填充到一个连续地图中。

and I wish to obtain something close to (I didn't do it perfectly):

将一个云2D图像填充到一个连续地图中。

How can I do this with python? My initial image is a 2D numpy array of 0 and 255 values.

答案1

得分: 3

以下是代码的翻译部分:

  1. import cv2
  2. import matplotlib.pyplot as plt
  3. import numpy as np
  4. img = cv2.imread('img.png', 0)
  5. img = 255
  6. kernel = np.ones((2, 2), np.uint8)
  7. dilation = cv2.dilate(img, kernel, iterations=25)
  8. plt.imshow(dilation, cmap="gray")

给出的结果如下:

将一个云2D图像填充到一个连续地图中。

您可以通过更改卷积核和迭代次数来调整结果。

英文:

You can try this:

  1. import cv2
  2. import matplotlib.pyplot as plt
  3. import numpy as np
  4. img = cv2.imread('img.png', 0)
  5. img = 255
  6. kernel = np.ones((2, 2), np.uint8)
  7. dilation = cv2.dilate(img, kernel, iterations=25)
  8. plt.imshow(dilation, cmap="gray")

It gives:

将一个云2D图像填充到一个连续地图中。

You can adjust the result by changing the kernel and the number of iterations.

答案2

得分: 2

以下是使用Python/OpenCV进行这项操作的一种方法。

  • 以灰度方式读取输入图像。
  • 在阈值0处进行二值化。
  • 应用形态学膨胀操作以连接区域,然后进行闭合操作以填补小间隙。
  • 保存结果。
  1. import cv2
  2. import numpy as np
  3. # 以灰度方式读取输入图像
  4. img = cv2.imread('2D_cloud.png', cv2.IMREAD_GRAYSCALE)
  5. # 在阈值0处进行二值化
  6. thresh = cv2.threshold(img, 0, 255, cv2.THRESH_BINARY)[1]
  7. # 应用形态学膨胀和闭合操作
  8. kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (15, 15))
  9. dilate = cv2.morphologyEx(thresh, cv2.MORPH_DILATE, kernel, iterations=1)
  10. kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (9, 9))
  11. result = cv2.morphologyEx(dilate, cv2.MORPH_CLOSE, kernel, iterations=1)
  12. # 保存结果
  13. cv2.imwrite('2D_cloud_thresh.jpg', thresh)
  14. cv2.imwrite('2D_cloud_dilate.jpg', dilate)
  15. cv2.imwrite('2D_cloud_result.jpg', result)
  16. # 显示结果
  17. cv2.imshow('thresh', thresh)
  18. cv2.imshow('dilate', dilate)
  19. cv2.imshow('result', result)
  20. cv2.waitKey(0)

阈值化结果:

将一个云2D图像填充到一个连续地图中。

膨胀结果:

将一个云2D图像填充到一个连续地图中。

闭合结果:

将一个云2D图像填充到一个连续地图中。

英文:

Here is one way to do that in Python/OpenCV.

  • Read the input as grayscale
  • Threshold at 0
  • Apply morphology dilate to connect and then close to fill tiny gaps
  • Save the results

Input:

将一个云2D图像填充到一个连续地图中。

  1. import cv2
  2. import numpy as np
  3. # read the input as grayscale
  4. img = cv2.imread('2D_cloud.png', cv2.IMREAD_GRAYSCALE)
  5. # thresh at 0
  6. thresh = cv2.threshold(img, 0, 255, cv2.THRESH_BINARY)[1]
  7. # apply morphology dilate and close
  8. kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (15,15))
  9. dilate = cv2.morphologyEx(thresh, cv2.MORPH_DILATE, kernel, iterations=1)
  10. kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (9,9))
  11. result = cv2.morphologyEx(dilate, cv2.MORPH_CLOSE, kernel, iterations=1)
  12. # save results
  13. cv2.imwrite('2D_cloud_thresh.jpg', thresh)
  14. cv2.imwrite('2D_cloud_dilate.jpg', dilate)
  15. cv2.imwrite('2D_cloud_result.jpg', result)
  16. # show results
  17. cv2.imshow('thresh', thresh)
  18. cv2.imshow('dilate', dilate)
  19. cv2.imshow('result', result)
  20. cv2.waitKey(0)

Threshold:

将一个云2D图像填充到一个连续地图中。

Dilate:

将一个云2D图像填充到一个连续地图中。

Close Result:

将一个云2D图像填充到一个连续地图中。

huangapple
  • 本文由 发表于 2023年3月7日 04:46:32
  • 转载请务必保留本文链接:https://go.coder-hub.com/75655681.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定