Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

I would like to inform you that I solved this problem using MinRect function instead of boundingbox.Therefore I am able to find specific rotation angle of the barcode with "MinRect.angle" function.So, there is no need to rotate it using for loop.We do not have to do any operation in the for loop to know the angle of rotation.

After finding contours of the threshold image with this :

finding_contours.boundRect = minAreaRect(finding_contours.contours[finding_contours.i]);

Here is the code:

if ((zbar_scanner.barcode_string == "") || (zbar_scanner.barcode_string.length() <= 5)){

    cout << "Angle of minrect" << Bounding_rect_global.angle << endl;
    rotation_transform = Bounding_rect_global.angle;
    zbar_scanner.Image_Transform = Geometric_Transformation(zbar_scanner.cut_image_zbar1);
    imshow("Rotated Image", zbar_scanner.Image_Transform);

    zbar_scanner.scanner1.set_config(ZBAR_NONE, ZBAR_CFG_ENABLE, 1);

    Image image1(zbar_scanner.Image_Transform.cols, zbar_scanner.Image_Transform.rows, "Y800",    zbar_scanner.Image_Transform.data, zbar_scanner.Image_Transform.cols * zbar_scanner.Image_Transform.rows);


    zbar_scanner.scanner1.scan(image1);

    imshow("Rotated Image", zbar_scanner.Image_Transform);

    for (Image::SymbolIterator symbol1 = image1.symbol_begin();
        symbol1 != image1.symbol_end(); ++symbol1) {


                zbar_scanner.barcode_after_transform.append(symbol1->get_data());

             }

    if ((zbar_scanner.barcode_after_transform != "") || (zbar_scanner.barcode_after_transform.length() > 5)){

                    return zbar_scanner.barcode_after_transform;
                }

                image1.set_data(NULL, 0);
                return zbar_scanner.barcode_after_transform;    
        }

Here is the rotation function:

Mat Morphology::Geometric_Transformation(Mat& CutImage){

Geometric_transform geometric_transform;

    if ((CutImage.size().height) && (CutImage.size().width)){
        geometric_transform.src_center = Point2f(CutImage.cols / 2, CutImage.rows / 2); /// 2.0F /10.0f

        geometric_transform.rot_mat = getRotationMatrix2D(geometric_transform.src_center, rotation_transform, 1.0);

        geometric_transform.rot_imagebox = RotatedRect(geometric_transform.src_center, CutImage.size(), rotation_transform).boundingRect();

        geometric_transform.rot_mat.at<double>(0, 2) += geometric_transform.rot_imagebox.width / 2.0 - geometric_transform.src_center.x;//look for at command
        geometric_transform.rot_mat.at<double>(1, 2) += geometric_transform.rot_imagebox.height / 2.0 - geometric_transform.src_center.y;


        warpAffine(CutImage, geometric_transform.dst, geometric_transform.rot_mat, geometric_transform.rot_imagebox.size());

        cvtColor(geometric_transform.dst, geometric_transform.dstcopy, CV_RGB2GRAY);
        //warpPerspective(CutImage, dst, rot_mat, Size(CutImage.size().width + 50, CutImage.size().height + 50));


    }


    //------free memory of all unnecessarry images--------------------------
    geometric_transform.dst.release();
    CutImage.release();
    geometric_transform.rot_mat.release();

    return geometric_transform.dstcopy;

}

I would like to inform you that I solved this problem using MinRect function instead of boundingbox.Therefore I am able to find specific rotation angle of the barcode with "MinRect.angle" function.So, there is no need to rotate it using for loop.We do not have to do any operation in the for loop to know the angle of rotation.

After finding contours of the threshold image with this :

finding_contours.boundRect = minAreaRect(finding_contours.contours[finding_contours.i]);

Here is the code:

if ((zbar_scanner.barcode_string == "") || (zbar_scanner.barcode_string.length() <= 5)){

    cout << "Angle of minrect" << Bounding_rect_global.angle << endl;
    rotation_transform = Bounding_rect_global.angle;
    zbar_scanner.Image_Transform = Geometric_Transformation(zbar_scanner.cut_image_zbar1);
    imshow("Rotated Image", zbar_scanner.Image_Transform);

    zbar_scanner.scanner1.set_config(ZBAR_NONE, ZBAR_CFG_ENABLE, 1);

    Image image1(zbar_scanner.Image_Transform.cols, zbar_scanner.Image_Transform.rows, "Y800",    zbar_scanner.Image_Transform.data, zbar_scanner.Image_Transform.cols * zbar_scanner.Image_Transform.rows);


    zbar_scanner.scanner1.scan(image1);

    imshow("Rotated Image", zbar_scanner.Image_Transform);

    for (Image::SymbolIterator symbol1 = image1.symbol_begin();
        symbol1 != image1.symbol_end(); ++symbol1) {


                zbar_scanner.barcode_after_transform.append(symbol1->get_data());

             }

    if ((zbar_scanner.barcode_after_transform != "") || (zbar_scanner.barcode_after_transform.length() > 5)){

                    return zbar_scanner.barcode_after_transform;
                }

                image1.set_data(NULL, 0);
                return zbar_scanner.barcode_after_transform;    
        }

