距离变换代码实现(c代码)
来源:互联网 发布:制作游戏的软件 编辑:程序博客网 时间:2024/06/06 19:38
#include <stdio.h>#include <stdlib.h>// Removal Function - FVs that are the Voronoi sites of Voronoi cells that do not intersect Rd are removedstatic int RemoveEDT(const float du, const float dv, const float dw, const float u, const float v, const float w){ float a = v - u; float b = w - v; float c = w - u; // See Eq. 2 from Maurer et al., 2003 return ((c * dv - b * du - a * dw) > (a * b * c));}static int constructPartialVoronoi(float* D, float* g, int* h, int length){ // // Construct partial voronoi diagram (see Maurer et al., 2003, Fig. 5, lines 1-14) // Note: short variable names are used to mimic the notation of the paper // int el = -1; for (int k = 0; k < length; k++) { float fk = D[k]; if (fk != -1.0f) { if (el < 1) { el++; g[el] = fk; h[el] = k; } else { while ((el >= 1) && RemoveEDT(g[el - 1], g[el], fk, h[el - 1], h[el], k)) { el--; } el++; g[el] = fk; h[el] = k; } } } return(el);}static void queryPartialVoronoi(const float* g, const int* h, const int ns, float* D, int length){ // // Query partial Voronoi diagram (see Maurer et al., 2003, Fig. 5, lines 18-24) // int el = 0; for (int k = 0; k < length; k++) { while ((el < ns) && ((g[el] + ((h[el] - k)*(h[el] - k))) >(g[el + 1] + ((h[el + 1] - k)*(h[el + 1] - k))))) { el++; } D[k] = g[el] + (h[el] - k)*(h[el] - k); }}static void voronoiEDT(float* D, float* g, int* h, int length){ // Note: g and h are working vectors allocated in calling function int ns = constructPartialVoronoi(D, g, h, length); if (ns == -1) return; queryPartialVoronoi(g, h, ns, D, length); return;}static void processDimN(int width, int height/*const mwSize *dims, const mwSize ndims*/, float *D){ // Create temporary vectors for processing along dimension N float* g = (float*)calloc(width, sizeof(float)); int* h = (int*)calloc(width, sizeof(int)); // 若为二维数组,则得到第一维元素总数,即行数。注意,matlab按列存储,C按行存储。 for (int k = 0; k < height; k++) { // Process vector voronoiEDT(&D[k*width], g, h, width); } free(g); free(h);}// mxI为输入,unsigned char*类型,mxD为输出,float* 类型// 注意,matlab中mxI是逻辑类型,需要1-mxI才是真正的输入;此处省略了这一步。// 输入为二值化图像,大于阈值的不为0,小于阈值的为0。void computeEDT(float *mxD, const unsigned char *mxI, int width, int height){ float* D1 = (float*)calloc(width, sizeof(float)); float* g = (float*)calloc(width, sizeof(float)); int* h = (int*)calloc(width, sizeof(int)); for (int k = 0; k < width; k++) { for (int i = 0; i < height; i++) { D1[i] = (mxI[i*width + k] == 0) ? 0.0f : -1.0f; } voronoiEDT(D1, g, h, height); for (int i = 0; i < height; i++) { mxD[i*width + k] = D1[i]; } } free(D1); free(g); free(h); processDimN(width, height, mxD);}
阅读全文
0 0
- 距离变换代码实现(c代码)
- FFT变换c代码
- 图像识别中距离变换的原理及作用详解,并附用OpenCV中的distanceTransform实现距离变换的代码!
- hough变换 正在的c代码风格实现
- 编辑距离及代码实现
- 文字编辑距离代码实现
- 模糊距离分析 及 java代码实现
- 经纬度距离计算Java实现代码
- 快速离散余弦变换代码实现(FDCT)
- Radon变换及其Matlab代码实现
- Haar小波变换代码实现
- 透视变换实现代码(全)
- 逆透视变换详解 及 代码实现
- 求二叉树中节点的最大距离的C程序实现代码
- AES-C代码实现
- ARP C代码实现
- C代码实现数组
- C代码实现数组
- Spring aop面向切面编程
- Announcing Zuul: Edge Service in the Cloud
- List子类 ArrayList Vector 和 LinkedList
- 通过java.net.URLConnection发送HTTP请求的方法
- CH340 +Micro USB转串口 不能识别 不起振问题
- 距离变换代码实现(c代码)
- QT布局之水平垂直加Qss样式布局
- android ndk 02 C语言 二级指针 指针运算 指针与数组
- Mysql主从复制和读写分离Amoeba实现
- 测试
- VUE+WebPack游戏设计:为对战扑克牌增加能力槽
- js关闭当前弹出的小窗口并打开新窗口
- ORACLE Scheduler特性(7)Scheduler抛出的Events
- hdu2844