基于马尔科夫随机场的图像分割程序(python实现)
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了基于马尔科夫随机场的图像分割程序(python实现),小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含3303字,纯文字阅读大概需要5分钟。
内容图文
![基于马尔科夫随机场的图像分割程序(python实现)](/upload/InfoBanner/zyjiaocheng/840/7d19571064fa4ef2b2029b60421dbbd0.jpg)
最近无聊开始自学CV方向,从 Computer Vision:Algorithms and Applications 开始入门
文中提到了 Markov Random Fields
于是去了解了一下相关知识,为了巩固理解就进行了图像分割的应用
相关知识参考链接:https://blog.csdn.net/beattodeath/article/details/54630690
代码参考:https://blog.csdn.net/sxk20091111/article/details/80758050
代码是对上述参考链接的代码进行修改,使其能跑通。而第二个参考链接的代码其实就是第一个链接的python版本
import cv2
import numpy as np
img = cv2.imread('1.jpg')
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) #将图片二值化,彩色图片该方法无法做分割
img = gray
img_double = np.array(img, dtype = np.float64)
cluster_num = 2
max_iter =200
label=np.random.randint(1, cluster_num + 1, size = img_double.shape)
iter=0
f_u = np.array([0,1,0,0,0,0,0,0,0]).reshape(3, 3)
f_d = np.array([0,0,0,0,0,0,0,1,0]).reshape(3, 3)
f_l = np.array([0,0,0,1,0,0,0,0,0]).reshape(3, 3)
f_r = np.array([0,0,0,0,0,1,0,0,0]).reshape(3, 3)
f_ul = np.array([1,0,0,0,0,0,0,0,0]).reshape(3, 3)
f_ur = np.array([0,0,1,0,0,0,0,0,0]).reshape(3, 3)
f_dl = np.array([0,0,0,0,0,0,1,0,0]).reshape(3, 3)
f_dr = np.array([0,0,0,0,0,0,0,0,1]).reshape(3, 3)
while iter < max_iter:
iter = iter + 1
print(iter)
label_u = cv2.filter2D(np.array(label, dtype = np.uint8), -1 , f_u)
label_d = cv2.filter2D(np.array(label, dtype = np.uint8), -1 , f_d)
label_l = cv2.filter2D(np.array(label, dtype = np.uint8), -1 , f_l)
label_r = cv2.filter2D(np.array(label, dtype = np.uint8), -1 , f_r)
label_ul = cv2.filter2D(np.array(label, dtype = np.uint8), -1 , f_ul)
label_ur = cv2.filter2D(np.array(label, dtype = np.uint8), -1 , f_ur)
label_dl = cv2.filter2D(np.array(label, dtype = np.uint8), -1 , f_dl)
label_dr = cv2.filter2D(np.array(label, dtype = np.uint8), -1 , f_dr)
m, n = label.shape
p_c = np.zeros((cluster_num, m , n))
for i in range(cluster_num):
label_i = (i+1) * np.ones((m, n))
u_T = 1 * np.logical_not(label_i - label_u)
d_T = 1 * np.logical_not(label_i - label_d)
l_T = 1 * np.logical_not(label_i - label_l)
r_T = 1 * np.logical_not(label_i - label_r)
ul_T = 1 * np.logical_not(label_i - label_ul)
ur_T = 1 * np.logical_not(label_i - label_ur)
dl_T = 1 * np.logical_not(label_i - label_dl)
dr_T = 1 * np.logical_not(label_i - label_dr)
temp = u_T + d_T + l_T + r_T + ul_T + ur_T + dl_T + dr_T
p_c[i, :] = (1.0/8) * temp
p_c[p_c == 0] = 0.001
mu = np.zeros((1, cluster_num))
sigma = np.zeros((1, cluster_num))
for i in range(cluster_num):
index = np.where(label == (i+1))
data_c = img[index]
mu[0, i] = np.mean(data_c)
sigma[0, i] = np.var(data_c)
p_sc = np.zeros((cluster_num, m , n))
one_a = np.ones((m, n))
for j in range(cluster_num):
MU = mu[0, j]* one_a
p_sc[j, :] = (1.0/np.sqrt(2 * np.pi * sigma[0, j])) * np.exp(-1. * (( img - MU)**2) /(2 * sigma[0, j]))
X_out = np.log(p_c) + np.log(p_sc)
label_c = X_out.reshape(2, m * n)
label_c_t = label_c.T
label_m = np.argmax(label_c_t, axis = 1)
label_m = label_m + np.ones(label_m.shape) # 由于上一步返回的是index下标,与label其实就差1,因此加上一个ones矩阵即可
label= label_m.reshape(m, n)
label = label - np.ones(label.shape) # 为了出现0
lable_w = 255 * label #此处做法只能显示两类,一类用0表示另一类用255表示
cv2.imwrite('label.jpg', lable_w)
分割结果
实验体会
我认为MRF图像分割有以下不足
1. 必须人为设定分割类别数量
2. 这种分割与语义分割差得远,感觉没什么用处,分出来同一类的既有帽子又有脸的
好处
1. 好处就是不需要训练吧。。。
内容总结
以上是互联网集市为您收集整理的基于马尔科夫随机场的图像分割程序(python实现)全部内容,希望文章能够帮你解决基于马尔科夫随机场的图像分割程序(python实现)所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。