Skip to content

Instantly share code, notes, and snippets.

@angstyloop
Last active March 7, 2023 00:37
Show Gist options
  • Select an option

  • Save angstyloop/38aa2ceca01b592f4ca358f58d0ae3e0 to your computer and use it in GitHub Desktop.

Select an option

Save angstyloop/38aa2ceca01b592f4ca358f58d0ae3e0 to your computer and use it in GitHub Desktop.
Threshold an image to create a "binary" or "boolean" black and white image, in C with the VIPS library. Revised version of threshold1.c. Uses vips_image_new to create a dummy object. vips_object_local_array is used to create an array of VipsImage pointers - the array is hung off the dummy VipsObject, so everything gets cleaned up when the dummy …
/** threshold2.c
*
* COMPILE
*
* gcc -Wall -o threshold2 threshold2.c `pkg-config vips --cflags --libs`
*
* EXAMPLE
*
* ./threshold2 128 in.png out.png
*
*/
#include <vips/vips.h>
int main (int argc, char** argv) {
if (VIPS_INIT(argv[0]))
vips_error_exit(NULL);
if (argc != 4) {
printf("USAGE: %s <THRESHOLD (0-255)> <INPUT_IMAGE_PATH> "
"<OUTPUT_IMAGE_PATH>", argv[0]);
vips_error_exit(NULL);
}
VipsObject* context = (VipsObject*) vips_image_new();
VipsImage** t = (VipsImage**) vips_object_local_array(context, 4);
double threshold = strtod(argv[1], NULL);
if (!(t[0] = vips_image_new_from_file(argv[2],
"access", VIPS_ACCESS_SEQUENTIAL, NULL)) ||
vips_colourspace(t[0], &t[1], VIPS_INTERPRETATION_B_W, NULL) ||
vips_extract_band(t[1], &t[2], 0, "n", 1, NULL) ||
vips_moreeq_const1(t[2], &t[3], threshold, NULL) ||
vips_image_write_to_file(t[3], argv[3], NULL)) {
g_object_unref(context);
vips_error_exit(NULL);
}
g_object_unref(context);
return EXIT_SUCCESS;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment