Marr_Hildreth.rar

  • PUDN用户
    了解作者
  • Visual C++
    开发工具
  • 5.1MB
    文件大小
  • rar
    文件格式
  • 0
    收藏次数
  • 1 积分
    下载积分
  • 3
    下载次数
  • 2014-09-25 15:31
    上传日期
这是Marr_Hildreth边缘检测,虽然实现有点问题,但是所有模块都是好的
Marr_Hildreth.rar
内容介绍
#include "lib.h" float norm(float x, float y) { return (float)sqrt((double)(x*x + y*y)); } float distance(float a, float b, float c, float d) { return norm((a - c), (b - d)); } void marr(float s, IMAGE im) { int width; float **smx; int i, j, k, n; float **lgau, z; width = 3.35*s + 0.33; n = width * 2 + 1; printf("smoothing with a gaussian of size %dx%d\n", n, n); lgau = f2d(n, n); for (i = 0; i < n; i++) { for (j = 0; j < n; j++) { lgau[i][j] = LoG(distance((float)i, (float)j, (float)width, (float)width), s); } } smx = f2d(im->info->nr, im->info->nc); printf("convolution with LOG:\n"); convolution(im, lgau, n, n, smx, im->info->nr, im->info->nc); printf("Zero crossings:\n"); zero_cross(smx, im); for (i = 0; i < im->info->nr; i++) { for (j = 0; j <= width; j++) { im->data[i][j] = 0; } for (j = im->info->nc - width - 1; j < im->info->nc; j++) { im->data[i][j] = 0; } } for (j = 0; j < im->info->nc; j++) { for (i = 0; i <= width; i++) { im->data[i][j] = 0; } for (i = im->info->nr - width - 1; i < im->info->nr; i++) { im->data[i][j] = 0; } } free(smx[0]); free(smx); free(lgau[0]); free(lgau); } float gauss(float x, float sigma) { return (float)exp((double)((-x*x) / (2 * sigma*sigma))); } float meanGauss(float x, float sigma) { float z; z = (gauss(x, sigma) + gauss(x + 0.5, sigma) + gauss(x - 0.5, sigma)) / 3.0; z = z / (PI*2.0*sigma*sigma); return z; } float LoG(float x, float sigma) { float x1; x1 = gauss(x, sigma); return (x*x - 2 * sigma*sigma) / (sigma*sigma*sigma*sigma)*x1; } void convolution(IMAGE im, float **mask, int nr, int nc, float **res, int NR, int NC) { int i, j, ii, jj, k, kk, n, m; float x, y; k = nr / 2; kk = nc / 2; for (i = 0; i < NR; i++) { for (j = 0; j < NC; j++) { x = 0.0; for (ii = 0; ii < nr; ii++) { n = i - k + ii; if (n < 0 || n >= NR) { continue; } for (jj = 0; jj < nc; jj++) { m = j - kk + jj; if (m < 0 || m >= NC) { continue; } x += mask[ii][jj] * (float)(im->data[n][m]); } } res[i][j] = x; } } } void zero_cross(float **lapim, IMAGE im) { int i, j, k, m, n, dx, dy; float x, y, z; int xi, xj, yi, yj, count = 0; IMAGE deriv; for (i = 1; i < im->info->nr - 1; i++) { for (j = 1; j < im->info->nc - 1; j++) { im->data[i][j] = 0; if (lapim[i - 1][j] * lapim[i + 1][j] < 0) { im->data[i][j] = 255; continue; } if (lapim[i][j - 1] * lapim[i][j + 1] < 0) { im->data[i][j] = 255; continue; } if (lapim[i + 1][j-1] * lapim[i - 1][j + 1] < 0) { im->data[i][j] = 255; continue; } if (lapim[i-1][j - 1] * lapim[i + 1][j + 1] < 0) { im->data[i][j] = 255; continue; } } } } // //void dolap(float **x, int nr, int nc, float **y) //{ // int i, j, k, m, n; // float u, v; // for (i = 1; i < nr - 1; i++) // { // for (j = 1; j < nc - 1; j++) // { // y[i][j] = (x[i][j + 1] + x[i][j - 1] + x[i - 1][j] + x[i + 1][j]) - 4 * x[i][j]; // if (u>y[i][j]) // { // u = y[i][j]; // } // if (v < y[i][j]) // { // v = y[i][j]; // } // } // } // //} // // // // // //
评论
    相关推荐