-
Notifications
You must be signed in to change notification settings - Fork 5
/
minfilter.c
73 lines (62 loc) · 1.74 KB
/
minfilter.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
/** @file minfilter.c - Implementation
** @brief Minimum filter.
** @author Zhiwei Zeng
** @date 2018.05.04
**/
/*
Copyright (C) 2018 Zhiwei Zeng.
Copyright (C) 2018 Chengdu ZLT Technology Co., Ltd.
All rights reserved.
This file is part of the railway monitor toolkit and is made available under
the terms of the BSD license (see the COPYING file).
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include "minfilter.h"
/** @brief Minimum filter.
** @param image input image.
** @param width image width.
** @param height image height.
** @param minf_image minimum filtered image.
**/
void min_filter(const unsigned char *image, unsigned int width,
unsigned int height, unsigned int ksize,
unsigned char *minf_image)
{
unsigned int krad;
unsigned int x, y;
unsigned int kx, ky;
unsigned int val, minv;
krad = ksize >> 1;
for (y = krad; y < height - krad; y++) {
for (x = krad; x < width - krad; x++) {
minv = 0xFF;
for (ky = y - krad; ky <= y + krad; ky++) {
for (kx = x - krad; kx <= x + krad; kx++) {
val = image[ky * width + kx];
if (val < minv) {
minv = val;
}
}
}
minf_image[y * width + x] = minv;
}
}
for (y = 0; y < krad; y++) {
memmove(minf_image + y * width, minf_image + krad * width, width * sizeof(unsigned char));
}
for (y = height - krad; y < height; y++) {
memmove(minf_image + y * width, minf_image + (height - krad - 1) * width,
width * sizeof(unsigned char));
}
for (y = 0; y < height; y++) {
for (x = 0; x < krad; x++) {
minf_image[y * width + x] = minf_image[y * width + krad];
}
for (x = width - krad; x < width; x++) {
minf_image[y * width + x] = minf_image[y * width + width - krad - 1];
}
}
}