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

歡迎訪問 生活随笔!

生活随笔

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

编程问答

随机样本一致抽样

發布時間:2025/4/16 编程问答 16 豆豆
生活随笔 收集整理的這篇文章主要介紹了 随机样本一致抽样 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
<span style="font-size:18px;">function testRansac()% real model coef k = .5; b = 10; ptNum = 200; outlrRatio = .4; %非模型店的個數比率 inlrStd = 5; %距離模型點的距離 pts = genRansacTestPoints(ptNum,outlrRatio,inlrStd,[k b]);%%產生隨機點 線性點和噪聲點 figure,plot(pts(1,:),pts(2,:),'.'),hold on %% X = -ptNum/2:ptNum/2; plot(X,k*X+b,'k')err0 = sqrError(k,b,pts(:,1:ptNum*(1-outlrRatio))) %計算總的誤差 %% % RANSAC 用隨機一致抽樣的方法 iterNum = 300; thDist = 2; %閾值距離 thInlrRatio = .1; %模型點的比率 [t,r] = ransac(pts,iterNum,thDist,thInlrRatio); k1 = -tan(t); b1 = r/cos(t); plot(X,k1*X+b1,'r') err1 = sqrError(k1,b1,pts(:,1:ptNum*(1-outlrRatio)))% least square fitting coef2 = polyfit(pts(1,:),pts(2,:),1); k2 = coef2(1); b2 = coef2(2); plot(X,k2*X+b2,'g') err2 = sqrError(k2,b2,pts(:,1:ptNum*(1-outlrRatio)))endfunction err = sqrError(k,b,pts) % Calculate the square error of the fittheta = atan(-k); n = [cos(theta),-sin(theta)]; pt1 = [0;b]; err = sqrt(sum((n*(pts-repmat(pt1,1,size(pts,2)))).^2));end</span>

<span style="font-size:18px;">function [ pts ] = genRansacTestPoints( ptNum,outlrRatio,inlrStd,inlrCoef ) %GENRANSACTESTPOINTS Generate the points used by RANSAC function % PTS = GENRANSACTESTPOINTS(PTNUM,OUTLRRATIO,INLRSTD,INLRCOEF) PTS is % 2*PTNUM, including PTNUM points, among which ROUND(OUTLRRATIO*PTNUM) % are outliers, others are inliers. % The inliers are around the line: y = INLRCOEF(1)*x + INLRCOEF(2), % INLRSTD is the standard deviation of, the dist between inliers and the % line. The outliers outlrNum = round(outlrRatio*ptNum);%產生的非模型點的個數 inlrNum = ptNum-outlrNum; %模型點的個數k = inlrCoef(1); b = inlrCoef(2); %模型直線的斜率和截距 X = (rand(1,inlrNum)-.5)*ptNum; % X is in [-ptNum/2,ptNum/2] x的取值范圍 在總點的范圍內 Y = k*X+b; %直線上模型點的y值% add noise for inliers dist = randn(1,inlrNum)*inlrStd; %產生在點總數的正態隨機分布的數 theta = atan(k); %直線的斜率 X = X+dist*(-sin(theta)); %接近模型的模型點的產生 Y = Y+dist*cos(theta); %x和y方向上的增量 inlrs = [X;Y];outlrs = (rand(2,outlrNum)-.5)*ptNum;%費模型點的產生 % outlrs = (rand(2,outlrNum)-[ones(1,outlrNum)*.5;ones(1,outlrNum)*.1])*ptNum; pts = [inlrs,outlrs];end </span> 隨機一致抽樣的過程
<span style="font-size:18px;">function [ theta,rho ] = ransac( pts,iterNum,thDist,thInlrRatio ) %RANSAC Use RANdom SAmple Consensus to fit a line % RESCOEF = RANSAC(PTS,ITERNUM,THDIST,THINLRRATIO) PTS is 2*n matrix including % n points, ITERNUM is the number of iteration, THDIST is the inlier % distance threshold and ROUND(THINLRRATIO*SIZE(PTS,2)) is the inlier number threshold. The final % fitted line is RHO = sin(THETA)*x+cos(THETA)*y. % Yan Ke @ THUEE, xjed09@gmail.comsampleNum = 2;%采樣個數 ptNum = size(pts,2);%點的個數 thInlr = round(thInlrRatio*ptNum); %要求的模型點的個數 inlrNum = zeros(1,iterNum);%存儲每次迭代時模型點的個數 theta1 = zeros(1,iterNum);%每次迭代時計算出的直線角度 rho1 = zeros(1,iterNum); %每次迭代計算出的直線模型的截距for p = 1:iterNum %進入迭代% 1. fit using 2 random pointssampleIdx = randIndex(ptNum,sampleNum);%隨機選擇兩個點ptSample = pts(:,sampleIdx);d = ptSample(:,2)-ptSample(:,1); %兩個點的向量d = d/norm(d); % direction vector of the line 直線的單位向量% 2. count the inliers, if more than thInlr, refit; else iteraten = [-d(2),d(1)]; % unit normal vector of the linedist1 = n*(pts-repmat(ptSample(:,1),1,ptNum));%點到直線的距離 單位向量與計算點的向量內積inlier1 = find(abs(dist1) < thDist);%找到不符合模型的點的距離的地址inlrNum(p) = length(inlier1);%查看符合模型點的個數if length(inlier1) < thInlr, continue; endev = princomp(pts(:,inlier1)'); %PCA變換d1 = ev(:,1); %返回的系數theta1(p) = -atan2(d1(2),d1(1)); % save the coefsrho1(p) = [-d1(2),d1(1)]*mean(pts(:,inlier1),2); % end% 3. choose the coef with the most inliers [~,idx] = max(inlrNum);%找到模型點最多的那個點集的參數 theta = theta1(idx); rho = rho1(idx);end</span>


代碼的下載:

點擊打開鏈接


總結

以上是生活随笔為你收集整理的随机样本一致抽样的全部內容,希望文章能夠幫你解決所遇到的問題。

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