Here is the rotation function:

 Mat Morphology::Geometric_Transformation(Mat& CutImage){

 Geometric_transform geometric_transform;

     if ((CutImage.size().height) && (CutImage.size().width)){
         geometric_transform.src_center = Point2f(CutImage.cols / 2, CutImage.rows / 2); /// 2.0F /10.0f

         geometric_transform.rot_mat = getRotationMatrix2D(geometric_transform.src_center, rotation_transform, 1.0);

         geometric_transform.rot_imagebox = RotatedRect(geometric_transform.src_center, CutImage.size(), rotation_transform).boundingRect();

         geometric_transform.rot_mat.at<double>(0, 2) += geometric_transform.rot_imagebox.width / 2.0 - geometric_transform.src_center.x;//look for at command
         geometric_transform.rot_mat.at<double>(1, 2) += geometric_transform.rot_imagebox.height / 2.0 - geometric_transform.src_center.y;


         warpAffine(CutImage, geometric_transform.dst, geometric_transform.rot_mat, geometric_transform.rot_imagebox.size());

         cvtColor(geometric_transform.dst, geometric_transform.dstcopy, CV_RGB2GRAY);
         //warpPerspective(CutImage, dst, rot_mat, Size(CutImage.size().width + 50, CutImage.size().height + 50));


     }


     //------free memory of all unnecessarry images--------------------------
     geometric_transform.dst.release();
     CutImage.release();
     geometric_transform.rot_mat.release();

     return geometric_transform.dstcopy;
}

}

I would like to inform you that I solved this problem using MinRect function instead of boundingbox.Therefore I am able to find specific rotation angle of the barcode with "MinRect.angle" function.So, there is no need to rotate it using for loop.We do not have to do any operation in the for loop to know the angle of rotation.

After finding contours of the threshold image with this :

finding_contours.boundRect = minAreaRect(finding_contours.contours[finding_contours.i]);

Here is the code:

if ((zbar_scanner.barcode_string == "") || (zbar_scanner.barcode_string.length() <= 5)){

    cout << "Angle of minrect" << Bounding_rect_global.angle << endl;
    rotation_transform = Bounding_rect_global.angle;
    zbar_scanner.Image_Transform = Geometric_Transformation(zbar_scanner.cut_image_zbar1);
    imshow("Rotated Image", zbar_scanner.Image_Transform);

    zbar_scanner.scanner1.set_config(ZBAR_NONE, ZBAR_CFG_ENABLE, 1);

    Image image1(zbar_scanner.Image_Transform.cols, zbar_scanner.Image_Transform.rows, "Y800",    zbar_scanner.Image_Transform.data, zbar_scanner.Image_Transform.cols * zbar_scanner.Image_Transform.rows);


    zbar_scanner.scanner1.scan(image1);

    imshow("Rotated Image", zbar_scanner.Image_Transform);

    for (Image::SymbolIterator symbol1 = image1.symbol_begin();
        symbol1 != image1.symbol_end(); ++symbol1) {


                zbar_scanner.barcode_after_transform.append(symbol1->get_data());

             }

    if ((zbar_scanner.barcode_after_transform != "") || (zbar_scanner.barcode_after_transform.length() > 5)){

                    return zbar_scanner.barcode_after_transform;
                }

                image1.set_data(NULL, 0);
                return zbar_scanner.barcode_after_transform;    
        }

Here is the rotation function:

    Mat Morphology::Geometric_Transformation(Mat& CutImage){

    Geometric_transform geometric_transform;

        if ((CutImage.size().height) && (CutImage.size().width)){
            geometric_transform.src_center = Point2f(CutImage.cols / 2, CutImage.rows / 2); /// 2.0F /10.0f

            geometric_transform.rot_mat = getRotationMatrix2D(geometric_transform.src_center, rotation_transform, 1.0);

            geometric_transform.rot_imagebox = RotatedRect(geometric_transform.src_center, CutImage.size(), rotation_transform).boundingRect();

            geometric_transform.rot_mat.at<double>(0, 2) += geometric_transform.rot_imagebox.width / 2.0 - geometric_transform.src_center.x;//look for at command
            geometric_transform.rot_mat.at<double>(1, 2) += geometric_transform.rot_imagebox.height / 2.0 - geometric_transform.src_center.y;


            warpAffine(CutImage, geometric_transform.dst, geometric_transform.rot_mat, geometric_transform.rot_imagebox.size());

            cvtColor(geometric_transform.dst, geometric_transform.dstcopy, CV_RGB2GRAY);
            //warpPerspective(CutImage, dst, rot_mat, Size(CutImage.size().width + 50, CutImage.size().height + 50));


        }


        //------free memory of all unnecessarry images--------------------------
        geometric_transform.dst.release();
        CutImage.release();
        geometric_transform.rot_mat.release();

        return geometric_transform.dstcopy;
}

Here is the final image:

image description