Skip to content

Commit

Permalink
1.0: init version
Browse files Browse the repository at this point in the history
  • Loading branch information
zvezdochiot committed Dec 25, 2024
0 parents commit 2d873d9
Show file tree
Hide file tree
Showing 8 changed files with 694 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[submodule "src/stb"]
path = src/stb
url = https://github.com/nothings/stb
24 changes: 24 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
This is free and unencumbered software released into the public domain.

Anyone is free to copy, modify, publish, use, compile, sell, or
distribute this software, either in source code form or as a compiled
binary, for any purpose, commercial or non-commercial, and by any
means.

In jurisdictions that recognize copyright laws, the author or authors
of this software dedicate any and all copyright interest in the
software to the public domain. We make this dedication for the benefit
of the public at large and to the detriment of our heirs and
successors. We intend this dedication to be an overt act of
relinquishment in perpetuity of all present and future rights to this
software under copyright law.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.

For more information, please refer to <https://unlicense.org>
12 changes: 12 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
PNAME = stbithresedgediv
CFLAGS = -std=c99 -O2 -Wall -Wextra -Wno-unused-but-set-variable -Wno-unused-parameter -Werror
LDLIBS = -lm -s
SRCS = src/edgediv.c

all: $(PNAME)

$(PNAME): $(SRCS)
$(CC) $(CFLAGS) $^ $(LDLIBS) -o $@

clean:
rm -f $(PNAME)
64 changes: 64 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
# stbithresedgediv

