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

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 人工智能 > 循环神经网络 >内容正文

循环神经网络

matlab霍夫变换代码,[转载]Matlab实现霍夫变换

發(fā)布時(shí)間:2024/3/26 循环神经网络 39 豆豆
生活随笔 收集整理的這篇文章主要介紹了 matlab霍夫变换代码,[转载]Matlab实现霍夫变换 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

本代碼提供了matlab下求取經(jīng)過(guò)霍夫變換的直線斜率,并將其聯(lián)合,代碼見(jiàn)下方,實(shí)驗(yàn)結(jié)果見(jiàn)文末。

%?入口圖像為?BW,出口圖像為f

%optimize?from?main_optimize,?merely?select?2?lines,?one?has?positive

%slope,the?other?has?negative?slope

clear?all,close?all

BW=imread('D:ImagesNEWimg4b9faef664e03.jpg');

figure,imshow(BW);

BW=rgb2gray(BW);

%thresh=[0.01,0.17];

thresh=[0.01,0.10];

sigma=2;%定義高斯參數(shù)

f?=?edge(double(BW),'canny',thresh,sigma);

figure,subplot(121);

imshow(f,[]);

title('canny?Edge?Detect?Result');

[H,?theta,?rho]=?hough(f,?0.1);%cos(theta)*x+sin(theta)*y=rho

%imshow(theta,rho,H,[],'notruesize'),axis?on,axis?normal

%xlabel('theta'),ylabel('rho');

[r,c]=houghpeaks(H,10);

hold?on

lines=houghlines(f,theta,rho,r,c);

subplot(122);

imshow(f,[]),title('Hough?Transform?Detect?Result'),hold?on

nlind=0;%new?line?index

st=1;

%%%%%%%%%求斜率%%%%%%%%%%%%

for?k=1:length(lines)

%xy=[lines(k).point1;lines(k).point2];

xielv(k)=(lines(k).point2(1)-lines(k).point1(1))/(lines(k).point2(2)-lines(k).point1(2)+0.0001)

end

%%%%%%%%%將相同斜率的直線連起來(lái)%%%%%%%%%%%%

k=1;

while(k<=length(lines))

if(k~=length(lines))

k=k+1;

end

while(abs(xielv(k)-xielv(k-1))<0.0001)

k=k+1;

if(k>length(lines))

break;

end

end

if(abs(xielv(k-1))<0.05||abs(xielv(k-1))>=10)%eliminate?horizontal?and?vertical?lines,防治水平線和樓房

st=k;

if(k~=length(lines))

continue;

end

end

if(st==length(lines)&&k==st)

if(abs(xielv(k))>0.05&&abs(xielv(k))<10)

nlind=nlind+1;

newlines(nlind)=lines(st);

newlines(nlind).point2=lines(k).point2;

newxy=[newlines(nlind).point1;newlines(nlind).point2];

plot(newxy(:,2),newxy(:,1),'LineWidth',4,'Color',[.6?1.0?.8]);

end

break;

end

%end=k-1,start=st;?draw?line

nlind=nlind+1;

newlines(nlind)=lines(st);

newlines(nlind).point2=lines(k-1).point2;

newxy=[newlines(nlind).point1;newlines(nlind).point2];

plot(newxy(:,2),newxy(:,1),'LineWidth',4,'Color',[.6?1.0?.8]);

st=k;

end

fprintf('%d?lines?are?detected?in?sum.n',nlind);

% 入口圖像為 BW,出口圖像為f

%optimize from main_optimize, merely select 2 lines, one has positive

%slope,the other has negative slope

clear all,close all

BW=imread('D:ImagesNEWimg4b9faef664e03.jpg');

figure,imshow(BW);

BW=rgb2gray(BW);

%thresh=[0.01,0.17];

thresh=[0.01,0.10];

sigma=2;%定義高斯參數(shù)

f = edge(double(BW),'canny',thresh,sigma);

figure,subplot(121);

imshow(f,[]);

title('canny Edge Detect Result');

[H, theta, rho]= hough(f, 0.1);%cos(theta)*x+sin(theta)*y=rho

%imshow(theta,rho,H,[],'notruesize'),axis on,axis normal

%xlabel('theta'),ylabel('rho');

[r,c]=houghpeaks(H,10);

hold on

