Android - 使用OpenCV4Android旋转图像

时间:2015-03-03 20:41:38

标签: android opencv android-4.4-kitkat opencv4android

所以我试图用OpenCV旋转图像,但是我收到了一个我不理解的错误,并希望有人可以对它进行说明。

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

错误:

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)

编辑:为了清晰起见添加了更多内容

1 个答案:

答案 0 :(得分:1)

正如你可以从错误中读到的那样,问题在于`Utils.matToBitmap'中的断言。功能:

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

为了满足这个条件,必须满足所有三个部分。检查哪一个错误的最简单方法是打印所有这些值(src.dimsinfo.height(uint32_t)src.rowsinfo.width(uint32_t)src.cols)并检查所有部分条件的手动。最有可能的问题是info.height == (uint32_t)src.rowsinfo.width == (uint32_t)src.cols,但也请检查第一部分。如果这仍然无法帮助您解决问题,请在调用Utils.matToBitmap(img, returnFile)之前向我们提供更多信息 - 上面列出的变量值,并告诉我们returnFile变量是什么。