1

I am trying to crop an image then be able to display the image. I have four points in two arrays and it would make a rectangle shape. I have a code that i think would do the job but it is giving an error. Here is the code i hope you can help me out. I am working with android and only with Java.

int startX =locListX.get(0);
int startY =locListY.get(0);
Collections.sort(locListX);
Collections.sort(locListY);
int width=locListX.get(3)-locListX.get(0);
int height=locListY.get(3)-locListY.get(0);
Mat image = mFind;
Rect rectCrop = new Rect(startX, startY, width, height);
Mat imCrop=  new Mat(image,rectCrop); 
Utils.matToBitmap(imCrop, bmp5);
mGrayView.setImageBitmap(bmp5);
TheHippo
  • 61,720
  • 15
  • 75
  • 100
Auf Truh
  • 51
  • 1
  • 4
  • 1
    What error do you get? Please print also the content of both collections. – iiro Apr 12 '13 at 05:54
  • The collections is basically topleft, topright, bottomright, bottomleft with one get the x location and the other the y location i just want to get a rough rectangle of what i want to see. I used template match to get the points so i would just use the top left point as the start point and then use the sort to get the longest width and height for the image – Auf Truh Apr 12 '13 at 17:30
  • 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 /home/reports/ci/slave/opencv/modules/java/generator/src/cpp/util.cpp, line 107 nMatToBitmap catched cv::Exception: /home/report/ci/slave/opencv/modules/java/generator/src/utils.cpp:107: 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(KNIENV*,jclass,jlong,jobject,jboolean) – Auf Truh Apr 12 '13 at 17:54
  • I figured out my problem i messed up the creation of bmp5 – Auf Truh Apr 12 '13 at 21:09
  • try to use bufferedImage http://stackoverflow.com/questions/2386064/how-do-i-crop-an-image-in-java –  Mar 06 '14 at 16:51

1 Answers1

0

In this method you get four point and mat of document then you can cut this image using below method:

       public Bitmap warpDisplayImage(Mat inputMat) {
List<Point> newClockVisePoints = new ArrayList<>();

int resultWidth = inputMat.width();
int resultHeight = inputMat.height();

Mat startM = Converters.vector_Point2f_to_Mat(orderRectCorners(Previes method four poit list(like : List<Point> points)));

Point ocvPOut4 = new Point(0, 0);
Point ocvPOut1 = new Point(0, resultHeight);
Point ocvPOut2 = new Point(resultWidth, resultHeight);
Point ocvPOut3 = new Point(resultWidth, 0);



    ocvPOut3 = new Point(0, 0);
    ocvPOut4 = new Point(0, resultHeight);
    ocvPOut1 = new Point(resultWidth, resultHeight);
    ocvPOut2 = new Point(resultWidth, 0);
}

Mat outputMat = new Mat(resultWidth, resultHeight, CvType.CV_8UC4);

List<Point> dest = new ArrayList<Point>();
dest.add(ocvPOut3);
dest.add(ocvPOut2);
dest.add(ocvPOut1);
dest.add(ocvPOut4);


Mat endM = Converters.vector_Point2f_to_Mat(dest);

Mat perspectiveTransform = Imgproc.getPerspectiveTransform(startM, endM);

Imgproc.warpPerspective(inputMat, outputMat, perspectiveTransform, new Size(resultWidth, resultHeight), Imgproc.INTER_CUBIC);


Bitmap descBitmap = Bitmap.createBitmap(outputMat.cols(), outputMat.rows(), Bitmap.Config.ARGB_8888);
Utils.matToBitmap(outputMat, descBitmap);



return descBitmap;

}

user7176550
  • 1,501
  • 14
  • 19