add some binary morphological operations

This commit is contained in:
2023-04-13 18:03:05 +03:00
parent 03eecf2595
commit f3570498e1
18 changed files with 1743 additions and 235 deletions

View File

@@ -9,3 +9,6 @@ link_libraries(usefull_macros improc m)
add_executable(equalize equalize.c)
add_executable(generate generate.c)
add_executable(genu16 genu16.c)
add_executable(gauss gauss.c)
add_executable(poisson poisson.c)
add_executable(objdet objdet.c)

View File

@@ -16,11 +16,37 @@ Usage: genu16 [args] x1,y1[,w1] x2,y2[,w2] ... xn,yn[,w3] - draw 'stars' at coor
-b, --beta=arg beta Moffat parameter of 'star' images (default: 1)
-h, --height=arg resulting image height (default: 1024)
-i, --input=arg input file with coordinates and amplitudes (comma separated)
-o, --output=arg output file name (default: output.png)
-l, --lambda=arg lambda of Poisson noice (default: 10)
-o, --output=arg output file name (default: output.jpg)
-s, --halfwidth=arg FWHM of 'star' images (default: 3.5)
-w, --width=arg resulting image width (default: 1024)
## genu16
The same as 'generate', but works with 16-bit image and save it as 1-channel png (`ampi` now is weight).
The same as 'generate', but works with 16-bit image and save it as 1-channel png (`ampi` now is weight). No noice.
## gauss
Simulator of falling photons with given gaussian distribution.
Usage: gauss [args], where args are:
-?, --help show this help
-X, --xstd=arg STD of 'photons' distribution by X (default: 10)
-Y, --ystd=arg STD of 'photons' distribution by Y (default: 10)
-h, --height=arg resulting image height (default: 1024)
-n, --niter=arg iterations ("falling photons") number (default: 1000000)
-o, --output=arg output file name (default: output.png)
-w, --width=arg resulting image width (default: 1024)
-x, --xcenter=arg X coordinate of 'image' center (default: 512)
-y, --ycenter=arg Y coordinate of 'image' center (default: 512)
## poisson
Add poisson noice to every pixel of image.
-?, --help show this help
-h, --height=arg resulting image height (default: 1024)
-l, --lambda=arg mean (and dispersion) of distribution (default: 15.)
-o, --output=arg output file name (default: output.png)
-w, --width=arg resulting image width (default: 1024)

View File

@@ -31,7 +31,9 @@ int main(int argc, char **argv){
return 2;
}
int w = I->width, h = I->height;
double t0 = dtime();
uint8_t *eq = ilequalize8(I, 3, 0.1);
green("Equalize: %g ms\n", (dtime() - t0)*1e3);
ilImage_free(&I);
if(!eq) return 3;
ilImg3 *I3 = MALLOC(ilImg3, 1);

64
examples/gauss.c Normal file
View File

