weixin_47386125 2021-06-07 12:35 采纳率: 100%

# 用贝叶斯分类器做图像分割，但是一直出错，求大神解答？

``````# #贝叶斯分类器

from numpy import diag,dot,prod,exp,pi,sqrt

def guass(m,v,x):
if len(x.shape)==1:
n,d =1,x.shape[0]
else:
n,d =x.shape
S =diag(1/v)
x =x-m
y =exp(-0.5*diag(x,dot(S,x.T)))
return y*(2*pi)**(-d/2.0)/(sqrt(prod(v))+1e-6)
#
from numpy import mean,var,array
# from scipy import array
class BayesClassifier(object):
def __init__(self):
self.labels = []
self.mean = []
self.var =[]
self.n= 0

def train(self,data,labels=None):
if labels==None:
labels =range(len(data))
self.labels =labels
self.n =len(labels)
for c in data:
self.mean.append(mean(c,axis =0))
self.var.append(var(c,axis = 0))

def classify(self,points):
est_prob =array([guass(m,v,points)for m ,v in zip(self.mean,self.var)])
ndx = est_prob.argmax(axis=0)
est_labels = array([self.labels[n] for n in ndx])
return est_labels,est_prob
``````

``````from pygraph.classes.digraph import digraph
from pygraph.algorithms.minmax import maximum_flow
from bayes import BayesClassifier

"""
Graph Cut image segmentation using max-flow/min-cut.
"""

def build_bayes_graph(im, labels, sigma=1e2, kappa=1):
"""    Build a graph from 4-neighborhood of pixels.
Foreground and background is determined from
labels (1 for foreground, -1 for background, 0 otherwise)
and is modeled with naive Bayes classifiers."""

m, n = im.shape[:2]

# RGB vector version (one pixel per row)
vim = im.reshape((-1, 3))

# RGB for foreground and background
foreground = im[labels == 1].reshape((-1, 3))
background = im[labels == -1].reshape((-1, 3))
train_data = [foreground, background]

# train naive Bayes classifierbc
bc =BayesClassifier()
bc.train(train_data)

# get probabilities for all pixels
bc_lables, prob = bc.classify(vim)
prob_fg = prob[0]
prob_bg = prob[1]

# create graph with m*n+2 nodes
gr = digraph()

source = m * n  # second to last is source
sink = m * n + 1  # last node is sink

# normalize
for i in range(vim.shape[0]):
vim[i] = vim[i] / (linalg.norm(vim[i]) + 1e-9)

# go through all nodes and add edges
for i in range(m * n):
gr.add_edge((source, i), wt=(prob_fg[i] / (prob_fg[i] + prob_bg[i])))

gr.add_edge((i, sink), wt=(prob_bg[i] / (prob_fg[i] + prob_bg[i])))

if i % n != 0:  # left exists
edge_wt = kappa * exp(-1.0 * sum((vim[i] - vim[i - 1]) ** 2) / sigma)
if (i + 1) % n != 0:  # right exists
edge_wt = kappa * exp(-1.0 * sum((vim[i] - vim[i + 1]) ** 2) / sigma)
if i // n != 0:  # up exists
edge_wt = kappa * exp(-1.0 * sum((vim[i] - vim[i - n]) ** 2) / sigma)
if i // n != m - 1:  # down exists
edge_wt = kappa * exp(-1.0 * sum((vim[i] - vim[i + n]) ** 2) / sigma)

return gr

def cut_graph(gr, imsize):
"""    Solve max flow of graph gr and return binary
labels of the resulting segmentation."""

m, n = imsize
source = m * n  # second to last is source
sink = m * n + 1  # last is sink

# cut the graph
flows, cuts = maximum_flow(gr, source, sink)

# convert graph to image with labels
res = zeros(m * n)
for pos, label in list(cuts.items())[:-2]:  # don't add source/sink
res[pos] = label

return res.reshape((m, n))

def save_as_pdf(gr, filename, show_weights=False):
import graphviz as gv
dot = write(gr, weighted=show_weights)
gv.layout(gvv, 'fdp')
gv.render(gvv, 'pdf', filename)

def show_labeling(im, labels):
"""    Show image with foreground and background areas.
labels = 1 for foreground, -1 for background, 0 otherwise."""

imshow(im)
contour(labels, [-0.5, 0.5])
contourf(labels, [-1, -0.5], colors='b', alpha=0.25)
contourf(labels, [0.5, 1], colors='r', alpha=0.25)
# axis('off')
xticks([])
yticks([])

from scipy.misc import imresize
from PIL import Image
import numpy as np
from pylab import *
from skimage import transform

im = array(Image.open("cat.jpg"))
im =imresize(im,0.07)
print(im)
size = im.shape[:2]
print ("OK!!")

# add two rectangular training regions
labels = zeros(size)
labels[3:18, 3:18] = -1
labels[-18:-3, -18:-3] = 1
print ("OK!!")

# create graph
g = build_bayes_graph(im, labels, kappa=1)

# cut the graph
res = cut_graph(g, size)
print ("OK!!")

figure()
show_labeling(im, labels)

figure()
imshow(res)
gray()
axis('off')

show()
``````

• 写回答

#### 悬赏问题

• ¥15 结构体数组文件读取问题
• ¥15 写c++代码，第7题说一下使用的计算方法
• ¥15 postman接口自动化测试报告实践总结
• ¥15 有关c＋＋的问题，利用相关知识
• ¥15 求香农编码和解码的matlab代码
• ¥20 ROS中的TEB局部规划问题
• ¥20 关于#matlab#的问题：要求测出上面图片中所有同心圆的半径
• ¥20 epanet软件运行问题
• ¥15 Python 文件读取
• ¥60 dpabi进行Alff计算时脑池有干扰信号