Canny邊緣檢測原理與C 實現 2 實現部分

2021-08-03 03:50:59 字數 3284 閱讀 5165

本**實現完全脫離opencv,如果需要顯示,可以呼叫,以便觀察檢測效果。

首先,由於多次用到影象,所以定義影象資料結構,

data.h

#ifndef data_

#define data_

#include #include#include"memory.h"

using namespace std;

typedef unsigned char pixuc1;

typedef float pixfc1;

//單通道 型別影象

templateclass imgch1;

templateimgch1::imgch1(unsigned int height_,unsigned int width_,unsigned char initvalue):rows(height_),cols(width_)

templateimgch1::imgch1(unsigned int height_,unsigned int width_,pixvaluetype* dataptr_):rows(height_),cols(width_)

#endif

mycanny.h

#include "iostream"  

#include "math.h"

#include"data.h"

using namespace std;

class mycanny

;

mycanny.cpp

#include"mycanny.h"

//為了顯示,可以把類裡的show函式注釋掉,就可以在實現部分不依賴於opencv

#include"opencv.hpp"

using namespace cv;

void mycanny::operator()(const imgch1& srcimg_,imgch1& cannyimg_ ,int lowthread,int highthread,int size)

} }

} } //******************sobel運算元計算x、y方向梯度和梯度方向角********************

void mycanny::sobelgraddirection(const imgch1& imagesource, imgch1&sobelampxy, char *pointdrection)

imgch1imagesobelx(height,width,pixfc1(0));

imgch1imagesobely(height,width,pixfc1(0));

pixuc1 *p=imagesource.dataptr;

pixfc1 *px=imagesobelx.dataptr;

pixfc1 *py=imagesobely.dataptr;

int k=0;

for(int row=1;row<(imagesource.rows-1);row++)

float graddrection=atan2(grady,gradx)*57.3;//弧度轉換為度

if(graddrection<=-67.5&&graddrection<=-112.5||graddrection>=67.5&&graddrection<=-112.5)

pointdrection[k]=90;

else if(graddrection>=22.5&&graddrection<67.5||graddrection>=-157.5&&graddrection<-112.5)

pointdrection[k]=45;

else if(graddrection>=-67.5&&graddrection<22.5||graddrection>=112.5&&graddrection<157.5)

pointdrection[k]=-45;

else

pointdrection[k]=0;

k++;

}

}

imgch1imagesobelxy(height,width,pixfc1(0));

for(int row=0;row&imageinput, imgch1&imageoutput, char *pointdrection)

else if( imageinput.dataptr[(y-1)*width+x]>=lowthreshold&&imageinput.dataptr[(y-1)*width+x]!=255)

else if(imageinput.dataptr[(y-1)*width+x+1]>=lowthreshold&&imageinput.dataptr[(y-1)*width+x+1]!=255)

else if( imageinput.dataptr[y*width+x-1]>=lowthreshold&&imageinput.dataptr[y*width+x-1]!=255 )

else if( imageinput.dataptr[y*width+x+1]>=lowthreshold&&imageinput.dataptr[y*width+x+1]!=255 )

else if( imageinput.dataptr[(y+1)*width+x-1]>=lowthreshold&&imageinput.dataptr[(y+1)*width+x-1]!=255 )

else if( imageinput.dataptr[(y+1)*width+x]>=lowthreshold&& imageinput.dataptr[(y+1)*width+x]!=255 )

else if(imageinput.dataptr[(y+1)*width+x+1]>=lowthreshold&&imageinput.dataptr[(y+1)*width+x+1]!=255) }

void mycanny::touchar(const imgch1&floatimage,imgch1&imageuchar)

void mycanny::show(const imgch1&imageinput)

imshow(" ",show);

waitkey(0);

}

測試

#include"opencv.hpp"

using namespace cv;

#include"mycanny.h"

using namespace std;

int main()

Canny邊緣檢測原理及C 程式實現

canny邊緣檢測是被公認的檢測效果最好的邊緣檢測方法,是由john f.canny於1986年提出,演算法目標是找出乙個最優的邊緣檢測的方法,所謂最優即 1.好的檢測 演算法能夠盡可能的標識出影象的邊緣 2.好的定位 標識出的邊緣要盡可能的與實際邊緣相接近 3.最小響應 影象中的邊緣只能標識一次,...

Canny邊緣檢測原理及C 程式實現

原文 canny邊緣檢測原理及c 程式實現 canny邊緣檢測是被公認的檢測效果最好的邊緣檢測方法,是由john f.canny於1986年提出,演算法目標是找出一 個最優的邊緣檢測的方法,所謂最優即 1.好的檢測 演算法能夠盡可能的標識出影象的邊緣 2.好的定位 標識出的 邊緣要盡可能的與實際邊緣...

Canny邊緣檢測原理及C 程式實現

canny邊緣檢測是被公認的檢測效果最好的邊緣檢測方法,是由john f.canny於1986年提出,演算法目標是找出乙個最優的邊緣檢測的方法,所謂最優即 1.好的檢測 演算法能夠盡可能的標識出影象的邊緣 2.好的定位 標識出的邊緣要盡可能的與實際邊緣相接近 3.最小響應 影象中的邊緣只能標識一次,...