lines=houghlines(f,theta,rho,r,c);

subplot(122);

imshow(f,[]),title('Hough Transform Detect Result'),hold on

nlind=0;%new line index

st=1;

%%%%%%%%%求斜率%%%%%%%%%%%%

for k=1:length(lines)

%xy=[lines(k).point1;lines(k).point2];

xielv(k)=(lines(k).point2(1)-lines(k).point1(1))/(lines(k).point2(2)-lines(k).point1(2)+0.0001)

end

%%%%%%%%%將相同斜率的直線連起來(lái)%%%%%%%%%%%%

k=1;

while(k<=length(lines))

if(k~=length(lines))

k=k+1;

end

while(abs(xielv(k)-xielv(k-1))<0.0001)

k=k+1;

if(k>length(lines))

break;

end

end

if(abs(xielv(k-1))<0.05||abs(xielv(k-1))>=10)%eliminate horizontal and vertical lines,防治水平線和樓房

st=k;

if(k~=length(lines))

continue;

end

end

if(st==length(lines)&&k==st)

if(abs(xielv(k))>0.05&&abs(xielv(k))<10)

nlind=nlind+1;

newlines(nlind)=lines(st);

newlines(nlind).point2=lines(k).point2;

newxy=[newlines(nlind).point1;newlines(nlind).point2];

plot(newxy(:,2),newxy(:,1),'LineWidth',4,'Color',[.6 1.0 .8]);

end

break;

end

%end=k-1,start=st; draw line

nlind=nlind+1;

newlines(nlind)=lines(st);

newlines(nlind).point2=lines(k-1).point2;

newxy=[newlines(nlind).point1;newlines(nlind).point2];

plot(newxy(:,2),newxy(:,1),'LineWidth',4,'Color',[.6 1.0 .8]);

st=k;

end

fprintf('%d lines are detected in sum.n',nlind);

實(shí)驗(yàn)結(jié)果:

原圖:

未優(yōu)化的霍夫變換:

優(yōu)化后:

************************************另一版本******************************

總結(jié)如下:

(1)

對(duì)圖片預(yù)處理,這里必須說(shuō)明的是,純種的Hough變換只適應(yīng)黑白圖片,換句話說(shuō),在使用它之前,你已經(jīng)提取出該圖片的邊緣了。

(2) 找到圖片中的“黑點(diǎn)”也就是要處理的邊緣,假設(shè)其在直角坐標(biāo)系的下標(biāo)為(x,

y),對(duì)其進(jìn)行坐標(biāo)變換ρ=x*cosθ+y*sinθ,其中0

(3) 判斷(ρj, θj)與哪個(gè)數(shù)組元素對(duì)應(yīng),并讓該數(shù)組元素加1。

(4) 比較數(shù)組元素值的大小,最大值對(duì)應(yīng)的(ρj, θj)就是這些共線點(diǎn)對(duì)應(yīng)的直線方程的參數(shù)。共線方程為

ρj = xcosθj + ysinθj

在matlab中,使用hough、houghpeaks和houghlines可以簡(jiǎn)單的檢測(cè)到直線。例子如下。

I = imread('test12.bmp');?%讀入示例圖片%

Imshow(I);?%顯示示例圖片%

Img =

edge(I,'prewitt');?%利用prewitt算子提取邊緣%

Imshow(Img);?%顯示提取邊緣的圖片%

[H, T, R] =

hough(Img);?%hough變換%

imshow(sqrt(H),

[]);?%hough變換的結(jié)果%

P = houghpeaks(H, 15, 'threshold', ceil(0.3*max(H(:))));

%尋找最大點(diǎn)%

lines = houghlines(Img, T, R, P,'FillGap',10,'MinLength',20

);

%返回找到的直線%

figure, imshow(I), hold on

max_len = 0;

for k = 1:length(lines)

xy = [lines(k).point1; lines(k).point2];

plot(xy(:,1),xy(:,2),'LineWidth',2,'Color','green');

plot(xy(1,1),xy(1,2),'x','LineWidth',2,'Color','yellow');

plot(xy(2,1),xy(2,2),'x','LineWidth',2,'Color','red');

end

總結(jié)

以上是生活随笔為你收集整理的matlab霍夫变换代码,[转载]Matlab实现霍夫变换的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。