OpenCV Rotation (Deskewing) in Android - C++ to Java Conversion

asked 2014-08-20 10:19:51 -0600

Mauricio gravatar image

Hello. I'm trying to do the rotation (deskewing) of a text guiding me from this post, but it's written in C++ and I'm working with opencv4Android sdk, so trying to do the conversion to Java. I'm having trouble especially with this part of the code:

std::vector<cv::Point> points;
cv::Mat_<uchar>::iterator it = img.begin<uchar>();
cv::Mat_<uchar>::iterator end = img.end<uchar>();
for (; it != end; ++it)
    if (*it)
        points.push_back(it.pos());

Since Java has no iterator for OpenCV Mat, I had to find some way to the same task other way, so with the help of this link I did the following:

List <Point> points = new ArrayList<Point>();
for (int i = 0; i < img.rows(); i++) {
    for (int j = 0; j < img.cols(); j++) {
        double pixel = img.get(i, j)[0];
        if (pixel != 0.0)
            points.add(new Point(i,j));
        }
}

But does not work, there is never a pixel = 0.0 and so the p array just fills with every point.

Please tell what I'm getting wrong. Does "it.pos()" get a Point?

Here's my complete code:

public Mat deskew(double angle)
{    
    Mat cropped = new Mat();
    try {
        if (img == null){
            img = Highgui.imread(filename, 0);
        }

        //invert the colors of our image
        Core.bitwise_not(img, img);

        //find edges and add points to a list
        List <Point> p = new ArrayList<Point>();
        for (int i = 0; i < img.rows(); i++) {
            for (int j = 0; j < img.cols(); j++) {
                double pixel = img.get(i, j)[0];
                if (pixel != 0.0)
                    p.add(new Point(i,j));
            }
        }

        Log.d(TAG, "Array of points: "+p.toString());

        //conversion of ArrayList of Points to Array
        Point[] array_point = (Point[]) p.toArray();
        MatOfPoint2f points= new MatOfPoint2f(array_point);
        Log.d(TAG, "Mat Point: "+points.dump());

        //Rotate text
        RotatedRect box = Imgproc.minAreaRect(points);
        Mat rot_mat = Imgproc.getRotationMatrix2D(box.center, angle, 1);
        Mat rotated = new Mat();
        Imgproc.warpAffine(img, rotated, rot_mat, img.size(), Imgproc.INTER_CUBIC);

        //Crop image
        Size box_size = box.size;
        if (box.angle < -45.) {
            //std::swap(box_size.width, box_size.height);
            double aux = box_size.width;
            box_size.width = box_size.height;
            box_size.height = aux;
        }

        Imgproc.getRectSubPix(rotated, box_size, box.center, cropped);
    } catch (Exception e) {
        e.printStackTrace();
        Log.e(TAG, "Error in DESKEW");
        Log.e(TAG, e.getMessage());
    }

    return cropped;
}
edit retag flag offensive close merge delete

Comments

I don't have a 100% answer but I did find another way to rotate an image: http://grokbase.com/t/gg/android-open...

Mytheral gravatar imageMytheral ( 2015-03-11 09:08:35 -0600 )edit