我正在尝试找出这张数码显微图像中胶水区域的总面积。
我的问题是我似乎无法填充内部区域,但是我可以成功获得胶水区域的轮廓。
这是我目前的代码:
from skimage.io import imread
from scipy.ndimage import distance_transform_edt
from skimage.color import rgb2hed
from skimage.filters import threshold_otsu
from skimage.morphology import opening, closing, disk
import numpy as np
from scipy import fftpack
from matplotlib.colors import LogNorm
from scipy import ndimage
from skimage import img_as_ubyte
import cv2
# Read the image
im = imread("10C_11_lugol_smaller.jpg")
# Show the image
plt.figure()
plt.imshow(im)
# Convert image from RGB to HED
hed = rgb2hed(im)
# Remove Stripes
im_fft = fftpack.fft2(hed[:, :, 0])
plt.figure()
plt.imshow((np.abs(im_fft)).astype(np.uint8), norm=LogNorm())
plt.colorbar()
plt.title('Fourier transform')
keep_fraction = 0.1
im_fft2 = im_fft.copy()
# Set r and c to be the number of rows and columns of the array
r, c = im_fft2.shape
im_fft2[int(r * keep_fraction):int(r * (1 - keep_fraction))] = 0
im_fft2[:, int(c * keep_fraction):int(c * (1 - keep_fraction))] = 0
plt.figure()
plt.imshow((np.abs(im_fft2)).astype(np.uint8), norm=LogNorm())
plt.colorbar()
plt.title('Filtered Spectrum')
# Reconstructing
im_new = fftpack.ifft2(im_fft2).real
plt.figure()
plt.imshow(im_new)
plt.title('Reconstructed Image')
# Creating the Otsu Threshold on the first layer
t = threshold_otsu(im_new)
mask = im_new > t
mask = closing(opening(mask, disk(1)), disk(1))
# Show the result of the thresholding
fig = plt.figure()
plt.subplot(1, 2, 1)
plt.imshow(im)
plt.subplot(1, 2, 2)
plt.imshow(mask)
# Filling the contour
cv_image = img_as_ubyte(mask)
kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (5, 5))
res = cv2.morphologyEx(cv_image, cv2.MORPH_OPEN, kernel)
plt.figure()
plt.imshow(res, plt.cm.gray)
plt.title('Filled Contours')
# Apply the distance transform on the results
distance = distance_transform_edt(mask)
plt.figure()
plt.imshow(distance)
plt.colorbar()
plt.show()
有人可以帮忙吗:(
提前感谢您的阅读!