gpt4 book ai didi

python - Otsu 掩膜内阈值化

转载 作者:太空宇宙 更新时间:2023-11-03 22:17:06 26 4
gpt4 key购买 nike

我正在使用 Python 并尝试对图像进行 Otsu 阈值处理,但仅限于蒙版内部(是的,我有一个图像和一个蒙版图像)。这意味着图像上较少的像素将包含在用于计算 Otsu 阈值的直方图中。

我目前正在使用没有 mask 图像的 cv2.threshold 函数,不知道如何做这种工作。

ret, OtsuMat = cv2.threshold(GaborMat, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU)

由于此函数还合并了掩码外的像素,我认为它会给出不太准确的阈值。

这是图像及其 mask 的示例:

https://drive.google.com/drive/folders/1p8JMhncJs19oOWO9RdkWuEADVGqE-gzQ?usp=sharing

希望有一个 OpenCV 或其他库函数可以轻松地完成它(并且还可以进行快速计算),但我们将不胜感激。

最佳答案

我尝试使用 skimage 中的 threshold_otsu() 方法和 Numpy 掩码数组。我不知道是否有更快的方法 - skimage 通常经过很好的优化。如果其他人想使用我的示例数据并尝试其他想法,请随意 - 尽管需要支付一次投票的服务费;-)

#!/usr/bin/env python3

import cv2
import numpy as np
import numpy.ma as ma
from skimage.filters import threshold_otsu

# Set up some repeatable test data, 4 blocks 100x100 pixels each of random normal np.uint8s centred on 32, 64, 160,192
np.random.seed(42)
a=np.random.normal(size = (100,100), loc = 32,scale=10).astype(np.uint8)
b=np.random.normal(size = (100,100), loc = 64,scale=10).astype(np.uint8)
c=np.random.normal(size = (100,100), loc = 160,scale=10).astype(np.uint8)
d=np.random.normal(size = (100,100), loc = 192,scale=10).astype(np.uint8)
# Stack (concatenate) the 4 squares horizontally across the page
im = np.hstack((a,b,c,d))
# Next line is just for debug
cv2.imwrite('start.png',im)

这给了我们这个:

enter image description here

# Now make a mask revealing only left half of image, centred on 32 and 64
mask=np.zeros((100,400))
mask[:,200:]=1
masked = ma.masked_array(im,mask)
print(threshold_otsu(masked.compressed())) # Prints 47

# Now do same revealing only right half of image, centred on 160 and 192
masked = ma.masked_array(im,1-mask)
print(threshold_otsu(masked.compressed())) # Prints 175

测试数据的直方图是这样的,x轴是0..255

enter image description here


适应您自己的示例数据,我明白了:

#!/usr/bin/env python3

import cv2
import numpy as np
import numpy.ma as ma
from skimage.filters import threshold_otsu

# Load images
im = cv2.imread('eye.tif', cv2.IMREAD_UNCHANGED)
mask = cv2.imread('mask.tif', cv2.IMREAD_UNCHANGED)

# Calculate Otsu threshold on entire image
print(threshold_otsu(im)) # prints 130

# Now do same for masked image
masked = ma.masked_array(im,mask>0)
print(threshold_otsu(masked.compressed())). # prints 124

关于python - Otsu 掩膜内阈值化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56535330/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com