日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

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

發布時間:2023/12/14 编程问答 34 豆豆
生活随笔 收集整理的這篇文章主要介紹了 直线检测——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) == 3Img = 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:MaxSigmanImg = 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');endendlines = 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');endend% 標準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');endend% 顯示檢測到的直線type = 1;if type == 1radonlines(nImg,theta,rho',peaks,type);elseif type == 2lines = 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');endendend%基于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');endend% 顯示檢測到的直線[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);%rhoc1=peaks(count,2); %thetarho1=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 gridgrid1=[x1' y1'];grid2=[x2' y2'];grid3=[x3' y3'];%computer the covariancetheta=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 columnYfreqDomain=fft(L3);L=abs(YfreqDomain)/h;L1=L(1:rho,:);L2=L(rho+1:rho*2-1,:);Ra=flipud([L2L1]);NormalRa=abs(Ra)/max(max(abs(Ra)));%Peak DetectionH = 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');endendfigure; imshow(nImg/255,[]);title('Multi-Layer Fourier'); hold on; for count=1:size(peaks,1)r1a=peaks(count,1);%rhoc1=peaks(count,2); %thetarho1=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; endif 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);endx =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 pairsxy = [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 thresholdfillgap_idx = find(dist_sq > fillgap_sq);idx = [0; fillgap_idx; size(xy,1)];for p = 1:length(idx) - 1p1 = xy(idx(p) + 1,:); % offset by 1 to convert to 1 based indexp2 = xy(idx(p + 1),:); % set the end (don't offset by one this time)linelength_sq = sum((p2-p1).^2);if linelength_sq >= minlength_sqnumlines = numlines + 1;lines(numlines).point1 = p1;lines(numlines).point2 = p2;lines(numlines).theta = theta(peaks(k,2));lines(numlines).rho = rho(peaks(k,1));endendend %for k = 1:size(peaks,1) elseerror('type = 1 or type = 2'); end %if typeend %function radonlinesfunction [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 radonpixelsfunction [r_new,c_new] = reSortRadonPixels(r,c)if isempty(r)r_new = r;c_new = c;return; endr_range = max(r) - min(r); c_range = max(c) - min(c);if r_range > c_rangesorting_order = [1,2]; elsesorting_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) >= thresholdpeaks = [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;elsedone = 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峰值的默認值為1idx = 2; if nargin > 1if ~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 pairsdone = false;while ~doneinput = 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 > narginerror(message('images:houghpeaks:valForhoughpeaksMissing', inputStr))endswitch inputStrcase '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))endif (any(nhood > size(h)))error(message('images:radonpeaks:tooBigNHoodSize', inputStr))end otherwise%should never get hereerror(message('images:radonpeaks:internalError'))endif idx >= nargindone = true;endidx=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為奇數 endif isempty(threshold)threshold = 0.5 * max(h(:)); %設置默認值 end

3. 模型效果

3.1 Hough變換直線檢測(Matlab內建函數)

3.2 Radon變換直線檢測

3.3 合擊-分進直線檢測


總結

以上是生活随笔為你收集整理的直线检测——Radon变换/霍夫变换/基于快速傅里叶变换的直线检测的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。