-1

I'm trying to rotate an image without cropping and I find a way on C++.

Rotate an image without cropping in OpenCV in C++

Now, I get some problem on Android, I cannot find at() method on Android. Here is my Android code:

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

I've no idea on

rot.at<double>(0,2) += bbox.width/2.0 - center.x;
rot.at<double>(1,2) += bbox.height/2.0 - center.y;
Dmitrii Z.
  • 2,287
  • 3
  • 19
  • 29
LittleTin
  • 23
  • 7

2 Answers2

0

Here is my code try it.. I tested in my last project and its working.

Found code online. & I didnt tried yet.

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);
}
Raj
  • 1,083
  • 8
  • 22
0

Get a pixel value

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

Update a pixel value

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

In both cases arrays are double[][]

arxakoulini
  • 723
  • 7
  • 22