@@ -0,0 +1,64 @@
/*
* This file is part of the improclib project.
* Copyright 2023 Edward V. Emelianov <edward.emelianoff@gmail.com>.
*
* 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 3 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, see <http://www.gnu.org/licenses/>.
*/
#include "improclib.h"
#include <usefull_macros.h>
#include <stdio.h>
static int help = 0, w = 1024, h = 1024, Niter = 1000000;
static double xsigma = 10., ysigma = 10., x0 = 512., y0 = 512.;
static char *outp = "output.png";
static myoption cmdlnopts[] = {
{"help", NO_ARGS, NULL, '?', arg_int, APTR(&help), "show this help"},
{"width", NEED_ARG, NULL, 'w', arg_int, APTR(&w), "resulting image width (default: 1024)"},
{"height", NEED_ARG, NULL, 'h', arg_int, APTR(&h), "resulting image height (default: 1024)"},
{"output", NEED_ARG, NULL, 'o', arg_string, APTR(&outp), "output file name (default: output.png)"},
{"xstd", NEED_ARG, NULL, 'X', arg_double, APTR(&xsigma), "STD of 'photons' distribution by X (default: 10)"},
{"ystd", NEED_ARG, NULL, 'Y', arg_double, APTR(&ysigma), "STD of 'photons' distribution by Y (default: 10)"},
{"xcenter", NEED_ARG, NULL, 'x', arg_double, APTR(&x0), "X coordinate of 'image' center (default: 512)"},
{"ycenter", NEED_ARG, NULL, 'y', arg_double, APTR(&y0), "Y coordinate of 'image' center (default: 512)"},
{"niter", NEED_ARG, NULL, 'n', arg_int, APTR(&Niter), "iterations (\"falling photons\") number (default: 1000000)"},
end_option
};
int main(int argc, char **argv){
initial_setup();
parseargs(&argc, &argv, cmdlnopts);
if(help) showhelp(-1, cmdlnopts);
if(w < 1 || h < 1) ERRX("Wrong image size");
if(xsigma < DBL_EPSILON || ysigma < DBL_EPSILON) ERRX("STD should be >0");
if(Niter < 1) ERRX("Iteration number should be a large positive number");
ilImage *I = ilImage_new(w, h, IMTYPE_U8);
if(!I) ERRX("Can't create image %dx%d pixels", w, h);
int hits = 0;
for(int i = 0; i < Niter; ++i){
//int x = (int)ilNormal(x0, sigma), y = (int)ilNormal(y0, sigma);
double x, y;
ilNormalPair(&x, &y, x0, y0, xsigma, ysigma);
if(x < 0 || x >= I->width || y < 0 || y >= I->height) continue;
uint8_t *pix = I->data + (int)x + ((int)y)*I->width;
if(*pix < 255) ++*pix;
++hits;
}
int ret = ilwrite_png(outp, I->width, I->height, 1, I->data);
ilImage_free(&I);
if(!ret) return 1;
printf("File %s ready; %d hits of %d\n", outp, hits, Niter);
return 0;
}

View File

