赞
踩
1. 直线检测
1.1. Radon直线检测原理
基于Radon变换的直线检测的目的就是检测根据角度变化时出现的“局部峰值”,即可以确定直线的方向,同时,峰值大小能够确定直线上点的个数
1.2. Hough 直线检测原理
将直线利用极坐标表示时,一条直线即可通过角度和长度确定,通过对角度和长度计算累计图,寻找峰值点即可确定一条直线:
1.3. 正弦图合击-分进直线检测
2. 实现代码
%LineDetection.m
%Author: HSW
%Date: 2015/4/21
%HARBIN INSTITUTE OF TECHNOLOGY
%
%Set Matlab
close all;
clear all;
clc;
% Add Path
addpath(genpath('MultiLayerLineUtil\'));
addpath(genpath('SingleLayerLineUtil\'));
addpath(genpath('RadonLineUtil\'));
% 测试图像路径和结果保存路径
ImageFilePath = 'TestImage\';
SaveFilePath = 'Results\';
type = ['*.png';'*.bmp';'*.jpg'];
MaxSigma = 1;
sigmastep = 0.05;
% 考虑是否需要设置Neighborstep 和 MaxNeighbor,因为每张图都不一样
% ,特别是随着噪声的增大时,Neighbor需要增大可能更好
Neighborstep = 5;
MaxNeighbor = 60;
Neighbor = 11;
%比较算法如下
%1. 标准Hough变换直线检测
%2. 标准Radon变换直线检测
%3. 基于Fourier变换的Radon变换直线检测
%4. 基于MultiLayer Fourier变换的Radon变换直线检测
%5. 零填充Fourier变换的Radon变换直线检测(扩大图像)
% imgdir = dir(fullfile(ImageFilePath,type(1,:))); %修改为type(2,:)
% 处理.bmp格式图片, type(3,:),处理.jpg格式图片,这里是为了批处理(但是这里不需要)
% for iterimage = 1:length(imgdir)
% Img = imread(fullfile(ImageFilePath,imgdir(iterimage).name));
% Img = imread(fullfile(ImageFilePath,'NewLine2.png'));Nhood = [51,51]; Numpeaks = 1; %注意修改peaks的数目
% Img = imread(fullfile(ImageFilePath,'NewOneLine.png'));Nhood = [51,51]; Numpeaks = 1; %注意修改peaks的数目
% Img = imread(fullfile(ImageFilePath,'OneLine1.png'));Nhood = [51,51]; Numpeaks = 1; %注意修改peaks的数目
% Img = imread(fullfile(ImageFilePath,'OneLine.png'));Nhood = [51,51]; Numpeaks = 1; %注意修改peaks的数目
% Img = imread(fullfile(ImageFilePath,'Line30_256.png')); Nhood = [31,31]; Numpeaks = 30; %注意修改peaks的数目
Img = imread(fullfile(ImageFilePath,'Line30_512.png')); Nhood = [31,31]; Numpeaks = 31; %注意修改peaks的数目
% Img = 255-imread(fullfile(ImageFilePath,'house.png'));Nhood = [51,51]; Numpeaks = 11; %注意修改peaks的数目
% Img = imread(fullfile(ImageFilePath,'half.png'));Nhood = [51,51]; Numpeaks = 1; %注意修改peaks的数目
% Img = imread(fullfile(ImageFilePath,'paper.png'));Nhood = [11,11]; Numpeaks = 3; %注意修改peaks的数目
% Img = imread(fullfile(ImageFilePath,'5line.png'));Nhood = [31,31]; Numpeaks = 5; %注意修改peaks的数目
if size(Img,3) == 3
Img = rgb2gray(Img);
end
NextPow1 = nextpow2(size(Img,1));
NextPow2 = nextpow2(size(Img,2));
if abs(size(Img,1) - 2^NextPow1) > abs(size(Img,1) - 2^(NextPow1 - 1))
NextPow1 = NextPow1 -1;
end
if abs(size(Img,2) - 2^NextPow2) > abs(size(Img,2) - 2^(NextPow2 - 1))
NextPow2 = NextPow2 - 1;
end
NextPow = max(NextPow1,NextPow2);
Img = imresize(Img,[2^NextPow, 2^NextPow],'bicubic');
for itersigma = 1:MaxSigma
nImg = double(Img) + (itersigma - 1)*randn(size(Img)); %添加噪声
% 标准Hough变换直线检测
EdImg = edge(nImg,'canny');
% title('待检测图像');
[R,theta,rho] = hough(EdImg,'ThetaResolution',1);
% 峰值个数
%检测的峰值为2xNumpeaks的数组, rho = peaks(:,2), theta = peaks(:,1)
peaks = houghpeaks(R,Numpeaks,'Threshold',0.1*max(R(:)),'Nhood',Nhood);
%显示检测到的峰值
if ~isempty(peaks)
figure;
% subplot(1,3,2);
imshow(R,[]);
title('正弦图像');
for iter = 1:size(peaks,1)
hold on;
scatter(peaks(iter,2),peaks(iter,1),'r');
end
end
lines = houghlines(EdImg,theta,rho,peaks,'FillGap',5,'MinLength',7);
if ~isempty(lines)
figure;
imshow(nImg/255,[]);
hold on;
title('Hough变换');
for iter = 1:length(lines)
xy = [lines(iter).point1;lines(iter).point2];
plot(xy(:,1),xy(:,2),'LineWidth',1,'Color','green');
end
end
% 标准Radon变换直线检测
EdImg = edge(nImg,'sobel');
% 角分辨率
theta = 0:0.5:179.5;
%进行Radon变换
[R,rho] = radon(EdImg,theta);
%检测的峰值为2xNumpeaks的数组, rho = peaks(:,2), theta = peaks(:,1)
peaks = radonpeaks(R,Numpeaks,'Threshold',0,'Nhood',Nhood);
%显示检测到的峰值
if ~isempty(peaks)
figure;
imshow(R,[]);
title('正弦图');
for iter = 1:size(peaks)
hold on;
scatter(peaks(iter,2),peaks(iter,1),'r');
end
end
% 显示检测到的直线
type = 1;
if type == 1
radonlines(nImg,theta,rho',peaks,type);
elseif type == 2
lines = radonlin
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。