在 Android 上使用 OpenCV at() 方法

Use OpenCV at() method on Android

我想在不裁剪的情况下旋转图像,我在 C++ 上找到了一种方法。

Rotate an image without cropping in OpenCV in C++

现在,我在 Android 上遇到了一些问题,我在 Android 上找不到 at() 方法。 这是我的 Android 代码:

private Mat rotateImg(Mat image, double angle) {
    int centerX = Math.round(image.width() / 2);
    int centerY = Math.round(image.height() / 2);
    Point center = new Point(centerY, centerX);
    //Size rotatedSize = new Size(image.width(), image.height());
    Mat M = Imgproc.getRotationMatrix2D(center, angle, 1);
    Rect bbox = new RotatedRect(center, image.size(), angle).boundingRect();
    /*
    rot.at<double>(0,2) += bbox.width/2.0 - center.x;
    rot.at<double>(1,2) += bbox.height/2.0 - center.y;
    */
    Imgproc.warpAffine(image, image, M, bbox.size());
    return image;
}

我不知道

rot.at<double>(0,2) += bbox.width/2.0 - center.x;
rot.at<double>(1,2) += bbox.height/2.0 - center.y;

这是我的代码,试试吧。我在上一个项目中测试过它的工作情况。

在线找到代码。 &我还没试过。

public void rotateImage(Bitmap bitmap, double angle) {
        Mat inputBitmapToMap = new Mat();
        Utils.bitmapToMat(bitmap, inputBitmapToMap);

        Point src_center = new Point(inputBitmapToMap.cols() / 2.0F, inputBitmapToMap.rows() / 2.0F);
        Mat rot_mat = Imgproc.getRotationMatrix2D(src_center, angle, 1.0);

        Rect bbox = new RotatedRect(src_center, inputBitmapToMap.size(), angle).boundingRect();
        rot_mat.put(0, 2, rot_mat.get(0, 2)[0] + bbox.width / 2.0 - src_center.x);
        rot_mat.put(1, 2, rot_mat.get(1, 2)[0] + bbox.height / 2.0 - src_center.y);

        Mat destMat = new Mat();
        Imgproc.warpAffine(inputBitmapToMap, destMat, rot_mat, inputBitmapToMap.size());

        Bitmap destBitmap = Bitmap.createBitmap(destMat.width(), destMat.height(), Bitmap.Config.RGB_565);
        Utils.matToBitmap(destMat, destBitmap);
}

获取像素值

matImage.get(i,j)[0];

更新像素值

matImage.put(i,j,array[i][j]);

两种情况下数组都是double[][]