📄 minima.c
字号:
/*----------------------------------------------------------------------PROGRAM: minima.cDATE: 3/9/94AUTHOR: Baback Moghaddam, baback@media.mit.edu------------------------------------------------------------------------ This function computes the local minima of an input array and returns them sorted in ascending order in the pre-allocated Points structure minima(float **array, int imin, int imax, int jmin, int jmax, Points *myPoints) (imin,imax,jmin,jmax) specify the regions over which the minima detection is to be carried out---------------------------------------------------------------------- */#include <stdio.h>#include <stdlib.h>#include <string.h>#include <math.h>#include <float.h>#include "util.h"#include "io.h"struct Points { int n; int *x; int *y; float *f;};struct Point { int x; int y; float f;};int compare(struct Point **p1, struct Point **p2);void minima(float **A, int imin, int imax, int jmin, int jmax, struct Points *Ps);void minima(float **A, int imin, int imax, int jmin, int jmax, struct Points *Ps){ register int i,j; int N, c; int *X, *Y; float *F, f; struct Point *P; struct Point **Pp; N = (imax-imin)*(jmax-jmin)/9.0; /* max # of minima possible */ P = malloc(sizeof(struct Point) * N); Pp = malloc(sizeof(struct Point *) * N); /* run 3-by-3 minima detector over region of interest */ c = 0; for (i=imin+1; i<imax; i++) for (j=jmin+1; j<=jmax; j++) { f = A[i][j]; if (A[i-1][j-1]>f && A[i-1][j]>f && A[i-1][j+1]>f && \ A[i][j-1]>f && A[i][j+1]>f && \ A[i+1][j-1]>f && A[i+1][j]>f && A[i+1][j+1]>f) { P[c].x = i; P[c].y = j; P[c].f = f; Pp[c] = &(P[c]); c++; } } N = c; /* sort Point array */ qsort(Pp, N, sizeof(struct Point *), (int (*)(void **, void **)) compare); /* copy the minima into the Points structure */ Ps->n = N; for (i=0; i<N; i++) { Ps->x[i+1] = Pp[i]->x; Ps->y[i+1] = Pp[i]->y; Ps->f[i+1] = Pp[i]->f; } /* free up structures */ free(P); free(Pp);} int compare(struct Point **p1, struct Point **p2){ int r=0; if ((*p1)->f < (*p2)->f) r = -1; else if ((*p1)->f > (*p2)->f) r = +1; return r;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -