我有下面的图像:
我希望得到接近(我做得不完美):
我怎么用python来做呢?我的初始图像是一个0和255值的2D numpy数组。
ubof19bj1#
你可以试试这个:
import cv2import matplotlib.pyplot as pltimport numpy as npimg = cv2.imread('img.png', 0)img[img > 0] = 255kernel = np.ones((2, 2), np.uint8)dilation = cv2.dilate(img, kernel, iterations=25)plt.imshow(dilation, cmap="gray")
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")
它给出:
可以通过更改核和迭代次数来调整结果。
zed5wv102#
下面是在Python/OpenCV中实现这一点的一种方法。
输入:
import cv2import numpy as np# read the input as grayscaleimg = cv2.imread('2D_cloud.png', cv2.IMREAD_GRAYSCALE)# thresh at 0thresh = cv2.threshold(img, 0, 255, cv2.THRESH_BINARY)[1]# apply morphology dilate and closekernel = 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 resultscv2.imwrite('2D_cloud_thresh.jpg', thresh)cv2.imwrite('2D_cloud_dilate.jpg', dilate)cv2.imwrite('2D_cloud_result.jpg', result)# show resultscv2.imshow('thresh', thresh)cv2.imshow('dilate', dilate)cv2.imshow('result', result)cv2.waitKey(0)
# 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)
阈值:
扩张:
关闭结果:
2条答案
按热度按时间ubof19bj1#
你可以试试这个:
它给出:
可以通过更改核和迭代次数来调整结果。
zed5wv102#
下面是在Python/OpenCV中实现这一点的一种方法。
输入:
阈值:
扩张:
关闭结果: