Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Segmentation of perspectively distorted barcodes

There are images with perspectively distorted barcodes in them.

enter image description here

They are located and decoded using ZBar.

zbar barcode location

Now I do not only need the rough location, but the four real corner points of the barcode, that define the enclosing 4-point polygon.

enter image description here

I tried different approaches, but did not yet get the desired result. One of them was:

  • convert image to grayscale
  • threshold image
  • erode image
  • floodFill beginning with a pixel known to be part of barcode
  • obtain the contour around the floodFill result

But around this contour I now would need to find the minimum best fitting 4-point polygon, which seems to be not that easy.

Do you have ideas for better approaches?

like image 733
Tobias Hermann Avatar asked Sep 27 '22 07:09

Tobias Hermann


1 Answers

You could use the following code and try to reduce your contour to 4-point polygon via approxPoly

vector approx;

for (size_t i = 0; i < contours.size(); i++)
{
    approxPolyDP(Mat(contours[i]), approx, 
             arcLength(Mat(contours[i]), true)*0.02, true);

    if (approx.size() == 4 &&
        fabs(contourArea(Mat(approx))) > 1000 &&
        isContourConvex(Mat(approx)))
    {
        double maxCosine = 0;

        for( int j = 2; j < 5; j++ )
        {
            double cosine = fabs(angle(approx[j%4], approx[j-2], approx[j-1]));
            maxCosine = MAX(maxCosine, cosine);
        }

        if( maxCosine < 0.3 )
            squares.push_back(approx);
    }
}

http://opencv-code.com/tutorials/detecting-simple-shapes-in-an-image/

You can also try the following methods, maybe they will produce good enough results for you: http://docs.opencv.org/modules/imgproc/doc/structural_analysis_and_shape_descriptors.html?highlight=minarearect#minarearect

or

http://docs.opencv.org/modules/imgproc/doc/structural_analysis_and_shape_descriptors.html?highlight=convexhull#convexhull

like image 112
Daniel Albertini Avatar answered Oct 06 '22 01:10

Daniel Albertini