mirror of
https://github.com/eddyem/fitsmaniplib.git
synced 2026-03-21 09:11:08 +03:00
Add simple median filtering, TODO: create adaptive median
This commit is contained in:
@@ -3,9 +3,9 @@ project(examples)
|
||||
include_directories(../)
|
||||
link_libraries(FITSmanip cfitsio m)
|
||||
|
||||
#add_executable(fitsstat fitsstat.c)
|
||||
add_executable(keylist keylist.c)
|
||||
add_executable(imstat imstat.c)
|
||||
add_executable(listtable listtable.c)
|
||||
add_executable(gd gd.c)
|
||||
target_link_libraries(gd -lgd)
|
||||
target_link_libraries(gd -lgd)
|
||||
add_executable(imstat imstat.c)
|
||||
add_executable(keylist keylist.c)
|
||||
add_executable(listtable listtable.c)
|
||||
add_executable(median med.c)
|
||||
|
||||
@@ -10,16 +10,19 @@ Usage: gd [args]
|
||||
|
||||
Where args are:
|
||||
|
||||
-T, --transform=arg type of intensity transformation (log, sqr, exp, pow)
|
||||
-E, --histeq histogram equalisation
|
||||
-H, --hcuthigh=arg histogram cut-off high limit
|
||||
-L, --hcutlow=arg histogram cut-off low limit
|
||||
-T, --transform=arg type of intensity transformation (exp, lin, log, pow, sqrt)
|
||||
-h, --help show this help
|
||||
-i, --inname=arg name of input file
|
||||
-l, --histlvl=arg amount of levels for histogram calculation
|
||||
-n, --hdunumber=arg open image from given HDU number
|
||||
-o, --outpname=arg output file name (jpeg)
|
||||
-p, --palette=arg convert as given palette
|
||||
-p, --palette=arg convert as given palette (br, cold, gray, hot, jet)
|
||||
-r, --rewrite rewrite output file
|
||||
-t, --textline=arg add text line to output image (at bottom)
|
||||
|
||||
|
||||
## imstat.c
|
||||
|
||||
Usage: imstat [args] input files
|
||||
|
||||
@@ -35,6 +35,9 @@ typedef struct{
|
||||
int nhdu; // HDU number to read image from
|
||||
int rewrite; // rewrite output file
|
||||
int nlvl; // amount of histogram levels
|
||||
int histeq; // histogram equalisation
|
||||
double histcutlow; // low limit of histogram cut-off
|
||||
double histcuthigh; // top limit -//-
|
||||
} glob_pars;
|
||||
|
||||
/*
|
||||
@@ -52,15 +55,18 @@ static glob_pars G = {
|
||||
*/
|
||||
static myoption cmdlnopts[] = {
|
||||
// common options
|
||||
{"help", NO_ARGS, NULL, 'h', arg_int, APTR(&help), _("show this help")},
|
||||
{"help", NO_ARGS, NULL, 'h', arg_none, APTR(&help), _("show this help")},
|
||||
{"inname", NEED_ARG, NULL, 'i', arg_string, APTR(&G.fitsname), _("name of input file")},
|
||||
{"outpname",NEED_ARG, NULL, 'o', arg_string, APTR(&G.outfile), _("output file name (jpeg)")},
|
||||
{"textline",NEED_ARG, NULL, 't', arg_string, APTR(&G.text), _("add text line to output image (at bottom)")},
|
||||
{"palette", NEED_ARG, NULL, 'p', arg_string, APTR(&G.palette), _("convert as given palette (linear, br, hot)")},
|
||||
{"palette", NEED_ARG, NULL, 'p', arg_string, APTR(&G.palette), _("convert as given palette (br, cold, gray, hot, jet)")},
|
||||
{"hdunumber",NEED_ARG, NULL, 'n', arg_int, APTR(&G.nhdu), _("open image from given HDU number")},
|
||||
{"transform",NEED_ARG, NULL, 'T', arg_string, APTR(&G.transform), _("type of intensity transformation (log, sqr, exp, pow)")},
|
||||
{"transform",NEED_ARG, NULL, 'T', arg_string, APTR(&G.transform), _("type of intensity transformation (exp, lin, log, pow, sqrt)")},
|
||||
{"rewrite", NO_ARGS, NULL, 'r', arg_none, APTR(&G.rewrite), _("rewrite output file")},
|
||||
{"histlvl", NEED_ARG, NULL, 'l', arg_int, APTR(&G.nlvl), _("amount of levels for histogram calculation")},
|
||||
{"hcutlow", NEED_ARG, NULL, 'L', arg_double, APTR(&G.histcutlow),_("histogram cut-off low limit")},
|
||||
{"hcuthigh",NEED_ARG, NULL, 'H', arg_double, APTR(&G.histcuthigh),_("histogram cut-off high limit")},
|
||||
{"histeq", NO_ARGS, NULL, 'E', arg_none, APTR(&G.histeq), _("histogram equalisation")},
|
||||
end_option
|
||||
};
|
||||
|
||||
@@ -159,9 +165,14 @@ static intens_transform gettransf(const char *transf){
|
||||
case 's':
|
||||
return TRANSF_SQR;
|
||||
break;
|
||||
default:
|
||||
return TRANSF_WRONG;
|
||||
}
|
||||
fprintf(stderr, "Possible arguments of " COLOR_RED "\"Transformation\"" COLOR_OLD ":\n");
|
||||
fprintf(stderr, "exp - exponential transform\n");
|
||||
fprintf(stderr, "linear (default) - linear transform (do nothing)\n");
|
||||
fprintf(stderr, "log - logariphmic transform\n");
|
||||
fprintf(stderr, "pow - x^2\n");
|
||||
fprintf(stderr, "sqrt - sqrt(x)\n");
|
||||
return TRANSF_WRONG;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -184,17 +195,27 @@ static image_palette palette_transform(char *p){
|
||||
case 'G':
|
||||
return PALETTE_GRAY;
|
||||
break;
|
||||
case 'h':
|
||||
case 'h': // hot, help
|
||||
case 'H':
|
||||
return PALETTE_HOT;
|
||||
switch(p[1]){
|
||||
case 'o':
|
||||
case 'O':
|
||||
return PALETTE_HOT;
|
||||
break;
|
||||
}
|
||||
break;
|
||||
case 'j':
|
||||
case 'J':
|
||||
return PALETTE_JET;
|
||||
break;
|
||||
default:
|
||||
return PALETTE_WRONG;
|
||||
}
|
||||
fprintf(stderr, "Possible arguments of " COLOR_RED "\"palette\"" COLOR_OLD ":\n");
|
||||
fprintf(stderr, "br - blue->red->yellow->white\n");
|
||||
fprintf(stderr, "cold - black->blue->cyan->white\n");
|
||||
fprintf(stderr, "gray (default) - simple gray\n");
|
||||
fprintf(stderr, "hot - black->red->yellow->white\n");
|
||||
fprintf(stderr, "jet - black->white->blue\n");
|
||||
return PALETTE_WRONG;
|
||||
}
|
||||
|
||||
void print_histo(histogram *H){
|
||||
@@ -212,17 +233,19 @@ void print_histo(histogram *H){
|
||||
int main(int argc, char *argv[]){
|
||||
initial_setup();
|
||||
parse_args(argc, argv);
|
||||
if(!G.fitsname) ERRX(_("No input filename given!"));
|
||||
if(!G.outfile) ERRX(_("Point the name of output file!"));
|
||||
intens_transform tr = TRANSF_LINEAR;
|
||||
if(G.transform) tr = gettransf(G.transform);
|
||||
if(tr == TRANSF_WRONG) ERRX(_("Wrong transform: %s"), G.transform);
|
||||
if(!file_absent(G.outfile) && !G.rewrite) ERRX(_("File %s exists"), G.outfile);
|
||||
image_palette colormap = PALETTE_GRAY;
|
||||
if(G.palette){ // convert normalized image due to choosen palette
|
||||
colormap = palette_transform(G.palette);
|
||||
if(colormap == PALETTE_WRONG) ERRX(_("Wrong colormap name"));
|
||||
if(colormap == PALETTE_WRONG) ERRX(_("Wrong colormap: %s"), G.palette);
|
||||
}
|
||||
intens_transform tr = TRANSF_LINEAR;
|
||||
if(G.transform){
|
||||
tr = gettransf(G.transform);
|
||||
if(tr == TRANSF_WRONG) ERRX(_("Wrong transform: %s"), G.transform);
|
||||
}
|
||||
if(!G.fitsname) ERRX(_("No input filename given!"));
|
||||
if(!G.outfile) ERRX(_("Point the name of output file!"));
|
||||
if(!file_absent(G.outfile) && !G.rewrite) ERRX(_("File %s exists"), G.outfile);
|
||||
DBG("Open file %s", G.fitsname);
|
||||
FITS *f = FITS_read(G.fitsname);
|
||||
if(!f) ERRX(_("Failed to open"));
|
||||
@@ -244,12 +267,25 @@ int main(int argc, char *argv[]){
|
||||
st = get_imgstat(dblimg, NULL);
|
||||
#endif
|
||||
DBG("NOW: MIN=%g, MAX=%g, AVR=%g, STD=%g", st->min, st->max, st->mean, st->std);
|
||||
green("Histogram before transformations:\n");
|
||||
histogram *h = dbl2histogram(dblimg, G.nlvl);
|
||||
print_histo(h);
|
||||
histogram_free(&h);
|
||||
if(G.histeq){ // equalize histogram
|
||||
if(!dbl_histeq(dblimg, G.nlvl))
|
||||
ERRX(_("Can't do histogram equalization"));
|
||||
}
|
||||
if(G.histcutlow > DBL_EPSILON || G.histcuthigh > DBL_EPSILON){
|
||||
if(!dbl_histcutoff(dblimg, G.nlvl, G.histcutlow, G.histcuthigh))
|
||||
ERRX(_("Can't make histogram cut-off"));
|
||||
}
|
||||
if(!mktransform(dblimg, st, tr)) ERRX(_("Can't do given transform"));
|
||||
#ifdef EBUG
|
||||
st = get_imgstat(dblimg, NULL);
|
||||
#endif
|
||||
DBG("After transformation: MIN=%g, MAX=%g, AVR=%g, STD=%g", st->min, st->max, st->mean, st->std);
|
||||
histogram *h = dbl2histogram(dblimg, G.nlvl);
|
||||
green("Histogram after transformations:\n");
|
||||
h = dbl2histogram(dblimg, G.nlvl);
|
||||
print_histo(h);
|
||||
histogram_free(&h);
|
||||
uint8_t *colored = convert2palette(dblimg, colormap);
|
||||
|
||||
107
examples/med.c
Normal file
107
examples/med.c
Normal file
@@ -0,0 +1,107 @@
|
||||
/*
|
||||
* This file is part of the FITSmaniplib project.
|
||||
* Copyright 2019 Edward V. Emelianov <edward.emelianoff@gmail.com>, <eddy@sao.ru>.
|
||||
*
|
||||
* 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 "common.h"
|
||||
|
||||
/*
|
||||
* Median filtering of image
|
||||
*/
|
||||
|
||||
typedef struct{
|
||||
char *fitsname; // input file name
|
||||
char *outfile; // output file name
|
||||
int rewrite; // rewrite existing file
|
||||
int medr; // radius of filter
|
||||
} glob_pars;
|
||||
|
||||
/*
|
||||
* here are global parameters initialisation
|
||||
*/
|
||||
static int help;
|
||||
static glob_pars G = {
|
||||
.medr = 1,
|
||||
};
|
||||
|
||||
/*
|
||||
* Define command line options by filling structure:
|
||||
* name has_arg flag val type argptr help
|
||||
*/
|
||||
static myoption cmdlnopts[] = {
|
||||
// common options
|
||||
{"help", NO_ARGS, NULL, 'h', arg_int, APTR(&help), _("show this help")},
|
||||
{"fitsname",NEED_ARG, NULL, 'i', arg_string, APTR(&G.fitsname), _("name of input file")},
|
||||
{"outpname",NEED_ARG, NULL, 'o', arg_string, APTR(&G.outfile), _("output file name (jpeg)")},
|
||||
{"rewrite", NO_ARGS, NULL, 'r', arg_none, APTR(&G.rewrite), _("rewrite output file")},
|
||||
{"radius", NEED_ARG, NULL, 'R', arg_int, APTR(&G.medr), _("radius of median (0 for cross 3x3)")},
|
||||
end_option
|
||||
};
|
||||
|
||||
/**
|
||||
* Parse command line options and return dynamically allocated structure
|
||||
* to global parameters
|
||||
* @param argc - copy of argc from main
|
||||
* @param argv - copy of argv from main
|
||||
* @return allocated structure with global parameters
|
||||
*/
|
||||
static glob_pars *parse_args(int argc, char **argv){
|
||||
int i;
|
||||
char *helpstring = "Usage: %%s [args]\n\n\tWhere args are:\n";
|
||||
change_helpstring(helpstring);
|
||||
// parse arguments
|
||||
parseargs(&argc, &argv, cmdlnopts);
|
||||
if(help) showhelp(-1, cmdlnopts);
|
||||
if(argc > 0){
|
||||
for (i = 0; i < argc; i++)
|
||||
printf("Ignore extra argument: %s\n", argv[i]);
|
||||
}
|
||||
return &G;
|
||||
}
|
||||
|
||||
int main(int argc, char *argv[]){
|
||||
initial_setup();
|
||||
parse_args(argc, argv);
|
||||
if(!G.fitsname) ERRX(_("No input filename given!"));
|
||||
if(!G.outfile) ERRX(_("No output filename given!"));
|
||||
if(G.medr < 0) ERRX(_("Median radius should be >= 0"));
|
||||
if(!file_absent(G.outfile) && !G.rewrite) ERRX(_("File %s exists"), G.outfile);
|
||||
FITS *f = FITS_read(G.fitsname);
|
||||
if(!f) ERRX(_("Failed to open %s"), G.fitsname);
|
||||
f->curHDU = NULL;
|
||||
int i;
|
||||
for(i = 1; i <= f->NHDUs; ++i){
|
||||
if(f->HDUs[i].hdutype == IMAGE_HDU){f->curHDU = &f->HDUs[i]; break;}
|
||||
}
|
||||
if(!f->curHDU) ERRX(_("No image HDUs in %s"), G.fitsname);
|
||||
green("First HDU with image: #%d\n", i);
|
||||
FITSimage *img = f->curHDU->contents.image;
|
||||
if(img->naxis != 2) ERRX(_("Support only 2-dimensional images"));
|
||||
doubleimage *dblimg = image2double(img);
|
||||
if(!dblimg) ERRX(_("Can't convert image to double"));
|
||||
doubleimage *filtered = get_median(dblimg, G.medr);
|
||||
if(!filtered) ERRX(_("WTF?"));
|
||||
if(!image_rebuild(img, filtered->data)) ERRX(_("Can't rebuild image"));
|
||||
doubleimage_free(&dblimg);
|
||||
f->filename = G.outfile;
|
||||
bool w = FALSE;
|
||||
if(file_absent(G.outfile)) w = FITS_write(G.outfile, f);
|
||||
else w = FITS_rewrite(f);
|
||||
if(!w) ERRX(_("Can't write %s"), f->filename);
|
||||
FITS_free(&f);
|
||||
return 0;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user