[英]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.