Classical Computer Vision: Value Domain II: Threshold
Thresholding
Thresholding is the simplest segmentation method.
Imagine that I have an object in an image and I want to segment it from the background. The thresholding is based on the variation of intensity between that object and the background.
Basically, we compare the pixel value to a threshold value and take an action. For example, we could put 0 for every pixel that is equal or lower than the threshold.
import matplotlib.pyplot as plt
import skimage.io as io
from skimage import data, color
from skimage.transform import rescale, resize, downscale_local_mean
from skimage.filters import threshold_mean
import numpy as np
apple_original = io.imread("red-apple-1326427.jpg")
print(apple_original.shape)
plt.imshow(apple_original)
plt.show()
def image_binary_threshold(image, min_thr, max_thr):
res = image.copy()
for i in range(0, 3):
k = res[:, :, i] < min_thr[i]
res[k] = [0, 0, 0]
for i in range(0, 3):
k = res[:, :, i] > max_thr[i]
res[k] = [0, 0, 0]
return res
def image_cut_by_diff(image, mean, mean_diff):
res = image.copy()
k1 = (res[:, :, 0] < (mean + mean_diff))
k2 = (res[:, :, 0] > (mean - mean_diff))
k3 = (res[:, :, 1] < (mean + mean_diff))
k4 = (res[:, :, 1] > (mean - mean_diff))
k5 = (res[:, :, 2] < (mean + mean_diff))
k6 = (res[:, :, 2] > (mean - mean_diff))
k7 = (k1 & k2) & (k3 & k4) & (k5 & k6)
res[k7] = [0, 0, 0]
return res
Comentários
Postar um comentário