探測邊界演算法
1. 如何利用opencv實現彩色圖像邊緣檢測演算法
在opencv中顯示邊緣檢測很簡單,只需調用一個cvCanny函數,其使用的是Canny演算法來實現對圖像的邊緣檢測.
函數原型為:
void cvCanny( const CvArr* image,CvArr* edges,double threshold1,double threshold2, int aperture_size=3 );
第一個參數為待檢測的圖像,注意一點,其必須是灰度圖.
第二個參數為輸出的邊緣圖,其也是一個灰度圖.
後三個參數與Canny演算法直接相關,threshold1和threshold2 當中的小閾值用來控制邊緣連接,大的閾值用來控制強邊緣的初始分割,aperture_size運算元內核大小,可以去看看Canny演算法.
從彩色圖到灰度圖需要使用到cvCvtColor函數,其接受三個參數,第一為輸入,第二為輸出,第三個為轉換的標識,我們這邊是RGB到GRAY,使用的是CV_RGB2GRAY.
參考demo代碼如下:
#include <iostream>
#include <string>
#include <sstream>
#include <opencv/cv.h>
#include <opencv/highgui.h>
using namespace std;
int String2int(const string& str_)
{
int _nre = 0;
stringstream _ss;
_ss << str_;
_ss >> _nre;
return _nre;
}
void DoCanny(const string& strFileName_)
{
//原彩色圖片
IplImage* _pIplImageIn = cvLoadImage(strFileName_.data());
if (_pIplImageIn == NULL)
{
return;
}
//彩色圖片轉換成灰度圖放置的圖片
IplImage* _pIplImageCanny = cvCreateImage(cvGetSize(_pIplImageIn), _pIplImageIn->depth, 1);
cvCvtColor(_pIplImageIn, _pIplImageCanny, CV_RGB2GRAY);//CV_RGB2GRAY將rgb圖轉成灰度圖
//只有邊緣路徑的圖片
IplImage* _pIplImageOut = cvCreateImage(cvGetSize(_pIplImageIn), IPL_DEPTH_8U, 1);
//邊緣檢測只能作用於灰度圖
if (_pIplImageCanny->nChannels != 1)
{
return;
}
//邊緣檢測操作
cvCanny(_pIplImageCanny, _pIplImageOut, 1, 110, 3);
cvNamedWindow("Src");
cvShowImage("Src", _pIplImageIn);
cvNamedWindow("Canny");
cvShowImage("Canny", _pIplImageOut);
cvWaitKey(0);
cvReleaseImage(&_pIplImageIn);
cvReleaseImage(&_pIplImageCanny);
cvReleaseImage(&_pIplImageOut);
cvDestroyWindow("Src");
cvDestroyWindow("Canny");
}
int main(int argc, char* argv[])
{
if (argc < 2)
{
cout << "You should give the filename of picture!" << endl;
return -1;
}
DoCanny(argv[1]);
return 0;
}
2. 基於matlab的邊緣檢測的robert運算元的演算法怎麼寫
matlab本身有庫函數的。直接調用啊
VC代碼:
void BianYuanJianCeDib::Robert()
{
LPBYTE p_data; //原圖數據區指針
int wide,height; //原圖長、寬
int i,j; //循環變數
int pixel[4]; //Robert運算元
p_data=this->GetData ();
wide=this->GetWidth ();
height=this->GetHeight ();
LPBYTE temp=new BYTE[wide*height]; //新圖像緩沖區
//設定新圖像初值為255
memset(temp,255, wide*height);
//由於使用2*2的模板,為防止越界,所以不處理最下邊和最右邊的兩列像素
for(j=0;j<height-1;j++)
for(i=0;i<wide-1;i++)
{
//生成Robert運算元
pixel[0]=p_data[j*wide+i];
pixel[1]=p_data[j*wide+i+1];
pixel[2]=p_data[(j+1)*wide+i];
pixel[3]=p_data[(j+1)*wide+i+1];
//處理當前像素
temp[j*wide+i]=(int)sqrt((pixel[0]-pixel[3])*(pixel[0]-pixel[3])
+(pixel[1]-pixel[2])*(pixel[1]-pixel[2]));
}
//將緩沖區中的數據復制到原圖數據區
memcpy(p_data, temp,wide*height);
//刪除緩沖區
delete temp;
}
3. canny演算法的最優邊緣准則
Canny 的目標是找到一個最優的邊緣檢測演算法,最優邊緣檢測的含義是:
(1)最優檢測:演算法能夠盡可能多地標識出圖像中的實際邊緣,漏檢真實邊緣的概率和誤檢非邊緣的概率都盡可能小;
(2)最優定位準則:檢測到的邊緣點的位置距離實際邊緣點的位置最近,或者是由於雜訊影響引起檢測出的邊緣偏離物體的真實邊緣的程度最小;
(3)檢測點與邊緣點一一對應:運算元檢測的邊緣點與實際邊緣點應該是一一對應。
為了滿足這些要求 Canny 使用了變分法(calculus of variations),這是一種尋找優化特定功能的函數的方法。最優檢測使用四個指數函數項表示,但是它非常近似於高斯函數的一階導數。
4. Matlab邊緣檢測問題
用mesh語句似乎可以,具體也不了解你的情況,感覺怪怪的,發一段我以前些的程序,用羅伯特運算元寫的,把運算元一改就是sobel了。兩種邊緣檢測近似演算法奉上:
clc
close all
clear all
%%%生成高斯平滑濾波模板%%%
%%%%%%%%%%%%%%%%%%%%%%%%%
hg=zeros(3,3); %設定高斯平滑濾波模板的大小為3*3
delta=0.5;
for x=1:1:3
for y=1:1:3
u=x-2;
v=y-2;
hg(x,y)=exp(-(u^2+v^2)/(2*pi*delta^2));
end
end
h=hg/sum(hg(:));
%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%讀入圖像%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%
f = imread('1111.tif'); % 讀入圖像文件
f=rgb2gray(im2double(f));
imshow(f)
title('原始圖像');
[m,n]=size(f);
ftemp=zeros(m,n);
rowhigh=m-1;
colhigh=n-1;
%%%高斯濾波%%%
for x=2:1:rowhigh-1
for y=2:1:colhigh-1
mod=[f(x-1,y-1) f(x-1,y) f(x-1,y+1); f(x,y-1) f(x,y) f(x,y+1);f(x+1,y-1) f(x+1,y) f(x+1,y+1)];
A=h.*mod;
ftemp(x,y)=sum(A(:));
end
end
f=ftemp
figure,imshow(f)
title('通過高斯濾波器後的圖像');
% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% %%%利用roberts運算元進行邊緣檢測%%%
% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
sx=[-1 -2 -1;0 0 0;1 2 1];
sy=[-1 0 1;-2 0 2;-1 0 1];
for x=2:1:rowhigh-1
for y=2:1:colhigh-1
mod=[f(x-1,y-1) f(x-1,y) f(x-1,y+1); f(x,y-1) f(x,y) f(x,y+1);f(x+1,y-1) f(x+1,y) f(x+1,y+1)];
fsx=sx.*mod;
fsy=sy.*mod;
ftemp(x,y)=sqrt((sum(fsx(:)))^2+(sum(fsy(:)))^2);
end
end
fr=im2uint8(ftemp);
figure,imshow(fr)
title('用roberts運算元邊緣檢測的原始圖像');
%%%域值分割%%%
TH1=60; %設定閾值
for x=2:1:rowhigh-1
for y=2:1:colhigh-1
if (fr(x,y)>=TH1)&((fr(x,y-1) <= fr(x,y)) & (fr(x,y) > fr(x,y+1)) )
fr(x,y)=200;
elseif(fr(x,y)>=TH1)&( (fr(x-1,y) <=fr(x,y)) & (fr(x,y) >fr(x+1,y)))
fr(x,y)=200;
else fr(x,y)=50;
end
end
end
figure,imshow(fr)
title('用roberts運算元邊緣檢測並細化後的圖像');
%%%%%%%%%%%%%%%%%%%%%%%%%%
利用第一種近似演算法進行邊緣檢測%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%3*3的sobel運算元%%%%%%%%
sx=[-1 -2 -1;0 0 0;1 2 1];
sy=[-1 0 1;-2 0 2;-1 0 1];
%sx=[0 1 2;-1 0 1;-2 -1 0];
%sy=[-2 -1 0;-1 0 1;0 1 2];
for x=2:1:rowhigh-1
for y=2:1:colhigh-1
mod=[f(x-1,y-1) f(x-1,y) f(x-1,y+1); f(x,y-1) f(x,y) f(x,y+1);f(x+1,y-1) f(x+1,y) f(x+1,y+1)];
fsx=sx.*mod;
fsy=sy.*mod;
ftemp(x,y)=abs(sum(fsx(:)))+abs(sum(fsy(:)));
end
end
fs=im2uint8(ftemp);
figure,imshow(fs)
title('用第一種近似演算法進行邊緣檢測的原始圖像');
%%%域值分割%%%
TH2=200; %設定閾值
for x=2:1:rowhigh-1
for y=2:1:colhigh-1
if (fs(x,y)>=TH2)&((fs(x,y-1) <= fs(x,y)) & (fs(x,y) > fs(x,y+1)) )
fs(x,y)=200;
elseif(fs(x,y)>=TH2)&( (fs(x-1,y) <=fs(x,y)) & (fs(x,y) >fs(x+1,y)))
fs(x,y)=200;
else fs(x,y)=50;
end
end
end
figure,imshow(fs)
title('採用第一種近似演算法進行邊緣檢測後的圖像');
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%利用第二種近似演算法進行邊緣檢測%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%3*3的sobel運算元%%%%%%%%
sx=[-1 -2 -1;0 0 0;1 2 1];
sy=[-1 0 1;-2 0 2;-1 0 1];
%sx=[0 1 2;-1 0 1;-2 -1 0];
%sy=[-2 -1 0;-1 0 1;0 1 2];
for x=2:1:rowhigh-1
for y=2:1:colhigh-1
mod=[f(x-1,y-1) f(x-1,y) f(x-1,y+1); f(x,y-1) f(x,y) f(x,y+1);f(x+1,y-1) f(x+1,y) f(x+1,y+1)];
fsx=sx.*mod;
fsy=sy.*mod;
ftemp(x,y)=max(abs(sum(fsx(:))),abs(sum(fsy(:))));
end
end
fs=im2uint8(ftemp);
figure,imshow(fs)
title('用第二種近似演算法進行邊緣檢測的原始圖像');
%%%域值分割%%%
TH2=200; %設定閾值
for x=2:1:rowhigh-1
for y=2:1:colhigh-1
if (fs(x,y)>=TH2)&((fs(x,y-1) <= fs(x,y)) & (fs(x,y) > fs(x,y+1)) )
fs(x,y)=200;
elseif(fs(x,y)>=TH2)&( (fs(x-1,y) <=fs(x,y)) & (fs(x,y) >fs(x+1,y)))
fs(x,y)=200;
else fs(x,y)=50;
end
end
end
figure,imshow(fs)
title('採用第二種近似演算法進行邊緣檢測後的圖像');