@@ -1710,24 +1710,40 @@ namespace output {
17101710 }
17111711
17121712 BinaryImage OutputGenerator::binarize (QImage const & image) const {
1713+ if ((image.format () == QImage::Format_Mono)
1714+ || (image.format () == QImage::Format_MonoLSB)) {
1715+ return BinaryImage (image);
1716+ }
1717+
17131718 BlackWhiteOptions blackWhiteOptions = m_colorParams.blackWhiteOptions ();
17141719 BlackWhiteOptions::BinarizationMethod binarizationMethod = blackWhiteOptions.getBinarizationMethod ();
17151720
1721+ QImage imageToBinarize = image;
1722+
17161723 BinaryImage binarized;
17171724 switch (binarizationMethod) {
17181725 case BlackWhiteOptions::OTSU: {
1719- GrayscaleHistogram hist (image );
1726+ GrayscaleHistogram hist (imageToBinarize );
17201727 BinaryThreshold const bw_thresh (BinaryThreshold::otsuThreshold (hist));
17211728
1722- binarized = BinaryImage (image , adjustThreshold (bw_thresh));
1729+ binarized = BinaryImage (imageToBinarize , adjustThreshold (bw_thresh));
17231730 break ;
17241731 }
17251732 case BlackWhiteOptions::SAUVOLA: {
17261733 QSize windowsSize = QSize (blackWhiteOptions.getWindowSize (),
17271734 blackWhiteOptions.getWindowSize ());
17281735 double sauvolaCoef = blackWhiteOptions.getSauvolaCoef ();
17291736
1730- binarized = imageproc::binarizeSauvola (image, windowsSize, sauvolaCoef);
1737+ if (blackWhiteOptions.isWhiteOnBlackMode ()) {
1738+ imageToBinarize = imageproc::toGrayscale (imageToBinarize);
1739+ imageToBinarize.invertPixels ();
1740+ }
1741+
1742+ binarized = imageproc::binarizeSauvola (imageToBinarize, windowsSize, sauvolaCoef);
1743+
1744+ if (blackWhiteOptions.isWhiteOnBlackMode ()) {
1745+ binarized.invert ();
1746+ }
17311747 break ;
17321748 }
17331749 case BlackWhiteOptions::WOLF: {
@@ -1737,7 +1753,16 @@ namespace output {
17371753 unsigned char upperBound = (unsigned char ) blackWhiteOptions.getWolfUpperBound ();
17381754 double wolfCoef = blackWhiteOptions.getWolfCoef ();
17391755
1740- binarized = imageproc::binarizeWolf (image, windowsSize, lowerBound, upperBound, wolfCoef);
1756+ if (blackWhiteOptions.isWhiteOnBlackMode ()) {
1757+ imageToBinarize = imageproc::toGrayscale (imageToBinarize);
1758+ imageToBinarize.invertPixels ();
1759+ }
1760+
1761+ binarized = imageproc::binarizeWolf (imageToBinarize, windowsSize, lowerBound, upperBound, wolfCoef);
1762+
1763+ if (blackWhiteOptions.isWhiteOnBlackMode ()) {
1764+ binarized.invert ();
1765+ }
17411766 break ;
17421767 }
17431768 }
0 commit comments