Ask Your Question
0

Image rotation problem in for loop(Geometric transformation)

asked 2017-12-04 08:46:43 -0600

esconda gravatar image

updated 2017-12-04 08:50:53 -0600

Nowaday I am trying to write barcode reader program with opencv and zbar.The code works correctly, but the zbar plugin only reads the bar code on the horizontal and vertical axes.For this reason, I want to rotate the cross image at the end of the image output.So zbar will read the picture correctly.

But unfortunately I could not rotate the image correctly in the for loop. It rotates the image but does not do this for 180 degrees.I tried to write the code correctly, but I could not reach the end result.I think there's a problem with the warpaffine geometric tarnsformation function.

Any help will be apreciated to find the solution.

Here is example image outputs which is not properly rotated according to specified angle in for loop with explaination : image description

For loop(main code part) , which will be able to rotate image untill 120 degree of angle :

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


    for (float tr = 0; tr <= 120; tr += 20){

        cout << "ANGLE OF ROTATION" << tr << endl;


            zbar_scanner.Image_Transform = Geometric_Transformation(zbar_scanner.cut_image_zbar1, tr);//call rotate function.It is available below.zbar_scanner.cut_image_zbar1 is the image that will be processed and rotated

        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);

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

            // print out barcode string in each turn
            cout << "DECODED " << symbol1->get_type_name() << " SYMBOL \"" << symbol1->get_data() << '"' << endl;
            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;
            break;//if barcode is read, break the for loop
        }

        image1.set_data(NULL, 0);
    }
}

Here is the rotation function which is called above by

zbar_scanner.Image_Transform=Geometric_Transformation(zbar_scanner.cut_image_zbar1,tr);

Mat Morphology::Geometric_Transformation(Mat& CutImage, float Transform){

src_center = Point2f(CutImage.cols /2 , CutImage.rows / 2); /// 2.0F /10.0f

rot_mat = getRotationMatrix2D(src_center, -Transform, 1);

rot_imagebox = RotatedRect(src_center, CutImage.size(), -Transform).boundingRect();

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

if ((CutImage.cols > 0) && (CutImage.rows > 0)){
    warpAffine(CutImage, dst, rot_mat, rot_imagebox.size()); //rotate image with given angle by for loop value

    cvtColor(dst, dst, CV_RGB2GRAY);
    imshow("Rotated Image", dst);

}
return dst; //return otput image for each rotation

}

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted
2

answered 2017-12-11 01:08:45 -0600

esconda gravatar image

updated 2017-12-11 01:17:42 -0600

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

edit flag offensive delete link more

Question Tools

1 follower

Stats

Asked: 2017-12-04 08:46:43 -0600

Seen: 592 times

Last updated: Dec 11 '17