#include "EDLib.h"
EdgeMap *DetectEdgesByED(unsigned char *srcImg, int width, int height, GradientOperator op, int GRADIENT_THRESH, int ANCHOR_THRESH, double smoothingSigma)
{
GaussianSmooth(srcImg, smoothingSigma, width, height);
unsigned char* EdgeAreas;
unsigned char* EdgeDirections;
EdgeAreas = (unsigned char *)malloc(sizeof(unsigned char)*width*height);
EdgeDirections = (unsigned char *)malloc(sizeof(unsigned char)*width*height);
memset(EdgeAreas,0x00,sizeof(unsigned char)*width*height);
memset(EdgeDirections,0x00,sizeof(unsigned char)*width*height);
Edge_Areas_Directions(srcImg, width, height, op, GRADIENT_THRESH, EdgeAreas, EdgeDirections);
unsigned char* AnchorMap;
AnchorMap = (unsigned char *)malloc(sizeof(unsigned char)*width*height);
memset(AnchorMap,0x00,sizeof(unsigned char)*width*height);
vector<Pixel> anchor;
FindAnchor(EdgeAreas, EdgeDirections, width, height, ANCHOR_THRESH, AnchorMap, anchor);
EdgeMap *edgemap;
edgemap = (EdgeMap *)malloc(sizeof(EdgeMap)*width*height);
memset(edgemap,0x00,sizeof(EdgeMap)*width*height);
LinkAnchor(EdgeAreas, EdgeDirections, width, height, GRADIENT_THRESH, AnchorMap, edgemap);
edgemap->width = width;
edgemap->height = height;
return edgemap;
free (EdgeAreas);
free (EdgeDirections);
free (AnchorMap);
free (edgemap);
}
void GaussianSmooth(unsigned char* srcImg, double sigma, int width, int height)
{
int ksize = 5;
sigma = sigma > 0 ? sigma : -sigma;
//计算一维高斯核
double *kernel = new double[ksize];
double scale = -0.5/(sigma*sigma);
const double PI = 3.141592653;
double cons = 1/sqrt(-scale / PI);
double sum = 0;
int kcenter = ksize/2;
int i = 0, j = 0;
for(i = 0; i < ksize; i++)
{
int x = i - kcenter;
*(kernel+i) = cons * exp(x * x * scale);//一维高斯函数
sum += *(kernel+i);
}
//归一化,确保高斯权值在[0,1]之间
for(i = 0; i < ksize; i++)
{
*(kernel+i) /= sum;
}
unsigned char* temp;
unsigned char* temp1;
temp = (unsigned char *)malloc(sizeof(unsigned char)*width*height);
temp1 = (unsigned char *)malloc(sizeof(unsigned char)*width*height);
memset(temp,0x00,sizeof(unsigned char)*width*height);
memset(temp1,0x00,sizeof(unsigned char)*width*height);
//x方向一维高斯模糊
for(int y = 0; y < height; y++)
{
for(int x = 0; x < width; x++)
{
double mul = 0;
sum = 0;
for(i = -kcenter; i <= kcenter; i++)
{
if((x+i) >= 0 && (x+i) < width)
{
mul +=srcImg[y*width + (x+i)]*(*(kernel+kcenter+i));
sum += (*(kernel+kcenter+i));
}
}
temp1[y*width + x] = mul/sum;
}
}
//y方向一维高斯模糊
for(int x = 0; x < width; x++)
{
for(int y = 0; y < height; y++)
{
double mul = 0;
sum = 0;
for(i = -kcenter; i <= kcenter; i++)
{
if((y+i) >= 0 && (y+i) < height)
{
mul += temp1[(y+i)*width + x]*(*(kernel+kcenter+i));
sum += (*(kernel+kcenter+i));
}
}
temp[y*width + x] = mul/sum;
}
}
memcpy(srcImg,temp,height*width*sizeof(unsigned char));
delete[] kernel;
free (temp);
free (temp1);
}
void Edge_Areas_Directions(unsigned char* srcImg, int width, int height, GradientOperator op, int GRADIENT_THRESH, unsigned char* EdgeAreas, unsigned char* EdgeDirections)
{
IplImage* Edge_Areas = cvCreateImage(cvSize(width,height),IPL_DEPTH_8U,1);
float g_x, g_y, grad;
unsigned char a00, a01, a02;
unsigned char a10, a11, a12;
unsigned char a20, a21, a22;
cvZero(Edge_Areas);
for (int i=1; i<height; i++){
for (int j=1; j<width; j++) {
a00 = srcImg[(i-1)*width + (j-1)];
a01 = srcImg[(i-1)*width + j];
a02 = srcImg[(i-1)*width + (j+1)];
a10 = srcImg[i*width + (j-1)];
a11 = srcImg[i*width + j];
a12 = srcImg[i*width + (j+1)];
a20 = srcImg[(i+1)*width + (j-1)];
a21 = srcImg[(i+1)*width + j];
a22 = srcImg[(i+1)*width + (j+1)];
if(op == PREWITT_OPERATOR) {
//Prewitt算子求梯度
g_y= a20 * (1) + a21 * (1) + a22 * (1)+ (a00 * (-1) + a01 * (-1) + a02 * (-1));
g_x= a02 * (1) + a12 * (1) + a22 * (1)+ a00 * (-1) + a10 * (-1) + a20 * (-1);
grad = sqrt(g_x*g_x + g_y*g_y);
}
else if(op == SOBEL_OPERATOR){
//Sobel算子求梯度
g_y= a20 * (1) + a21 * (2) + a22 * (1)+ (a00 * (-1) + a01 * (-2) + a02 * (-1));
g_x= a02 * (1) + a12 * (2) + a22 * (1)+ a00 * (-1) + a10 * (-2) + a20 * (-1);
grad = sqrt(g_x*g_x + g_y*g_y);
}
else{
//Scharr算子求梯度
g_y= a20 * (3) + a21 * (10) + a22 * (3)+ (a00 * (-3) + a01 * (-10) + a02 * (-3));
g_x= a02 * (3) + a12 * (10) + a22 * (3)+ a00 * (-3) + a10 * (-10) + a20 * (-3);
grad = sqrt(g_x*g_x + g_y*g_y);
}
if (grad > GRADIENT_THRESH) {
EdgeAreas[i*width + j] = grad;
*(Edge_Areas->imageData + i*Edge_Areas->widthStep + j) = EdgeAreas[i*width + j];
}
if (abs(g_x) >= abs(g_y)) {
EdgeDirections[i*width + j] = 1;
}
}
}
cvShowImage("EdgeAreas",Edge_Areas);
cvReleaseImage(&Edge_Areas);
}
void FindAnchor(unsigned char* EdgeAreas, unsigned char* EdgeDirections, int width, int height, int ANCHOR_THRESH, unsigned char* AnchorMap, vector<Pixel>& anchor)
{
unsigned char a00, a01, a02;
unsigned char a10, a11, a12;
unsigned char a20, a21, a22;
IplImage* Anchor_Map = cvCreateImage(cvSize(width,height),IPL_DEPTH_8U,1);
cvZero(Anchor_Map);
int scan_step = 2;
Pixel pixel;
for (int i=1; i<height; i+=scan_step) {
for (int j = 1; j<width; j ++) {
a00 = EdgeAreas[(i-1)*width + (j-1)];
a01 = EdgeAreas[(i-1)*width + j];
a02 = EdgeAreas[(i-1)*width + (j+1)];
a10 = EdgeAreas[i*width + (j-1)];
a11 = EdgeAreas[i*width + j];
a12 = EdgeAreas[i*width + (j+1)];
a20 = EdgeAreas[(i+1)*width + (j-1)];
a21 = EdgeAreas[(i+1)*width + j];
a22 = EdgeAreas[(i+1)*width + (j+1)];
unsigned char a = EdgeDirections[i*width + j];
if ((int)a == 1) { //垂直边缘
if ((a11-a10) > ANCHOR_THRESH && (a11-a12) > ANCHOR_THRESH) {
AnchorMap[i*width + j] = 255;
pixel.r = i;
pixel.c = j;
pixel.grad = EdgeAreas[i*width + j];
anchor.push_back(pixel);
*(Anchor_Map->imageData + i*Anchor_Map->widthStep + j) = AnchorMap[i*width + j];
}
}
else { //水平边缘
if ((a11-a01) > ANCHOR_THRESH && (a11-a21) > ANCHOR_THRESH) {
AnchorMap[i*width + j] = 255;
pixel.r = i;
pixel.c = j;
pixel.grad = EdgeAreas[i*width + j];
anchor.push_back(pixel);
*(Anchor_Map->imageData + i*Anchor_Map->widthStep + j) = AnchorMap[i*width + j];
}
}
}
}
cvShowImage("AnchorMap",Anchor_Map);
cvReleaseImage(&Anchor_Map);
}
void LinkAnchor(unsigned char* EdgeAreas, unsigned char* EdgeDirections, int width, int height,int GRADIENT_THRESH, unsigned char* AnchorMap, EdgeMap *edgemap)
{
IplImage* Edge_Map = cvCreateImage(cvSize(width,height),IPL_DEPTH_8U,1);
cvZero(Edge_Map);
int x, y;
bool a, b, c;
Pixel* pixels;
EdgeSegment* segments;
unsigned char *edgeImg;
pixels = (Pixel *)malloc(sizeof(Pixel)*width*height);
segments = (EdgeSegment *)malloc(sizeof(EdgeSegment)*width*height);
edgeImg = (unsigned char *)malloc(sizeof(unsigned char)*width*height);
memset(pixels,0x00,sizeof(Pixel)*width*height);
memset(segments,0x00,sizeof(EdgeSegment)*width*height);
memset(edgeImg,0x00,sizeof(unsigned char)*width*height);
for(int y=0; y<height; y++) {
for(int x=0; x<width; x++) {
while(EdgeAreas[y*width + x] > GRADIENT_THRESH && AnchorMap[y*width + x] == 255 && edgeImg[y*width + x] != 255.0)
{
if(EdgeDirections[y*width + x] == 0)
goto horizontal;
if(EdgeDirections[y*width + x] == 1)
goto vertical;
horizontal:
edgeImg[y*width + x] = 255;
*(Edge_Map->imageData + y*Edge_Map->widthStep + x) = edgeImg[y*width + x];
//水平向左连接
if (EdgeAre
EDPF: A Realtime Parameter-Free Edge Segment Detector 实现
需积分: 50 39 浏览量
2016-07-11
19:46:52
上传
评论
收藏 219KB RAR 举报
lichade124080
- 粉丝: 2
- 资源: 3
最新资源
- 最入门的爬虫代码 python.docx
- 爬虫零基础入门-爬取天气预报.pdf
- 最通俗易懂的 MongoDB 非结构化文档存储数据库教程.zip
- 以mongodb为数据库的订单物流小项目.zip
- 腾讯云-mongodb数据库, 项目部署.zip
- 腾讯 APIJSON 的 MongoDB 数据库插件.zip
- 理解非关系型数据库和关系型数据库的区别.zip
- 操作简单的Mongodb网页web管理工具,基于Spring Boot2.0支持mongodb集群.zip
- tms-mongodb-web,提供访问mongodb数据的REST API和可灵活扩展的mongodb web 客户端.zip
- SpringBoot整合mongodb学习MongoTemplate和MongoRepository两种方式CRUD使用.zip
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