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
|
/*
* $Id: linecorrect.c 22817 2020-05-14 07:05:40Z yeti-dn $
* Copyright (C) 2003-2018 David Necas (Yeti), Petr Klapetek, Luke Somers.
* E-mail: yeti@gwyddion.net, klapetek@gwyddion.net, lsomers@sas.upenn.edu.
*
* 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 <libgwyddion/gwymacros.h>
#include <libgwyddion/gwymath.h>
#include <libprocess/arithmetic.h>
#include <libprocess/correct.h>
#include <libprocess/filters.h>
#include <libprocess/linestats.h>
#include <libprocess/stats.h>
#include <libgwymodule/gwymodule-process.h>
#include <libgwydgets/gwystock.h>
#include <libgwydgets/gwyradiobuttons.h>
#include <libgwydgets/gwydgetutils.h>
#include <app/gwyapp.h>
#define LINECORR_RUN_MODES GWY_RUN_IMMEDIATE
typedef struct {
gdouble *a;
gdouble *b;
guint n;
} MedianLineData;
static gboolean module_register (void);
static void line_correct_step (GwyContainer *data,
GwyRunType run);
static void mark_inverted_lines (GwyContainer *data,
GwyRunType run);
static GwyModuleInfo module_info = {
GWY_MODULE_ABI_VERSION,
&module_register,
N_("Corrects line defects (mostly experimental algorithms)."),
"Yeti <yeti@gwyddion.net>, Luke Somers <lsomers@sas.upenn.edu>",
"1.12",
"David Nečas (Yeti) & Petr Klapetek & Luke Somers",
"2004",
};
GWY_MODULE_QUERY2(module_info, linecorrect)
static gboolean
module_register(void)
{
gwy_process_func_register("line_correct_step",
(GwyProcessFunc)&line_correct_step,
N_("/_Correct Data/Ste_p Line Correction"),
GWY_STOCK_LINE_LEVEL,
LINECORR_RUN_MODES,
GWY_MENU_FLAG_DATA,
N_("Correct steps in lines"));
gwy_process_func_register("line_correct_inverted",
(GwyProcessFunc)&mark_inverted_lines,
N_("/_Correct Data/Mark _Inverted Rows"),
NULL,
LINECORR_RUN_MODES,
GWY_MENU_FLAG_DATA,
N_("Mark lines with inverted sign"));
return TRUE;
}
static void
calcualte_segment_correction(const gdouble *drow,
gdouble *mrow,
gint xres,
gint len)
{
const gint min_len = 4;
gdouble corr;
gint j;
if (len >= min_len) {
corr = 0.0;
for (j = 0; j < len; j++)
corr += (drow[j] + drow[2*xres + j])/2.0 - drow[xres + j];
corr /= len;
for (j = 0; j < len; j++)
mrow[j] = (3*corr
+ (drow[j] + drow[2*xres + j])/2.0 - drow[xres + j])/4.0;
}
else {
for (j = 0; j < len; j++)
mrow[j] = 0.0;
}
}
static void
line_correct_step_iter(GwyDataField *dfield,
GwyDataField *mask)
{
const gdouble threshold = 3.0;
gint xres, yres, i, j, len;
gdouble u, v, w;
const gdouble *d, *drow;
gdouble *m, *mrow;
yres = gwy_data_field_get_yres(dfield);
xres = gwy_data_field_get_xres(dfield);
d = gwy_data_field_get_data_const(dfield);
m = gwy_data_field_get_data(mask);
w = 0.0;
for (i = 0; i < yres-1; i++) {
drow = d + i*xres;
for (j = 0; j < xres; j++) {
v = drow[j + xres] - drow[j];
w += v*v;
}
}
w = w/(yres-1)/xres;
for (i = 0; i < yres-2; i++) {
drow = d + i*xres;
mrow = m + (i + 1)*xres;
for (j = 0; j < xres; j++) {
u = drow[xres + j];
v = (u - drow[j])*(u - drow[j + 2*xres]);
if (G_UNLIKELY(v > threshold*w)) {
if (2*u - drow[j] - drow[j + 2*xres] > 0)
mrow[j] = 1.0;
else
mrow[j] = -1.0;
}
}
len = 1;
for (j = 1; j < xres; j++) {
if (mrow[j] == mrow[j-1])
len++;
else {
if (mrow[j-1])
calcualte_segment_correction(drow + j-len, mrow + j-len,
xres, len);
len = 1;
}
}
if (mrow[j-1]) {
calcualte_segment_correction(drow + j-len, mrow + j-len,
xres, len);
}
}
gwy_data_field_sum_fields(dfield, dfield, mask);
}
static void
line_correct_step(GwyContainer *data,
GwyRunType run)
{
GwyDataField *dfield, *mask;
GwyDataLine *shifts;
GQuark dquark;
gdouble avg;
gint id;
g_return_if_fail(run & LINECORR_RUN_MODES);
gwy_app_data_browser_get_current(GWY_APP_DATA_FIELD, &dfield,
GWY_APP_DATA_FIELD_KEY, &dquark,
GWY_APP_DATA_FIELD_ID, &id,
0);
g_return_if_fail(dfield && dquark);
gwy_app_undo_qcheckpointv(data, 1, &dquark);
avg = gwy_data_field_get_avg(dfield);
shifts = gwy_data_field_find_row_shifts_trimmed_mean(dfield,
NULL, GWY_MASK_IGNORE,
0.5, 0);
gwy_data_field_subtract_row_shifts(dfield, shifts);
g_object_unref(shifts);
mask = gwy_data_field_new_alike(dfield, TRUE);
line_correct_step_iter(dfield, mask);
gwy_data_field_clear(mask);
line_correct_step_iter(dfield, mask);
g_object_unref(mask);
gwy_data_field_filter_conservative(dfield, 5);
gwy_data_field_add(dfield, avg - gwy_data_field_get_avg(dfield));
gwy_data_field_data_changed(dfield);
gwy_app_channel_log_add_proc(data, id, id);
}
static gdouble
row_correlation(const gdouble *row1, gdouble avg1, gdouble rms1,
const gdouble *row2, gdouble avg2, gdouble rms2,
guint n, gdouble total_rms)
{
gdouble s = 0.0;
while (n--) {
s += (*row1 - avg1)*(*row2 - avg2);
row1++;
row2++;
}
s /= (rms1*rms2 + total_rms*total_rms);
return s;
}
static void
mark_inverted_lines(GwyContainer *data,
GwyRunType run)
{
GwyDataField *dfield, *existing_mask, *mask;
GwyDataLine *avgline, *rmsline;
GQuark mquark;
gdouble *weight;
const gdouble *avg, *rms;
gint xres, yres, i, from, j, id;
gdouble total_rms, s, wmax;
gboolean have_negative = FALSE, inverted;
const gdouble *d;
g_return_if_fail(run & LINECORR_RUN_MODES);
gwy_app_data_browser_get_current(GWY_APP_DATA_FIELD, &dfield,
GWY_APP_MASK_FIELD, &existing_mask,
GWY_APP_MASK_FIELD_KEY, &mquark,
GWY_APP_DATA_FIELD_ID, &id,
0);
g_return_if_fail(dfield && mquark);
total_rms = gwy_data_field_get_rms(dfield);
xres = gwy_data_field_get_xres(dfield);
yres = gwy_data_field_get_yres(dfield);
if (total_rms <= 0.0 || yres < 3 || xres < 3)
return;
avgline = gwy_data_line_new(yres, yres, FALSE);
gwy_data_field_get_line_stats(dfield, avgline, GWY_LINE_STAT_MEAN,
GWY_ORIENTATION_HORIZONTAL);
avg = gwy_data_line_get_data(avgline);
rmsline = gwy_data_line_new(yres, yres, FALSE);
gwy_data_field_get_line_stats(dfield, rmsline, GWY_LINE_STAT_RMS,
GWY_ORIENTATION_HORIZONTAL);
rms = gwy_data_line_get_data(rmsline);
d = gwy_data_field_get_data_const(dfield);
/* Calculate neighbour row correlations. */
weight = g_new0(gdouble, yres-1);
for (i = 0; i < yres-1; i++) {
weight[i] = row_correlation(d + i*xres, avg[i], rms[i],
d + (i+1)*xres, avg[i+1], rms[i+1],
xres, total_rms);
if (weight[i] < 0.0)
have_negative = TRUE;
}
if (!have_negative) {
g_object_unref(avgline);
g_object_unref(rmsline);
g_free(weight);
return;
}
/* Find the most positively correlated block. */
from = 0;
for (i = 0; i < yres-2; i++) {
if (weight[i]*weight[i+1] < 0.0) {
s = 0.0;
for (j = from; j <= i; j++)
s += weight[j];
for (j = from; j <= i; j++)
weight[j] = s;
from = i+1;
}
}
s = 0.0;
for (j = from; j < yres-1; j++)
s += weight[j];
for (j = from; j < yres-1; j++)
weight[j] = s;
wmax = 0.0;
from = 0;
for (i = 0; i < yres-1; i++) {
if (weight[i] > wmax) {
wmax = weight[i];
from = i;
}
}
g_object_unref(avgline);
g_object_unref(rmsline);
mask = gwy_data_field_new_alike(dfield, TRUE);
gwy_si_unit_set_from_string(gwy_data_field_get_si_unit_z(mask), NULL);
/* Extend the sign of this block downwards. Note weight[i] represents the
* sign between rows i and i+1. */
inverted = FALSE;
for (i = from; i < yres-1; i++) {
if (weight[i] < 0.0)
inverted = !inverted;
if (inverted)
gwy_data_field_area_fill(mask, 0, i+1, xres, 1, 1.0);
}
/* Extend the sign of this block upwards. Note weight[i] represents the
* sign between rows i and i+1. */
inverted = FALSE;
for (i = j = from; i >= 0; i--) {
if (weight[i] < 0.0)
inverted = !inverted;
if (inverted)
gwy_data_field_area_fill(mask, 0, i, xres, 1, 1.0);
}
g_free(weight);
if (!existing_mask && gwy_data_field_get_max(mask) <= 0.0) {
g_object_unref(mask);
return;
}
gwy_app_undo_qcheckpointv(data, 1, &mquark);
if (existing_mask) {
gwy_data_field_copy(mask, existing_mask, FALSE);
gwy_data_field_data_changed(existing_mask);
}
else {
gwy_container_set_object(data, mquark, mask);
}
g_object_unref(mask);
gwy_app_channel_log_add_proc(data, id, id);
}
/* vim: set cin et ts=4 sw=4 cino=>1s,e0,n0,f0,{0,}0,^0,\:1s,=0,g1s,h0,t0,+1s,c3,(0,u0 : */
|