1 | /* |
---|
2 | max3.c implements ... |
---|
3 | |
---|
4 | goes through and tests to see if the pixel is a maximum in its 3x3 neighbourhood |
---|
5 | |
---|
6 | 2001 written by Phil Torr |
---|
7 | Microsoft Research Cambridge |
---|
8 | */ |
---|
9 | |
---|
10 | #include <math.h> |
---|
11 | #include <stdio.h> |
---|
12 | #include "mex.h" |
---|
13 | |
---|
14 | void mexFunction ( |
---|
15 | int nlhs, /* number of expected outputs */ |
---|
16 | mxArray **plhs, /* matrix pointer array returning outputs */ |
---|
17 | int nrhs, /* number of inputs */ |
---|
18 | const mxArray **prhs /* matrix pointer array for inputs */ |
---|
19 | ) { |
---|
20 | int width, height, i, j, l, k, border =1; |
---|
21 | double *out, *in, max; |
---|
22 | bool is_max; |
---|
23 | |
---|
24 | /* parameter checks */ |
---|
25 | if ((nrhs != 1) || (nlhs != 1)) { |
---|
26 | mexErrMsgTxt ("Usage: Y = max3 (im1)\n\n"); |
---|
27 | return; |
---|
28 | } |
---|
29 | |
---|
30 | /* reading the parameters */ |
---|
31 | height = mxGetM (prhs [0]); |
---|
32 | width = mxGetN (prhs [0]); |
---|
33 | in = (double *) mxGetPr (prhs [0]); |
---|
34 | |
---|
35 | /* require memory for return */ |
---|
36 | plhs [0] = mxCreateDoubleMatrix (height, width, mxREAL); |
---|
37 | out = (double *) mxGetPr (plhs [0]); |
---|
38 | |
---|
39 | |
---|
40 | //fill in border pixels with a negative... |
---|
41 | for (i = 0; i < height; i++) |
---|
42 | { |
---|
43 | j = 0; |
---|
44 | out [j * height + i] = -10001.0; |
---|
45 | j = width-1; |
---|
46 | out [j * height + i] = -10001.0; |
---|
47 | } |
---|
48 | |
---|
49 | |
---|
50 | //fill in border pixels with a negative... |
---|
51 | for (j = border; j < width-border; j++) |
---|
52 | { |
---|
53 | i = 0; |
---|
54 | out [j * height + i] = -10001.0; |
---|
55 | i = height-1; |
---|
56 | out [j * height + i] = -10001.0; |
---|
57 | } |
---|
58 | |
---|
59 | |
---|
60 | /* check for maximum */ |
---|
61 | for (i = border; i < height-border; i++) |
---|
62 | for (j = border; j < width-border; j++) { |
---|
63 | max = in [j * height + i]; |
---|
64 | is_max = true; |
---|
65 | if (max >0.0) |
---|
66 | { |
---|
67 | /* finding the maximum in the neighbourhood */ |
---|
68 | for (l = -border; l < border+1; l++) |
---|
69 | for (k = -border; k < border+1; k++) |
---|
70 | if (in [(j + l) * height + (i + k)] >= max) { |
---|
71 | if ((l!=0) || (k!=0)) |
---|
72 | //max = in [(j + l) * height + (i + k)]; |
---|
73 | is_max = false; |
---|
74 | } |
---|
75 | } |
---|
76 | else |
---|
77 | is_max = false; //if not over threshold, here zero |
---|
78 | if (is_max) |
---|
79 | out [j * height + i] = max; |
---|
80 | else |
---|
81 | out [j * height + i] = -10001.0; |
---|
82 | } |
---|
83 | |
---|
84 | return; |
---|
85 | } |
---|