【PR学习笔记三】用SVM工具箱实现水天线检测
2014-04-10 23:47
399 查看
要求对一幅水面图像用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系统的本子上就跑了三分钟,会不会略慢啊
下面的实现中用到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系统的本子上就跑了三分钟,会不会略慢啊
相关文章推荐
- OpenCv学习笔记--支持向量机SVM线性可分情况下的OpenCv实现的超详细注释(2)
- OpenCv学习笔记--支持向量机SVM之C++的实现(1)
- 《OpenCV 3计算机视觉:Python语言实现》学习笔记——目标跟踪中基本运动检测的思考
- (学习笔记二)——基于opencv人脸检测原理及实现
- 【cuda学习笔记】2.纹理对象API的使用,实现sobel边缘检测
- CS231n课程学习笔记(二)——SVM的实现
- 【PR学习笔记二】【搬砖】SVM(支持向量机)基础
- 【OpenCV学习笔记】【教程翻译】四(车牌检测之SVM分类)
- 【OpenCV学习笔记 019】SIFT和SURF算法实现目标检测
- 学习笔记——Kaggle_Digit Recognizer (SVM算法 Python实现)
- (学习笔记二)——基于opencv人脸检测原理及实现
- Android开发学习笔记-实现联网检测程序版本
- [matlab]机器学习及SVM工具箱学习笔记
- 轻量级远程调用框架-Hessian学习笔记-Demo实现
- B-树的学习笔记与C实现
- Spring MVC 学习笔记6 —— 实现简单的用户管理(2)添加和验证
- 学习笔记之lvm基本应用及其扩展和缩减实现
- Deep Q-Network 学习笔记(二)—— Q-Learning与神经网络结合使用(有代码实现)
- 树莓派学习笔记——实现modbus RTU从机
- Mybatis学习笔记三:实现增删查改