@@ -22,7 +22,7 @@
#include <string.h>
static int w = 1024, h = 1024, help = 0;
static double fwhm = 3.5, beta = 1.;
static double fwhm = 3.5, beta = 1., lambda = 10.;
static char *outp = "output.jpg", *inp = NULL;
static ilPattern *star = NULL, *cross = NULL;
@@ -34,6 +34,7 @@ static myoption cmdlnopts[] = {
{"output", NEED_ARG, NULL, 'o', arg_string, APTR(&outp), "output file name (default: output.jpg)"},
{"halfwidth",NEED_ARG, NULL, 's', arg_double, APTR(&fwhm), "FWHM of 'star' images (default: 3.5)"},
{"beta", NEED_ARG, NULL, 'b', arg_double, APTR(&beta), "beta Moffat parameter of 'star' images (default: 1)"},
{"lambda", NEED_ARG, NULL, 'l', arg_double, APTR(&lambda), "lambda of Poisson noice (default: 10)"},
{"input", NEED_ARG, NULL, 'i', arg_string, APTR(&inp), "input file with coordinates and amplitudes (comma separated)"},
end_option
};
@@ -63,10 +64,15 @@ static void addstar(ilImg3 *I, const char *str){
printf("Add 'star' at %d,%d (ampl=%d)\n", x,y,a);
uint8_t c[3] = {a,a,a};
ilImg3_drawpattern(I, star, x, y, c);
}
static void addcross(ilImg3 *I, const char *str){
int x, y, a;
if(!getpars(str, &x, &y, &a)) return;
printf("Add 'cross' at %d,%d (ampl=%d)\n", x,y,a);
ilImg3_drawpattern(I, cross, x, y, ilColor_red);
}
static void addfromfile(ilImg3 *I){
static void addfromfile(ilImg3 *I, void (*fn)(ilImg3*, const char*)){
FILE *f = fopen(inp, "r");
if(!f){
WARN("Can't open %s", inp);
@@ -74,7 +80,7 @@ static void addfromfile(ilImg3 *I){
}
char *line = NULL;
size_t n = 0;
while(getline(&line, &n, f) > 0) addstar(I, line);
while(getline(&line, &n, f) > 0) fn(I, line);
fclose(f);
}
@@ -92,8 +98,15 @@ int main(int argc, char **argv){
star = ilPattern_star(par, par, fwhm, beta);
cross = ilPattern_xcross(25, 25);
for(int i = 0; i < argc; ++i) addstar(I, argv[i]);
if(inp) addfromfile(I);
if(inp) addfromfile(I, addstar);
ilPattern_free(&star);
double t0 = dtime();
ilImg3_addPoisson(I, lambda);
green("Poisson noice took %gms\n", (dtime()-t0) * 1e3);
if(!ilImg3_jpg(outp, I, 95)) WARNX("Can't save %s", outp);
for(int i = 0; i < argc; ++i) addcross(I, argv[i]);
if(inp) addfromfile(I, addcross);
ilPattern_free(&cross);
uint8_t color[] = {255, 0, 100};
//uint8_t color[] = {0, 0, 0};
ilImg3_putstring(I, "Test string", 450, 520, color);
@@ -106,7 +119,7 @@ int main(int argc, char **argv){
ilImg3_jpg("outpsubimage.jpg", s, 95);
ilImg3_free(&s);
}else WARNX("Bad subimage parameters");
int ret = ilImg3_jpg(outp, I, 95);
int ret = ilImg3_jpg("crosses.jpg", I, 95);
//int ret = ilImg3_png(outp, I);
ilImg3_free(&I);
if(!ret) return 4;

95
examples/objdet.c Normal file
View File

@@ -0,0 +1,95 @@
/*
* This file is part of the improclib project.
* Copyright 2023 Edward V. Emelianov <edward.emelianoff@gmail.com>.
*
* 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 3 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, see <http://www.gnu.org/licenses/>.
*/
// detect objects on an image
#include "improclib.h"
#include <usefull_macros.h>
#include <stdio.h>
#include <string.h>
static int help = 0, ndilat = 0, neros = 0;
double bg = -1.;
static char *infile = NULL, *outbg = NULL, *outbin = NULL;
static myoption cmdlnopts[] = {
{"help", NO_ARGS, NULL, 'h', arg_int, APTR(&help), "show this help"},
{"input", NEED_ARG, NULL, 'i', arg_string, APTR(&infile), "input file name"},
{"obg", NEED_ARG, NULL, 0, arg_string, APTR(&outbg), "input minus bg jpeg filename"},
{"background",NEED_ARG, NULL, 'b', arg_double, APTR(&bg), "background level (default: auto)"},
{"obin", NEED_ARG, NULL, 0, arg_string, APTR(&outbin), "--obg after binarizing"},
{"ndilat", NEED_ARG, NULL, 'd', arg_int, APTR(&ndilat), "amount of dilations after erosions"},
{"neros", NEED_ARG, NULL, 'e', arg_int, APTR(&neros), "amount of image erosions"},
end_option
};
int main(int argc, char **argv){
initial_setup();
parseargs(&argc, &argv, cmdlnopts);
if(help) showhelp(-1, cmdlnopts);
if(!infile) ERRX("Point name of input file");
ilImage *I = ilImage_read(infile);
if(!I) ERR("Can't read %s", infile);
if(bg < 0. && !ilImage_background(I, &bg)) ERRX("Can't calculate background");
uint8_t ibg = (int)(bg + 0.5);
printf("Background level: %d\n", ibg);
int w = I->width, h = I->height, wh = w*h;
ilImage *Ibg = ilImage_sim(I);
memcpy(Ibg->data, I->data, wh);
uint8_t *idata = (uint8_t*) Ibg->data;
for(int i = 0; i < wh; ++i) idata[i] = (idata[i] > ibg) ? idata[i] - ibg : 0;
if(outbg) ilwrite_jpg(outbg, Ibg->width, Ibg->height, 1, idata, 95);
double t0 = dtime();
uint8_t *Ibin = ilImage2bin(I, bg);
if(!Ibin) ERRX("Can't binarize image");
green("Binarization: %gms\n", 1e3*(dtime()-t0));
if(neros > 0){
t0 = dtime();
uint8_t *eros = ilerosionN(Ibin, w, h, neros);
FREE(Ibin);
Ibin = eros;
green("%d erosions: %gms\n", neros, 1e3*(dtime()-t0));
}
if(ndilat > 0){
t0 = dtime();
uint8_t *dilat = ildilationN(Ibin, w, h, ndilat);
FREE(Ibin);
Ibin = dilat;
green("%d dilations: %gms\n", ndilat, 1e3*(dtime()-t0));
}
if(outbin){
ilImage *tmp = ilbin2Image(Ibin, w, h);
ilwrite_jpg(outbin, tmp->width, tmp->height, 1, (uint8_t*)tmp->data, 95);
ilImage_free(&tmp);
}
ilConnComps *comps;
t0 = dtime();
size_t *labels = ilCClabel4(Ibin, w, h, &comps);
green("Labeling: %gms\n", 1e3*(dtime()-t0));
if(labels && comps->Nobj > 1){
printf("Detected %zd components\n", comps->Nobj-1);
ilBox *box = comps->boxes + 1;
for(size_t i = 1; i < comps->Nobj; ++i, ++box){
printf("\t%4zd: s=%d, LU=(%d, %d), RD=(%d, %d)\n", i, box->area, box->xmin, box->ymin, box->xmax, box->ymax);
}
FREE(labels);
FREE(comps);
}
;
return 0;
}

55
examples/poisson.c Normal file
View File

@@ -0,0 +1,55 @@
/*
* This file is part of the improclib project.
* Copyright 2023 Edward V. Emelianov <edward.emelianoff@gmail.com>.
*
* 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 3 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, see <http://www.gnu.org/licenses/>.
*/
#include "improclib.h"
#include <usefull_macros.h>
#include <stdio.h>
static int help = 0, w = 1024, h = 1024;
static double lambda = 15.;
static char *outp = "output.png";
static myoption cmdlnopts[] = {
{"help", NO_ARGS, NULL, '?', arg_int, APTR(&help), "show this help"},
{"width", NEED_ARG, NULL, 'w', arg_int, APTR(&w), "resulting image width (default: 1024)"},
{"height", NEED_ARG, NULL, 'h', arg_int, APTR(&h), "resulting image height (default: 1024)"},
{"output", NEED_ARG, NULL, 'o', arg_string, APTR(&outp), "output file name (default: output.png)"},
{"lambda", NEED_ARG, NULL, 'l', arg_double, APTR(&lambda), "mean (and dispersion) of distribution (default: 15.)"},
end_option
};
int main(int argc, char **argv){
initial_setup();
parseargs(&argc, &argv, cmdlnopts);
if(help) showhelp(-1, cmdlnopts);
if(w < 1 || h < 1) ERRX("Wrong image size");
if(lambda < 1.) ERRX("LAMBDA should be >=1");
ilImage *I = ilImage_new(w, h, IMTYPE_U8);
if(!I) ERRX("Can't create image %dx%d pixels", w, h);
int npix = I->height * I->width;
uint8_t *d = I->data;
for(int i = 0; i < npix; ++i, ++d){
int ampl = ilPoisson(lambda);
*d = ampl < 255 ? ampl : 255;
}
int ret = ilwrite_png(outp, I->width, I->height, 1, I->data);
ilImage_free(&I);
if(!ret) return 1;
printf("File %s ready\n", outp);
return 0;
}