简体   繁体   中英

Android - Rotating an Image with OpenCV4Android

So I'm attempting to rotate an image with OpenCV but I'm getting an error I don't understand and hoping someone can shed some light on it.

private Bitmap doStuff(Bitmap image)
{
    Mat img = new Mat();

    Utils.BitmaptoMat(image, img);

    Points[] pointsArray = new Point[3];

    pointsArray = getPoints(img); //Standard Template Matching

    double xDiff = pointArray[1].x - pointArray[0].x;
    double yDiff = pointArray[1].y - pointArray[0].y;
    double angle = Math.toDegrees(Math.atan2(yDiff, xDiff); //Angle Returned is -178.43493091440774

    img = roatate(img, angle); //Method call

    Utils.matToBitmap(img, returnFile) //Error is thrown when it hits this line
}

private Mat rotate(Mat img, double angle)
{
    double radians = Math.toRadians(angle); //radians is -3.1142770450250317
    double sin = Math.abs(Math.sin(radians)); // sin is 0.027312211802207727
    double cos = Math.abs(Math.cos(radians)); //cos is 0.9996269519608159

    int newWidth = (int) (img.width() * cos + img.height() * sin); //newWidth is 652
    int newHeight = (int) (img.width() * sin + img.height() * cos); //newHeight is 497

    // rotating image
    Point center = new Point(newWidth/2, newHeight/2);
    Mat rotImage = Imgproc.getRotationMatrix2D(center, angle, 1.0); 
    //1.0 means 100 % scale
    Size size = new Size(newWidth, newHeight);
    Imgproc.warpAffine(img, img, rotImage, size, Imgproc.INTER_LINEAR + Imgproc.CV_WARP_FILL_OUTLIERS);

    return img;
}

Error:

OpenCV Error: Assertion failed (src.dims == 2 && info.height == (uint32_t)src.rows && info.width == (uint32_t)src.cols) in void Java_org_opencv_android_Utils_nMatToBitmap2(JNIEnv*, jclass, jlong, jobject, jboolean), file /hdd2/buildbot/slaves/slave_ardbeg1/50-SDK/opencv/modules/java/generator/src/cpp/utils.cpp, line 97

nMatToBitmap catched cv::Exception: /hdd2/buildbot/slaves/slave_ardbeg1/50-SDK/opencv/modules/java/generator/src/cpp/utils.cpp:97: error: (-215) src.dims == 2 && info.height == (uint32_t)src.rows && info.width == (uint32_t)src.cols in function void Java_org_opencv_android_Utils_nMatToBitmap2(JNIEnv*, jclass, jlong, jobject, jboolean)

Edit: Added more for clarity

As you can read from error, the problem is with assertion inside `Utils.matToBitmap' function:

Assertion failed (src.dims == 2 && info.height == (uint32_t)src.rows && info.width == (uint32_t)src.cols) 

To satisfy this condition all three parts must be satisfied. The easiest way to check which one is wrong is just print all those values ( src.dims , info.height , (uint32_t)src.rows , info.width and (uint32_t)src.cols ) and check all parts of condition manually. Most likely the problem is with info.height == (uint32_t)src.rows or info.width == (uint32_t)src.cols , but check the first part too. If this still won't help you solve your problem, give us more informations - values of variables listed above before calling Utils.matToBitmap(img, returnFile) and also tell us what is returnFile variable.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM