使用马尔可夫链将RGB图像转换为黑白(0-1)

问题描述

我正在努力寻找这个问题的答案,我不太了解马尔可夫链的使用,我想得到一些帮助:

[输入图像] https://i.stack.imgur.com/r9XCE.png

[输出图像示例] https://i.stack.imgur.com/3pllU.png

这就是我们的开始,经典。

我只想将此图像转换为黑白,但我必须使用我根本不了解的马尔可夫链概率

有人能给我一些提示吗? 我相信我们必须选择随机像素 N 次并应用一些可能的魔法将其更改为黑色或白色(基于概率和邻居)

提前致谢(我不是在寻找代码,而是要实现的逻辑)

解决方法

这是使用马尔可夫链对图像进行二值化的一种方法:

假设(根据马尔可夫特性)一个像素值仅取决于它的邻居(让我们假设一个 4-nbd),让我们估计一个像素是白色的概率,因为它的 nbd 个像素中有 n 个是白色的,即对于 4 -nbd,让我们首先计算 P(x(i,j)=1 | n 的 nbrs 也是 1) 的条件概率,其中对于 4-nbd,n=0,1,2,3,4(同样,让我们使用全局阈值计算概率,如以下代码所示,这可以被认为是训练阶段):

from skimage.io import imread
im = imread('https://i.stack.imgur.com/r9XCE.png')

def count_nbrs(im,i,j,th): # counts number of nbd pixels are 1 given a pixel,with threshold th
    count = 0
    count += np.mean(im[i-1,j]) > th
    count += np.mean(im[i+1,j]) > th
    count += np.mean(im[i,j-1]) > th
    count += np.mean(im[i,j+1]) > th
    return count

th = 140 #np.mean(im) # a global threshold
nnbrs = 5 # use 4-nbd
freq = np.zeros(nnbrs)
tot = np.zeros(nnbrs)
h,w,_ = im.shape
for i in range(1,h-1):
   for j in range(1,w-1):
      count = count_nbrs(im,th)
      if np.mean(im[i,j]) > th:
         freq[count] += 1
      tot[count] += 1
prob = freq/tot 
print(prob)
# Prob(x(i,j)=1|n of its nbrs are 1) in the image,for n=0,4
# [0.00775595 0.09712838 0.48986784 0.91385768 0.99566323]

现在让我们使用这些估计概率将彩色图像中的每个像素更改为黑色和白色,具体取决于其 nbd(这可以被认为是测试阶段):

h,_ = im.shape
im1 = np.zeros((h,w))
for i in range(1,h-1):
    for j in range(1,w-1):
        c = count_nbrs(im,th) # count number of neighbors with white pixel
        im1[i,j] = 255*(prob[c] > 0.5) # use Prob vector to determine value of the pixel
plt.imshow(im1,'gray')
plt.show()

获得的二值图像质量远优于全局阈值(请查看)。

enter image description here

您可以随机选择像素并计算给定1 nbrs(阈值)的数量,像素值为1的概率,相应地设置像素值,使用以下代码进行大量迭代N,将导致类似的二值图像。

N = 100000
h,w))
for k in range(N):
    i = np.random.randint(1,h-1,1)[0]
    j = np.random.randint(1,w-1,1)[0]
    c = count_nbrs(im,th)
    im1[i,j] = 255*(prob[c] > 0.5) 
plt.imshow(im1,'gray')
plt.show()

enter image description here

下一个动画展示了如何使用上述代码生成二进制图像。

enter image description here