Implements greyscale thresholding

This commit is contained in:
David Carley
2015-11-17 12:15:34 -06:00
parent 5dfeaa9fd1
commit 3af62446fc
14 changed files with 189 additions and 4 deletions

View File

@@ -262,4 +262,21 @@ namespace sharp {
*out = sharpened;
return 0;
}
int Threshold(VipsObject *context, VipsImage *image, VipsImage **out, int threshold) {
VipsImage *greyscale;
if (vips_colourspace(image, &greyscale, VIPS_INTERPRETATION_B_W, nullptr)) {
return -1;
}
vips_object_local(context, greyscale);
image = greyscale;
VipsImage *thresholded;
if (vips_moreeq_const1(image, &thresholded, threshold, nullptr)) {
return -1;
}
vips_object_local(context, thresholded);
*out = thresholded;
return 0;
}
} // namespace sharp

View File

@@ -24,6 +24,11 @@ namespace sharp {
*/
int Sharpen(VipsObject *context, VipsImage *image, VipsImage **out, int radius, double flat, double jagged);
/*
* Perform thresholding on an image. If the image is not greyscale, will convert before thresholding.
* Pixels with a greyscale value greater-than-or-equal-to `threshold` will be pure white. All others will be pure black.
*/
int Threshold(VipsObject *context, VipsImage *image, VipsImage **out, int threshold);
} // namespace sharp
#endif // SRC_OPERATIONS_H_

View File

@@ -40,6 +40,7 @@ using sharp::Composite;
using sharp::Normalize;
using sharp::Blur;
using sharp::Sharpen;
using sharp::Threshold;
using sharp::ImageType;
using sharp::DetermineImageType;
@@ -104,6 +105,7 @@ struct PipelineBaton {
int sharpenRadius;
double sharpenFlat;
double sharpenJagged;
int threshold;
std::string overlayPath;
double gamma;
bool greyscale;
@@ -142,6 +144,7 @@ struct PipelineBaton {
sharpenRadius(0),
sharpenFlat(1.0),
sharpenJagged(2.0),
threshold(0),
gamma(0.0),
greyscale(false),
normalize(false),
@@ -502,6 +505,7 @@ class PipelineWorker : public AsyncWorker {
bool shouldAffineTransform = xresidual != 0.0 || yresidual != 0.0;
bool shouldBlur = baton->blurSigma != 0.0;
bool shouldSharpen = baton->sharpenRadius != 0;
bool shouldThreshold = baton->threshold != 0;
bool hasOverlay = !baton->overlayPath.empty();
bool shouldPremultiplyAlpha = HasAlpha(image) && (shouldAffineTransform || shouldBlur || shouldSharpen || hasOverlay);
@@ -686,6 +690,15 @@ class PipelineWorker : public AsyncWorker {
image = extractedPost;
}
// Threshold - must happen before blurring, due to the utility of blurring after thresholding
if (shouldThreshold) {
VipsImage *thresholded;
if (Threshold(hook, image, &thresholded, baton->threshold)) {
return Error();
}
image = thresholded;
}
// Blur
if (shouldBlur) {
VipsImage *blurred;
@@ -1216,6 +1229,7 @@ NAN_METHOD(pipeline) {
baton->sharpenRadius = To<int32_t>(Get(options, New("sharpenRadius").ToLocalChecked()).ToLocalChecked()).FromJust();
baton->sharpenFlat = To<double>(Get(options, New("sharpenFlat").ToLocalChecked()).ToLocalChecked()).FromJust();
baton->sharpenJagged = To<double>(Get(options, New("sharpenJagged").ToLocalChecked()).ToLocalChecked()).FromJust();
baton->threshold = To<int32_t>(Get(options, New("threshold").ToLocalChecked()).ToLocalChecked()).FromJust();
baton->gamma = To<int32_t>(Get(options, New("gamma").ToLocalChecked()).ToLocalChecked()).FromJust();
baton->greyscale = To<bool>(Get(options, New("greyscale").ToLocalChecked()).ToLocalChecked()).FromJust();
baton->normalize = To<bool>(Get(options, New("normalize").ToLocalChecked()).ToLocalChecked()).FromJust();