EdgeDiv ("EdgePlus" + "BlurDiv") threshold an image based of Gauss blur.
Used:
* Gauss blur: [iir_gauss_blur](https://github.com/arkanis/iir_gauss_blur).
* STB: [stb](https://github.com/nothings/stb).

```
O = (E < T) ? BLACK : WHITE
T = ThresholdBimadal(E)
E = FilterEdgeDiv(I, B)
B = FilterBlur(I)
bounds:
BLACK, if E < lower,
WHITE, if E > upper.
```

This filter was first applied in [STEX](https://github.com/ImageProcessing-ElectronicPublications/scantailor-experimental) (2023)
in a single-component version (Y).

Here this filter is implemented in a full-color version.

## Usage

`./stbithresedgediv [-h] [-i] [-s sigma] [-k coeff] [-d delta] [-l lower] [-u upper] input-file output.png`

`-s sigma` The sigma of the gauss normal distribution (number >= 0.5).
Larger values result in a stronger blur.

`-k coeff` The coefficient local threshold.

`-d delta` The regulator threshold.

`-l lower` The bound lower threshold.

`-u upper` The bound upper threshold.

`-i` Info to `stdout`.

`-h` display this help and exit.

You can use either sigma to specify the strengh of the blur.

The performance is independent of the blur strengh (sigma). This tool is an
implementation of the paper "Recursive implementaion of the Gaussian filter"
by Ian T. Young and Lucas J. van Vliet.

stb_image and stb_image_write by Sean Barrett and others is used to read and
write images.

## Installation

- Clone the repo or download the source `git clone --recurse-submodules https://github.com/ImageProcessing-ElectronicPublications/stbithresedgediv`
- Execute `make`
- Done. Either use the `stbithresedgediv` executable directly or copy it somewhere in your PATH.

## Links

* STB: [stb](https://github.com/nothings/stb).
* Gauss blur: [iir_gauss_blur](https://github.com/arkanis/iir_gauss_blur).
148 changes: 148 additions & 0 deletions src/edgediv.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@
#define _GNU_SOURCE
#include <stdio.h>
#include <unistd.h>

#define STB_IMAGE_IMPLEMENTATION
#define STBI_FAILURE_USERMSG
#include "stb/stb_image.h"
#define STB_IMAGE_WRITE_IMPLEMENTATION
#include "stb/stb_image_write.h"
#define IIR_GAUSS_BLUR_IMPLEMENTATION
#include "iir_gauss_blur.h"
#define THRESHOLD_EDGEDIV_IMPLEMENTATION
#include "thresedgediv.h"

void usage(char* progname)
{
fprintf(stderr,
"%s %s %s\n",
"Usage:", progname, "[-h] [-i] [-s sigma] [-k coeff] [-d delta] [-l lower] [-u upper] input-file output.png\n"
"EdgeDiv (EdgePlus + BlurDiv) threshold an image and save it as PNG.\n"
);
}

void help(float sigma, float coef, float delta, unsigned char bound_lower, unsigned char bound_upper)
{
fprintf(stderr,
"%s %f %s %f %s %f %s %d %s %d %s\n",
" -s sigma The sigma of the gauss normal distribution (number >= 0.5, default =", sigma, ").\n"
" Larger values result in a stronger blur.\n"
" -k coeff The coefficient local threshold (number, default =", coef, ").\n"
" -d delta The regulator threshold (number, default =", delta, ").\n"
" -l lower The bound lower threshold (integer, default =", bound_lower, ").\n"
" -u upper The bound upper threshold (integer, default =", bound_upper, ").\n"
" -i info to stdout.\n"
" -h display this help and exit.\n"
"\n"
"You can use either sigma to specify the strengh of the blur.\n"
"\n"
"The performance is independent of the blur strengh (sigma). This tool is an\n"
"implementation of the paper \"Recursive implementaion of the Gaussian filter\"\n"
"by Ian T. Young and Lucas J. van Vliet.\n"
"\n"
"stb_image and stb_image_write by Sean Barrett and others is used to read and\n"
"write images.\n"
);
}

unsigned char* image_copy(unsigned int width, unsigned int height, unsigned char components, unsigned char* image)
{
size_t image_size = height * width * components;
unsigned char* dest = (unsigned char*)malloc(image_size * sizeof(unsigned char));
if (dest != NULL)
{
for (size_t i = 0; i < image_size; i++)
{
dest[i] = image[i];
}
}
return dest;
}

int main(int argc, char** argv)
{
int info = 0;
float sigma = 10.0f;
float coef = 0.75f;
float delta = 0.0f;
unsigned char bound_lower = 0;
unsigned char bound_upper = 255;

int opt;
while ( (opt = getopt(argc, argv, "is:k:d:l:u:h")) != -1 )
{
switch(opt)
{
case 'i':
info = 1;
break;
case 's':
sigma = strtof(optarg, NULL);
break;
case 'k':
coef = strtof(optarg, NULL);
break;
case 'd':
delta = strtof(optarg, NULL);
break;
case 'l':
bound_lower = strtol(optarg, NULL, 10);
break;
case 'u':
bound_upper = strtol(optarg, NULL, 10);
break;
case 'h':
usage(argv[0]);
help(sigma, coef, delta, bound_lower, bound_upper);
return 0;
default:
usage(argv[0]);
return 1;
}
}

// Need at least two filenames after the last option
if (argc < optind + 2)
{
usage(argv[0]);
return 1;
}

int width = 0, height = 0, components = 1;
unsigned char* image = stbi_load(argv[optind], &width, &height, &components, 0);
if (image == NULL)
{
fprintf(stderr, "Failed to load %s: %s.\n", argv[optind], stbi_failure_reason());
return 2;
}

unsigned char* blur = image_copy(width, height, components, image);
if (blur == NULL)
{
fprintf(stderr, "ERROR: not use memmory\n");
return 3;
}

if (info > 0)
{
fprintf(stderr, "INFO: image %s\n", argv[optind]);
fprintf(stderr, "INFO: width %d\n", width);
fprintf(stderr, "INFO: height %d\n", height);
fprintf(stderr, "INFO: components %d\n", components);
fprintf(stderr, "INFO: sigma %f\n", sigma);
fprintf(stderr, "INFO: coeff. %f\n", coef);
fprintf(stderr, "INFO: delta %f\n", delta);
fprintf(stderr, "INFO: bound lower %d\n", bound_lower);
fprintf(stderr, "INFO: bound upper %d\n", bound_upper);
}
iir_gauss_blur(width, height, components, blur, sigma);
image_threshold_edgediv(width, height, components, coef, delta, bound_lower, bound_upper, info, image, blur);

if ( stbi_write_png(argv[optind+1], width, height, components, image, 0) == 0 )
{
fprintf(stderr, "Failed to save %s.\n", argv[optind+1]);
return 4;
}

return 0;
}
Loading

0 comments on commit 2d873d9

Please sign in to comment.