[filename,pathname,flag] = uigetfile('.jpg','请导入图像文件'); pic = imread([pathname,filename]); figure; imshow(pic); %% 确定训练集 TrainData_background = zeros(20,3,'double'); TrainData_foreground = ones(20,3,'double'); % 背景采样 msgbox('请选择20个背景样本点','Background Samples','help'); pause; for run = 1:20 [x,y] = ginput(1); %ginput函数直接提取像素点,返回这个点的坐标 hold on; plot(x,y,'r*'); x = uint8(x); y = uint8(y); TrainData_background(run,1) = pic(x,y,1); TrainData_background(run,2) = pic(x,y,2); TrainData_background(run,3) = pic(x,y,3); end % 待分割出来的前景采样 msgbox('请选择20个前景样本点','Foreground Samples','help'); pause; for run = 1:20 [x,y] = ginput(1); hold on; plot(x,y,'ro'); x = uint8(x); y = uint8(y); TrainData_foreground(run,1) = pic(x,y,1); TrainData_foreground(run,2) = pic(x,y,2); TrainData_foreground(run,3) = pic(x,y,3); end % let background be 0 & foreground 1 TrainLabel = [zeros(length(TrainData_background),1); ... ones(length(TrainData_foreground),1)]; %% 建立支持向量机 基于libsvm TrainData = [TrainData_background;TrainData_foreground]; model = svmtrain(TrainLabel, TrainData, '-t 1 -d 3'); %% 进行预测 i.e.进行图像分割 基于libsvm preTrainLabel = svmpredict(TrainLabel, TrainData, model); [m,n,k] = size(pic); TestData = double(reshape(pic,m*n,k)); TestLabal = svmpredict(zeros(length(TestData),1), TestData, model); %% ind = reshape([TestLabal,TestLabal,TestLabal],m,n,k); ind = logical(ind); pic_seg = pic; pic_seg(~ind) = 255; figure; imshow(pic_seg); figure; subplot(1,2,1); imshow(pic); subplot(1,2,2); imshow(pic_seg); %% toc将这些代码转换成python语言
时间: 2024-03-10 11:48:42 浏览: 17
```
import cv2
import numpy as np
from sklearn import svm
from tkinter import Tk
from tkinter.filedialog import askopenfilename
from tkinter.messagebox import showinfo
# 选择图像文件
root = Tk()
root.withdraw()
file_path = askopenfilename(filetypes=[('JPEG', '*.jpg')])
pic = cv2.imread(file_path)
# 显示图像
cv2.imshow('pic', pic)
# 确定训练集
train_data_background = np.zeros([20, 3])
train_data_foreground = np.ones([20, 3])
# 背景采样
showinfo('Background Samples', '请选择20个背景样本点')
for run in range(20):
(x, y) = cv2.ginpuit(1)
cv2.circle(pic, (x, y), 1, (0, 0, 255), -1)
train_data_background[run, 0] = pic[x, y, 0]
train_data_background[run, 1] = pic[x, y, 1]
train_data_background[run, 2] = pic[x, y, 2]
# 待分割出来的前景采样
showinfo('Foreground Samples', '请选择20个前景样本点')
for run in range(20):
(x, y) = cv2.ginpuit(1)
cv2.circle(pic, (x, y), 1, (0, 0, 255), -1)
train_data_foreground[run, 0] = pic[x, y, 0]
train_data_foreground[run, 1] = pic[x, y, 1]
train_data_foreground[run, 2] = pic[x, y, 2]
# let background be 0 & foreground 1
train_label = np.concatenate((np.zeros(len(train_data_background)), np.ones(len(train_data_foreground))))
# 建立支持向量机
train_data = np.concatenate((train_data_background, train_data_foreground))
model = svm.SVC(kernel='poly', degree=3)
model.fit(train_data, train_label)
# 进行预测,即进行图像分割
(m, n, k) = pic.shape
test_data = pic.reshape((m*n, k))
test_label = model.predict(test_data)
ind = np.reshape(test_label, (m, n))
pic_seg = pic.copy()
pic_seg[ind==0] = 255
# 显示分割结果
cv2.imshow('pic_seg', pic_seg)
cv2.waitKey(0)
cv2.destroyAllWindows()
```