HOWTO: Detect a blank scanned page


Outdated Content Notice

This KB article contains somewhat outdated examples. DotImage has a BlankPageDetectionCommand which is a bit more "intelligent" about its detection. This article is being maintained as the methods described are still useful in some cases.

Updated Content

Using the BlankPageDetectionCommand is a more "intelligent method"...

[C#]

private void OnImageAcquired(object sender, AcquireEventArgs e)
{
	try
	{
    	if (e.Image != null)
    	{
                AtalaImage img = AtalaImage.FromBitmap(e.Image);
    		BlankPageDetectionCommand bpd = new BlankPageDetectionCommand();
    		bpd.ApplyToAnyPixelForamt = true;
    		// set any desired bpd.Property items you like here
    		BlankPageDetectionResults  result = bpd.Apply(img);
    		
    		if (!result.IsImageBlank)
    		{
    			// img is NOT blank.. do whatever you need to do with img here
    		}
    		
    		// proper cleanup
    		if (!result.IsImageSourceImage)
    		{
    			// if the result.Image is not a reference to img, 
    			// we need to dispose it, least we waste memory
    			result.Image.Dispose();
    		}
    		img.Dispose();
    		
    	}
   	} catch (Exception ex) {
   		// proper logging here
   	}
}

LegacyArticle Content

If you ever need to detect blank pages, you can use the GetDocumentHistogram method from the Histogram class. Here is an example that's specific to our DotTwain Acquisition classes ImageAcquired event.

[C#]

private void OnImageAcquired(object sender, AcquireEventArgs e)
{
	try
	{
    	if (e.Image != null)
    	{
    		// Convert to an AtalaImage. (assuming you are using DotTwain)
            AtalaImage img = AtalaImage.FromBitmap(e.Image);
            // We need a 1-bit image.
            AtalaImage tmp = img.GetChangedPixelFormat(PixelFormat.Pixel1bppIndexed);
            // Check for separator.
            Histogram hist = new Histogram(tmp);
            int[] doc = hist.GetDocumentHistogram();
            float tolerance = (doc[0] + doc[1]) * 0.005f;
            // If black is greater than the tolerance, it's a real page.
            if (doc[1] > tolerance)
            {
            	// Save the page with your normal code here using img, NOT tmp
            	//When done make sure to ...
            	img.Dispose();
            }
            tmp.Dispose();
        }
    }
    catch{}
}


[Visual Basic]

Private  Sub OnImageAcquired(ByVal sender As Object, ByVal e As AcquireEventArgs) Handles Twain.ImageAcquired
    Try        
    	If Not e.Image Is Nothing Then
        	' Convert to an AtalaImage. (assuming you are using DotTwain)
            Dim img As AtalaImage = AtalaImage.FromBitmap(e.Image)
            ' We need a 1-bit image.
            Dim tmp As AtalaImage = img.GetChangedPixelFormat(PixelFormat.Pixel1bppIndexed)
            ' Check for separator.
            Dim hist As Histogram = New Histogram(tmp)
            Dim doc() As Integer = hist.GetDocumentHistogram()
            Dim tolerance As single = (doc(0) + doc(1)) * 0.005f
            ' If black is greater than the tolerance, it's a real page.
            If doc(1) > tolerance Then
            	' Save the page with your regular code here.. use img not tmp.
            	' when done don't forget to
            	img.Dispose()	
            End If
            tmp.Dispose()
        End If
    End Try
End Sub

If you find some pages which are not blank are being detected as blank, you can decrease the percentage used to calculate the tolerance.

Original Article:

Q10050 - HOWTO: Detect a blank scanned page