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 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491
|
/*
* $Id: edge.c 24547 2021-11-26 13:41:31Z yeti-dn $
* Copyright (C) 2003-2021 David Necas (Yeti), Petr Klapetek.
* E-mail: yeti@gwyddion.net, klapetek@gwyddion.net.
*
* This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public
* License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any
* later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied
* warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
* details.
*
* You should have received a copy of the GNU General Public License along with this program; if not, write to the
* Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
#include "config.h"
#include <string.h>
#include <gtk/gtk.h>
#include <libgwyddion/gwymacros.h>
#include <libgwyddion/gwymath.h>
#include <libprocess/gwyprocess.h>
#include <libgwydgets/gwystock.h>
#include <libgwymodule/gwymodule-process.h>
#include <app/gwyapp.h>
#define EDGE_RUN_MODES GWY_RUN_IMMEDIATE
static gboolean module_register(void);
static void edge (GwyContainer *data,
GwyRunType run,
const gchar *name);
static void laplacian_do (GwyDataField *dfield,
GwyDataField *show);
static void canny_do (GwyDataField *dfield,
GwyDataField *show);
static void rms_do (GwyDataField *dfield,
GwyDataField *show);
static void rms_edge_do (GwyDataField *dfield,
GwyDataField *show);
static void nonlinearity_do(GwyDataField *dfield,
GwyDataField *show);
static void hough_lines_do (GwyDataField *dfield,
GwyDataField *show);
static void harris_do (GwyDataField *dfield,
GwyDataField *show);
static void inclination_do (GwyDataField *dfield,
GwyDataField *show);
static void step_do (GwyDataField *dfield,
GwyDataField *show);
static void sobel_do (GwyDataField *dfield,
GwyDataField *show);
static void prewitt_do (GwyDataField *dfield,
GwyDataField *show);
static void slope_map (GwyContainer *data,
GwyRunType run);
static GwyModuleInfo module_info = {
GWY_MODULE_ABI_VERSION,
&module_register,
N_("Several edge detection methods (Laplacian of Gaussian, Canny, and some experimental), creates presentation."),
"Petr Klapetek <klapetek@gwyddion.net>",
"1.15",
"David Nečas (Yeti) & Petr Klapetek",
"2004",
};
GWY_MODULE_QUERY2(module_info, edge)
static gboolean
module_register(void)
{
gwy_process_func_register("edge_laplacian",
(GwyProcessFunc)&edge,
N_("/_Presentation/_Edge Detection/_Laplacian of Gaussian"),
NULL,
EDGE_RUN_MODES,
GWY_MENU_FLAG_DATA,
N_("Laplacian of Gaussian step detection "
"presentation"));
gwy_process_func_register("edge_canny",
(GwyProcessFunc)&edge,
N_("/_Presentation/_Edge Detection/_Canny"),
NULL,
EDGE_RUN_MODES,
GWY_MENU_FLAG_DATA,
N_("Canny edge detection presentation"));
gwy_process_func_register("edge_rms",
(GwyProcessFunc)&edge,
N_("/_Presentation/_Edge Detection/_RMS"),
NULL,
EDGE_RUN_MODES,
GWY_MENU_FLAG_DATA,
N_("Local RMS value based step detection "
"presentation"));
gwy_process_func_register("edge_rms_edge",
(GwyProcessFunc)&edge,
N_("/_Presentation/_Edge Detection/RMS _Edge"),
NULL,
EDGE_RUN_MODES,
GWY_MENU_FLAG_DATA,
N_("Local RMS value based step detection with postprocessing"));
gwy_process_func_register("edge_nonlinearity",
(GwyProcessFunc)&edge,
N_("/_Presentation/_Edge Detection/Local _Nonlinearity"),
NULL,
EDGE_RUN_MODES,
GWY_MENU_FLAG_DATA,
N_("Local nonlinearity based edge detection "
"presentation"));
gwy_process_func_register("edge_hough_lines",
(GwyProcessFunc)&edge,
N_("/_Presentation/_Edge Detection/_Hough Lines"),
NULL,
EDGE_RUN_MODES,
GWY_MENU_FLAG_DATA,
/* FIXME */
N_("Hough lines presentation"));
gwy_process_func_register("edge_harris",
(GwyProcessFunc)&edge,
N_("/_Presentation/_Edge Detection/_Harris Corner"),
NULL,
EDGE_RUN_MODES,
GWY_MENU_FLAG_DATA,
/* FIXME */
N_("Harris corner presentation"));
gwy_process_func_register("edge_inclination",
(GwyProcessFunc)&edge,
N_("/_Presentation/_Edge Detection/_Inclination"),
NULL,
EDGE_RUN_MODES,
GWY_MENU_FLAG_DATA,
N_("Local inclination visualization presentation"));
gwy_process_func_register("edge_step",
(GwyProcessFunc)&edge,
N_("/_Presentation/_Edge Detection/_Step"),
NULL,
EDGE_RUN_MODES,
GWY_MENU_FLAG_DATA,
N_("Fine step detection presentation"));
gwy_process_func_register("edge_sobel",
(GwyProcessFunc)&edge,
N_("/_Presentation/_Edge Detection/_Sobel"),
NULL,
EDGE_RUN_MODES,
GWY_MENU_FLAG_DATA,
N_("Sobel edge presentation"));
gwy_process_func_register("edge_prewitt",
(GwyProcessFunc)&edge,
N_("/_Presentation/_Edge Detection/_Prewitt"),
NULL,
EDGE_RUN_MODES,
GWY_MENU_FLAG_DATA,
N_("Prewitt edge presentation"));
gwy_process_func_register("slope_map",
(GwyProcessFunc)&slope_map,
N_("/_Integral Transforms/Local Slope"),
NULL,
EDGE_RUN_MODES,
GWY_MENU_FLAG_DATA,
N_("First derivative slope transformation"));
return TRUE;
}
static void
edge(GwyContainer *data, GwyRunType run, const gchar *name)
{
static const struct {
const gchar *name;
void (*func)(GwyDataField *dfield, GwyDataField *show);
}
functions[] = {
{ "edge_canny", canny_do, },
{ "edge_harris", harris_do, },
{ "edge_hough_lines", hough_lines_do, },
{ "edge_inclination", inclination_do, },
{ "edge_laplacian", laplacian_do, },
{ "edge_nonlinearity", nonlinearity_do, },
{ "edge_rms", rms_do, },
{ "edge_rms_edge", rms_edge_do, },
{ "edge_step", step_do, },
{ "edge_sobel", sobel_do, },
{ "edge_prewitt", prewitt_do, },
};
GwyDataField *dfield, *showfield;
GQuark dquark, squark;
GwySIUnit *siunit;
gint id;
guint i;
g_return_if_fail(run & EDGE_RUN_MODES);
gwy_app_data_browser_get_current(GWY_APP_DATA_FIELD_KEY, &dquark,
GWY_APP_DATA_FIELD, &dfield,
GWY_APP_DATA_FIELD_ID, &id,
GWY_APP_SHOW_FIELD_KEY, &squark,
GWY_APP_SHOW_FIELD, &showfield,
0);
g_return_if_fail(dfield && dquark && squark);
gwy_app_undo_qcheckpointv(data, 1, &squark);
if (!showfield) {
showfield = gwy_data_field_new_alike(dfield, FALSE);
siunit = gwy_si_unit_new(NULL);
gwy_data_field_set_si_unit_z(showfield, siunit);
g_object_unref(siunit);
gwy_container_set_object(data, squark, showfield);
g_object_unref(showfield);
}
for (i = 0; i < G_N_ELEMENTS(functions); i++) {
if (gwy_strequal(name, functions[i].name)) {
functions[i].func(dfield, showfield);
break;
}
}
if (i == G_N_ELEMENTS(functions)) {
g_warning("edge does not provide function `%s'", name);
gwy_data_field_copy(dfield, showfield, FALSE);
}
gwy_data_field_normalize(showfield);
gwy_data_field_data_changed(showfield);
gwy_app_channel_log_add_proc(data, id, id);
}
/* Note this is the limiting case when LoG reduces for discrete data just to Laplacian */
static void
laplacian_do(GwyDataField *dfield, GwyDataField *show)
{
gwy_data_field_copy(dfield, show, FALSE);
gwy_data_field_filter_laplacian(show);
}
static void
canny_do(GwyDataField *dfield, GwyDataField *show)
{
/* Now we use fixed threshold, but in future, there could be API with some setting. We could also do smooting
* before applying filter.*/
gwy_data_field_copy(dfield, show, FALSE);
gwy_data_field_filter_canny(show, 0.1);
}
static void
rms_do(GwyDataField *dfield, GwyDataField *show)
{
gwy_data_field_copy(dfield, show, FALSE);
gwy_data_field_filter_rms(show, 5);
}
static void
rms_edge_do(GwyDataField *dfield, GwyDataField *show)
{
GwyDataField *tmp;
gint xres, yres, i, j;
gdouble *d;
const gdouble *t;
gdouble s;
gwy_data_field_copy(dfield, show, FALSE);
xres = gwy_data_field_get_xres(show);
yres = gwy_data_field_get_yres(show);
tmp = gwy_data_field_duplicate(show);
gwy_data_field_filter_rms(tmp, 5);
t = gwy_data_field_get_data_const(tmp);
d = gwy_data_field_get_data(show);
for (i = 0; i < yres; i++) {
gint iim = MAX(i-2, 0)*xres;
gint iip = MIN(i+2, yres-1)*xres;
gint ii = i*xres;
for (j = 0; j < xres; j++) {
gint jm = MAX(j-2, 0);
gint jp = MIN(j+2, xres-1);
s = t[ii + jm] + t[ii + jp] + t[iim + j] + t[iip + j]
+ (t[iim + jm] + t[iim + jp] + t[iip + jm] + t[iip + jp])/2.0;
s /= 6.0;
d[ii + j] = MAX(t[ii + j] - s, 0);
}
}
g_object_unref(tmp);
}
static gdouble
fit_local_plane_by_pos(gint n,
const gint *xp, const gint *yp, const gdouble *z,
gdouble *bx, gdouble *by)
{
gdouble m[12], b[4];
gint i;
gwy_clear(m, 6);
gwy_clear(b, 4);
for (i = 0; i < n; i++) {
m[1] += xp[i];
m[2] += xp[i]*xp[i];
m[3] += yp[i];
m[4] += xp[i]*yp[i];
m[5] += yp[i]*yp[i];
b[0] += z[i];
b[1] += xp[i]*z[i];
b[2] += yp[i]*z[i];
b[3] += z[i]*z[i];
}
m[0] = n;
gwy_assign(m + 6, m, 6);
if (gwy_math_choleski_decompose(3, m))
gwy_math_choleski_solve(3, m, b);
else
b[0] = b[1] = b[2] = 0.0;
*bx = b[1];
*by = b[2];
return (b[3] - (b[0]*b[0]*m[6+0] + b[1]*b[1]*m[6+2] + b[2]*b[2]*m[6+5])
- 2.0*(b[0]*b[1]*m[6+1] + b[0]*b[2]*m[6+3] + b[1]*b[2]*m[6+4]));
}
static void
nonlinearity_do(GwyDataField *dfield, GwyDataField *show)
{
static const gdouble r = 2.5;
gint xres, yres, i, j, size;
gdouble qx, qy;
gint *xp, *yp;
gdouble *d, *z;
xres = gwy_data_field_get_xres(dfield);
yres = gwy_data_field_get_yres(dfield);
d = gwy_data_field_get_data(show);
qx = gwy_data_field_get_dx(dfield);
qy = gwy_data_field_get_dx(dfield);
size = gwy_data_field_get_circular_area_size(r);
z = g_new(gdouble, size);
xp = g_new(gint, 2*size);
yp = xp + size;
for (i = 0; i < yres; i++) {
for (j = 0; j < xres; j++) {
gdouble bx, by, s0r;
gint n;
n = gwy_data_field_circular_area_extract_with_pos(dfield, j, i, r, z, xp, yp);
s0r = fit_local_plane_by_pos(n, xp, yp, z, &bx, &by);
bx /= qx;
by /= qy;
d[i*xres + j] = sqrt(MAX(s0r, 0.0)/(1.0 + bx*bx + by*by));
}
}
g_free(xp);
g_free(z);
}
static void
inclination_do(GwyDataField *dfield, GwyDataField *show)
{
static const gdouble r = 2.5;
gint xres, yres, i, j, size;
gdouble qx, qy;
gint *xp, *yp;
gdouble *d, *z;
xres = gwy_data_field_get_xres(dfield);
yres = gwy_data_field_get_yres(dfield);
d = gwy_data_field_get_data(show);
qx = gwy_data_field_get_dx(dfield);
qy = gwy_data_field_get_dx(dfield);
size = gwy_data_field_get_circular_area_size(r);
z = g_new(gdouble, size);
xp = g_new(gint, 2*size);
yp = xp + size;
for (i = 0; i < yres; i++) {
for (j = 0; j < xres; j++) {
gdouble bx, by;
gint n;
n = gwy_data_field_circular_area_extract_with_pos(dfield, j, i, r, z, xp, yp);
fit_local_plane_by_pos(n, xp, yp, z, &bx, &by);
bx /= qx;
by /= qy;
d[i*xres + j] = atan(hypot(bx, by));
}
}
g_free(xp);
g_free(z);
}
static void
step_do(GwyDataField *dfield, GwyDataField *show)
{
static const gdouble r = 2.5;
gint xres, yres, i, j, size;
gdouble *d, *z;
xres = gwy_data_field_get_xres(dfield);
yres = gwy_data_field_get_yres(dfield);
d = gwy_data_field_get_data(show);
size = gwy_data_field_get_circular_area_size(r);
z = g_new(gdouble, size);
for (i = 0; i < yres; i++) {
for (j = 0; j < xres; j++) {
gint n;
n = gwy_data_field_circular_area_extract(dfield, j, i, r, z);
gwy_math_sort(n, z);
d[i*xres + j] = sqrt(z[n-1 - n/3] - z[n/3]);
}
}
g_free(z);
}
static void
hough_lines_do(GwyDataField *dfield, GwyDataField *show)
{
GwyDataField *x_gradient, *y_gradient;
gwy_data_field_copy(dfield, show, FALSE);
gwy_data_field_filter_canny(show, 0.1);
x_gradient = gwy_data_field_duplicate(dfield);
gwy_data_field_filter_sobel(x_gradient, GWY_ORIENTATION_HORIZONTAL);
y_gradient = gwy_data_field_duplicate(dfield);
gwy_data_field_filter_sobel(y_gradient, GWY_ORIENTATION_VERTICAL);
gwy_data_field_hough_line_strenghten(show, x_gradient, y_gradient, 1, 0.2);
}
static void
harris_do(GwyDataField *dfield, GwyDataField *show)
{
GwyDataField *x_gradient, *y_gradient;
gwy_data_field_copy(dfield, show, FALSE);
x_gradient = gwy_data_field_duplicate(dfield);
gwy_data_field_filter_sobel(x_gradient, GWY_ORIENTATION_HORIZONTAL);
y_gradient = gwy_data_field_duplicate(dfield);
gwy_data_field_filter_sobel(y_gradient, GWY_ORIENTATION_VERTICAL);
gwy_data_field_filter_harris(x_gradient, y_gradient, show, 20, 0.1);
}
static void
sobel_do(GwyDataField *dfield, GwyDataField *show)
{
gwy_data_field_copy(dfield, show, FALSE);
gwy_data_field_filter_sobel_total(show);
}
static void
prewitt_do(GwyDataField *dfield, GwyDataField *show)
{
gwy_data_field_copy(dfield, show, FALSE);
gwy_data_field_filter_prewitt_total(show);
}
static void
slope_map(GwyContainer *data, GwyRunType run)
{
GwyDataField *dfield, *sfield, *buf;
GwySIUnit *xyunit, *zunit;
gint oldid, newid;
g_return_if_fail(run & EDGE_RUN_MODES);
gwy_app_data_browser_get_current(GWY_APP_DATA_FIELD, &dfield,
GWY_APP_DATA_FIELD_ID, &oldid,
0);
g_return_if_fail(dfield);
sfield = gwy_data_field_new_alike(dfield, FALSE);
buf = gwy_data_field_new_alike(dfield, FALSE);
gwy_data_field_filter_slope(dfield, sfield, buf);
gwy_data_field_hypot_of_fields(sfield, sfield, buf);
g_object_unref(buf);
xyunit = gwy_data_field_get_si_unit_xy(sfield);
zunit = gwy_data_field_get_si_unit_z(sfield);
gwy_si_unit_divide(zunit, xyunit, zunit);
newid = gwy_app_data_browser_add_data_field(sfield, data, TRUE);
gwy_app_set_data_field_title(data, newid, _("Slope map"));
gwy_app_channel_log_add_proc(data, oldid, newid);
g_object_unref(sfield);
}
/* vim: set cin columns=120 tw=118 et ts=4 sw=4 cino=>1s,e0,n0,f0,{0,}0,^0,\:1s,=0,g1s,h0,t0,+1s,c3,(0,u0 : */
|