Search code examples
opencvimage-processingraspberry-pibarcodezbar

Barcode pre-processing for zbar


I'm using a raspberry pi camera to read barcodes from an image, and the object needs to be relatively far from the camera in my application. The barcode before processing: Before

Zbar was unable to decode anything from this image, and I decided to try and edit the image manually to get it to decode. After drawing clear white lines between the single lines of the barcode and changing the contrast of the image, this result was decoded by ZBar: After

How can I programmatically go from the first image to the second image (I am using OpenCV with python right now)?


Solution

  • The problem is quite simple considering the darkest area is black and any greyish area needs to be converted to white. Try computing the mean intensity of the image.

    cv::Scalar avgPixelIntensity = cv::mean( image );

    Using this value as threshold, you can do Image threshold. This will be quite fast.

    You can also try Otsu Thresholding which is an adaptive thresholding technique. This will be slower than above method but will provide better results in cases when image has other artefacts such as shadows.

    See here: Image Thresholding Opencv.