Skip to content

Commit cf4f8a9

Browse files
committed
1.2025.04.04: add filter and threshold Grain
1 parent 2a3d77e commit cf4f8a9

12 files changed

+386
-61
lines changed

src/imageproc/Binarize.cpp

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -778,6 +778,35 @@ BinaryImage binarizeRobust(
778778
return bw_img;
779779
} // binarizeRobust
780780

781+
/*
782+
* Grain:
783+
* I
784+
* B = BLUR(I,r)
785+
* D = GRAIN(I,B) {d = i-b+127}
786+
* S = BLUR(D,r)
787+
* N = GRAIN(S,D) {n = s-d+127}
788+
* T = GRAIN(D,N) {t = d-n+127} {t = d-s+d-127+127 = 2d-s}
789+
* O = k * T + (1 - k) * I
790+
*/
791+
BinaryImage binarizeGrain(
792+
GrayImage const& src,
793+
int const radius,
794+
float const k,
795+
int const delta,
796+
unsigned char const bound_lower,
797+
unsigned char const bound_upper)
798+
{
799+
if (src.isNull())
800+
{
801+
return BinaryImage();
802+
}
803+
804+
GrayImage gray(grayGrain(src, radius, k));
805+
BinaryImage bw_img(binarizeBiModal(gray, delta, bound_lower, bound_upper));
806+
807+
return bw_img;
808+
} // binarizeGrain
809+
781810
BinaryImage binarizeMScale(
782811
GrayImage const& src,
783812
int const radius,

src/imageproc/Binarize.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -272,6 +272,19 @@ IMAGEPROC_EXPORT BinaryImage binarizeRobust(
272272
unsigned char lower_bound = 0,
273273
unsigned char upper_bound = 255);
274274

275+
/**
276+
* \brief Image prefilter Grain for local/global thresholding method.
277+
*
278+
* Grain, zvezdochiot 2025. "Adaptive/global document image binarization".
279+
*/
280+
IMAGEPROC_EXPORT BinaryImage binarizeGrain(
281+
GrayImage const& src,
282+
int radius = 15,
283+
float k = 0.5f,
284+
int delta = 0,
285+
unsigned char lower_bound = 0,
286+
unsigned char upper_bound = 255);
287+
275288
/**
276289
* \brief Image binarization using MultiScale thresholding method.
277290
*

0 commit comments

Comments
 (0)