Filling a cloud 2D image into a continous map
Question:
Answers:
You can try this:
import cv2
import matplotlib.pyplot as plt
import numpy as np
img = cv2.imread('img.png', 0)
img[img > 0] = 255
kernel = np.ones((2, 2), np.uint8)
dilation = cv2.dilate(img, kernel, iterations=25)
plt.imshow(dilation, cmap="gray")
It gives:
You can adjust the result by changing the kernel and the number of iterations.
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:
import cv2
import numpy as np
# read the input as grayscale
img = cv2.imread('2D_cloud.png', cv2.IMREAD_GRAYSCALE)
# thresh at 0
thresh = cv2.threshold(img, 0, 255, cv2.THRESH_BINARY)[1]
# apply morphology dilate and close
kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (15,15))
dilate = cv2.morphologyEx(thresh, cv2.MORPH_DILATE, kernel, iterations=1)
kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (9,9))
result = cv2.morphologyEx(dilate, cv2.MORPH_CLOSE, kernel, iterations=1)
# save results
cv2.imwrite('2D_cloud_thresh.jpg', thresh)
cv2.imwrite('2D_cloud_dilate.jpg', dilate)
cv2.imwrite('2D_cloud_result.jpg', result)
# show results
cv2.imshow('thresh', thresh)
cv2.imshow('dilate', dilate)
cv2.imshow('result', result)
cv2.waitKey(0)
Threshold:
Dilate:
Close Result:
You can try this:
import cv2
import matplotlib.pyplot as plt
import numpy as np
img = cv2.imread('img.png', 0)
img[img > 0] = 255
kernel = np.ones((2, 2), np.uint8)
dilation = cv2.dilate(img, kernel, iterations=25)
plt.imshow(dilation, cmap="gray")
It gives:
You can adjust the result by changing the kernel and the number of iterations.
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:
import cv2
import numpy as np
# read the input as grayscale
img = cv2.imread('2D_cloud.png', cv2.IMREAD_GRAYSCALE)
# thresh at 0
thresh = cv2.threshold(img, 0, 255, cv2.THRESH_BINARY)[1]
# apply morphology dilate and close
kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (15,15))
dilate = cv2.morphologyEx(thresh, cv2.MORPH_DILATE, kernel, iterations=1)
kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (9,9))
result = cv2.morphologyEx(dilate, cv2.MORPH_CLOSE, kernel, iterations=1)
# save results
cv2.imwrite('2D_cloud_thresh.jpg', thresh)
cv2.imwrite('2D_cloud_dilate.jpg', dilate)
cv2.imwrite('2D_cloud_result.jpg', result)
# show results
cv2.imshow('thresh', thresh)
cv2.imshow('dilate', dilate)
cv2.imshow('result', result)
cv2.waitKey(0)
Threshold:
Dilate:
Close Result: