0

I have been searching the internet for a while now. The problem is that the solution to my problem in mostly available in either python or C++. I have tried to replicate the code but no luck.

I have detected a card (Rectangle) and I am able to crop it if the rectangle is straight but if the rectangle is rotated at an angle I get a image that cuts the card.

Image showing what I want to achieve...

What I want to achieve

My working code for straight Image.

Bitmap abc = null;
Point topleft, topright, bottomleft, bottomright;

float xRatio = (float) original.getWidth() / sourceImageView.getWidth();
float yRatio = (float) original.getHeight() / sourceImageView.getHeight();

float x1 = (points.get(0).x) * xRatio;
float x2 = (points.get(1).x) * xRatio;
float x3 = (points.get(2).x) * xRatio;
float x4 = (points.get(3).x) * xRatio;
float y1 = (points.get(0).y) * yRatio;
float y2 = (points.get(1).y) * yRatio;
float y3 = (points.get(2).y) * yRatio;
float y4 = (points.get(3).y) * yRatio;

Point p1 = new Point(x1, y1);
Point p2 = new Point(x2, y2);
Point p3 = new Point(x3, y3);
Point p4 = new Point(x4, y4);
List<Point> newpoints = new ArrayList<Point>();
newpoints.add(p1);
newpoints.add(p2);
newpoints.add(p3);
newpoints.add(p4);
Collections.sort(newpoints, new Comparator<Point>() {

    public int compare(Point o1, Point o2) {
        return Double.compare(o1.x, o2.x);
    }
});
if (newpoints.get(0).y > newpoints.get(1).y) {

    bottomleft = newpoints.get(0);
    topleft = newpoints.get(1);
} else {
    bottomleft = newpoints.get(1);
    topleft = newpoints.get(0);
}
if (newpoints.get(2).y > newpoints.get(3).y) {
    bottomright = newpoints.get(2);
    topright = newpoints.get(3);
} else {
    bottomright = newpoints.get(3);
    topright = newpoints.get(2);
}
final Mat newimage = new Mat();
Bitmap bmp32 = original.copy(Bitmap.Config.ARGB_8888, true);
org.opencv.android.Utils.bitmapToMat(bmp32, newimage);
final float dd = getAngle(bottomleft, bottomright);

Mat finalMat = new Mat(newimage, new org.opencv.core.Rect(topleft, bottomright));
abc = RotateBitmap(createBitmapfromMat(finalMat), (-dd));

Current code when rectangle is straight :

enter image description here enter image description here

Current code when rectangle is rotated:

enter image description here enter image description here

Links to similar questions : Link 1 Link 2

Rishabh Lashkari
  • 638
  • 1
  • 8
  • 22
  • and in which step are you stuck? describe a little bit more your problem, like I do not know how to do x instruction in C++ in java.... I think you need to use functions like findHomography and warpAffine from OpenCV to achieve the similar result... I do not see those in your code – api55 Sep 07 '18 at 08:30
  • The rectangle I want to crop is rotated and when I crop it using the above method the rectangle get cut and I want the complete rectangle – Rishabh Lashkari Sep 07 '18 at 08:59
  • can you add an image of your current result? – api55 Sep 07 '18 at 09:13
  • edited the question. – Rishabh Lashkari Sep 07 '18 at 09:32
  • you crop before rotating the image... you should crop after the rotation – api55 Sep 07 '18 at 10:47
  • Your rotation looks fine. Seems like you just crop at the wrong coordinates. – T A Sep 07 '18 at 13:20
  • @api55 if I crop after image rotation the rectangle with not be at the detected points. – Rishabh Lashkari Sep 10 '18 at 05:30
  • @TA I know that, I need to know how do I get the correct coordinates after Image rotation – Rishabh Lashkari Sep 10 '18 at 05:31
  • 1
    If you know the image coodinates before your rotation as well as the magnitude of the rotation you can easily calculate your resulting coordinates using a [rotation matrix](https://en.wikipedia.org/wiki/Rotation_matrix) – T A Sep 10 '18 at 05:54

0 Answers0