繁体   English   中英

3 种颜色的阈值:黑色、白色和灰色,而不仅仅是黑色和白色

[英]thresholding in 3 color : black, white, and gray , instead of just black and white

我希望 can threshold 有 3 种颜色:黑色、白色和灰色,而不仅仅是黑色和白色,稍后我可以将贴纸与原始 img 分开

现在我的.py脚本可以进行阈值处理,使图像的颜色变为黑白

import numpy as np
import glob
import matplotlib.pyplot as plt
import skimage.io
import skimage.color
import skimage.filters



# load the image
image = skimage.io.imread("/home/student_joy/desktop/optimization_11_10/original_duplicate.png")[:,:,:3]


# image = imageio.imread(image_name)[:,:,:3]
# img = rgb2gray(image)

fig, ax = plt.subplots()
plt.imshow(image)


# convert the image to grayscale
gray_image = skimage.color.rgb2gray(image)

# blur the image to denoise
blurred_image = skimage.filters.gaussian(gray_image, sigma=1.0)

fig, ax = plt.subplots()
plt.imshow(blurred_image, cmap="gray")

# create a histogram of the blurred grayscale image
histogram, bin_edges = np.histogram(blurred_image, bins=256, range=(0.0, 1.0))

fig, ax = plt.subplots()
plt.plot(bin_edges[0:-1], histogram)
plt.title("Grayscale Histogram")
plt.xlabel("grayscale value")
plt.ylabel("pixels")
plt.xlim(0, 1.0)

# create a mask based on the threshold
t = 0.72
binary_mask = blurred_image < t

fig, ax = plt.subplots()
plt.imshow(binary_mask, cmap="gray")


plt.show() 

查看结果图片: https://imgur.com/a/u4KvF7Z

我不确定如何正确设置 binary_mask,即t

binary_mask = blurred_image < t

当您评估右侧的 boolean 表达式时,这将给出 false (0) 或 true (1) 作为掩码的目标值(如果您确实需要掩码,这是有意义的,它是二进制图像)。

如果你只想分开贴纸区域(所以删除所有灰色),你会做类似的事情

t1 = 0.5
t2 = 0.72
binary_mask = 1 - ((t1 < blurred_image) & (blurred_image < t2))

或使用cv.threshold()进行类似的操作。

关于找到您的阈值:如果您点击此链接如何从直方图中获取阈值? 并查看“Otsu 的二值化”部分,它解释了如何为具有双峰直方图的区域设置阈值。 把刚刚发现的灰色区域遮掉,只留下贴纸的黑白,然后让大津处理rest。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM