| 1 |
#include "cc3_connected_component.h" |
|---|
| 2 |
#include "cc3_ilp.h" |
|---|
| 3 |
|
|---|
| 4 |
|
|---|
| 5 |
#define SELECTED 255 |
|---|
| 6 |
#define NOT_SELECTED 0 |
|---|
| 7 |
#define MARKED 2 |
|---|
| 8 |
#define FINAL_SELECTED 1 |
|---|
| 9 |
|
|---|
| 10 |
|
|---|
| 11 |
|
|---|
| 12 |
int count (cc3_image_t * img, int x, int y, int steps) |
|---|
| 13 |
{ |
|---|
| 14 |
int size; |
|---|
| 15 |
int width, height; |
|---|
| 16 |
cc3_pixel_t p; |
|---|
| 17 |
|
|---|
| 18 |
width = img->width; |
|---|
| 19 |
height = img->height; |
|---|
| 20 |
|
|---|
| 21 |
size = 0; |
|---|
| 22 |
cc3_get_pixel (img, x, y, &p); |
|---|
| 23 |
if (p.channel[0] == SELECTED) |
|---|
| 24 |
size = 1; |
|---|
| 25 |
steps--; |
|---|
| 26 |
if (steps == 0) |
|---|
| 27 |
return size; |
|---|
| 28 |
p.channel[0] = MARKED; |
|---|
| 29 |
cc3_set_pixel (img, x, y, &p); |
|---|
| 30 |
|
|---|
| 31 |
if (x > 1) { |
|---|
| 32 |
cc3_get_pixel (img, x - 1, y, &p); |
|---|
| 33 |
if (p.channel[0] == SELECTED) |
|---|
| 34 |
size += count (img, x - 1, y, steps); |
|---|
| 35 |
} |
|---|
| 36 |
if (x < width) { |
|---|
| 37 |
cc3_get_pixel (img, x + 1, y, &p); |
|---|
| 38 |
if (p.channel[0] == SELECTED) |
|---|
| 39 |
size += count (img, x + 1, y, steps); |
|---|
| 40 |
} |
|---|
| 41 |
if (y > 1) { |
|---|
| 42 |
cc3_get_pixel (img, x, y - 1, &p); |
|---|
| 43 |
if (p.channel[0] == SELECTED) |
|---|
| 44 |
size += count (img, x, y - 1, steps); |
|---|
| 45 |
} |
|---|
| 46 |
if (y < height) { |
|---|
| 47 |
cc3_get_pixel (img, x, y + 1, &p); |
|---|
| 48 |
if (p.channel[0] == SELECTED) |
|---|
| 49 |
size += count (img, x, y + 1, steps); |
|---|
| 50 |
} |
|---|
| 51 |
|
|---|
| 52 |
|
|---|
| 53 |
if( g_cc_conf.connectivity==L8_CONNECTED) |
|---|
| 54 |
{ |
|---|
| 55 |
if (x > 1 && y > 1) { |
|---|
| 56 |
cc3_get_pixel (img, x - 1, y - 1, &p); |
|---|
| 57 |
if (p.channel[0] == SELECTED) |
|---|
| 58 |
size += count (img, x - 1, y - 1, steps); |
|---|
| 59 |
} |
|---|
| 60 |
if (x < width && y > 1) { |
|---|
| 61 |
cc3_get_pixel (img, x + 1, y - 1, &p); |
|---|
| 62 |
if (p.channel[0] == SELECTED) |
|---|
| 63 |
size += count (img, x + 1, y - 1, steps); |
|---|
| 64 |
} |
|---|
| 65 |
if (x > 1 && y < height) { |
|---|
| 66 |
cc3_get_pixel (img, x - 1, y + 1, &p); |
|---|
| 67 |
if (p.channel[0] == SELECTED) |
|---|
| 68 |
size += count (img, x - 1, y + 1, steps); |
|---|
| 69 |
} |
|---|
| 70 |
if (x < width && y < height) { |
|---|
| 71 |
cc3_get_pixel (img, x + 1, y + 1, &p); |
|---|
| 72 |
if (p.channel[0] == SELECTED) |
|---|
| 73 |
size += count (img, x + 1, y + 1, steps); |
|---|
| 74 |
} |
|---|
| 75 |
} |
|---|
| 76 |
|
|---|
| 77 |
return size; |
|---|
| 78 |
} |
|---|
| 79 |
|
|---|
| 80 |
int reduce (cc3_image_t * img, int x, int y, int steps, int remove) |
|---|
| 81 |
{ |
|---|
| 82 |
int size; |
|---|
| 83 |
int width, height; |
|---|
| 84 |
cc3_pixel_t p; |
|---|
| 85 |
|
|---|
| 86 |
width = img->width; |
|---|
| 87 |
height = img->height; |
|---|
| 88 |
|
|---|
| 89 |
|
|---|
| 90 |
size = 0; |
|---|
| 91 |
cc3_get_pixel (img, x, y, &p); |
|---|
| 92 |
if (p.channel[0] == MARKED) |
|---|
| 93 |
size = 1; |
|---|
| 94 |
steps--; |
|---|
| 95 |
if (steps == 0) |
|---|
| 96 |
return size; |
|---|
| 97 |
|
|---|
| 98 |
if (remove) |
|---|
| 99 |
p.channel[0] = NOT_SELECTED; |
|---|
| 100 |
else |
|---|
| 101 |
p.channel[0] = FINAL_SELECTED; |
|---|
| 102 |
|
|---|
| 103 |
cc3_set_pixel (img, x, y, &p); |
|---|
| 104 |
|
|---|
| 105 |
if (x > 1) { |
|---|
| 106 |
cc3_get_pixel (img, x - 1, y, &p); |
|---|
| 107 |
if (p.channel[0] == MARKED) |
|---|
| 108 |
size += reduce (img, x - 1, y, steps, remove); |
|---|
| 109 |
} |
|---|
| 110 |
if (x < width) { |
|---|
| 111 |
cc3_get_pixel (img, x + 1, y, &p); |
|---|
| 112 |
if (p.channel[0] == MARKED) |
|---|
| 113 |
size += reduce (img, x + 1, y, steps, remove); |
|---|
| 114 |
} |
|---|
| 115 |
if (y > 1) { |
|---|
| 116 |
cc3_get_pixel (img, x, y - 1, &p); |
|---|
| 117 |
if (p.channel[0] == MARKED) |
|---|
| 118 |
size += reduce (img, x, y - 1, steps, remove); |
|---|
| 119 |
} |
|---|
| 120 |
if (y < height) { |
|---|
| 121 |
cc3_get_pixel (img, x, y + 1, &p); |
|---|
| 122 |
if (p.channel[0] == MARKED) |
|---|
| 123 |
size += reduce (img, x, y + 1, steps, remove); |
|---|
| 124 |
} |
|---|
| 125 |
|
|---|
| 126 |
if( g_cc_conf.connectivity==L8_CONNECTED) |
|---|
| 127 |
{ |
|---|
| 128 |
if (x > 1 && y > 1) { |
|---|
| 129 |
cc3_get_pixel (img, x - 1, y - 1, &p); |
|---|
| 130 |
if (p.channel[0] == MARKED) |
|---|
| 131 |
size += reduce (img, x - 1, y - 1, steps, remove); |
|---|
| 132 |
} |
|---|
| 133 |
if (x < width && y > 1) { |
|---|
| 134 |
cc3_get_pixel (img, x + 1, y - 1, &p); |
|---|
| 135 |
if (p.channel[0] == MARKED) |
|---|
| 136 |
size += reduce (img, x + 1, y - 1, steps, remove); |
|---|
| 137 |
} |
|---|
| 138 |
if (x > 1 && y < height) { |
|---|
| 139 |
cc3_get_pixel (img, x - 1, y + 1, &p); |
|---|
| 140 |
if (p.channel[0] == MARKED) |
|---|
| 141 |
size += reduce (img, x - 1, y + 1, steps, remove); |
|---|
| 142 |
} |
|---|
| 143 |
if (x < width && y < height) { |
|---|
| 144 |
cc3_get_pixel (img, x + 1, y + 1, &p); |
|---|
| 145 |
if (p.channel[0] == MARKED) |
|---|
| 146 |
size += reduce (img, x + 1, y + 1, steps, remove); |
|---|
| 147 |
} |
|---|
| 148 |
} |
|---|
| 149 |
return size; |
|---|
| 150 |
} |
|---|
| 151 |
|
|---|
| 152 |
|
|---|
| 153 |
void cc3_connected_component_reduce (cc3_image_t * img, ccr_config_t conf) |
|---|
| 154 |
{ |
|---|
| 155 |
int x, y, size; |
|---|
| 156 |
int width, height; |
|---|
| 157 |
cc3_pixel_t p; |
|---|
| 158 |
|
|---|
| 159 |
|
|---|
| 160 |
|
|---|
| 161 |
g_cc_conf.connectivity=conf.connectivity; |
|---|
| 162 |
g_cc_conf.max_depth=conf.connectivity; |
|---|
| 163 |
g_cc_conf.min_blob_size=conf.connectivity; |
|---|
| 164 |
|
|---|
| 165 |
width = img->width; |
|---|
| 166 |
height = img->height; |
|---|
| 167 |
|
|---|
| 168 |
for (y = 0; y < height; y++) |
|---|
| 169 |
for (x = 0; x < width; x++) { |
|---|
| 170 |
cc3_get_pixel (img, x, y, &p); |
|---|
| 171 |
if (p.channel[0] == SELECTED) { |
|---|
| 172 |
size = count (img, x, y, conf.max_depth); |
|---|
| 173 |
if (size < conf.min_blob_size) |
|---|
| 174 |
reduce (img, x, y, conf.max_depth,1); |
|---|
| 175 |
else |
|---|
| 176 |
reduce (img, x, y,conf.max_depth, 0); |
|---|
| 177 |
} |
|---|
| 178 |
} |
|---|
| 179 |
|
|---|
| 180 |
|
|---|
| 181 |
|
|---|
| 182 |
for (y = 0; y < height; y++) |
|---|
| 183 |
for (x = 0; x < width; x++) { |
|---|
| 184 |
cc3_get_pixel (img, x, y, &p); |
|---|
| 185 |
if (p.channel[0] == FINAL_SELECTED) { |
|---|
| 186 |
p.channel[0] = SELECTED; |
|---|
| 187 |
cc3_set_pixel (img, x, y, &p); |
|---|
| 188 |
} |
|---|
| 189 |
} |
|---|
| 190 |
} |
|---|
| 191 |
|
|---|