您的位置:首页 > 编程语言 > MATLAB

【PR学习笔记三】用SVM工具箱实现水天线检测

2014-04-10 23:47 369 查看
要求对一幅水面图像用SVM实现分类,检测出水天线



下面的实现中用到steven gunn编写的纯matlab版svm工具箱

close all;
f = imread('水天线原图.jpg');
imshow(f);
title('鼠标左键点天空,鼠标中键点陆地,鼠标右键点水,Enter完成输入');
[x,y,button] = ginput; %读取鼠标点击坐标值和类型
% trNum1 = size ?怎样分别统计天空陆地和水面的训练样本个数
[trNum, n] = size(x); % 统计训练样本个数
[row, colum, value] = size(f);

% fR = f(:,:,1);
% fG = f(:,:,2);
% fB = f(:,:,3);

trX = zeros(trNum,3); %训练向量初始化
trY = zeros(trNum); %训练类别初始化
figure,
subplot(1,2,1);
title('训练样本分布');
hold on
for i=1:trNum;
trX(i,:) = [f(round(y(i)),round(x(i)),1),f(round(y(i)),round(x(i)),2),f(round(y(i)),round(x(i)),3)]; %行数是训练样本数,RGB值构成三列;注意坐标的转换
% trX(i,:) = [fR(round(x(i)),round(y(i))),fG(round(x(i)),round(y(i))),fB(round(x(i)),round(y(i)))];
switch button(i)
case 1
trY(i) = -1;
plot(trX(i,1),trX(i,2),'r+');
case 2
trY(i) = -1;
plot(trX(i,1),trX(i,2),'b*');
case 3
trY(i) = 1;
plot(trX(i,1),trX(i,2),'go');

end
end
% legend('天空','陆地','水面','Location','SouthEast');
xlabel('R');
ylabel('G') ;
hold off

ker = 'rbf'; %采用rbf核函数
C = 10; %容忍度
trX = trX(:,1:2); %将R,G值作为特征训练
[nsv,alpha,bias]=svc(trX,trY,ker,C);

% subplot(1,2,2);
% svcplot(trX,trY,ker,alpha, bias);

fprintf('分类中,请等待。。');
tStart = clock;
for i=1:row;
for j=1:colum;
tstX = double(f(i,j,1:2));
preY = svcoutput(trX,trY,tstX,ker,alpha,bias); %预测过程:逐个像素分类
if (preY==1)
g(i,j)=255;
else
g(i,j)=0;
end
end
end
subplot(1,2,2);
title('分类预测结果');
imshow(g); %显示分类结果
tStop = clock;
fprintf('\n预测和绘图过程耗时:');
fprintf(num2str(etime(tStop,tStart)));

分类过程花费大约三分钟,最后的结果是介个样子滴。。



目前还只是实现的一个分类的可视化结果,还没有进行标注,后面有时间会继续改进;

另:原图是719*577的分辨率,在我i5处理器4G内存64bit系统的本子上就跑了三分钟,会不会略慢啊
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息