首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何消除skimage.measure输出的compare_ssim的SSIM图像差异周围的边缘

如何消除skimage.measure输出的compare_ssim的SSIM图像差异周围的边缘
EN

Stack Overflow用户
提问于 2020-10-30 18:45:02
回答 1查看 154关注 0票数 0

我正在尝试编写一个代码来计算显微镜图像上的轨迹所占的面积,如下所示:

由于轨迹不是均匀的(我的意思是,它们没有独特的灰度,因为它们的边缘较暗,中间较亮),我不能仅仅通过比较它们的灰度和背景的灰度来做到这一点,因为在轨迹的某些部分是相同的。

因此,我尝试将图像与背景图像进行比较:

我这样做是为了提取这两个图像之间的差异(对应于轨迹本身)。为此,我使用了skimage中的compare_ssim函数。我使用的代码如下:

代码语言:javascript
复制
from skimage.measure import compare_ssim 
import imutils
import cv2
import numpy as np

# load the two input images
imageA = cv2.imread("./b.jpg")
imageB = cv2.imread("./a.jpg")
# convert the images to grayscale
grayA = cv2.cvtColor(imageA, cv2.COLOR_BGR2GRAY)
grayB = cv2.cvtColor(imageB, cv2.COLOR_BGR2GRAY)
    
# compute the Structural Similarity Index (SSIM) between the two
# images, ensuring that the difference image is returned
(score, diff) = compare_ssim(grayA, grayB, full=True)
diff = (diff * 255).astype("uint8")
# show the diff image
cv2.imshow("Diff", diff)

我得到的diff图像如下所示:

这很好,因为现在轨迹中的灰度与背景的灰度有很好的区分,我可以计算轨迹所占的面积(轨迹中有一些点比较亮,但它们很少,对我的目的来说还可以)。

然而,问题是在diff图像中,不仅轨迹显示为黑色,而且轨迹周围还有一个粗大的边界:

这个边缘使我的面积估计不正确。所以我想知道如何消除这个边缘,或者至少让它变得更薄。

如果这是不可能的,如果您能展示另一个Python函数来实现我计算轨道占用的面积的目的,这将是非常有帮助的。

EN

回答 1

Stack Overflow用户

发布于 2020-11-02 19:38:38

总是有一种更好的方法来做同样的事情,但我在这里使用了一种简单的方法,您可以稍后根据需要进行改进或调整:

算法:

Flood首先,适当的填充阈值将只保留edges

  • Secondly,形态学(根据阈值方法的不同,膨胀或侵蚀)将使边缘变薄。最后,要去除除痕迹之外的所有内容,可以使用_<_<

>E211>算法,然后计算白色像素以获得您的面积(以像素为单位)。<代码>H212<代码>G213

结果:

在最终结果中:The count of the white of pixels is: 52219

代码:

代码语言:javascript
复制
#========================
# Import Libraries
#========================
import numpy as np
import matplotlib.pyplot as plt
import cv2
from skimage.morphology import flood_fill

#========================
# Read images
#========================
img = cv2.imread('1.png',0)
bck = cv2.imread('2.png',0)

#========================
# Gaussian Blur
#========================
gauss = cv2.GaussianBlur(img, (5,5), 1)
bck_gauss = cv2.GaussianBlur(bck, (5,5), 1)
#========================
# Thresholding
#========================
_,thresh1 = cv2.threshold(gauss,127,255,cv2.THRESH_BINARY)
_,bck_th = cv2.threshold(bck_gauss,127,255,cv2.THRESH_BINARY_INV)

# Get rid of black borders
thresh1 = thresh1 + bck_th

#========================
# Morphology
#========================
kernel = np.zeros((7,7),np.uint8)
kernel[2,:] = 1
kernel[:,2] = 1
dilation = cv2.dilate(thresh1,kernel,iterations = 1)

#========================
# Flood Fill 
#========================

res = flood_fill(dilation,(1,1), 0)
print("The count of the white of pixels is: ", int(np.sum(res)/np.max(res)))
#========================
# Visualize Results
#========================
plt.figure(num='Blobs')

plt.subplot(221)
plt.imshow(img, cmap='gray')
plt.title('Original')
plt.axis('off')

plt.subplot(222)
plt.imshow(thresh1, cmap='gray')
plt.title('Thresholded')
plt.axis('off')

plt.subplot(223)
plt.imshow(dilation, cmap='gray')
plt.title('Morphology')
plt.axis('off')

plt.subplot(224)
plt.imshow(res, cmap='gray')
plt.title('Final Result')
plt.axis('off')

plt.show()
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/64606837

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档