必威体育Betway必威体育官网
当前位置:首页 > IT技术

直线检测——Radon变换/霍夫变换/基于快速傅里叶变换的直线检测

时间:2019-10-18 07:11:15来源:IT技术作者:seo实验室小编阅读:90次「手机版」
 

radon变换

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 = radonlines(EdImg,theta,rho',peaks,type,20,40,2);
        if ~isempty(lines)
            figure;
            imshow(nImg);
            title('Radon变换');
            for iter = 1:length(lines)
                hold on;
                xy = [lines(iter).point1;lines(iter).point2];
                plot(xy(:,1),xy(:,2),'LineWidth',1,'Color','green');
            end
        end
    end
    
    %基于Fourier变换的Radon变换直线检测
    shiftImg = fftshift(nImg);
    fftImg = FractionalFT(shiftImg,1,1); % 进行fourier变换
    Center = round(0.5*size(fftImg)); %中心坐标
    gridfftImg = [(-0.5*size(fftImg,1):0.5*size(fftImg,1)-1)',(-0.5*size(fftImg,2):0.5*size(fftImg,2)-1)'];
    
    MaxRho = sqrt(sum(sum((0.5*size(fftImg)).^2)));
    Nrho = round(MaxRho);
    Ntheta = length(0:0.5:179.5);
    de=59;di=26;
    ax = [1,1,1];
    ay = ax;
    fftImg = xvMAPPingOpt(gridfftImg,gridfftImg,gridfftImg,fftImg,fftImg,fftImg,Nrho,Ntheta+1,ax,ay,de,di,Center(1),Center(2));
    fftImg = fftImg(1:Nrho,1:Ntheta);
    
    conjfftImg = flipud(conj(fftImg));
    TwofftImg = [conjfftImg;fftImg(2:end,:)];
    radonImg = fft(TwofftImg);
    radonImg = abs(radonImg)/size(fftImg,1);
    L1 = radonImg(1:size(fftImg,1),:);
    L2 = radonImg(size(fftImg,1)+1:2*size(fftImg,1)-1,:);
    Ra = flipud([L2;L1]);
%     NormalRa = abs(Ra)/max(max(abs(Ra)));
    %     % 峰值个数
    %     Numpeaks = 3;
    %检测的峰值为2xNumpeaks的数组, rho = peaks(:,2), theta = peaks(:,1)
    peaks = radonpeaks(abs(Ra)*10000,Numpeaks,'Threshold',0.1*10000*ceil(max(max(abs(Ra)))),'Nhood',Nhood);
    
    %显示检测到的峰值
    if ~isempty(peaks)
        figure;
        imshow(log(abs(Ra) + 1),[]);
        for iter = 1:size(peaks,1)
            hold on;
            scatter(peaks(iter,2),peaks(iter,1),'r');
        end
    end
    
    % 显示检测到的直线
    [h,w,ncolor]=size(nImg);
    r=0.5*sqrt(2)*w;
    Factor = Nrho/size(Img,1);

    centery=floor(h*0.5)+1;
    centerx=floor(w*0.5)+1;
    r=0.5*sqrt(2)*w;
    figure;
    imshow(nImg/255,[]);
    title('Single-Layer Fourier'); 
    hold on;
    for count=1:size(peaks,1)
        r1a=peaks(count,1);%rho
        c1=peaks(count,2); %theta
        rho1=Factor*(r1a-Nrho-1)/Nrho*r;%为什么要乘以0.5?
        theta1=(c1-1)/Ntheta*180;
        if rho1 <= 0 && theta1 <= 90 
             % 直线在左下角
            theta2 = 90-theta1;
            slope = tan((theta2)*pi/180);
            y1 = centery - rho1*sin(theta1*pi/180);
            x1 = centerx + rho1*cos(theta1*pi/180);
        elseif rho1 < 0 && theta1 > 90
             % 直线在右下角
            theta2 = 270-theta1;
            theta1 = 180-theta1;
            slope = tan((theta2)*pi/180);
            y1 = centery - rho1*sin(theta1*pi/180);
            x1 = centerx - rho1*cos(theta1*pi/180);
        elseif rho1 > 0 && theta1 < 90 
            % 直线在右上角
            theta2 = 90 - theta1;
            slope = tan((theta2)*pi/180);
            y1 = centery - rho1*sin(theta1*pi/180);
            x1 = centerx + rho1*cos(theta1*pi/180);
        else
            % 直线在左上角
            theta2 = 270-theta1;
            theta1 = 180-theta1;
            slope = tan((theta2)*pi/180);
            y1 = centery - rho1*sin(theta1*pi/180);
            x1 = centerx - rho1*cos(theta1*pi/180);
        end 
        x =centerx-size(Img,2):centerx+size(Img,2);
        y = slope*(x-x1) + y1;
        plot(x,y,'g')
    end
    
    % 多层fourier变换直线检测
    ax=[];
    ay=[];
    I1=double(nImg);
    I1=fftshift(I1);
    [h,w]=size(I1);
    x0=-w*0.5:w*0.5-1;y0=-h*0.5:h*0.5-1;
    centery=floor(h*0.5)+1;
    centerx=floor(w*0.5)+1;
    
    a1=0.8;a2=0.9;
    x1=a1*x0;y1=a2*y0;
    ax=[ax a1];ay=[ay a2];
    f1=FractionalFT(I1,a1,a2);
    a1=0.9;a2=0.7;
    x2=x0*a1;y2=y0*a2;
    ax=[ax a1];ay=[ay a2];
    f2=FractionalFT(I1,a1,a2);
    a1=1;a2=1;
    x3=x0*a1;y3=y0*a2;
    ax=[ax a1];ay=[ay a2];
    f3=FractionalFT(I1,a1,a2);
    %creat grid
    grid1=[x1' y1'];grid2=[x2' y2'];grid3=[x3' y3'];
    %computer the covariance
    theta=Ntheta;rho=Nrho;
    de=59;di=26;%thL=0.5*dr; thH=10*dr;%0.8*sigma;
    L1=xvMappingOpt(grid1,grid2,grid3,f1,f2,f3,rho,theta+1,ax,ay,de,di,centery,centerx);
    L1=L1(1:rho,1:theta);
    L2=flipud(conj(L1));
    
    %L3=cat(1,L2,L1);
    L3=[L2;L1(2:rho,:)];%忽略重复的0度,并且相当于求了共轭
    % %perform 1D FFT for each column
    YfreqDomain=fft(L3);
    L=abs(YfreqDomain)/h;
    L1=L(1:rho,:);L2=L(rho+1:rho*2-1,:);
    Ra=flipud([L2
        L1]);
    NormalRa=abs(Ra)/max(max(abs(Ra)));
    
    %Peak Detection
    H = log(abs(Ra)+1)*10000;
    peaks=radonpeaks(H,Numpeaks,'threshold',ceil(0.0001*max(H(:))),'Nhood',Nhood);

    %显示检测到的峰值
    if ~isempty(peaks)
        figure;
        imshow(log(abs(Ra) + 1),[]);
        for iter = 1:size(peaks,1)
            hold on;
            scatter(peaks(iter,2),peaks(iter,1),'r');
        end
    end
    figure; 
    imshow(nImg/255,[]);
    title('Multi-Layer Fourier'); 
    hold on; 
    for count=1:size(peaks,1)
        r1a=peaks(count,1);%rho
        c1=peaks(count,2); %theta
        rho1=Factor*(r1a-Nrho-1)/Nrho*r;%为什么要乘以0.5?

        theta1=(c1-1)/Ntheta*180;
        if rho1 <= 0 && theta1 <= 90 
             % 直线在左下角
            theta2 = 90-theta1;
            slope = tan((theta2)*pi/180);
            y1 = centery - rho1*sin(theta1*pi/180);
            x1 = centerx + rho1*cos(theta1*pi/180);
        elseif rho1 < 0 && theta1 > 90
             % 直线在右下角
            theta2 = 270-theta1;
            theta1 = 180-theta1;
            slope = tan((theta2)*pi/180);
            y1 = centery - rho1*sin(theta1*pi/180);
            x1 = centerx - rho1*cos(theta1*pi/180);
        elseif rho1 > 0 && theta1 < 90 
            % 直线在右上角
            theta2 = 90 - theta1;
            slope = tan((theta2)*pi/180);
            y1 = centery - rho1*sin(theta1*pi/180);
            x1 = centerx + rho1*cos(theta1*pi/180);
        else
            % 直线在左上角
            theta2 = 270-theta1;
            theta1 = 180-theta1;
            slope = tan((theta2)*pi/180);
            y1 = centery - rho1*sin(theta1*pi/180);
            x1 = centerx - rho1*cos(theta1*pi/180);
        end 
        x =centerx-size(Img,2):centerx+size(Img,2);
        y = slope*(x-x1) + y1;
        plot(x,y,'g')
    end %for itersigma
end
% end%for iterimage



% % 读入图像
% % 实验1 peaks = houghpeaks(R,Numpeaks,'Nhood',[51,51]);
% Img = imread('paper.png');
%
% % 实验2 peaks = houghpeaks(R,Numpeaks,'Nhood',[51,51]);
% % Img = imread('ThreeLine.bmp');
% % Img = 255 - Img;
%
% % 实验3 peaks = houghpeaks(R,Numpeaks,'Nhood',[11,11]);
% % Img = imread('SevenLine.png');
% if size(Img,3) == 3
%     Img = rgb2gray(Img);
% end

Radon变换直线检测代码:

function lines = radonlines(varargin)
%RADONLINES extract line segments based on Radon transform.
%
%   LINES = HOUGHLINES(...,PARAM1,VAL1,PARAM2,VAL2) sets various
%   parameters. Parameter names can be abbreviated, and case
%   does not matter. Each string parameter is followed by a value
%   as indicated below:
%
%   'FillGap'   Positive real scalar.
%               When HOUGHLINES finds two line segments associated
%               with the same Hough transform bin that are separated
%               by less than 'FillGap' distance, HOUGHLINES merges
%               them into a single line segment.
%
%               Default: 20 直线进行合并
%
%   'MinLength' Positive real scalar.
%               Merged line segments shorter than 'MinLength'
%               are discarded.
%
%               Default: 40 直线的最短长度
%
%   Class Support
%   -------------
%   BW can be logical or numeric and it must be real, 2-D, and nonsparse.
% Author: HSW
% Date: 2015/4/21
% HARBIN INSTITUTE OF TECHNOLOGY
%
center = floor((0.5*size(varargin{1})));
centery = center(1);
centerx = center(2);
theta = varargin{2};
rho = varargin{3};
peaks = varargin{4};
type = varargin{5}; % 选择画直线还是画线段
if isempty(peaks)
    disp('no peaks');
    return;
end

if type == 1
    % 画直线
    figure;
    imshow(varargin{1});
    hold on;
    for iter = 1:size(peaks,1)
        rho1 = rho(peaks(iter,1));
        theta1 = theta(peaks(iter,2));
        if rho1 <= 0 && theta1 <= 90
            % 直线在左下角
            theta2 = 90-theta1;
            slope = tan((theta2)*pi/180);
            y1 = centery - rho1*sin(theta1*pi/180);
            x1 = centerx + rho1*cos(theta1*pi/180);
        elseif rho1 < 0 && theta1 > 90
            % 直线在右下角
            theta2 = 270-theta1;
            theta1 = 180-theta1;
            slope = tan((theta2)*pi/180);
            y1 = centery - rho1*sin(theta1*pi/180);
            x1 = centerx - rho1*cos(theta1*pi/180);
        elseif rho1 > 0 && theta1 < 90
            % 直线在右上角
            theta2 = 90 - theta1;
            slope = tan((theta2)*pi/180);
            y1 = centery - rho1*sin(theta1*pi/180);
            x1 = centerx + rho1*cos(theta1*pi/180);
        else
            % 直线在左上角
            theta2 = 270-theta1;
            theta1 = 180-theta1;
            slope = tan((theta2)*pi/180);
            y1 = centery - rho1*sin(theta1*pi/180);
            x1 = centerx - rho1*cos(theta1*pi/180);
        end
        x =centerx-size(varargin{1},2):centerx+size(varargin{1},2);
        y = slope*(x-x1) + y1;
        plot(x,y,'g')
        
    end
elseif type == 2
    % 画线段
    fillgap = varargin{6};
    minlength = varargin{7};
    delta = varargin{8};
    minlength_sq = minlength^2;
    fillgap_sq = fillgap^2;
    numlines = 0;
    [y,x] = find(varargin{1});
    nonzeropix = [x,y] - 1;
    lines = struct([]);
    for k = 1:size(peaks,1)
        [r,c] = radonpixels(nonzeropix,theta,rho,delta,peaks(k,:),center);
        if isempty(r)
            continue;
        end
        % Compute distance^2 between the point pairs
        xy = [c r]; % x,y pairs in coordinate system with the origin at (1,1)
        diff_xy_sq = diff(xy,1,1).^2;
        dist_sq = sum(diff_xy_sq,2);
        
        % Find the gaps larger than the threshold
        fillgap_idx = find(dist_sq > fillgap_sq);
        idx = [0; fillgap_idx; size(xy,1)];
        for p = 1:length(idx) - 1
            p1 = xy(idx(p) + 1,:); % offset by 1 to convert to 1 based index
            p2 = xy(idx(p + 1),:); % set the end (don't offset by one this time)
            
            linelength_sq = sum((p2-p1).^2);
            if linelength_sq >= minlength_sq
                numlines = numlines + 1;
                lines(numlines).point1 = p1;
                lines(numlines).point2 = p2;
                lines(numlines).theta = theta(peaks(k,2));
                lines(numlines).rho = rho(peaks(k,1));
            end
        end
    end %for k = 1:size(peaks,1)
else
    ERROR('type = 1 or type = 2');
end %if type

end %function radonlines

function [r,c] = radonpixels(nonzeropix,theta,rho,delta,peak,center)
x = nonzeropix(:,1);
y = nonzeropix(:,2);
centery = center(1);
centerx = center(2);
rho1 = rho(peak(1));
theta1 = theta(peak(2));
if rho1 <= 0 && theta1 <= 90
    % 直线在左下角
    theta2 = 90-theta1;
    slope = tan((theta2)*pi/180);
    y1 = centery - rho1*sin(theta1*pi/180);
    x1 = centerx + rho1*cos(theta1*pi/180);
elseif rho1 < 0 && theta1 > 90
    % 直线在右下角
    theta2 = 270-theta1;
    theta1 = 180-theta1;
    slope = tan((theta2)*pi/180);
    y1 = centery - rho1*sin(theta1*pi/180);
    x1 = centerx - rho1*cos(theta1*pi/180);
elseif rho1 > 0 && theta1 < 90
    % 直线在右上角
    theta2 = 90 - theta1;
    slope = tan((theta2)*pi/180);
    y1 = centery - rho1*sin(theta1*pi/180);
    x1 = centerx + rho1*cos(theta1*pi/180);
else
    % 直线在左上角
    theta2 = 270-theta1;
    theta1 = 180-theta1;
    slope = tan((theta2)*pi/180);
    y1 = centery - rho1*sin(theta1*pi/180);
    x1 = centerx - rho1*cos(theta1*pi/180);
end
idx = find(abs(slope*(x-x1) + y1 - y) <= delta); %进行直线拟合
r = y(idx) + 1;
c = x(idx) + 1;
[r,c] = reSortRadonPixels(r,c);
end% function radonpixels

function [r_new,c_new] = reSortRadonPixels(r,c)

if isempty(r)
    r_new = r;
    c_new = c;
    return;
end

r_range = max(r) - min(r);
c_range = max(c) - min(c);

if r_range > c_range
    sorting_order = [1,2];
else
    sorting_order = [2,1];
end
[rc_new] = sortrows([r,c],sorting_order);
r_new = rc_new(:,1);
c_new = rc_new(:,2);
end % function reSortRadonPixels

function peaks = radonpeaks(varargin)
% RADONPEAKS Identify peaks in Radon transform. 
%   PEAKS = HOUGHPEAKS(H,NUMPEAKS) locates peaks in the Hough 
%   transform matrix, H, generated by the HOUGH function. NUMPEAKS 
%   specifies the maximum number of peaks to identify. PEAKS is 
%   a Q-by-2 matrix, where Q can range from 0 to NUMPEAKS. Q holds
%   the row and column coordinates of the peaks. If NUMPEAKS is 
%   omitted, it defaults to 1.
%
%   PEAKS = HOUGHPEAKS(...,PARAM1,VAL1,PARAM2,VAL2) sets various 
%   parameters. Parameter names can be abbreviated, and case 
%   does not matter. Each string parameter is followed by a value 
%   as indicated below:
%
%   'Threshold' Nonnegative scalar.
%               Values of H below 'Threshold' will not be considered
%               to be peaks. Threshold can vary from 0 to Inf.
%   
%               Default: 0.5*max(H(:))
%
%   'NHoodSize' Two-element vector of positive odd integers: [M N].% odd 奇数
%               'NHoodSize' specifies the size of the suppression
%               neighborhood. This is the neighborhood around each 
%               peak that is set to zero after the peak is identified.
%
%               Default: smallest odd values greater than or equal to
%                        size(H)/50.
%
%   Class Support
%   -------------
%   H is the output of the HOUGH function. NUMPEAKS is a positive
%   integer scalar.
%
%   example
%   -------
%   Locate and display two peaks in the Hough transform of the 
%   rotated circuit.tif image.
%
%      I  = imread('circuit.tif');
%      BW = edge(imrotate(I,50,'crop'),'canny');
%      [H,T,R] = hough(BW);
%      P  = houghpeaks(H,2);
%      imshow(H,[],'xdata',T,'YData',R,'InitialMagnification','fit');
%      xlabel('\theta'), ylabel('\rho');
%      axis on, axis normal, hold on;
%      plot(T(P(:,2)),R(P(:,1)),'s','color','white');
%
%   See also HOUGH and HOUGHLINES.

%   Author: HSW
%   HARBIN INSTITUTE OF TECHNOLOGY 
[h, numpeaks, threshold, nhood] = parseInputs(varargin{:});
% h: radon 变换的输出
% numpeaks: 峰值的个数
% threshold: 峰值的最小值, 默认为0.5*max(H(:))
% nhood: 包含两个奇数的数组[M,N], 当峰值识别出来后,设置为0 

% initialize the loop variables
done = false;
hnew = h;
nhood_center = (nhood-1)/2;% 抑制块的中心位置,例如nhood = [5,5], 则nhood_center = [2,2]
peaks = [];
% 循环搜索峰值
while ~done
  [dummy max_idx] = max(hnew(:)); %#ok寻找现有的最大值
  [p, q] = ind2sub(size(hnew), max_idx);
  
  p = p(1); q = q(1);
  if hnew(p, q) >= threshold
    peaks = [peaks; [p q]]; %#ok<AGROW> % add the peak to the list
    
    % Suppress this maximum and its close neighbors.
    p1 = p - nhood_center(1); p2 = p + nhood_center(1);
    q1 = q - nhood_center(2); q2 = q + nhood_center(2);
    % throw away neighbor coordinates that are out of bounds in
    % the rho direction.
    [qq, pp] = meshgrid(q1:q2, max(p1,1):min(p2,size(h,1)));
    pp = pp(:); qq = qq(:);
    
    % For coordinates that are out of bounds in the theta
    % direction, we want to consider that H is antisymmetric
    % along the rho axis for theta = +/- 90 degrees.
    theta_too_low = find(qq < 1);
    qq(theta_too_low) = size(h, 2) + qq(theta_too_low);
    pp(theta_too_low) = size(h, 1) - pp(theta_too_low) + 1;
    theta_too_high = find(qq > size(h, 2));
    qq(theta_too_high) = qq(theta_too_high) - size(h, 2);
    pp(theta_too_high) = size(h, 1) - pp(theta_too_high) + 1;
    
    % Convert to linear indices to zero out all the values.
    hnew(sub2ind(size(hnew), pp, qq)) = 0; %设置为0 
    
    done = size(peaks,1) == numpeaks;
  else
    done = true;
  end
end

%-----------------------------------------------------------------------------
function [h, numpeaks, threshold, nhood] = parseInputs(varargin)

narginchk(1,6); % 参数个数小于1 大于6报错

h = varargin{1};
% validateattributes(h, {'double'}, {'real', '2d', 'nonsparse', 'nonempty',...
%                    'finite', 'integer'}, ...
%                    mfilename, 'H', 1);
% hough变换的中h中的取值必然为非负整数,但是,radon变换中只能保证为非负的

validateattributes(h, {'double'}, {'real', '2d', 'nonsparse', 'nonempty',...
                   'finite'}, ...
              mfilename, 'H', 1);

numpeaks = 1; % set default value for numpeaks峰值的默认值为1

idx = 2;
if nargin > 1
  if ~ischar(varargin{2})
    numpeaks = varargin{2};
    validateattributes(numpeaks, {'double'}, {'real', 'scalar', 'integer', ...
                        'positive', 'nonempty'}, mfilename, 'NUMPEAKS', 2);
    idx = 3;
  end
end

% Initialize to empty
nhood = [];
threshold = [];

% Process parameter-value pairs
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
validStrings = {'Threshold','NHoodSize'};

if nargin > idx-1 % we have parameter/value pairs
  done = false;

  while ~done
    input = varargin{idx};
    inputStr = validatestring(input, validStrings,mfilename,'PARAM',idx);
    
    idx = idx+1; %advance index to point to the VAL portion of the input 
    
    if idx > nargin
      error(message('images:houghpeaks:valForhoughpeaksMissing', inputStr))
    end
    
    switch inputStr
      
     case 'Threshold'
      threshold = varargin{idx};
      validateattributes(threshold, {'double'}, {'real', 'scalar','nonnan' ...
                          'nonnegative'}, mfilename, inputStr, idx);
     
     case 'NHoodSize'
      nhood = varargin{idx};
      validateattributes(nhood, {'double'}, {'real', 'vector', ...
                          'finite','integer','positive','odd'},...
                    mfilename, inputStr, idx);
      
      if (any(size(nhood) ~= [1,2]))
        error(message('images:radonpeaks:invalidNHoodSize', inputStr))
      end
      
       if (any(nhood > size(h)))
        error(message('images:radonpeaks:tooBigNHoodSize', inputStr))
      end     
      
     otherwise
      %should never get here
      error(message('images:radonpeaks:internalError'))
    end
    
    if idx >= nargin
      done = true;
    end
    
    idx=idx+1;
  end
end

% Set the defaults if necessary
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
if isempty(nhood)
  nhood = size(h)/50; 
  nhood = max(2*ceil(nhood/2) + 1, 1); % Make sure the nhood size is odd.确保nhood为奇数
end

if isempty(threshold)
  threshold = 0.5 * max(h(:)); %设置默认值
end

3. 模型效果

3.1 Hough变换直线检测(Matlab内建函数

3.2 Radon变换直线检测

3.3 合击-分进直线检测

相关阅读

碰撞检测

1.矩形与矩形碰撞 一般规则的物体碰撞都可以处理成矩形碰撞,实现的原理就是检测两个矩形是否重叠。 矩形1的参数是:左上角的坐标是

点到直线的距离公式

然后

MHDD检测不到硬盘的解决办法

进入MHDD4.6,进入软件后,只发现了光驱,没有发现硬盘。在BIOS下却能看到硬盘。可能是因为硬盘是SATA的问题,有2种方法可以解决。方法一

DCT变换自学笔记

一、DCT简介此处,DCT指Discrete Cosine Transform,意思是离散余弦变换(下文均用DCT表示),其常见用途是对音视频进行数据压缩。维基百科

拉普拉斯变换拾遗

1、前言 因笔者学生时代,复变函数相关课程,学得并不认真,以致于工作后,阅读相关论文时,遇到拉普拉斯变换时,常不知所言,遂心生“负师友规

分享到:

栏目导航

推荐阅读

热门阅读