');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%letbackgroundbe0&foreground1TrainLabel=[zeros(length(TrainData_background),1);...ones(length(TrainData_foreground),1)];%%建立支持向量机基于libsvmTrainData=[TrainData_background;TrainData_foreground];model=svmtrain(TrainLabel,TrainData,'-t1-d1');%%进行预测i.e.进行图像分割基于libsvmpreTrainLabel=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)=0;figure;imshow(pic_seg);figure;subplot(1,2,1);imshow(pic);xlabel('原始图像');subplot(1,2,2);imshow(pic_seg);xlabel('分割出的目标');%%toc;