Filling a cloud 2D image into a continous map

Question:

I have the following image:

cloudtocher2dinterpolator

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

enter image description here

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

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:

enter image description here

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

Answered By: bb1

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:

enter image description here

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:

enter image description here

Dilate:

enter image description here

Close Result:

enter image description here

Answered By: fmw42